PDA

View Full Version : using glDrawArrays



NOAH IV
02-03-2001, 09:09 AM
Hi, my glDrawArrays is drawing nothing... Please, is this the right
way to implement it?

This is what I'm doing:


// at the inicialization I do this:
glEnableClientState(GL_VERTEX_ARRAY);


// then, first I get the number of memory spaces
int NumberOfElements = ....->Counter();
// each element has 4 vertices and each vertice has 3 coordinates
int NumberOfMemorySpaces = 4*3*NumberOfElements;


VertexArray = (float *)calloc(NumberOfMemorySpaces,sizeof(GL_FLOAT));
int ArrayIndex = 0;

// then I put the coordinates in the array like this:
// ( and this is what I think is wrong... )
for( int i=0; i<NumberOfElements; i++)
{
VertexArray[ArrayIndex++] = (float)Obs[i]->Vertex1.x;
VertexArray[ArrayIndex++] = (float)Obs[i]->Vertex1.y;
VertexArray[ArrayIndex++] = 0.0;
VertexArray[ArrayIndex++] = (float)Obs[i]->Vertex2.x;
VertexArray[ArrayIndex++] = (float)Obs[i]->Vertex1.y;
VertexArray[ArrayIndex++] = 0.0;
VertexArray[ArrayIndex++] = (float)Obs[i]->Vertex2.x;
VertexArray[ArrayIndex++] = (float)Obs[i]->Vertex2.y;
VertexArray[ArrayIndex++] = 0.0;
VertexArray[ArrayIndex++] = (float)Obs[i]->Vertex1.x;
VertexArray[ArrayIndex++] = (float)Obs[i]->Vertex2.y;
VertexArray[ArrayIndex++] = 0.0;
}

// then I assign the pointer with that:
glVertexPointer(3, GL_FLOAT, 0, VertexArray);
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

// This is what I do when I want to draw the elements:
glDrawArrays(GL_QUADS, 0, NumberOfElements);

Thanks

Wolfling
02-03-2001, 04:25 PM
I once got that problem, too. I'm not sure how i fixed it, as there might be a number of reasons. Did you create a color before (or maybe use a colorpointer)? What headers / libraries did you add (my progs didn't draw graphics when I used opengl.lib, i fixed it by changing to opengl32.lib)?

NOAH IV
02-04-2001, 08:04 AM
Yes Wolfling, I tried to use both, vertex color and glColor...
I also checked the opengl dlls and lib, and they are both opengl32.

The glVertexPointer generates an GL_INVALID_OPERATION error. And I don't know if my vertexarray is correct... Must it be a sequencial array (float*) or it should be declared as a matrix ( float**)?

The other calls don't generate errors...

Relic
02-04-2001, 08:55 AM
This is wrong:
VertexArray = (float *)calloc(NumberOfMemorySpaces,sizeof(GL_FLOAT));
If has to be sizeof(float)!!

One more:
glDrawArrays(GL_QUADS, 0, NumberOfElements)
is wrong. Count has to be the number of vertices which is 4*NumberOfElements in your case.

And one comment:
There is no need to use 3 floats per vertex in the array, if you never have z != 0.0.

NOAH IV
02-04-2001, 02:42 PM
Thanks Relic, I've got your point. I made the corrections but it still doesn't work.

And the glVertexPointer still generates a GL_INVALID_OPERATION error. http://www.opengl.org/discussion_boards/ubb/frown.gif

The drawing method is now like the following:


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glDrawArrays(GL_QUADS, 0, 4*NumberOfElements);

Relic
02-04-2001, 10:35 PM
Sorry, no more ideas with this small piece of code. Strange GL_INVALID_OPERATION is not an error mentioned in the glVertexPointer specs.
I would grab a working vertex array demo, check if that works, and modify until your functionality is in it.

Thaellin
02-05-2001, 05:27 AM
You might look for something around the pointer function - perhaps you used to be using a begin/end pair and they are still out there somewhere, surrounding the call?

Perhaps you have not enabled the arrays, via glEnableClientState()?

Basically, what you have got looks sane, so the problem must lie somewhere else.

-- Jeff

mango
02-05-2001, 05:56 PM
You can do a loop and use glArrayElement. I could not use glDrawArrays with just a vertex array.