2
votes

I was doing my OpenGL program and I happen to have a problem, can you find what is the matter with it?

It rotates a plane with the y axis as the centre when I am using glOrtho it is working fine, but gluPerspective it is not.

There is some problem with the gluPerspective because when I change the angle to 0 I can just see something but not the whole thing. But when I change it to 45. Nothing comes on the screen and I'm not getting the clue about the near and far values.

 #include iostream  
 #include "Xlib.h"  
 #include "gl.h"  
 #include "glu.h"  
 #include "glut.h"  
 #include math.h  
void setupRC()  
{  
    glClearColor(1,0,0,1);  
    glColor3f(0,0,0);  
}  
void timerfunc(int value)  
{  
    glutPostRedisplay();  
    glutTimerFunc(1, timerfunc ,1);  
}  
void RenderScene()   
{  
    glColor3f(0,0,0);  
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
    static GLfloat rot = 0.0f,x =0.0f , y=1.0f , z=0.0f;  
    rot++;  
    glPushMatrix();  

    glRotatef(rot,0.0,1.0,0.0);  
    glBegin(GL_POLYGON);  
        glVertex3i(1,-1,0);  
        glVertex3i(1,1,0);  
        glVertex3i(-1,1,0);  
        glVertex3i(-1,-1,0);  
        glVertex3i(1,-1,0);  

    glEnd();  
    glPopMatrix();  
    if (rot == 360)  
        rot = 0;  
    glutSwapBuffers();  
}  
void ChangeSize(GLint w, GLint h)  
{  
    if(h==0)  
        h = 1;  
    GLfloat aspectratio = (GLfloat)w/(GLfloat)h;  

    glViewport(0,0,w,h);  
    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();  
    gluPerspective(45.0f, aspectratio, 1,1000);  
    /*if(w <= h)  
        glOrtho(-100,100,-100/aspectratio, 100/aspectratio, 1,-1);  
    else   
        glOrtho(-100*aspectratio, 100*aspectratio , -100,100,1,-1);*/  
    glMatrixMode(GL_MODELVIEW);  
    glLoadIdentity();  

}  

int main(int argc , char **argv)  
{  
    glutInit(&argc, argv);  
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);  
    glutInitWindowSize(800,600);  
    glutInitWindowPosition(0,0);  
    glutCreateWindow("chelsea");  
    glutTimerFunc(1, timerfunc , 1);  
    setupRC();  
    glutDisplayFunc(RenderScene);  
    glutReshapeFunc(ChangeSize);  
    glutMainLoop();  

    return 0;  
}  
1
Perhaps create a more descriptive title?torger
Using complete words and punctuation makes your question easier to read and thus more lightly to get an answer.Nifle
oh k. wl see to that. Thanks for the feedbackyuneek
stackoverflow is supposed to check the right answer, if it fits to you.Yuriy Vikulov

1 Answers

9
votes

You problem is: your polygon and your camera are at the same coordinates. (The polygon is not in front of the camera, it is on it.)

  1. When the polygon is in its initial position, it is exactly aligned with the camera which cannot see it;
  2. when it begins rotating, a part of it goes in front of the camera, but it is too close to be displayed.

However, setting the fovy to 0° when building your perspective matrix yields a (really) wide angle camera, which is able to see (really) close objects -- explaining why you can see something, "but not the whole thing".

If you want to be able to see "the whole thing" with fovy=45, move your polygon away from the camera by applying a negative translation along the Z axis:

void RenderScene()
{
    ...
    glPushMatrix();
    glTranslatef(0.,0.,-10.); // Move away from camera
    glRotatef(rot,0.0,1.0,0.0);
    glBegin(GL_POLYGON);
        ...
    glEnd();
    glPopMatrix();
    ...
}