Problems with Reshape and Matrixmode

First of all, sorry for my bad english, but I’m not an native-speaker.

I’m interessted in learning OpenGL, but have some problems with it.
Here is a small code from
http://glprogramming.com/red/chapter03.html
<code>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix /
/
viewing transformation /
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (1.0, 2.0, 1.0); /
modeling transformation */
glutWireCube (1.0);
glFlush ();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
</code>
There is a function called ‘init’. For what kind of stuff is it? Why can’t we write the first lines of ‘int main’ in these function?
The function ‘reshape’ is for dealing with windows, which changed their height and width. When a window is changing his height/width, will only ‘reshape’ execute or also ‘display’?

The second big problem is, understanding OpenGL’s Matrixmode GL_MODELVIEW and GL_PROJECTION.
With GL_MODELVIEW, I’m able to paint my GL_QUADS, GL_LINES etc.
GL_PROJECTION will be used for perspective.
So far.
In the example I gave, ‘Display’, is no matrixmode are given. I think, GL_MODELVIEW is working to set my objects in the coordinatesystem up.
Why GL_PROJECTION is used in ‘Reshape’?
I’m not able to understand. Sometimes, MODELVIEW stands before PROJECTION, sometimes the other way.

Can you help me please?

Use square brackets for your code tags as explained in the Forum posting guidelines.

Yes, you can put the first lines in the init function. Your programming style will not necessarily match someone else’s style.

When a window is changing his height/width, will only ‘reshape’ execute or also ‘display’?

I think it will not. That is why it should call glutPostRedisplay or whatever that GLUT function was called.

GL_PROJECTION will be used for perspective.

In this case yes, it uses glFrustum which gives a perspective view.

In the example I gave, ‘Display’, is no matrixmode are given. I think, GL_MODELVIEW is working to set my objects in the coordinatesystem up.

The 2 matrices are setup by reshape (int w, int h).

Why GL_PROJECTION is used in ‘Reshape’?

Because setting it once is enough. If you want to move it to display(), go ahead.

I’m not able to understand. Sometimes, MODELVIEW stands before PROJECTION, sometimes the other way.

The 2 don’t have anything to do with each other.

Does it matter if the drive way is in front of your house or behind your house? Which do you prefer?
Whatever you prefer, you still will have 1 modelview matrix and 1 projection matrix.

Thanks for your answer.

Now, I want to create a own cube in the center of my system.
I added a view, what I want to do.
My code so far


#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>


void Init()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glEnable(GL_DEPTH_TEST);
}


void Display (void)
{
    glEnable(GL_DEPTH_TEST);
    // 0 blue   1 cyan  2 yellow  3 green
    // 4 magent 5 red   6 black  7 white
    
    GLfloat Col[8][3]={
        {0.0, 0.0, 1.0},
        {0.0, 1.0, 1.0},
        {1.0, 1.0, 0.0},
        {0.0, 1.0, 0.0},
        {1.0, 0.0, 1.0},
        {1.0, 0.0, 0.0},
        {0.0, 0.0, 0.0},
        {1.0, 1.0, 1.0}};

    GLfloat P[8][3]={
        {0.0, 0.0, 0.0},
        {1.0, 0.0, 0.0},
        {1.0, 0.0, 1.0},
        {0.0, 0.0, 1.0},
        {0.0, 1.0, 0.0},
        {1.0, 1.0, 0.0},
        {0.0, 1.0, 1.0},
        {1.0, 1.0, 1.0}};

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    glBegin(GL_QUADS);
        glColor3fv(Col[6]); 
        glVertex3fv(P[0]);

        glColor3fv(Col[5]); 
        glVertex3fv(P[1]);

        glColor3fv(Col[2]); 
        glVertex3fv(P[5]);
        
        glColor3fv(Col[3]); 
        glVertex3fv(P[4]);
    glEnd();

    glBegin(GL_QUADS);
        glColor3fv(Col[0]); 
        glVertex3fv(P[3]);

        glColor3fv(Col[4]); 
        glVertex3fv(P[2]);

        glColor3fv(Col[7]); 
        glVertex3fv(P[7]);
        
        glColor3fv(Col[1]); 
        glVertex3fv(P[6]);
    glEnd();
    
    glBegin(GL_QUADS);
        glColor3fv(Col[4]); 
        glVertex3fv(P[3]);

        glColor3fv(Col[5]); 
        glVertex3fv(P[1]);

        glColor3fv(Col[2]); 
        glVertex3fv(P[5]);
        
        glColor3fv(Col[7]); 
        glVertex3fv(P[7]);
    glEnd();
    
    glBegin(GL_QUADS);
        glColor3fv(Col[6]); 
        glVertex3fv(P[0]);

        glColor3fv(Col[0]); 
        glVertex3fv(P[3]);

        glColor3fv(Col[1]); 
        glVertex3fv(P[6]);
        
        glColor3fv(Col[3]); 
        glVertex3fv(P[4]);
    glEnd();
    
    gluLookAt(0.5, 1.5, 1.5, 0.5, 0.5 ,.5, 0, 1, 0);
    
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();
    glOrtho (-2, 2, -2, 2, -2, 2);
    gluPerspective(90, 1, 0.1, 100);
   glFlush();
}

void Reshape (int width, int height)
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glViewport(0,0,1024,768);
    
    gluLookAt(0.5, 1.5, 1.5, 0.5, 0.5 ,.5, 0, 1, 0);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90, 1024/768, 0.1, 100);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0.5, 1.5, 1.5, 0.5, 0.5 ,.5, 0, 1, 0);

}



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

    glutCreateWindow("Cube");
    
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_ALPHA |  GLUT_DEPTH );

    glutInitWindowPosition(0, 0);
    
    glutInitWindowSize(1024, 768);

    glutDisplayFunc(Display);

   glutReshapeFunc(Reshape);    

    Init();

    glutMainLoop();

    return 0;
}

My placed my camera with gluLookAt (im my picture: eye).
Now, I used gluPerspective to manage, what I want to see (in my picture it is the the eye’s view). When I compile, I see just ja white screen.

What went wrong?

I suggest that you load a identity matrix before doing anything to the modelview matrix, otherwise each matrix would accumulate.

glMatrixMode(GL_MODELVIEW);
gluLookAt(0.5, 1.5, 1.5, 0.5, 0.5 ,.5, 0, 1, 0);