PDA

View Full Version : What is the actual problem?



moonie
06-15-2003, 10:41 PM
How come when i press L on my keyboard, a line is not drawn joining the 2 points together? Here is my program source code:

#include <windows.h>
#include <gl\gl.h>
#include <gl\glut.h>
#include <iostream.h>
void init(void);
void display(void);
void keyboard (unsigned char key, int x, int y);

float x1,y1,z1,x2,y2,z2;

int main(int argc, char *argv[])
{

cout << "Enter the x1 coordinate of the 1st sphere(range between -1 to 1) :";
cin >> x1;
cout << "Enter the y1 coordinate of the 1st sphere(range between -1 to 1) :";
cin >> y1;
cout << "Enter the z1 coordinate of the 1st sphere(range between -1 to 1) :";
cin >> z1;
cout << "Enter the x2 coordinate of the 2nd sphere(range between -1 to 1) :";
cin >> x2;
cout << "Enter the y2 coordinate of the 2nd sphere(range between -1 to 1) :";
cin >> y2;
cout << "Enter the z2 coordinate of the 2nd sphere(range between -1 to 1) :";
cin >> z2;

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(900,900);
glutInitWindowPosition(200,50);
glutCreateWindow("Fastrak Project");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0;

}

void init(void)
{
glClearColor(0.0f ,0.0f ,0.0f ,0.0f);
glClearDepth(1.0f);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glPopMatrix();
gluPerspective(45.0f,(GLfloat)900/(GLfloat)900,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPopMatrix();

}

void display(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0,-5.0f);
glRotatef(-65,1.0f,1.0f,1.0f);
glColor3d(1.0,0.5,1.0);
glutWireCube(2.00);

glPushMatrix();
glTranslatef(x1,y1,z1);
glColor3f(0.5,0.5,1.0);
glutSolidSphere(0.02,10,10);
glPopMatrix();

glPushMatrix();
glTranslatef(x2,y2,z2);
glColor3f(0.5,1.0,0.5);
glutSolidSphere(0.02,10,10);
glPopMatrix();

glFlush();
glutSwapBuffers();

}

void keyboard (unsigned char key, int x, int y)

{
switch (key)
{
case 27:
exit(0);
break;

case 'L':
case 'l':
glColor3f(1.0,1.0,0.0);
glBegin(GL_LINES);
glVertex3f(x1,y1,z1);
glVertex3f(x2,y2,z2);
glEnd();
glutPostRedisplay();
break;
}
}

errno
06-15-2003, 11:13 PM
first, fix some bug in the init function:
you have to do
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)900/(GLfloat)900,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

then, in the keyboard function, when L is pressed, you render the line and you call glutPostRedisplay()
you tell glut to call your display function, which clear the buffers and draw the cube and the spheres.

the line is drawn, but you clear the color buffer just after.

you should do in the keyboard function:
case 'L':
L_have_been_pressed=true;
glutPostRedisplay();
break;

and in the display function:
...
if (L_have_been_pressed)
{
// draw the line
}
...

mikael_aronsson
06-16-2003, 12:28 AM
Hi !

You cannot put any openGL code in the keyboard callback, you should just call glutPostReDisplay() and update some kind of state variables, then the display callback should take care of the drawing.

Mikael

moonie
06-16-2003, 02:38 AM
Originally posted by yoyo:
first, fix some bug in the init function:
you have to do
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)900/(GLfloat)900,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

then, in the keyboard function, when L is pressed, you render the line and you call glutPostRedisplay()
you tell glut to call your display function, which clear the buffers and draw the cube and the spheres.

the line is drawn, but you clear the color buffer just after.

you should do in the keyboard function:
case 'L':
L_have_been_pressed=true;
glutPostRedisplay();
break;

and in the display function:
...
if (L_have_been_pressed)
{
// draw the line
}
...



How come after i try your suggestion but the line is still not drawn? Pls help.