glDrawElements and vertex transformation

Started by
9 comments, last by bubu LV 14 years, 8 months ago
Hi all, I'm writing a new terrain engine for my game, and I have a question that will determine how I structure things. Let's say I have a vertex buffer of 1000 vertices. Let's say I am calling glDrawElements using indexed triangles into that vertex buffer. If I call glDrawElements 10 times, each time referencing a different set of 100 different vertices inside the vertex buffer, will the entire 1000 vertices be transformed 10 different times? How does OpenGL, or the hardware, handle transformation of vertices in this case? Any information would be much appreciated! Thanks, Kevin
Advertisement
Only those vertices that are used by glDrawPrimitive call will be transformed.
Oops! I meant to say glDrawElements instead of glDrawPrimitive.

Just to be clearer, let's say I'm doing something like this:

float vertexdata[1000 * 3]; // 1000 x, y, z coordinates
...
glVertexPointer(3, GL_FLOAT, 0, vertexdata);

for(int i = 0; i < 10; i++)
{
unsigned short indices[100 * 3];
...
glDrawElements(GL_TRIANGLES, 100, GL_UNSIGNED_SHORT, indices);
}



Will this cause 1000 vertex transformations or 10000 transformations?

Quote:Original post by bubu LV
Only those vertices that are used by glDrawPrimitive call will be transformed.


I see, that helps a lot. Thanks bubu. I do have a followup question, too:

If I then call glDrawElements 10 times, but all using the same vertex indices, then the same vertices will be transformed an extra 9 times, right? That is, if the hardware doesn't have some kind of caching mechanism in place?
if your not using shaders then each vertex that is sent will be transformed everytime, send the same vertex twice it will be transformed twice. Ionly mention shaders sicne you could have a shader that doens't do any transformation.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Quote:Original post by caywen
If I then call glDrawElements 10 times, but all using the same vertex indices, then the same vertices will be transformed an extra 9 times, right? That is, if the hardware doesn't have some kind of caching mechanism in place?
There is a vertex cache, but I have doubts that it offers much good across multiple draw calls.

It isn't really clear to me why you would need to render the same set of vertex data 10 times in a row - at least not without changing the transformation matrices or other states in-between. Can you give an example of why you need to do this?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
Quote:Original post by caywen
If I then call glDrawElements 10 times, but all using the same vertex indices, then the same vertices will be transformed an extra 9 times, right? That is, if the hardware doesn't have some kind of caching mechanism in place?
There is a vertex cache, but I have doubts that it offers much good across multiple draw calls.

It isn't really clear to me why you would need to render the same set of vertex data 10 times in a row - at least not without changing the transformation matrices or other states in-between. Can you give an example of why you need to do this?


Well, if I was limited in texture units, and I needed to issue multiple calls to render multiple passes, I would do such a thing, right?

Actually, in my case, I'm creating a splat-based terrain system, and my idea was to upload the terrain vertex data once, and then multiple splats that have indices into the terrain vertices. Because the splats can be semi-transparent, many of them overlap in their geometry significantly.

At least, that's my current thinking, and I'm open to any better suggestions on terrain rendering. However, as this is an iPhone app, I'm a bit constrained on hardware.
Quote:Original post by Nanoha
if your not using shaders then each vertex that is sent will be transformed everytime, send the same vertex twice it will be transformed twice. Ionly mention shaders sicne you could have a shader that doens't do any transformation.


Thanks a lot for the reply. Since this has to work on 1st gen iPhone (I think 3G's PVR SGX has shaders?) I think I have to rely on more old school techniques.
hmm depending on how many passes you do, it might be faster to make a copy of your vertices and rpe multiply everything but I think this would only work if you have shaders availible (so you could remove the normal transformations from taking place). Mind then your shifting the load off the gpu to the cpu, no idea how iphones work, wheather they have a seperate cpu/gpu or whether its all cpu and software.

if its cpu (software rendering), you can use shaders and your redrawing vertices each pass significantly then pre multiplying might be an option, otherwise I think you'll just have to stick to what your doing currently.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Quote:Original post by Nanoha
if its cpu (software rendering), you can use shaders and your redrawing vertices each pass significantly then pre multiplying might be an option, otherwise I think you'll just have to stick to what your doing currently.


iPhones have GL ES 1.1 so there are no shaders. The next iPhone will have GL ES 2.0
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement