New to OpenGL. Trying to figure it all out.

Started by
2 comments, last by EngineCoder 11 years, 8 months ago
I have looked around at tutorials for OpenGL, and found that I like these: http://www.opengl-tu...ners-tutorials/ alot. I'm not sure if those have the best methods though. My real questions are a range. First of all, using VBOs, what would be the best method of updating the data each frame? Initializing a mesh to render(not every frame): I don't believe there is much of a problem or difference between using 3-4 buffers of arrays and using 1 buffer of a struct array. Then, indexing that vertex data. Lastly, creating pointers for each data area of a vertex (position, UV, normals, color), and sending the buffered indices.
[source lang="cpp"]glGenBuffers(1, &mesh->mVertexId);
glBindBuffer(GL_ARRAY_BUFFER, mesh->mVertexId); // Bind the buffer (vertex array data)

glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * mesh->mVertices.size(), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Vertex) * mesh->mVertices.size(), &mesh->mVertices);

IndexArray(mesh->mVertices, mesh->mIndex);

glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), OFFSET(12));
glNormalPointer(GL_FLOAT, sizeof(Vertex), OFFSET(20));
glColorPointer(4, GL_FLOAT, sizeof(Vertex), OFFSET(32));
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), OFFSET(0));

glGenBuffers(1, &mesh->mVertexArrayId); // Generate buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->mVertexArrayId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->mIndex.size()/3 * sizeof(byte), &mesh->mIndex, GL_STATIC_DRAW);

mMeshBuffer.push_back(meshr); //Dont mind "meshr" its something else containing the Mesh*[/source]
Those tutorials do it similarly, but do away with the "pointer" function calls and split each area of vertex data into separate buffers. I think what I'm doing is better, but I could be wrong?
Now, for per-frame rendering stuff, I will have either a separate VBO for each mesh, or find a way to batch certain ones together, keeping them independent for transformation. This is what I have gathered as what seemed to be well (in my function called each frame):
[source lang="cpp"] Mesh* mesh = mMeshBuffer->GetMesh();
glBindBuffer(GL_ARRAY_BUFFER, mesh->mVertexId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mMeshBuffer->GetMesh()->mVertexArrayId);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

glUseProgram(mesh->mShader.id);

glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), OFFSET(12));
glNormalPointer(GL_FLOAT, sizeof(Vertex), OFFSET(20));
glColorPointer(4, GL_FLOAT, sizeof(Vertex), OFFSET(32));
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), OFFSET(0));

glDrawElements(GL_TRIANGLES, mesh->mVertices.size(), GL_INT, OFFSET(0));

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);[/source]
While those tuts do it differently. Many sources that i've seen, in fact, have different methods here for rendering their buffered vertex data. Like, sending the data to the shader with glVertexAttribPointer and again, not using glXXXXPointer. Mind you, I haven't finished the series yet, and the code of them is fairly static (understandably), so it's long and harder for me to understand. I'm here to ask which of any seems to be the best (yet fairly manageable in easiness) method for rendering a mesh (whose vertices wont change) with a dynamic transform (I assume glRotatef, glTranslatef, and glScalef work okay; or are those as obsolete as straight drawing (function call for each vertex)? If so, shedding light on matrix manipulation with GL'd be nice), texture (simple for now), normals, and, possibly, a shader (I'm not completely sure I know what a shader entails (and it's advantages over using C++ for certain things) when applying it to a mesh) in a VBO. Also, any useful information/tips about related things would help. Thanks.
EDIT: Also, glBufferData vs glBufferData (as NULL/nullptr) with glBufferSubData?
Advertisement

While those tuts do it differently. Many sources that i've seen, in fact, have different methods here for rendering their buffered vertex data. Like, sending the data to the shader with glVertexAttribPointer and again, not using glXXXXPointer.


glVertexAttribPointer() is the way you should go. glXXXXPointer() is the deprecated legacy way of doing it. While being a little easier, to begin with, you can not realize the full potential of OpenGL.

It is perfectly possible, and even recommended, to not split each "area" into separate buffers. If they are stored interleaved in one buffer (a struct array), some graphic cards will have a better cache behavior.

Having a separate VBO for each mesh is a common way to go.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
Thanks. So for using the struct as my vertices, to specify offset, I can simply do that with glVertexAttribPointer? Like this?
[source lang="cpp"] glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, mesh->mVertexId);
glVertexAttribPointer( // Vertex Position
0, // The attribute we want to configure
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
52, // stride
(void*)0 // array buffer offset
);

// Then the next item in my struct: (The struct is padded to 64-byte, values are float(4-byte))
glEnableVertexAttribArray(1);
glVertexAttribPointer( // UVs
1, // The attribute we want to configure
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
56, // stride
(void*)12 // array buffer offset
);[/source]
I think most of what I was asking is answered. Do I still have to do the ClientState enabling/disabling? Lastly, I am just confused a bit with matrix manipulation. As I understand, I would have my matrix, use GLMs translate, rotate, and scale functions upon it (well, for rotation its easier to me to simply use matrix *= mat4_cast(quatrot);) then pass the "MVP" to the shader. This MVP stands for Projection * View * Model, correct? So, model would be the transform of my object, projection is my perspective matrix from the camera, and view is the transformation matrix of the camera (created using lookAt())? Then set the position in the shader with:
gl_Position = MVP;
If that is all correct, then my inquiries for now seem to be answered.
Almost forgot, know of any reasons shaders/GLSL can be better for certain things than C++? I know that it runs on the GPU, but for what things is that an advantage?

Then set the position in the shader with:
gl_Position = MVP;

No, you must multiply the vertex position with that matrix: gl_Position = MVP * a_position;
My iOS 3D action/hacking game: http://itunes.apple....73873?ls=1&mt=8
Blog

This topic is closed to new replies.

Advertisement