I have a couple quads drawn in a scene. They are meant to be the front and back of a cube, but I haven’t drawn the remaining sides because I am having an issue just with the front and back.
When the display function gets called, the quads move. However, I use the same call to gluLookAt and I have not edited the object vertex positions.
Here is my code:
#include <GL/glut.h>
#include <stdio.h>
float X_MIN = -3.0;
float X_MAX = 3.0;
float Y_MIN = -3.0;
float Y_MAX = 3.0;
int WINDOW_WIDTH = 500;
int WINDOW_HEIGHT = 500;
GLfloat x_view = 3.0, y_view = 3.0, z_view = 0.0;
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;
GLfloat x_up = 0.0, y_up = 1.0, z_up = 0.0;
float v=0.0f;
GLfloat dnear = 1.0, dfar = 8.0;
void setupView()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Specify the viewing parameters
gluLookAt(x_view, y_view, z_view, xref, yref, zref, x_up, y_up, z_up);
// Projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(120.0, 1.0f, dnear, dfar);
// Viewport
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
}
// Clear buffers, setup view, enable depth
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
setupView();
glEnable(GL_DEPTH_TEST);
}
// ascii key and (x,y) of mouse when the key is pressed
void keyboardCb(unsigned char key, int x, int y)
{
switch(key)
{
case 'a':
printf("\nPressed a\n");
v += 0.01;
break;
}
// May have changed the camera location (implement later), so redraw the scene
glutPostRedisplay();
}
void drawCube()
{
glColor3f(1.0, 0.0, 0.0);
int z = 1;
// front
glBegin(GL_QUADS);
glVertex3i(-1, -1, z);
glVertex3i(1, -1, z);
glVertex3i(1, 1, z);
glVertex3i(-1, 1, z);
glEnd();
// back
glColor3f(0.0, 0.0, 0.0);
z = -1;
glBegin(GL_QUADS);
glVertex3i(-1, -1, z);
glVertex3i(1, -1, z);
glVertex3i(1, 1, z);
glVertex3i(-1, 1, z);
glEnd();
}
void display()
{
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT);
// Draw the cube
drawCube();
// Setup viewing parameters
setupView();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 100);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Hello world!");
init();
// Setup callbacks
glutKeyboardFunc(keyboardCb);
glutDisplayFunc(display);
glutMainLoop();
printf("Exiting normally");
return 0;
}
Whenever I press the ‘a’ key, glutPostRedisplay gets called, and the object is no longer at its original location. My expectation is that the scene does not change at all since I’m not calling gluLookAt with different values, and I’m not doing anything with the object coordinates.
I am wondering if the matrix calls in setupView get concatenated onto the previous matrix calls in that function? If I remove the call to setupView in display, then when I press ‘a’ I cannot see the quads at all.
Please let me know if you have an idea of why my object is moving around when display gets called despite me not (knowingly) changing the values of gluLookAt or changing object coordinates.