PDA

View Full Version : Rectangular mesh not rendered



iris_raj
09-20-2004, 09:23 PM
I want to render a rectangluar mesh ( 5 by 5). I have written the following code. But nothing is getting displayed.What may be possible reason.



void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}

void display(void)
{
float x, y;
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity(); /* clear the matrix */
/* viewing transformation */
for (x = -5; x <= 5; x += 1.0f) // x += 1.0f Stands For 1 Meter Of Space In This Example
{
glVertex3f(x, 5, 0);
glVertex3f(x,-5, 0);
}

// Draw The Horizontal Lines
for (y = -5; y <= 5; y += 1.0f) // y += 1.0f Stands For 1 Meter Of Space In This Example
{
glVertex3f( 5, y, 0);
glVertex3f(-5, y, 0);
}
glFlush ();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if(w<=h)
gluOrtho2D(0.0,1.0,0.0,1.0*(GLfloat)h/(GLfloat)w);
else
gluOrtho2D(0.0,1.0*(GLfloat)h/(GLfloat)w,0.0,1.0);
glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

mikael_aronsson
09-20-2004, 09:52 PM
Hi !

There are a number of problems, you use an identity matrix for the model view, so you end up at 0,0,0 and you put your vertices with z=0, you need to move the "camera" away a bit along the z axis at least, using gluLookAt or glTranslate.

And finally you use glVertex calls without a glBegin() first... you must specify what to render with the glVertex calls, like glBegin( GL_QUADS); and put a glEnd(); after the last call to glVertex, it is possible that there are more problems what that's a few to get you started.

Note: use glGetError() to check for errors.

Mikael

09-21-2004, 12:29 AM
Thanks for you suggestion. I have made the following changes. But it is still not rendering.

1. Added glBegin and glEnd() before rendering the mesh
2. Added glTranslatef(0.0f,0.0f,5.0f);

But it is not still rendering

Relic
09-21-2004, 12:43 AM
The error is in the gluOrtho2D calls and the glTranslate and the GL_QUADS in the previous advice were both wrong. You need to put a glBegin(GL_LINES), glEnd() around the loops, quads are defined differently.

The vertices you send are in the range from (x, y) = ([-5,5], [-5,5]). Z is always 0.
Your glOrtho call only defines a space from about 0.0 to 1.0 to draw, instead you need to do
glOrtho(-5.0, 5.0, -5.0, 5.0, -1.0, 1.0) to get the geometry into your viewing space. (Multiply with the aspect ratio like you did to get squares.)
To translate the geometry with glTranslate(0,0,5) will put them behind the zfar plane and not display anything, don't do that. It's necessary for perspective viewing, not for ortho.

09-21-2004, 01:19 AM
Thanks sir, It is now rendering the mess