Domanda

Just fooling around with opengl and glut. I was trying to make cube full visibe so I decided that I could make it with gluPerspective(); So here's the code

#include <iostream>
#include <GL/glut.h>
#include <GL/gl.h>

using namespace std; 

float _angle = 0.1f;

void render(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();      

    gluPerspective(90.0, 1, 0.1, 100.0); 
    gluLookAt(1, 1, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);

    glPushMatrix();  
      glRotatef(_angle, 0.0f,0.0f,1.0f);
      glColor3f(1.0f,0.0f,0.0f);
      glutSolidCube (1.0);
    glPopMatrix();
    _angle +=0.03f; 

    // check OpenGL error
    GLenum err;
    while ((err = glGetError()) != GL_NO_ERROR) {
        cerr << "OpenGL error: " << err << endl;
    }

   glutSwapBuffers();
   glutPostRedisplay();


}

void init(){
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

    glutInitWindowPosition(100,100);
    glutInitWindowSize(640, 480);
    glutCreateWindow("test");
    glClearColor(0.2,0.2,0.2,0.0);

    glEnable(GL_COLOR_MATERIAL);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glutDisplayFunc(render);

    glutMainLoop();
}


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

    glutInit(&argc, argv);
    init();
    return 0;
}

and that is result:

enter image description here

how part of cube looks without gluPerspective(): enter image description here

How can I make it looks normal?

È stato utile?

Soluzione

I would not necessarily say that the normals are inverted when you mess with the projection matrix, as much as the winding direction is reversed.

OpenGL determines polygon winding, which is used for culling front/backward facing polygons, after projection. When your projection matrix and modelview matrix do not agree on which direction Z should face (in other words handedness) you will get these weird sorts of issues.

Traditionally, the projection matrix is setup to invert Z when it is multiplied by a modelview matrix in OpenGL. The fixed-function pipeline has always worked this way, and this is what produces OpenGL's right-handed coordinate system. If you flip Z coordinates you change the direction vertices are wound post-projection.

Having said all of that, I do not think this is actually your problem. You are using per-vertex lighting here, and you have combined the modelview matrix with the projection matrix. When it comes time to do per-vertex lighting calculations they are being done in the wrong coordinate space. I would suggest that you separate modelview from projection...

glMatrixMode   (GL_PROJECTION);
glLoadIdentity ();

gluPerspective (90.0, 1, 0.1, 100.0);

glMatrixMode   (GL_MODELVIEW);
glLoadIdentity ();      

gluLookAt      (1, 1, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);

Now you have properly separated your transformation matrices and fixed-function lighting among other things should work as expected.

Update:

In addition to what I described above, there is one other thing you forgot to do. Depth testing is disabled by default in OpenGL (since there is no guarantee that you even have a depth buffer); you need to enable it.

glEnable (GL_DEPTH_TEST);

And your aspect ratio is not setup correctly when you call gluPerspective (...), it should be 640.0f/480.0f instead of 1.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top