Perspective

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective

Post by Benjamin100 »

Pornomag wrote:OK I honestly just tried to get your code working (which I did), it does work with 0 degrees field of view, it just doesn't shrink the triangle for some reason
Yes, I know, WHY?
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Perspective

Post by qpHalcy0n »

Watch your attitude, kid.

Code: Select all

void initialize_it(void)   //sets up graphic stuff.
{
   glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
   glutInitWindowSize(400,200);
   glutInitWindowPosition(100,50);
   glutCreateWindow("Moving Square");
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(45.0f, ScreenWidthInPixels / ScreenHeightInPixels, 0.1f, 100.0f);
//   glEnable(GL_DEPTH_TEST);        // Don't need depth testing 
//   glDepthFunc(GL_LEQUAL);

    // Perspective is a "camera" idea, since you have a global camera in this demo we can just set the perspective once //
    // There is no "view" transform in your case. However, you have a model transform. It does not make sense to  //
    // apply model transforms in initialization routines, these are applied per frame //
//   glMatrixMode(GL_MODELVIEW);
//   glLoadIdentity();
}

Code: Select all

void rect_display(void)   
{
   glClearColor(0, 0, 0, 0);   
   glClear(GL_COLOR_BUFFER_BIT); 

    // Make the model/view matrix the current one. All matrix ops happen to the top matrix on the model/view matrix stack //
    // Load an identity matrix into the top matrix of this stack //
    // glTranslatef multiplies the current matrix (identity) with a translation matrix //
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0, 0,translationAmount);
   translationAmount-=0.0001;
   glBegin(GL_TRIANGLES);
        
        glColor3f(0.0, 0.0, 1.0);  //front.
        
        glVertex3f(0.3, 0.8, 0.2);
        glVertex3f(0.3, 0.2, 0.2);
        glVertex3f(0.8, 0.8, 0.2);
   
   glEnd();
   
//   glPopMatrix();
   
   glFlush();
   glutSwapBuffers();
   glutPostRedisplay();  
}

This code works. Your problem is that you were pushing the model/view matrix stack down and not loading a matrix into it. When you call glTranslate, there is garbage there and glTranslate implicitly invokes glMultMatrix. This multiplies the translation matrix with the garbage on the top of the stack. You need to load something into the top of the stack after you push. You need to focus much more heavily on transformations and basic data structures. Absolutely none of this code should work with a 0 degree FOV (All geometry will be clipped).
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective

Post by Benjamin100 »

Sorry.

OK, I'll look over some of that.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective

Post by Benjamin100 »

After taking a break I came back to it and now it seems to be working.

Thank you,

-Benjamin
Post Reply