将弃用的opengl函数转换为现代opengl(3.3)

时间:2015-05-25 05:33:11

标签: opengl deprecated

我需要帮助将IQM模型的渲染函数(void renderiqm())转换为现代OpenGL(没有弃用的函数,如enableClientState()等)。

renderiqm()函数的代码在这里:

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

这些是着色器:

顶点着色器:

"#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",

我试图将其转换为现代opengl(vbo,vao,shader版本330等),但我不知道哪个是失败的。你能告诉我将它转换成现代opengl的正确形式吗?

PD:即时通讯使用opengl版本3.3。

添加重要信息:

好的,这是我的renderiqm()修改函数:

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);
}

澄清: mainShader是一个经过测试的着色器界面,工作正常(这里没有错误), m_matrix是一个GLM接口,也可以正常工作。

vertexShader就是这个:

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\
";

我没有找到错误...在屏幕上我没有看到iqm模型..所有其他的东西画得很完美,但是iqm模型没有出现。

重要发现:

最近,我发现当Opengl Context为3.2+时会出现问题。如果我切换到opengl上下文的3.1版本,它与我修改过的函数完美配合。

我没有发现问题是什么...我把它与一个已弃用的功能联系起来,但我找不到。拜托,你能帮帮我吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

问题是没有vao的vbo初始化,我修复了它,现在模型正确渲染。