# OpenGL glDrawElements and vertex transformation

## Recommended Posts

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

##### Share on other sites
Only those vertices that are used by glDrawPrimitive call will be transformed.

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by bubu LVOnly 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?

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by caywenIf 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?

##### Share on other sites
Quote:
Original post by swiftcoder
Quote:
 Original post by caywenIf 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.

##### Share on other sites
Quote:
 Original post by Nanohaif 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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by Nanohaif 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

##### Share on other sites
Next iPhone (3GS) already has ES 2.0 :)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627704
• Total Posts
2978715
• ### Similar Content

• A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

-What I'm using:
C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.
-Questions
Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• 21
• 14
• 12
• 10
• 12