OpenGL Problem with code

G

George

I have marked the areas with comments as to what I would like to do and
what the code does. The two problem areas are in the key pressing and
mousing clicking areas. thanks for the help.

This code outputs a set of lines on the screen in different colors


/*****************************­***************
** problem2 .c **
** --------------- **
** **
** A template OpenGL program using GLUT **
** **
** **
******************************­**************/


#include "problem2.h"


/*****************************­**********************
** Globals! (In other words, I'm a C programmer) **
******************************­*********************/


int screen_width = 512, screen_height = 512;
GLenum smooth = GL_FALSE;
GLenum statecoord = GL_TRUE;
GLenum done = GL_FALSE;
int markx1, marky1, markx2, marky2;


void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);


}


/*****************************­**********************
** display function -- what to draw **
******************************­*********************/
void display ( void )
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0,0.0,0.0);
glBegin (GL_LINE_STRIP);
glVertex2i ( 1, 1 ); glVertex2i ( 1, 510 );
glVertex2i ( 1, 510 ); glVertex2i ( 510, 510 );
glVertex2i ( 510, 510 ); glVertex2i ( 510, 1 );
glVertex2i ( 510, 1 ); glVertex2i ( 1, 1 );
glEnd();


glColor3f(0.0,1.0,0.0);
glBegin(GL_LINES);
glVertex2i ( 32, 500 ); glVertex2i ( 82, 475 );
glVertex2i ( 30, 396 ); glVertex2i ( 90, 415 );
glVertex2i ( 82, 475 ); glVertex2i ( 30, 396 );
glEnd();


glColor3f(1.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2i ( 100, 420 ); glVertex2i ( 147, 427 );
glVertex2i ( 104, 366 ); glVertex2i ( 149, 368 );
glVertex2i ( 102, 324 ); glVertex2i ( 150, 300 );
glVertex2i ( 102, 324 ); glVertex2i ( 104, 366 );
glVertex2i ( 147, 427 ); glVertex2i ( 149, 368 );
glEnd();


/* Blue colored lines */
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2i ( 176, 340 ); glVertex2i ( 223, 341 );
glVertex2i ( 176, 340 ); glVertex2i ( 185, 228 );
glVertex2i ( 237, 240 ); glVertex2i ( 185, 228 );
glEnd();


/* white colored lines */
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
glVertex2i ( 250, 300 ); glVertex2i ( 250, 280 );
glVertex2i ( 248, 240 ); glVertex2i ( 253, 220 );
glEnd();


/* Yellow colored lines */
glColor3f(1.0,1.0,0.0);
glBegin(GL_LINES);
glVertex2i ( 293, 249 ); glVertex2i ( 307, 286 );
glVertex2i ( 307, 286 ); glVertex2i ( 307, 144 );
glVertex2i ( 264, 145 ); glVertex2i ( 347, 148 );
glEnd();


/* Cyan colored lines */
glColor3f(0.0,1.0,1.0);
glBegin(GL_LINES);
glVertex2i ( 415, 251 ); glVertex2i ( 371, 233 );
glVertex2i ( 371, 233 ); glVertex2i ( 376, 201 );
glVertex2i ( 376, 201 ); glVertex2i ( 431, 151 );
glVertex2i ( 431, 151 ); glVertex2i ( 368, 102 );
glEnd();


/* Orange colored lines */
glColor3f(1,.5,0);
glBegin(GL_LINES);
glVertex2i ( 444, 174 ); glVertex2i ( 474, 179 );
glVertex2i ( 474, 179 ); glVertex2i ( 442, 38 );
glVertex2i ( 391, 38 ); glVertex2i ( 493, 45 );
glEnd();

/* triggered if both endpoints in x and y **
** area to draw the lines with the mouse clicks */
if(done)
{
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
glVertex2i ( markx1, marky1 );
glVertex2i ( markx2, marky2 );
glEnd();
}


/* flush GL commands & swap buffers */
glFlush();
glutSwapBuffers();



}


/*****************************­**********************
** idle function... what to do when bored **
******************************­*********************/
void idle ( void )
{


}


/*****************************­**********************
** deal with user resizing window (BAD USER!) **
******************************­*********************/
void reshape( int w, int h )
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-512.0, 512.0, -512.0, 512.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


}


