tags:

views:

51

answers:

2

Hey guys i was writing a program to draw a square in my XY plane and make it rotate 360 degree but it is not working. since i am in my initial stages of my opengl it is too hard for me to debug it myself

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

Any help is welcome Thank you in advance

+1  A: 

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

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

John Burton
how can we find what the near and far are?
yuneek
Set near to something very small like 0.1 - everything with a Z value smaller than this won't show. Same for the far parameter, everything greater than this will disappear.
schnaader
so sir where does this z value start?i mean if z = 0 means what and the one i have written there is what i took from superbible. they have used a -ve value.and also i have edited my program.just have a look again. i didnt do much with the gluperspective part. check the glRotatef that i have included.thank you
yuneek
You have to work it out for your application. Basically what is the smallest distance from the camera you want to see things and the greatest. It can't be 0. So 0.1 and 20.0 would be reasonable for your program if I'm understanding it correctly.z = 0 starts zero distance away from the "camera" position but you can't use that as the math doesn't work out if you do Less than zero makes no sense as you can't see things behing the camera position.You want to keep them as close to what you need as possible as the bigger they are the worse the precision in your z buffer will be.
John Burton
i am still in the shade,let me put it wat i understudis it thatthe near value is the distance between the camera and the origin ie z=0 and far value is the distance detween the camera and the far as i want the camera to see?
yuneek
+1  A: 

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.

schnaader
OK,sir let me try it. thank you and i have given the color in the setupRC()glColor3f
yuneek
Ah, didn't recognize that, OK.
schnaader
sir do check the edited code again
yuneek
i got it to work. Thank you for all the help. Thank you so much
yuneek