PDA

View Full Version : Newbie questions!



EeeK
07-12-2000, 05:20 AM
These are the two questions I have encountered so far:
- when I wanted to draw a square using:

glBegin(GL_POLYGON); or glBegin(GL_QUADS);
glVertex3f(-5.0, -5.0, 0.0);
glVertex3f(5.0, -5.0, 0.0);
glVertex3f(-5.0, 5.0, 0.0);
glVertex3f(5.0, 5.0, 0.0);
glEnd();

the output is not a square but a polygon. Why?

- I got myself the Redbook and most of the examples does not require function protoype. I cut and pasted and compiled it without error but when I tried to write out 1 without function prototype, my compiler give me errors. Why is that so?

Thanks for the reply!

[This message has been edited by EeeK (edited 07-12-2000).]

Relic
07-12-2000, 06:17 AM
Watch out for the vertex winding order.
Your vertices are not building a convex polygon.
Try swapping the last two vertices to get counterclockwise orientation of the four vertices like this:

glBegin(GL_POLYGON); or glBegin(GL_QUADS);
glVertex3f(-5.0, -5.0, 0.0);
glVertex3f(5.0, -5.0, 0.0);
glVertex3f(5.0, 5.0, 0.0);
glVertex3f(-5.0, 5.0, 0.0);
glEnd();

Though with glBegin(GL_QUAD_STRIP) your order would have worked. Puzzled? http://www.opengl.org/discussion_boards/ubb/wink.gif

If you have
#include <GL/gl.h>
#include <GL/glu.h>
in you source files using OpenGL everything should compile fine.

"- I got myself the Redbook and most of the examples does not require function protoype. I cut and pasted and compiled it without error but when I tried to write out 1 without function prototype, my compiler give me errors. Why is that so?
"

Depending on the function you either have not included the file with the implementation into your project, not included the header and the lib containing the prototype and the implementation.
But that's a wild guess wiothout knowing what the function and the progrma looks like.

Hope that helps.



[This message has been edited by Relic (edited 07-12-2000).]

EeeK
07-14-2000, 01:48 AM
So you mean that in building a convex polygon, the vertices must be in the order of counter-clockwise? I tried and it work but how about with the z-coordinates?

glBegin(GL_POLYGON); or glBegin(GL_QUADS);
glVertex3f(-5.0, -5.0, 2.0);
glVertex3f(5.0, -5.0, 2.0);
glVertex3f(5.0, -5.0, -2.0);
glVertex3f(-5.0, -5.0, -2.0);
glEnd();

The above vertices are in counter-clockwise but the output is not a square. Why?

07-14-2000, 08:39 AM
Probably... your verts are getting clipped out because they are to close....

Question are you using perspective or ortho viewing?

If you want a square... try this

void init()
{
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
}


and


void display()
{

glClear (GL_COLOR_BUFFER_BIT); //clear the screen with black

glColor3f(.5,.75,0.3); //change to a green color
glBegin(GL_TRIANGLES); //begins the declaration of a triangle
//vertices
glVertex3f(.3,.5,0.0);
glVertex3f(.7, .5, 0.0);
glVertex3f(.5, .25, 0.0);
glEnd(); //end declaration

glColor3f(1.0, 1.0, 0); //change to yellow
glBegin(GL_POLYGON); //declare a polygon
//define verts
glVertex3f(0.15, .4,0.0);
glVertex3f(0.1, .3,0.0);
glVertex3f(0.2, .2,0.0);
glVertex3f(0.25, .2, 0.0);
glVertex3f(0.3,.1,0.0);

glEnd(); //end declaration

glColor3f(1.0, 0, 0); //change to red
glBegin(GL_POLYGON); //declare new poly
//define verts
glVertex3f(0.45, 0.9, 0.0);
glVertex3f(0.35, 0.8,0.0);
glVertex3f(0.35, 0.7, 0.0);
glVertex3f(0.45,0.6,0.0);
glVertex3f(0.55,0.6,0.0);
glVertex3f(0.65,0.7,0.0);
glVertex3f(0.65, 0.8,0.0);
glVertex3f(0.55,0.9, 0.0);

glEnd(); //end declaration
glFlush(); //perform all buffered gl instruction
//paint all polys

}

07-14-2000, 08:45 AM
poop im sorry just use this

void init()
{
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
}

//this function paints the window
void display()
{

glClear (GL_COLOR_BUFFER_BIT); //clear the screen with black

glColor3f(.5,.75,0.3); //change to a green color
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush();


}

Shadow
07-14-2000, 08:30 PM
Eeek, your second example is a square but
the output is what you see in real life.

its smaller when its far away

Relic
07-15-2000, 12:27 PM
"So you mean that in building a convex polygon, the vertices must be in the order of counter-clockwise?"

To be precise, it actually does not matter if the orientatoin is clockwise or counter-clockwise as long as the polygon is convex. You can select the winding order in OpenGL with glFrontFace() and default is GL_CCW (counter-clockwise).
You'll find OpenGL easier to learn if you keep some defaults.

EeeK
07-19-2000, 02:55 AM
Ok, here is part of my source for creating my first 3D application:

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0); //red
glVertex3f(-6.0, -6.0, 6.0); //left
glColor3f(0.0, 1.0, 0.0); //green
glVertex3f(6.0, -6.0, 6.0); //right
glColor3f(0.0, 0.0, 1.0); //blue
glVertex3f(0.0, 6.0, 0.0); //top

glColor3f(0.0, 1.0, 0.0); //green
glVertex3f(-6.0, -6.0, -6.0);
glColor3f(1.0, 0.0, 0.0); //red
glVertex3f(6.0, -6.0, -6.0);
glColor3f(0.0, 0.0, 1.0); //blue
glVertex3f(0.0, 6.0, 0.0);

glColor3f(1.0, 0.0, 0.0); //red
glVertex3f(-6.0, -6.0, 6.0);
glColor3f(0.0, 1.0, 0.0); //green
glVertex3f(-6.0, -6.0, -6.0);
glColor3f(0.0, 0.0, 1.0); //blue
glVertex3f(0.0, 6.0, 0.0);

glColor3f(0.0, 1.0, 0.0); //green
glVertex3f(6.0, -6.0, 6.0); //left
glColor3f(1.0, 0.0, 0.0); //red
glVertex3f(6.0, -6.0, -6.0); //right
glColor3f(0.0, 0.0, 1.0); //blue
glVertex3f(0.0, 6.0, 0.0); //top
glEnd();
glBegin(GL_QUADS);
glVertex3f(-6.0, -6.0, -6.0);
glVertex3f(-6.0, -6.0, 6.0);
glVertex3f(6.0, -6.0, 6.0);
glVertex3f(6.0, -6.0, -6.0);
glEnd();
glutSwapBuffers();
}

Why does color of the last polygon always override the color even it is at the back.
Please point out what mistakes I have made, thanks!

[This message has been edited by EeeK (edited 07-19-2000).]

Relic
07-19-2000, 07:40 AM
You mean why is the color of the quad always the same color as the last triangle vertex (here blue)?

This is by design. OpenGL is a state machine.
All commands changing one of the current states stay valid until the next change occurs. In your case the last time you set the color to blue, this color will be taken for all vertices thereafter until you issue the next color command.
Put another glColor call before the glBegin(GL_QUADS) and you'll see.

EeeK
07-19-2000, 08:06 PM
I found out what is wrong, I did not put the glEnable(GL_DEPTH_TEST); in the init().

Thanks for you reply thought.