PDA

View Full Version : perspective rendering



silicon_chipper
03-18-2008, 01:21 PM
Hi this is crazy but can anyone tell what is wrong with this code. I explain at the bottom.



struct tri{
float x1,y1,z1;
float x2,y2,z2;
float x3,y3,z3;
};

tri * trilist;
tri * trilist_head;
int bound;

bool init()

{


glClearColor(0.93f, 0.93f, 0.93f, 0.0f);



glEnable(GL_DEPTH_TEST);

glDepthFunc(GL_LEQUAL);

glClearDepth(1.0f);




ifstream infile("tris.dat");

infile>>bound;

trilist_head = (tri*)malloc(bound*sizeof(tri));


trilist=trilist_head;
for(int i =0;i<bound;i++)
{
infile>>trilist->x1;
infile>>trilist->y1;
infile>>trilist->z1;
infile>>trilist->x2;
infile>>trilist->y2;
infile>>trilist->z2;
infile>>trilist->x3;
infile>>trilist->y3;
infile>>trilist->z3;
trilist++;

}



return true;

}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();


trilist=trilist_head;


glBegin(GL_TRIANGLES);
for(int i=0;i<bound;i++)
{
glVertex3f(trilist->x1/10.f,trilist->y1/10.f, trilist->z1-15);
glVertex3f( trilist->x2/10.f,trilist->y2/10.f,trilist->z2-15);
glVertex3f(trilist->x3/10.f,trilist->y3/10.f,trilist->z3-15);

trilist++;
}
glEnd();


glFlush();

glutSwapBuffers();
}
void idle()

{


}
void resize(int w, int h)
{

glMatrixMode(GL_PROJECTION);

glLoadIdentity();



glViewport(0, 0, w, h);



gluPerspective(45.0f, 1.0f * w / h, 1.0f, 100);



glMatrixMode(GL_MODELVIEW);

glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);



glutInitWindowPosition(50, 50);

glutInitWindowSize(500, 500);



glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);



glutCreateWindow("12 - Perspective");



glutDisplayFunc(display);

glutReshapeFunc(resize);

glutIdleFunc(idle);



if (!init())

return 1;



glutMainLoop();



return 0;
}


This is the data set. If you don't want to figure out the data. Which I doubt you do. It is 6 triangles with positive vertices where x y and z are between 0 and 10.



6
0.16 4.7 2.24
1.16 5.7 3.24
2.16 4.7 3.24
2.9 6.44 4.98
3.9 7.44 5.98
3.9 6.44 4.98
3.9 6.44 4.98
3.9 7.44 5.98
4.4 6.94 5.48
4.94 6.48 6.02
5.94 7.48 7.02
5.94 6.48 6.02
5.94 6.48 6.02
5.94 7.48 7.02
6.44 6.98 6.52
4.64 8.18 6.72
5.64 9.18 7.72
6.64 8.18 7.72



I get a blank screen every time. I would really appreciate some help. I'm just doing this because my BSP tree is buggy and I want to be able to visualize an arbitrary set of triangles for debugging.

I should be able to figure this out, but I can't. Sorry. Thanks.

RenderBuffer
03-18-2008, 02:41 PM
You are enabling GL_DEPTH_TEST but you're not asking for a depth buffer when you call glutInitDisplayMode. You're also not clearing the depth buffer when you call glClear.

You might want to have your idle func call glutPostRedisplay().
I also like to have something like the following in my idle function:


#ifdef DEBUG
GLint gl_error = glGetError();
while (gl_error != 0) {
std::cerr << gluErrorString(gl_error) << std::endl;
gl_error = glGetError();
}
#endif