# OpenGL New to OpenGL. Trying to figure it all out.

## Recommended Posts

I have looked around at tutorials for OpenGL, and found that I like these: [url="http://www.opengl-tutorial.org/beginners-tutorials/"]http://www.opengl-tu...ners-tutorials/[/url] 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[i]->GetMesh();
glBindBuffer(GL_ARRAY_BUFFER, mesh->mVertexId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mMeshBuffer[i]->GetMesh()->mVertexArrayId);

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

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?

##### Share on other sites
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.
[/quote]

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.

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

##### Share on other sites
Then set the position in the shader with:
gl_Position = MVP;
[/quote]
No, you must multiply the vertex position with that matrix: gl_Position = MVP * a_position;

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627689
• Total Posts
2978659
• ### Similar Content

• 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:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 18
• 14
• 12
• 10
• 12