Newb : glVertex2fv(vertices[edges[i][1]]);

Hi there, I am very new to C and open GL, have spent a good while trying to understand this code segment.

const float vertices[numVertices][2] = {{0.0,0.0},{100.0,0.0},{0.0,100.0},{100.0,100.0},{50.0,150.0}};

const int edges[numEdges][2] = {{0,1},{1,3},{3,2},{2,0},{2,4},{3,4}};

I understand the declaration, in that the first dec are point arrays, and the 2nd is line construction between two points. but what I don’t get how glVertex2fv works and its args, what does vertices[edges[i][0]] equate to!

for(int i=0;i<numEdges;i++)
{
glVertex2fv(vertices[edges[i][0]]);
glVertex2fv(vertices[edges[i][1]]);
}

This for loop generates a house like (well u get the picture)
/\

Let’s take
glVertex2fv(vertices[edges[i][0]]);
as an example.

glVertex<u>2</u>fv means that you will be passing two values as arguments to the function
glVertex2<u>f</u>v means that the values you are passing to the function are of type float.
glVertex2f<u>v</u> means that you will be passing the values for the function as a pointer to an array ( in this case an array of 2 floats)

vertices[edges[i][0]] is a pointer to the 2-dimensional vertex at position edges[i][0].

Okay, right I see how multidimensionals work now. I think I’ve almost got you, but aren’t we only passing the method 1 arg,

glVertex2fv(vertices[edges[i][0]]);
i.e : this points to the first subelement in element 0 on the first iteration, which is : 0

then we run the 2nd method in the loop which is the second subelement in element 0, which is : 1

So each iteration of the loop outputs one line, this doesn’t quite add up to me as it seems the method should accept 2 args, as it would make more sense. But I can’t see it…

Why doesn’t it add up? you need 4 variables to draw a line. X and Y position of startpoint and X and Y position of endpoint. So you need to call glVertex2f twice, once for each point.

Pointers are not that hard to understand.
glVertex2fv(vertices[edges[i][0]]);
is equivalent to
glVertex2f(vertices[edges[i][0]][0],vertices[edges[i][0]][1]);

The only difference is that in the first case you provide a pointer to the first element because vertices[edges[i][0]] is equivalent to &vertices[edges[i][0]][0]

That’s because glVertex2fv takes it’s two expected arguments as a vector (array) of two floats. So if you pass a pointer p to such a function, it will in fact access p and p+1.

So the first call to glVertex2fv() references the vertex at index 0, and the second call references the vertex at index 1.
Because vertices is in fact a two-dimensional array, using vertices with a single index is of type float[2], which, in your example, comes down to {0.0,0.0} for index 0 and {100.0,0.0} for index 1 respectively.

It doesn’t add up to me because, its seems odd that a function (drawing the line) would be spread over two method calls.

How does it save the first point in memory and then when it has 2 points (after 2 method calls) it draws the line?

It only needs to remember the amount of points that is necessary to draw the type of primitive that was passed to glBegin(). So points are drawn immediately. Lines are drawn for every pair and triangles need three points. As soon as it has enough data to draw the primitive type you suggested (or a part of it) the command queue will get processed. e.g. if you want to draw triangles but you only specify 5 points, then the last 2 points will have no affect.

oh!!! Gotcha!!

This is quite a foreign concept to me coming from Java. Yup, I’m going backwards…

Excellent, thank you for your time -NiCo-

:slight_smile:

<nitpicking>

Even of English is not my native language, I am pretty sure it should be “effect”

http://xkcd.com/326/ :slight_smile:

Idd, that should’ve been “effect”. At least a forum-search for “affect” quickly reveals that I’m not the only one making this mistake by writing too fast :slight_smile:

how to fix a problem glvertex2f, its showing undeclared identifier

here is the code

glvertex2f(radiuscoords[j][0]+x,radiuscoords[j][1]+y);

um… you need #include <gl/gl.h> most likely. or if you have that in another file you need to include that file!

Also I think its glVertex2f