PDA

View Full Version : glVertex3f() ???



kuniva
04-01-2005, 12:45 PM
Hello, i recently got into coding with opengl, and i've got a question about the glVertex function.
Lets say i have this code. (note that its just the part i have a question about i left the rest out.)


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

// Draw the line
glBegin(GL_LINES);
glVertex3f(0.0f, 90.0f, 0.0f); //??? draws a vertical line upwards...
glVertex3f(0.0f, 0.0f, 0.0f); //???
glEnd();


// Flush drawing commands
glutSwapBuffers();
}This codes draw a vertical line for me. But the thing i dont understand is... How does it know when to draw vertical or horizontal etc.. I tried playing with the function and add numbers in it to see what the output would show me but it keeps confusing me.
I know the first values are (x, y, z) but when does it know when to draw vertically or horizontal?? Can someone please clear this up for me.

ZbuffeR
04-01-2005, 01:10 PM
from the doc :


GL_LINES :
Treates each pair of vertices as an independent line segment.
Vertices 2n-1 and 2n define line n. N/2 lines are drawn.

kuniva
04-01-2005, 02:12 PM
see thats what i dont get... :p

kuniva
04-01-2005, 03:05 PM
I also dont get it how to place the line where i want. Like what would i do if i want it in down in the right corner or in the top left? Can someone please explain me how this works.

sirbone72
04-03-2005, 08:01 AM
Try this:
glBegin(GL_LINES)
glVertex3f(0.0, -1.0, 0);
glVertex3f(1.0, -1.0, 0);
glEnd();

you should get a segment in the lowest line of your window (between the Y axis and the right boundary of the window).

I hope I've understood what you are asking for.

redarrow
04-03-2005, 09:47 AM
If you just want 2D lines rather use glVertex2f(x, y);
It'll make things a lot simpler to understand.

Just imagine a grid where x=0, y=0 is the bottom left corner of the screen.
The top right hand corner depends on your view setup, let's just say it's x=5, y=5.

This is really simple:

glVertex2f(0, 0);
glVertex2f(5, 5);

this would draw a line from the bottom left corner to the top right corner.

glVertex(0, 0);
glVertex(0, 5);

this would draw a line from the bottom left corner to the top right corner.

Just look at a grid like this:
_ _ _ _ _ _
|_|_|_|_|_|_|
|_|_|_|_|_|_|
|_|_|_|_|_|_|
|_|_|_|_|_|_|
|_|_|_|_|_|_|

x is horizontal y is vertical
x starts at the left and goes right,
y starts at the bottom and goes up.

so x=0, y=0 is the bottom left block
and x=5, y=5 would be the top left block.

therefore:
glVertex2f(0, 0);
glVertex2f(5, 5);
draws a line from block(0,0) to block(5,5).

Ok this example is not entirely perfect the orign 0,0 is normally in the center of the screen so the bottom left would actually be something like x=-5, y=-5

The z value in glVertex3f is depth i.e. how far "into" the screen, if you just want 2D lines you can just leave it as 0.0 but I would recommend using glVertex2f.

I also recommend getting the OpenGL RedBook - I can't tell how much it's helped me.
I forget where to find it but google will do that. ;-)

Hope this helps