0
\$\begingroup\$

i need help to convert the render function of IQM model (void renderiqm()) to modern OpenGL (without the deprecated functions like enableClientState(), etc).

The code of renderiqm() function is here:

https://github.com/lsalzman/iqm/blob/master/demo/gpu-demo.cpp

and these are the shaders:

Vertex Shaders:

"#version 120\n"
"#ifdef GL_ARB_uniform_buffer_object\n"
"  #extension GL_ARB_uniform_buffer_object : enable\n"
"  layout(std140) uniform animdata\n"
"  {\n"
"     uniform mat3x4 bonemats[80];\n"
"  };\n"
"#else\n"
"  uniform mat3x4 bonemats[80];\n"
"#endif\n"
"attribute vec4 vweights;\n"
"attribute vec4 vbones;\n"
"attribute vec4 vtangent;\n"
"void main(void)\n"
"{\n"
"   mat3x4 m = bonemats[int(vbones.x)] * vweights.x;\n"
"   m += bonemats[int(vbones.y)] * vweights.y;\n"
"   m += bonemats[int(vbones.z)] * vweights.z;\n"
"   m += bonemats[int(vbones.w)] * vweights.w;\n"
"   vec4 mpos = vec4(gl_Vertex * m, gl_Vertex.w);\n"
"   gl_Position = gl_ModelViewProjectionMatrix * mpos;\n"
"   gl_TexCoord[0] = gl_MultiTexCoord0;\n"
"   mat3 madjtrans = mat3(cross(m[1].xyz, m[2].xyz), cross(m[2].xyz, m[0].xyz), cross(m[0].xyz, m[1].xyz));\n"
"   vec3 mnormal = gl_Normal * madjtrans;\n"
"   vec3 mtangent = vtangent.xyz * madjtrans; // tangent not used, just here as an example\n"
"   vec3 mbitangent = cross(mnormal, mtangent) * vtangent.w; // bitangent not used, just here as an example\n"
"   gl_FrontColor = gl_Color * (clamp(dot(normalize(gl_NormalMatrix * mnormal), gl_LightSource[0].position.xyz), 0.0, 1.0) * gl_LightSource[0].diffuse + gl_LightSource[0].ambient);\n"
"}\n",

Fragment Shader:

"uniform sampler2D tex;\n"
"void main(void)\n"
"{\n"
"   gl_FragColor = gl_Color * texture2D(tex, gl_TexCoord[0].xy);\n"
"}\n"

i tried to convert this to modern opengl (vbo, vao, shader version 330, etc), but i dont know which is the fail. Can you show me the correct form to convert it to modern opengl?

Im using opengl version 3.3 and here is my renderiqm() modified function :

void renderiqm(GLSLProgram& mainShader)
{
    m_matrix->loadIdentity();
    matrixTransformations();

    mainShader.sendUniform("modelYAxisDiff",getYAxisDiff());
    mainShader.sendUniform4x4("modelMatrix", m_matrix->getModelMatrix());
    //envía la matriz de normales
//  mainShader.sendUniform4x4("normalMatrix", m_matrix->getNormalMatrix());
    mainShader.sendUniform3x3("normalMatrix", glm::value_ptr(m_matrix->getNormalMatrix()));
    //envía la matriz de transformación  (modelview)
    mainShader.sendUniform4x4("modelViewMatrix", glm::value_ptr(m_matrix->getModelViewMatrix()));
    mainShader.sendUniform("objColor", m_color);

    m_matrix->pushMatrix(MODEL_MATRIX);

    glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    glBufferData(GL_UNIFORM_BUFFER, ubosize, NULL, GL_STREAM_DRAW);
    glBufferSubData(GL_UNIFORM_BUFFER, bonematsoffset, numjoints*sizeof(Matrix3x4), outframe[0].a.v);
    glBindBuffer(GL_UNIFORM_BUFFER, 0);

    glBindBufferBase(GL_UNIFORM_BUFFER, 0, ubo);


    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    vertex *vert = NULL;

    GLsizei stride = sizeof(vert->position)+sizeof(vert->normal)+sizeof(vert->tangent)+sizeof(vert->texcoord);
    if(numframes > 0)
    {
        stride+= sizeof(vert->blendindex)+sizeof(vert->blendweight);
    }

    //vertices
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, stride, 0);

     //normales
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1,3,GL_FLOAT, GL_FALSE, stride, (void*)sizeof(vert->position));

    //tangente
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, stride, (void*)sizeof(vert->position)+sizeof(vert->normal));

    //texturas
    glEnableVertexAttribArray(3);
    glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, stride, (void*)sizeof(vert->position)+sizeof(vert->normal)+sizeof(vert->tangent));


    if(numframes > 0)
    {
        //blendweight
        glEnableVertexAttribArray(4);
        glVertexAttribPointer(4, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (void*)sizeof(vert->position)+sizeof(vert->normal)+sizeof(vert->tangent)+sizeof(vert->texcoord));

        //blendindex
        glEnableVertexAttribArray(5);
        glVertexAttribPointer(5, 4, GL_UNSIGNED_BYTE, GL_FALSE, stride, (void*)sizeof(vert->position)+sizeof(vert->normal)+sizeof(vert->tangent)+sizeof(vert->texcoord)+sizeof(vert->blendindex));
    }

    iqmtriangle *tris = NULL;

    //DEBUG
    int i=0;
    for(i = 0; i < m_textureIndices.size(); i++)
    {

        iqmmesh &m = meshes[i];
        glBindTexture(GL_TEXTURE_2D, textures[i] ? textures[i] : notexture);
        tTextures[i].bindTexture(0);

        glDrawElements(GL_TRIANGLES, 3*m.num_triangles, GL_UNSIGNED_INT, &tris[m.first_triangle]);
    }

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(2);
    glDisableVertexAttribArray(3);
    if(numframes > 0)
    {
        glDisableVertexAttribArray(4);
        glDisableVertexAttribArray(5);
    }

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    //glPopMatrix();
    m_matrix->popMatrix(MODEL_MATRIX);
}

