PDA

View Full Version : Replacing glMultMatrixd with Shaders ...



tmason
05-05-2014, 03:10 AM
Hello,

First, thanks to everyone for their help with my earlier posts. Much appreciated...

So, my current question has to do with replacing deprecated code.

I currently have a dirty mix of old and new stuff that goes like this:



/*

Somewhere in an initialize function

*/

glBindVertexArray(mVBONames[INDEX_VAO]);
// Create VBOs
glGenBuffers(VBO_COUNT - 1, mVBONames);

// Save vertex attributes into GPU
glBindBuffer(GL_ARRAY_BUFFER, mVBONames[VERTEX_VBO]);
glBufferData(GL_ARRAY_BUFFER, lPolygonVertexCount * VERTEX_STRIDE * sizeof(float), lVertices, GL_STATIC_DRAW);
delete[] lVertices;

if (mHasNormal)
{
glBindBuffer(GL_ARRAY_BUFFER, mVBONames[NORMAL_VBO]);
glBufferData(GL_ARRAY_BUFFER, lPolygonVertexCount * NORMAL_STRIDE * sizeof(float), lNormals, GL_STATIC_DRAW);
delete[] lNormals;
}

if (mHasUV)
{
glBindBuffer(GL_ARRAY_BUFFER, mVBONames[UV_VBO]);
glBufferData(GL_ARRAY_BUFFER, lPolygonVertexCount * UV_STRIDE * sizeof(float), lUVs, GL_STATIC_DRAW);
delete[] lUVs;
}

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mVBONames[INDEX_VBO]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, lPolygonCount * TRIANGLE_VERTEX_COUNT * sizeof(unsigned int), lIndices, GL_STATIC_DRAW);

glBindVertexArray(0);
delete[] lIndices;


And when I am about to draw later in code




[...]

glPushMatrix();
glMultMatrixd((const double*)pGlobalPosition);

[...]

glGenVertexArrays(1, &mVBONames[INDEX_VAO]);
glBindVertexArray(mVBONames[INDEX_VAO]);

[...]

const GLsizei lElementCount = mSubMeshes[pMaterialIndex]->TriangleCount * 3;
glDrawElements(GL_TRIANGLES, lElementCount, GL_UNSIGNED_INT, reinterpret_cast<const GLvoid *>(lOffset));

[...]

glBindVertexArray(0);

[...]
glPopMatrix();


I really want to move to shaders and thus remove myself completely from the glPush-Pop stuff and the glMultMatrixd stuff but I am not exactly sure at the moment how.

It seems like I would just:


Create another array buffer
send my global position into that array buffer
multiply the buffered vertice data with the global position buffer in a shader
and then go from there


But is that right?

I didn't see an exact tutorial online or writeup on how to replace the glMultMatrixd functionality in shaders as I searched.

Any help is appreciated.

Thank you for your time.

tonyo_au
05-05-2014, 10:28 PM
There is a useful library called glm that provides a replacement for gl-matrix functions (among other things)

Dark Photon
05-06-2014, 04:04 AM
I really want to move to shaders and thus remove myself completely from the glPush-Pop stuff and the glMultMatrixd stuff but I am not exactly sure at the moment how.
It depends on your needs but basically:



Write or choose a matrix library to use (e.g. glm)
Replace your matrix ops with calls to this library
When you need to pass the matrix value into the shader, explicitly populate a mat4 (or other matrix type) uniform using a glUniformMatrix* call (e.g. glUniformMatrix4fv)
Instead of referencing gl_ModelViewMatrix and similar in your matrix, reference your new mat4 uniform variable (e.g. myModelViewMatrix).

tmason
05-08-2014, 06:06 AM
Thank you, thanks to folks on this forum I got this working!