PDA

View Full Version : Simple Vertex Problem



openujs
09-06-2005, 07:06 AM
Why would the following code draw line but the bottow one does not.
glBegin(GL_LINES);
glVertex3f( 0.0,0.0, 0.0 );
glVertex3f( 10.0, 10.0, 0.0 );

glBegin(GL_LINES);
glVertex3f( 10.0,0.0, 0.0 );
glVertex3f( 10.0, 10.0, 0.0 );

Does every line has to be started from 0,0,0 coordinates?

Even this one does not work-
glBegin( GL_TRIANGLES );
glVertex3f(0.0f, 0.0f, 60.0f);
glVertex3f(-15.0f, 0.0f, 30.0f);
glVertex3f(15.0f,0.0f,30.0f);

09-06-2005, 07:14 AM
Don't forget you need a glEnd(); command after ever line or poly you draw to tell OpenGL you've finished drawing that object.

Try that first. :)

openujs
09-06-2005, 12:58 PM
oh yes. i have glEnd() at the end. but point being why the bottom 2 does not draw, are coordinates off?

Hlz
09-06-2005, 09:07 PM
Post the rest of your render code. You've probably done something silly like drawing black lines on a black background, or misconfigured your projection/camera matrix, or tried to begin a primitive in the middle of another one, or some sinister combination of all the above.

Is your app running without errors?

:-)

openujs
09-08-2005, 04:30 AM
void drawScene( void )
{

glClear( GL_COLOR_BUFFER_BIT );
//Use black pen
glColor3f( 0.0. 0.0,0.0 );

//Working
glBegin(GL_LINES);
glVertex3f( 0.0,0.0, 0.0 );
glVertex3f( 10.0, 10.0, 0.0 );

//Not working
//glBegin(GL_LINES);
//glVertex3f( 10.0,0.0, 0.0 );
//glVertex3f( 10.0, 10.0, 0.0 );

//Even this one does not work-
//glBegin( GL_TRIANGLES );
//glVertex3f(0.0f, 0.0f, 60.0f);
//glVertex3f(-15.0f, 0.0f, 30.0f);
//glVertex3f(15.0f,0.0f,30.0f);

glFlush();
glutSwapBuffers();
}

void init(void )
{

glClearColor( 1.0,1.0,1.0,1.0);
}

openujs
09-08-2005, 04:34 AM
and the last part is

int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
glutCreateWindow("test");
glutDisplayFunc( drawScene );
Init();
glutMainLoop();
return 0;
}

openujs
09-08-2005, 04:35 AM
hmm, i again forgot glEnd() at my drawScene(), assume it is there.

09-08-2005, 06:04 AM
Each glBegin() has to be closed by an glEnd(). Calling glBegin() twice without an closing glEnd() will result in an GL_INVALID_OPERATION condition and all following commands will be ignored.

So try this instead:


void drawScene( void )
{
glClear( GL_COLOR_BUFFER_BIT );
//Use black pen
glColor3f( 0.0. 0.0,0.0 );

//Working
glBegin(GL_LINES);
glVertex3f( 0.0,0.0, 0.0 );
glVertex3f( 10.0, 10.0, 0.0 );
glEnd();

glBegin(GL_LINES);
glVertex3f( 10.0,0.0, 0.0 );
glVertex3f( 10.0, 10.0, 0.0 );
glEnd();

glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 0.0f, 60.0f);
glVertex3f(-15.0f, 0.0f, 30.0f);
glVertex3f(15.0f,0.0f,30.0f);
glEnd();

glutSwapBuffers();
}

openujs
09-08-2005, 06:44 AM
I understand there should be only one glBegin() and glEnd(). My problem is some where, probably with vertex coordinates. Try the following code, if it draws for you or not. Supposed to draw a triangle, but it does not render.

void drawScene( void )

{

glClear( GL_COLOR_BUFFER_BIT );

//Use black pen

glColor3f( 0.0. 0.0,0.0 );

//Working

glBegin(GL_TRIANGLES);

glVertex3f(0.0f, 0.0f, 60.0f);

glVertex3f(-15.0f, 0.0f, 30.0f);

glVertex3f(15.0f,0.0f,30.0f);

glEnd();

glutSwapBuffers();

}

Relic
09-08-2005, 07:07 AM
If this is all your code (matrix setup at default) it's obvious why the first line draws (one tenth of its actual length) and no other.
Having the default identity matrix in both projection and modelview means the same as an glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0) setup with no changes to input vertex coordinates.
That is, everything which is not in the range between -1.0 and 1.0 in (x,y,z) is outside the viewing frustum.

