OpenGL: Shape changes position when display window pressed

时间:2018-03-25 19:19:14

标签: c opengl glut

I'm having trouble when I run my program that when I press the window where my 3D shape it changes position. When I press it for the second time, the shape would disappear.

Here is what my program looks like:

GLsizei winWidth = 500, winHeight = 500;
int userLongitude, userLatitude;
GLenum drawStyle;
bool isSolid = false;

void keyboardFunc(unsigned char, int, int);

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

void keyboardFunc(unsigned char Key, int x, int y) {
switch (Key) {
case 's':
    //drawStyle = GLU_FILL;
    isSolid = true;

    break;
case 'w':
    //drawStyle = GLU_LINE;
    isSolid = false;
    break;
}
//cout << Key << endl;
//glutPostRedisplay();
}
void wireQuadSurfs(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0, 0.0, 1.0);

gluLookAt(1.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);


GLUquadricObj *cylinder;
glPushMatrix();
glTranslatef(0.0, 1.2, 0.8);
glRotatef(45, 1, 0, 0);
cylinder = gluNewQuadric();


//cylinder, baseRad, topRad, height, slices, stacks
//gluCylinder(cylinder, 0.6, 0.6, 1.5, userLongitude, userLatitude);
//gluQuadricDrawStyle(cylinder, GLU_LINE);
//gluQuadricDrawStyle(cylinder, drawStyle);

if (isSolid) {
    gluQuadricDrawStyle(cylinder, GLU_FILL);
}
else {
    gluQuadricDrawStyle(cylinder, GLU_LINE);
}
gluCylinder(cylinder, 0.6, 0.6, 1.5, userLongitude, userLatitude);
glPopMatrix();
glFlush();
}

void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glViewport(0, 0, newWidth, newHeight);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, 0.0, 5.0);

glMatrixMode(GL_MODELVIEW);

glClear(GL_COLOR_BUFFER_BIT);
}

void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("CSC 313 HW 8 Part 2");

cout << "Enter number of longitude: ";
cin >> userLongitude;
cout << "Enter number of latitude: ";
cin >> userLatitude;
cout << "Switch between solid and wireframe views with 's' and 'w' keys" << endl;

glutKeyboardFunc(keyboardFunc);
init();
glutDisplayFunc(wireQuadSurfs);
glutReshapeFunc(winReshapeFcn);
glutMainLoop();
}

What I'm trying to do with my program is switch between the solid and wireframe of my shape. But when I click on the display window, it would change positions and then disappear from view. I'm not sure if it's because of my keyboard function or something else. Sorry for the lack of details, thank you for letting me know!

1 个答案:

答案 0 :(得分:1)

You have to initilaize the projection matrix stack with the identity matrix, before you set the orthograohic projection (see glLoadIdentity):

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, 0.0, 5.0);

Note, glOrtho multiplies the current matrix by the orthographic projection matrix.
This means it does not assign a new 4*4 matrix, it multiplies the current matrix by the new matrix and assigns the result to the matrix stack. Every time when winReshapeFcn is called, the matrix on the projection matrix stack would change progressively, in the code of your question.

If you call glLoadIdentity, then the matrix stack is initilized by the identity matrix. The call of glOrtho multiplies the identity matrix by the new orthographic projection. The result is the orthographic projection itself.