PDA

View Full Version : no rendering of object on screen????



abhi1988
06-30-2011, 01:32 AM
hello,

I really dont know whats wrong with the code.I am new to opengl and could not figure out ,why I am not seeing anything on screen.

#include<Windows.h>
#include<stdlib.h>
#include<gl/GL.h>
#include<gl/GLU.h>
#include<gl/glut.h>

using namespace std;

void initrendering()
{
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,0.3,0.8,1.0);
}

void keypres(unsigned char key, int w,int h)
{
switch(key){
case 27:exit(0);
}
}

void resize(int w,int h)
{
glViewport(0,0,w,h);
gluPerspective(60,(double)w/h,1.0,200.0);
}

bool movingup=false;
float trans=0.0f;
float rotat=0.0f;

void drawscene()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0,0.0,-5.0);

glTranslatef(0.0,trans,0.0);
glRotatef(rotat,0.0,1.0,0.0);

glutWireCube(1.0);

glPopMatrix();
// glFlush();

if (movingup)
trans -= 0.005f;
else
trans += 0.005f;

if (trans < -3.0f)
movingup = false;
else if (trans > 3.0f)
movingup = true;
rotat += 0.005f;

if (rotat > 360.0f)
rotat -= 360.0f;

glutSwapBuffers();
}


int main(int argc, char** argv) {

glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);

glutCreateWindow("UP AND DOWN");
initrendering();

glutDisplayFunc(drawscene);
glutIdleFunc(drawscene);
glutKeyboardFunc(keypres);
glutReshapeFunc(resize);

glutMainLoop();
return 0;
}


Please help, most of my codes are not rendering anything on screen.

Regards

Abhinav

mobeen
06-30-2011, 09:52 PM
General problem: Please always include you code in the [ code ][/ code ] tags.

This problem: Two things.
1) You have depth test enabled and you are not clearing the depth buffer bit. Change the glClear call to this.


glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);


2) You need to set the current matrix to projection before calling gluPerspective. So the resize function becomes this,


void resize(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,(double)w/h,1,200.0);
}


See if this helps.

abhi1988
07-01-2011, 11:56 PM
Thanks for the reply...
and yes it worked..thanks again