0
votes

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 =\

3

3 Answers

1
votes

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.

0
votes

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?

0
votes

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.