I am working on a n-body code with "glut functions" display. I would like to display each body with a 2D texture from a bmp image (a star).
The coordinates of each body (x, y, z) are in the "pos" array.
I try to use glTexCoordPointer with the following display function :
void drawPoints()
{
glEnable(GL_TEXTURE_2D); // Enable texture mapping.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glBindTexture(GL_TEXTURE_2D, texture[0]); // pick the texture.
glLoadIdentity(); // reset the view before we draw each star.
glTranslatef(0.0f, 0.0f, zoom); // zoom into the screen.
Please don't recommend VBOs for this class of problem. They're not a band-aid solution, and if a VBO needs to be dynamically updated they need careful planning and advance design before implementation. The OP is drawing so little geometry that using a VBO is extremely unlikely to offer any performance gain. If a VBO needs to be dynamically updated it may even cause significant performance loss. The OP clearly has trouble with setting up basic client-side vertex arrays so grafting a VBO on top of that will only make things worse. Let's get the vertex array code working right first, profile the application, determine if this part of the pipeline is actually a bottleneck, and then think about VBOs - not before.
Remember that Quake used immediate mode back in 1996, drew a few thousand polys per frame, and didn't suffer overly much from it. Jumping in with a recommendation of switching to VBOs every time one sees non-VBO code is not being in the least bit helpful.
So, with that said, to the OP:
The code you have posted is a little confusing. You're setting up your client states, your gl*Pointer calls, then you draw a quad in immediate mode, then you issue a glDrawArrays call with GL_POINTS (despite the fact that you're drawing quads). As correctly pointed out, your glVertexPointer and glTexCoordPointer calls are using the same array, you're using size 4 for both, and GL_DOUBLE is never a good idea (unless you know that you specifically absolutely need a double here - most gfx hardware is going to be much happier with floats).
Something like this looks more like what you're trying to achieve, but it's difficult to say at present.
void drawPoints (void)
{
// fill in the array we're going to draw from; all that glTranslatef in the immediate mode code
// does is move x, y and z by the translation, so we can accomplish the same result by adding
// the translation to the base coordinates. for extra performance, and if this data doesn't
// need to change at runtime, we could just set this array up once when the program starts.
// we're drawing quads so each quad is 4 vertices. make sure that the value of WHATEVER_MAXIMUM_I_SET
// is at least 4 * numBodies.
for (int i = 0; i < numBodies; i++)
{
drawData[i * 4 + 0].position[0] = -1 + pos;
drawData[i * 4 + 0].position[1] = -1 + pos[i + 1];
drawData[i * 4 + 0].position[2] = pos[i + 2];
drawData[i * 4 + 0].texcoord[0] = 0;
drawData[i * 4 + 0].texcoord[1] = 0;
// to do - if we got a pixel format with a stencil buffer we should clear stencil as well as depth here.
// this will give us > 20% extra performance.
glEnable (GL_TEXTURE_2D);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture (GL_TEXTURE_2D, texture[0]);
glLoadIdentity ();
glTranslatef (0.0f, 0.0f, zoom);
// vertex array setup - note the difference here. do you understand what is being done?
// in theory we only need to do this setup once for this program, but if we were drawing anything else
// that would change.
glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
glVertexPointer (3, GL_FLOAT, sizeof (myVertex), drawData[0].position);
glTexCoordPointer (2, GL_FLOAT, sizeof (myVertex), drawData[0].texcoord);
glColor4ub (30, 100, 120, 255);
// we're drawing quads and the number of vertices is 4 * numBodies as each body is a quad with 4 vertices
glDrawArrays (GL_QUADS, 0, numBodies * 4);
// in theory we don't even need to do this at all as we're just going to enable them again in the next frame
glDisableClientState (GL_TEXTURE_COORD_ARRAY);
glDisableClientState (GL_VERTEX_ARRAY);
glutSwapBuffers ();
}
Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.
Please don't recommend VBOs for this class of problem. They're not a band-aid solution, and if a VBO needs to be dynamically updated they need careful planning and advance design before implementation.
Whoops. Sorry. I missed that he passed along the position on every individual draw...
Thought he was dealing with a bunch of static geometry. And then I also assumed that he meant to draw a lot of it... :-/
Thanks a lot mhagain, It works ! I understand better now. Actually, I was confusing between array passed to glVertexPointer and glTexCoordPointer. Your code shows that we have to compute the coordinates of each quad vertex and this FOR each body. I thought that we would just need the "raw" positions of each body (containing in the "pos" array) without doing the shift.
So now, I am going to increase the performance of display by using texured objects with VBO.