yuneek
yuneek

Reputation: 145

why does not my opengl program work?

I was writing a program to draw a square in my XY plane and make it rotate 360 degree, but it is not working.

void setupRC()  
{  
    glClearColor(0,0,1,1);  
    glColor3f(1,0,0);  
}  
void timerfunc(int value)  
{  
    glutPostRedisplay();  
    glutTimerFunc(33, timerfunc ,1);  
}    
void RenderScene()   
{  
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
    glMatrixMode(GL_MODELVIEW);  
    glLoadIdentity();  
    static GLfloat rot = 0.0f,x =0.0f , y=10.0f , z=0.0f;  
    rot++;  
    x = 10 * cos(rot);  
    y = 10 * sin(rot);  
    glPushMatrix();
glRotatef(rot,0.0,1.0,0.0);
glBegin(GL_POLYGON);
    glVertex3i(10,-10,0);
    glVertex3i(10,10,0);
    glVertex3i(-10,10,0);
    glVertex3i(-10,-10,0);
    glVertex3i(10,-10,0);
    glEnd();  
    glPopMatrix();  
    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(60.0f, aspectratio, -1.0, 400.0);  
    glMatrixMode(GL_MODELVIEW);  
    glLoadIdentity();  

}  

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

    return 0;  
}  

I get no output, just a blank screen.

Upvotes: 1

Views: 358

Answers (2)

schnaader
schnaader

Reputation: 49731

You should really use glRotatef for rotating, because your code is wrong if you want a plane rotating around XY (x and y will be 0 for some values of rot and you'll get to see nothing at all).

And you should perhaps give your polygons a color using the glColor commands.

As JB said, your gluPerspective call seems wrong, too. See this link for a reference.

Upvotes: 1

jcoder
jcoder

Reputation: 30055

gluPerspective(60.0f, aspectratio, -1.0, 400.0);

Looks wrong, the near clipping plane needs to be a positive number

Upvotes: 2

Related Questions