/*****************************­**********************
** deal with mouse movement **
******************************­*********************/
void motion(int x, int y)
{


}


/*****************************­**********************
** deal with mouse clicks **
** in this area I want the right mouse button to **
** click on two areas and draw a line but it does **
** not draw the line where the mouse clicks it **
** draws it somewhere completely else **
******************************­*********************/
void button(int b, int st, int x, int y)
{
switch ( b )
{
default:
break;

case GLUT_LEFT_BUTTON:
{
switch( st )
{
case GLUT_DOWN:
break;
case GLUT_UP:
break;
}
}
break;
case GLUT_MIDDLE_BUTTON:
{
switch( st )
{
case GLUT_DOWN:
break;
case GLUT_UP:
break;
}
}
break;


case GLUT_RIGHT_BUTTON:
{
switch( st )
{
case GLUT_DOWN:
if(statecoord)
{
markx1 = x; marky1 = y;
}
else
{
markx2 = x; marky2 = y;
glutPostRedisplay();
done = GL_TRUE;
}
break;
case GLUT_UP:
statecoord = GL_FALSE;
break;
}


}


break;
}


}


/*****************************­**********************
** deal with menu commands **
******************************­*********************/
void menu( int value )
{
switch (value)
{
default:
break;

case 27: /* quit */
exit(0);
break;
}


glutPostRedisplay();



}


/*****************************­**********************
** deal with key strokes **
** in this area I would like the c button to change**
** the ShadeModel from GL_SMOOTH to GL_FLAT and **
** vice versa. this will create the colors to **
** change along the line **
******************************­*********************/
void keys(unsigned char key, int x, int y)
{
switch ( key )
{
default:
break;
case 'Q':
exit(0);
break;
case 'q':
exit(0);
break;
case 'C':
smooth = !smooth;
if (smooth) {
glShadeModel(GL_SMOOTH);
} else {
glShadeModel(GL_FLAT);
}
glutPostRedisplay();
break;
case 'c':
smooth = !smooth;
if (smooth) {
glShadeModel(GL_SMOOTH);
} else {
glShadeModel(GL_FLAT);
}
glutPostRedisplay();
break;
}


}


/*****************************­**********************
** extract the parameters from the command line **
******************************­*********************/
void ExtractCommandLineParameters( int argc, char **argv )
{
int i;

printf("\n");


/* while there are parameters to deal with */
for ( i = 1; i < argc; i++ )
{
if (!strcmp(argv, "-help"))
{
printf("Usage: %s [options] \n\n", argv[0]);
printf("Options:\n");
printf("\t-help Print this message and exit\n");
printf("\t-debug Print dubugging messages during
execution\n");


exit(0);
}
if (!strcmp(argv, "-d") || !strcmp(argv, "-debug"))
{
/* put debug switches here */
i++;
}


}



}


/*****************************­**********************
** main entry point **
******************************­*********************/

int main(int argc, char** argv)
{
/* Do all the general purpose startup stuff...*/
glutInit(&argc, argv);


/* get command line parameters */
ExtractCommandLineParameters( argc, argv );


/* we got a RGBA buffer and we're double buffering! */
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );


/* set some size for the window. */
glutInitWindowSize( screen_width, screen_height );


/* arbitrarily set an initial window position... */
glutInitWindowPosition( 100, 100 );


/* make the window. give it a cool title */
glutCreateWindow("Courtney Hayward Problem2");


/* set the callback functions */
init();
glutReshapeFunc( reshape );
glutIdleFunc( idle );
glutMouseFunc( button );
glutMotionFunc( motion );
glutKeyboardFunc( keys );
glutDisplayFunc( display );


/* setup the menu */
/* probably will do something here eventually... */


/* start it! */
glutMainLoop();
return 0
 
U

Ulrich Eckhardt

George said:
I have marked the areas with comments as to what I would like to do and
what the code does. The two problem areas are in the key pressing and
mousing clicking areas.

I didn't find a concise, short description of what it does and what you
think it should do, sorry. Also, make a _minimal_ example.

I only noticed two things:
1. your code in button() is ill-indented
2. you can bundle case labels, i.e. if 'c' and 'C' should do the same, you
can write

switch(...) {
case 'c':
case 'C':
// common code here
break;
}


Uli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top