• Create Account

## draw multiple 2D textured elements at once

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1youpi1  Members

Posted 29 October 2012 - 10:41 PM

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.

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
glTexCoordPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
glColor4ub(30, 100, 120, 255);

glTexCoord2f(0.0f, 0.0f);  glVertex3f(-1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);  glVertex3f( 1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);  glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);  glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();	// Done Drawing The Textured Quad

glDrawArrays(GL_POINTS, 0, numBodies);

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

glutSwapBuffers();

}


before this, I call the following function for loading the 2D texture :

// Load Bitmaps And Convert To Textures
{
Image *image1;

// allocate space for texture
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}

exit(1);
}

// Create Textures
glGenTextures(3, &texture[0]);

// linear filtered texture
glBindTexture(GL_TEXTURE_2D, texture[0]);   // 2d texture (x and y size)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
};


My main problem is that only one textured element is displayed at the center of the window (x=0, y=0) ( see figure in attachment).

It seems that I don't use correctly the function glTexCoordPointer . In the above drawPoints() function, I call it with :

glTexCoordPointer(4, GL_DOUBLE, 4*sizeof(double), pos);

where pos is the array of coordinates of each body (x, y, z).

I found another solution for plotting all the bodies with glTranslate in a loop on all the elements :


for(int i=0;i<numBodies;i++)
{
glPushMatrix();
glTranslatef(pos[i],pos[i+1],pos[i+2]);
glColor4ub(30, 100, 120, 255);
glTexCoord2f(0.0f, 0.0f);  glVertex3f(-1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();				// Done Drawing The Textured Quad
glPopMatrix();
}


You can see the expected result on the second image in attachment ( N_Bodies.png).

but I would like to avoid this solution and find a way to draw all the bodies at once (with glDrawArrays)

Any idea ?

#### Attached Thumbnails

Edited by youpi1, 30 October 2012 - 12:27 AM.

### #2SuperVGA  Members

Posted 30 October 2012 - 05:24 AM

void drawPoints()
{
...
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
glTexCoordPointer(4, GL_DOUBLE, 4*sizeof(double), pos);


glVertexPointer

This is given the pos array, which should hold the vertices of all the drawn primitives.

glTexCoordPointer

This should be given an array of uv coordinates (typically described by 2 components rather than 4), 1 for each vertex passed to the GPU.

You're passing the "pos" array to both, I don't think you should be doing that.

Upon fixing that, look into VBOs - That's what it looks like you're trying to use. I use VBOs for drawing multiple polygons at once, it's pretty fast.

Edited by SuperVGA, 30 October 2012 - 05:26 AM.

### #3mhagain  Members

Posted 30 October 2012 - 06:32 AM

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.

struct myVertex
{
float position[3];
float texcoord[2];
};

myVertex drawData[WHATEVER_MAXIMUM_I_SET];

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[i];
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;

drawData[i * 4 + 1].position[0] = 1 + pos[i];
drawData[i * 4 + 1].position[1] = -1 + pos[i + 1];
drawData[i * 4 + 1].position[2] = pos[i + 2];
drawData[i * 4 + 1].texcoord[0] = 1;
drawData[i * 4 + 1].texcoord[1] = 0;

drawData[i * 4 + 2].position[0] = 1 + pos[i];
drawData[i * 4 + 2].position[1] = 1 + pos[i + 1];
drawData[i * 4 + 2].position[2] = pos[i + 2];
drawData[i * 4 + 2].texcoord[0] = 1;
drawData[i * 4 + 2].texcoord[1] = 1;

drawData[i * 4 + 3].position[0] = -1 + pos[i];
drawData[i * 4 + 3].position[1] = 1 + pos[i + 1];
drawData[i * 4 + 3].position[2] = pos[i + 2];
drawData[i * 4 + 3].texcoord[0] = 0;
drawData[i * 4 + 3].texcoord[1] = 1;
}

// 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]);
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 ();
}

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #4SuperVGA  Members

Posted 30 October 2012 - 06:42 AM

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... :-/

### #5youpi1  Members

Posted 30 October 2012 - 08:43 AM

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.

Maybe I will bore you again on this forum

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.