Trouble changing the cam position, using gluLookAt

Hello!
My program is supposed to draw a cube (I’m using lines because, due to my noobness, I don’t know any better yet.), and then let the user look at it from different positions. When checking the eyex, eyey, eyez etc. values with breakpoints, everything seems to be fine- they’re modified when user presses corresponding keys. But the outcome of this doesn’t show up on the screen- only thing I get is my cube in basic position. Any ideas?
Here’s the code:


#include <GL/glut.h>
#include <stdlib.h>


enum { FULL_WINDOW, ASPECT_1_1, EXIT };

int iAspect = FULL_WINDOW;

GLdouble eyex = 0;
GLdouble eyey = 0;
GLdouble eyez = 3;

GLdouble centerx = 0;
GLdouble centery = 0;
GLdouble centerz = -100;


void Display (void)				//drawing the cube
{
	glClearColor(0,0,0,0);
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(eyex,eyey,eyez,centerx,centery,centerz,0,1,0);
	glColor3f(1,1,1);
	glBegin(GL_LINES);

	glVertex3f(1,1,1);
	glVertex3f(1,-1,1);

	glVertex3f(1,-1,1);
	glVertex3f(1,-1,-1);

	glVertex3f(1,-1,-1);
	glVertex3f(1,1,-1);

	glVertex3f(1,1,-1);
	glVertex3f(1,1,1);

	glEnd();
	glFlush();
	glColor3f(1,0,0);
	glBegin(GL_LINES);

	glVertex3f(-1,1,1);
	glVertex3f(-1,-1,1);
	
	glVertex3f(-1,-1,1);
	glVertex3f(-1,-1,-1);

	glVertex3f(-1,-1,-1);
	glVertex3f(-1,1,-1);

	glVertex3f(-1,1,-1);
	glVertex3f(-1,1,1);

	glVertex3f(1,1,1);
	glVertex3f(-1,1,1);

	glVertex3f(1,-1,1);
	glVertex3f(-1,-1,1);

	glVertex3f(1,-1,-1);
	glVertex3f(-1,-1,-1);

	glVertex3f(1,1,-1);
	glVertex3f(-1,1,-1);

	
	glFlush();
	glutSwapBuffers();
	return;
}
void Reshape (int width, int height)		//reshaping the rendering window
{
 
	glViewport(0,0,width,height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	if (iAspect == ASPECT_1_1)
	{
		if (width < height && width > 0)
		{
			glFrustum(-2.0,2.0,-2.0 * height / width, 2.0 * height / width, 1.0, 5.0);
		}
		else
		{
			if (width >= height && height > 0)
			{
				glFrustum(-2.0 * width / height, 2.0 * width / height, -2.0, 2.0, 1.0, 5.0);
			}
		}
	}
	else
	{
		glFrustum(-2.0,2.0,-2.0,2.0,1.0,5.0);
	}
	Display();
	return;
}

void Keyboard (unsigned char key, int x, int y)			//changing the z viewer position
{
	if (key == '+')
		eyez += 0.1;
	else
	{
		if (key == '-')
			eyez -= 0.1;
	}
	Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
}

void SpecialKeys (int key, int x, int y)		//changing the x and y viewer position
{
	switch(key)
	{
		case GLUT_KEY_LEFT:	eyex += 0.1;
							break;
		case GLUT_KEY_RIGHT:eyex -= 0.1;
							break;
		case GLUT_KEY_UP:	eyey -= 0.1;
							break;
		case GLUT_KEY_DOWN:	eyey += 0.1;
							break;
	}
	Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
	return;
}



void Menu(int value)
{
	switch(value)
	{
		case FULL_WINDOW:	iAspect = FULL_WINDOW;
							Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
							break;
		case ASPECT_1_1:	iAspect = ASPECT_1_1;
							Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
							break;
		case EXIT:			exit(69); //random prank code 
	}
	return;
}


int main(int argc, char* argv[])
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(400,400);
	glutCreateWindow("openGL!");
	glutDisplayFunc(Display);
	glutReshapeFunc(Reshape);
	glutKeyboardFunc(Keyboard);
	glutSpecialFunc(SpecialKeys);
	glutCreateMenu(Menu);
#ifdef WIN_32
	glutAddMenuEntry("Wyj&#347;cie",EXIT);
#else
	glutAddMenuEntry("Fullscreen rendering",FULL_WINDOW);
	glutAddMenuEntry("1:1 rendering",ASPECT_1_1);
	glutAddMenuEntry("EXIT",EXIT);
#endif
	glutAttachMenu(GLUT_RIGHT_BUTTON);
	glutMainLoop();
	return 1;
}

Also, I have tried to replace my Reshape() function with


void Reshape (int width, int height)		//reshaping the rendering window
{
	glViewport(0,0,width,height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(80.0, (double)width/(double)height, 0.1, 1000.0);

	Display();
	return;
}

what didn’t get me anywhere- the result was still the same.

Any help will be greatly appreciated.
Cheers!

You do not consistently have matched glBegin(GL_LINES) … glEnd() pairs.

A separate issue but useful for telling where to edit your code to fix the above problem – glutSwapBuffers() implicitly does a glFlush() so you don’t need to call this explicitly – so just replace your glFlush() with glEnd() before glutSwapBuffers() in Display(). That will solve two problems with one edit :wink:


void Display (void)				//drawing the cube
{
.... delete glFlush(); on line 41 since unecessary
.... then replace last one with glEnd
	glVertex3f(-1,1,-1);

	
	glEnd(); /// !!!!!! replaced glFlush
	glutSwapBuffers();
	return;
}

Thank You so very much!

Now could You tell me why the glFlush() was causing this error? Is it because I set the position (through gluLookAt() ) and then flushed the unfinished cube prematurely, so the changes wouldn’t apply properly to the whole scene?

No to your question. Your code was fine with the way you were setting the projection and modelview matrices. You simply were causing undefined behvior in the way your were drawing GL_LINES in with the glBegin/glEnd block – or missing block in your case.

glFlush() was not causing the error. It was harmless but redundant (read glutSwapBuffer). This was a secondary issue that was an unnecessary performance waster. I used glFlush as a reference point to tell you where to change the code only.

However, you were calling glBegin without a matching glEnd and that is the real problem. The way you use that is you sepcify a single gemoetry inside a begin/end pair – you weren’t doing this and that lead to undefined behavior.

Ok, it’s all clear now. Thanks a dozen!