09-08-2005, 09:11 AM
"matrix setup at default"
Having the default identity matrix in both projection and modelview means the same as an glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)
setup with no changes to input vertex coordinates.
That is, everything which is not in the range between -1.0 and 1.0 in (x,y,z) is outside the viewing frustum."

Relic,
where am I setting Matrix as default for both projection and modelview? what piece of code do I have to add in order other two to work? or How do I enhance the viewing frustum range? I am new to Opengl progamming.

Hlz
09-08-2005, 05:27 PM
Have you looked at the redbook?

Hlz

Relic
09-08-2005, 10:15 PM
What Hlz said ^^^^^^
OpenGL has a default state for everything, you didn't change the matrices.

Your geometry lies in the space of xyz coodinates (-15, 0, 0) to (15, 10, 60).
Lets assume your window is square then a cube with the maximum extend would hold all geometry.

Add this in your init function:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-60.0, 60.0, -60.0, 60.0, -60.0, 60.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

This will map the object space (input vertex coordinates) origin into the center of the window and the window edges are at coodinates +-60.
Your program will show all your given primitives then.

This is not perspective, farer objects don't get smaller.

To do this try this instead of the above code:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0, 1.0, 121.0); // field of view, aspect ratio, zNear (must be > 0), far (must be > zNear)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -61.0f); // Move eye position back to have the object space origin (0,0,0) in the center of the z-range. Read the redbook!

Note that I chose the parameters to have z = 60 exactly on the zFar plane. Decrease zFar and see how your triangle gets clipped.

Play with the field of view angle parameter of gluPerspective. That's the same as zooming with a camera.

09-09-2005, 10:51 AM
Relic,
Thanks for your post. Now I see where the problem is. Realizing that I need to grasp the understanding of Projection, ModelView transformation, I read the redbook. But even after reading those chapters 2 times, I failed the master the concept. For Example Why glLoadIdentity() is used and so on.
So Would you be kind and explain the projection concept with examples. You can email me if you want to us1915@hotmail.com

09-13-2005, 04:00 PM
Anyone can enlighten why nothing is being rendered using following code:

#include <windows.h>
#include<gl/glut.h>
#include<math.h>

// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//Set drawing color to white
glColor3f( 1.0,1.0, 1.0 );

glBegin( GL_TRIANGLES );
//glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 60.0f);
glVertex3f(-15.0f, 0.0f, 30.0f);
glVertex3f(15.0f,0.0f,30.0f);

glEnd();

glFlush();
glutSwapBuffers();

}

// Setup the rendering state
void SetupRC(void)
{
// Set clear color to blue
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// glViewport(0, 0,1200, 1200);
/*
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-160.0, 160.0,-160.0, 160.0, -160.0, 160.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 2.0, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glTranslatef(0.0f, 0.0f, 61.0f);
}

// Main program entry point
void main(void)
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Test");
SetupRC();
glutDisplayFunc(RenderScene);
glutMainLoop();
}

Relic
09-13-2005, 10:16 PM
Ok, some excursion on coordinates.
Object coordinates are in a right-handed system with z-axis pointing to you. Make the famous computer graphics aerobics with thumb = x-axis, forefinger = y-axis, middle finger = z-axis.
There z-coodinates 30 and 60 as you use are that many units away from your hand in z-axis direction.
The viewer is always at the modelview origin. This coodinate system is still right handed.
Now the scary part. OpenGL's perspective and screen coordinates are left handed. In screen coordinates zNear is mapped to 0.0, zFar is mapped to 1.0 and the depth compare is less per default, means xy remained, z-axis inverted. Use your left hand.

So what does that all mean?
To get your coordinates in front of the viewer (remember, at (0,0,0) in modelview right-handed coodinates) you must move all the geometry in -z direction, that is away from you in the modelview coordinate. That's analogous to moving a "camera" backwards, but this is how it's done in OpenGL.
Now how far? The perspective frustum you gave goes from zNear = 1.0 to zFar = 1000.0 which is unnecessary big, but will work.
The biggest z-coodinate is at 60, so you need at least -(60+zNear) units to get it exactly on the front plane. (Oops, just noticed I forgot the minus in my previous post. I edited it to be correct. Sorry about that.) You want it a little further back.
Uncomment the glTranslatef call I gave you and use a z-value like -100, or -200 or many others which move the geometry in front of the viewer.

You should do the experiments I mentioned. They really make sense for an OpenGL beginner.