0

I have a camera class and a Vector3 class(obvious what they mean), the Vector3 class constructor takes three float arguments each has a default value of zero, so calling Vector3() is equivalent to Vector3(0,0,0).

The camera class constructor also takes three arguments each is a Vector3, the positing , the viewpoint and the up vector.

here's my code, going to explain problem later :

#include "camera.h"
Vector3 StartPoint(1000,1000,1000);

Camera camera(Vector3(1000,1000,1100),Vector3(StartPoint),Vector3(0,1));
void resize(int width, int height)
{
    const float ar = (float) width / (float) height;
    glViewport(0, 0, width, height);    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2, 1000.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    camera.LookAt();
}   
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    camera.LookAt();   
    //draw some things
    glutSwapBuffers();
}   
void idle(void)
{    
    glutPostRedisplay();
}    

/* Program entry point */
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(300,200);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Tischfussball");
    glutReshapeFunc(resize);    
    glutDisplayFunc(display);   
    glutIdleFunc(idle);   
    glutMainLoop();
    return EXIT_SUCCESS;
} 

I have a camera class which works great with the code above, but when removing glLoadIdentity line from display function the screen appears just blank !

I searched and found something about idle function so I moved glLoadIdentity with camera.LookAt to idle function and didn't work.

I don't want to put code that I don't understand and I want to remove that line because I don't see any necessity in my logic, I mean glLoadIdentity just restores the matrix to the identity matrix(depending on last MatrixMode, it restores the MODELVIEW matrix here), it's not necessary at all to call it at each display(it's waste of performance I think).

I'm really confused, how does this work ?!!

EDIT

camera.LookAt() is equivalent to gluLookAt(camera.position.x,camera.position.y,camera.position.z,camera.view.x,camera.view.y,camera.view.z,camera.up.x,camera.up.y,camera.up.z)

there is no call to glTranslate* or glRotate* in the //draw some things part.

And the only calls to change camera variables are called on key event, I see the blank screen just after the window appears(no key pressed yet), I press some keys with no use.

niceman
  • 2,653
  • 29
  • 57

2 Answers2

2

When you call functions like glTranslate*() or glRotate*() in your camera class (or a function is called which uses them, like gluLookAt()), the functions will NOT set your view matrix to a specific state, but just transform them.

That means when this functions are called over and over by your display function without calling glLoadIdentity() between every frame, which sets the matrix to the identity state, the rotation or transformation will be summed up what probably leads to a black screen because after some iterations your camera is in the middle of nowhere.

And don't be afraid of calling such functions for performance reasons. You have to imagine that an application like a game calls thousands of matrix manipulation functions every frame. So one call won't kill any performance.

And i know this isn't the subject, but i can highly recommend you to use a newer OpenGL API. The way you do it is already deprecated.

Just google for something like "opengl 4 tutorial" or "modern opengl" to get in touch with a more up to date way to use OpenGL.

Edit:

Like you already said, there are no calls of matrix manipulation functions in your camera class. But the gluLookAt() function calls such functions.

You have to keep in mind that there is nothing like a 'camera' in OpenGL. The only way to move the virtual camera is to change the view matrix, and this is also done by functions like gluLookAt().

randhash
  • 463
  • 4
  • 15
  • hmmmm does this mean I can't call glRotatef ? I call them and the screen goes blank ! – niceman May 03 '15 at 19:11
  • You can call it, but the function won't set the matrix to a specific state but it will multiply the old matrix with a new So for example, when your camera gets rotated by 40 degree in your camera function, it will be rotated again by 40 degree in the next frame when you don't call `glLoadIdentity` The same goes for `glTranslate` and `gluLookAt` which calls these functions – randhash May 03 '15 at 19:15
0

I dont' know how your camera.lookAt() is exactly implemented, but I'm making the educated guess that it uses something like gluLookAt(). Now gluLookAt() (like all of GL matrix functions except glLoad*()) multiplies the current matrix of the currently selected matrix mode by the LookAt matrix it creates internally. In most cases, you want that current matrix to be the Identity, otherwise the camera transformation will not be what you expected.

I also guess that your //draw some things code actually modifies the GL_MODELVIEW matrix, so that it is not left at what it was after camera.LookAt() returned - which is perfectly fine, since the GL_MODIELVIEW matrix is the composition of the model matrix (which can vary per object) and the view matrix (which is typically constant throughout the whole frame).

So when you don't reset it, the transformations of the previous frame will accumulate, very likely moving your scene completely out of view.

derhass
  • 43,833
  • 2
  • 57
  • 78