0

I am a little confused when it comes to what function i should be using for my OpenGL Deferred Rendering.

I have a list of VBO( vertex buffer objects )

each one represents a type of vertex such a one might be (Position + Color)

I also have one for indeces

my problem comes when i want to render different objects

im use to the directX way where you 1)activate your vertex buffer 2)activate your index buffer 3) then you specify on your draw call your primitive type the start vertex from the vertex buffer, the min vertex index, the vertex count, the start indice and the primivite count

with openGL im lost because i can use:

glDrawElements - but this uses render mode, object count, the indice byte type and the indices

this just basically tells me it will render at the start of the VBO which wont help me any if i have multiple objects in there.

Anyone have any clue what rendering function openGL has that I can specify where in the VBO to start rendering from and specify my start vertex position? I hope someone has any idea =\

Franky Rivera
  • 553
  • 8
  • 20

3 Answers3

1

glDrawElements starts not from the start of the VBO, but from wherever you specify the pointer to start.

If you have a VBO and you want to start rendering from the second half of it, then you set the byte offset to start from as the last attribute of glVertexAttribPointer.

Tim
  • 35,413
  • 11
  • 95
  • 121
  • so in order to use glDrawElements i'll have to use glVertexAttribPointer right before it? – Franky Rivera Nov 01 '12 at 21:53
  • Yes, you must always have a pointer (or several) set to use glDrawElements (provided that you're not using the deprecated 'immediate mode' (glBegin/End etc). The pointers tell glDrawElements where to get the data from. – Tim Nov 01 '12 at 21:55
  • hmmm ok thanks. I always thought that when i'd use glVertexAttribPointer it was to tell it how to split my Vertices not where to start rendering from my vbo – Franky Rivera Nov 01 '12 at 22:08
0

Edit: I just realized that this might not be applicable if you're not using shaders, but since I'm not familiar with legacy OpenGL I'm not sure what benefit what my post has to you. What version are you using/targeting?

I recently asked a question regarding glDrawElements here. Hopefully what I posted gives some explanation/example.

The way I understand it: your indices are determined by you, so if you want to start at the 2nd vertex, you could try (a basic skeleton, does not have glVertexAttribPointer calls and such but i can add those if you want):

const float VBOdata = {
    //Some X and Y  coordinates for triangle1 (in the first quadrant)
    //(z = 0, w = 1 and are not strictly necessary here)
     0.0f,  1.0f, 0.0f, 1.0f //index 0
     0.0f,  0.0f, 0.0f, 1.0f  //1
     1.0f,  0.0f, 0.0f, 1.0f  //2

    //Some X and Y coordinates for triangle2 (in the second quadrant)
     0.0f, -1.0f, 0.0f, 1.0f  //3
     0.0f,  0.0f, 0.0f, 1.0f  //4
    -1.0f,  0.0f, 0.0f, 1.0f  //5

    //Now some color data (all white)
     1.0f, 1.0f, 1.0f, 1.0f,  //0
     1.0f, 1.0f, 1.0f, 1.0f,  //1
     1.0f, 1.0f, 1.0f, 1.0f,  //2

     1.0f, 1.0f, 1.0f, 1.0f,  //3
     1.0f, 1.0f, 1.0f, 1.0f,  //4
     1.0f, 1.0f, 1.0f, 1.0f,  //5
    };

const GLubyte indices[] = {
    3, 4, 5, 
};

glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, indices);

Where GL_TRIANGLES is the type of primitive, 3 is the number of elements inside of "indices", GL_UNSIGNED_BYTE is how to interpret them, and "indices" is just the name of the array.

This should draw only the second triangle. You can also try glDrawElementsBaseVertex, but I haven't personally tried it myself. What I gather is that you would generate/explicitly code your indices starting from 0, but give an offset so that it actually reads at 0+offset, 1+offset, and so on.

Does this help?

Community
  • 1
  • 1
Hydronium
  • 793
  • 1
  • 11
  • 28
  • i see what you're saying but what i was trying to control was that the indice would be loaded, as if to say "model 1, uses indices, 1,2,3,4" but then load "model 2, uses its own indices loaded as 1,2,3,4" well now u have 2 models fully in the same vbo, and ibo and glDrawElements would end up rendering model 1 when i try to use glDrawElements for model 2. – Franky Rivera Nov 01 '12 at 22:06
  • Ah, so keep the same indices between different draw calls, but replace the data in the VBO for the new model? Makes sense. What Tim says is correct then, you would use glVertexAttribPointer and point to a new location in the VBO, while leaving the GlDrawElements call the same. (Unless I've completely missed your point again, in which case I'm sorry.) – Hydronium Nov 01 '12 at 22:14
  • yeah the second part seems correct. im going to find a way to make the glVertexAttribPointer calls robust and that should probably fix that – Franky Rivera Nov 02 '12 at 00:55
0

You have several options.

You can reposition the start of each particular object's vertex data by issuing more glVertexAttribPointer calls before you draw that object. Or you can use glDrawElementsBaseVertex, where you get to specify a vertex index that all of the indices fetched by gets added to.

The latter should be available on any non-Intel hardware still supported.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • yeah i was looking around with glDrawElementsBaseVertex. it seemed to me the cleanest without ripping a ton of rendering components up – Franky Rivera Nov 03 '12 at 01:49