Clarification: mainShader is a tested shader interface that works fine (no errors here) and m_matrix is a GLM interface and also works fine.

the vertexShader would be this one:

string vAnimationShader = "\n\
#version 330                                                                \n\
                                                                            \n\
uniform mat4 projectionMatrix;                                              \n\
uniform mat4 modelViewMatrix;                                               \n\
uniform mat4 normalMatrix;                                                  \n\
uniform mat4 modelMatrix;                                                   \n\
uniform vec3 objColor;                                                      \n\
\n\
uniform mat4 DepthBiasMVP;                          \n\
smooth out vec4 ShadowCoord;\n\
\n\
smooth out vec2 texCoord;                                                   \n\
smooth out vec3 vNormal;                                                    \n\
smooth out vec3 vEyeSpacePos;                                               \n\
smooth out vec3 vWorldPos;                                                  \n\
smooth out vec3 vObjColor;                                                  \n\
\n\
layout (location = 0) in vec3 inPosition;                                   \n\
layout (location = 3) in vec2 inCoord;                                      \n\
layout (location = 1) in vec3 inNormal;                                     \n\
                                                                            \n\
uniform mat3x4 bonemats[80];\n\
\n\
layout (location = 4) in vec4 vweights;\n\
layout (location = 5) in vec4 vbones;\n\
layout (location = 2) in vec4 vtangent;\n\
void main(void)\n\
{\n\
    vObjColor = objColor;                                               \n\
   mat3x4 m = bonemats[int(vbones.x)] * vweights.x;\n\
   m += bonemats[int(vbones.y)] * vweights.y;\n\
  m += bonemats[int(vbones.z)] * vweights.z;\n\
   m += bonemats[int(vbones.w)] * vweights.w;\n\
    vec4 vEyeSpacePosVertex = modelViewMatrix*vec4(inPosition, 1.0);        \n\
    gl_Position = projectionMatrix*vEyeSpacePosVertex;                      \n\
   mat3 madjtrans = mat3(cross(m[1].xyz, m[2].xyz), cross(m[2].xyz, m[0].xyz), cross(m[0].xyz, m[1].xyz));\n\
   vec3 mnormal = inNormal * madjtrans;\n\
   vec3 mtangent = vtangent.xyz * madjtrans; // tangent not used, just here as an example\n\
   vec3 mbitangent = cross(mnormal, mtangent) * vtangent.w; // bitangent not used, just here as an example\n\
   texCoord = inCoord;      \n\
    vec4 vRes = normalMatrix*vec4(inNormal, 0.0);                           \n\
    vNormal = vRes.xyz;                                                     \n\
    vEyeSpacePos = vEyeSpacePosVertex.xyz;                                  \n\
    vec4 vWorldPosVertex = modelMatrix*vec4(inPosition, 1.0);               \n\
    vWorldPos = vWorldPosVertex.xyz;                                        \n\
    ShadowCoord = DepthBiasMVP * vWorldPosVertex;   \n\
}\n\
";

i didn't find the error... in the screen i dont see the iqm model.. all the other things draw perfectly, but iqm model doesn't appear.

IMPORTANT DISCOVERING:

recently, i have discovered that the problem occurs when the Opengl Context is 3.2+ . if I switch to the 3.1 Version of the opengl context , it works perfectly with my modified function.

i didn't find what the problem is... i associate that with a deprecated function but i cant findit. Please, can you help me? Thanks a lot.

\$\endgroup\$

closed as off-topic by Josh Jun 3 '15 at 15:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Josh
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ recently, i have discovered that the problem occurs when the Opengl Context is 3.2+ . if I switch to the 3.1 Version of the opengl context , it works perfectly with my modified function. i didn't find what the problem is... i associate that with a deprecated function but i cant findit. Please, can you help me? Thanks a lot. \$\endgroup\$ – user2303826 Jun 1 '15 at 23:26
0
\$\begingroup\$

Looks like you're not using Vertex Array Objects that are required by a modern OpenGL Core Profile context. It can be generated/used with calls glGenVertexArrays and glBindVertexArray.

\$\endgroup\$
  • \$\begingroup\$ Thanks SurvivalMachine, but isn't it.. i use vbo, ubo and bind these in the render function as you can see: glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBindBuffer(GL_ARRAY_BUFFER, vbo); im lost with this problem.. \$\endgroup\$ – user2303826 Jun 2 '15 at 23:25
0
\$\begingroup\$

The problem was the initialization of vbo without a vao (as say SurvivalMachine), i fixed it and now the model is rendering correctly.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.