c++ - OpenGL 绑定(bind)纹理到顶点缓冲对象(使用 CG 着色器)

标签 c++ opengl rendering texture-mapping vertex-buffer

我开门见山:

我已经为我的顶点创建了一个结构:

struct Vertex3D
{
    Vector3D position;
    Vector2D textureCoordinate;
    Vector3D normal;
}

然后我导入一个特定的 *.dae 文件并将其绑定(bind)到 OpenGL 顶点缓冲区,该缓冲区由 Vertex3D 结构化顶点列表构成。一切顺利,网格导入并显示,我可以使用着色器对其进行操作,但我遇到了问题。

我还做的是加载并为其分配纹理。之后我试图以这种方式显示它:

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(8);
glEnableVertexAttribArray(2);

glBindBuffer(GL_ARRAY_BUFFER, this->_entries[i].VB);
glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, sizeof(RomCommon::Vertex3D), 0); // Vertices
glVertexAttribPointer(8,2, GL_FLOAT, GL_FALSE, sizeof(RomCommon::Vertex3D), (const GLvoid*)sizeof(RomCommon::Vector3D)); // Supposed texture position
glVertexAttribPointer(2,3, GL_FLOAT, GL_FALSE, sizeof(RomCommon::Vertex3D),  (const GLvoid*)(sizeof(RomCommon::Vector2D) + sizeof(RomCommon::Vector3D))); // Supposed normal position
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->_entries[i].IB);
glBindTexture(GL_TEXTURE_2D, 3);
glDrawElements(GL_TRIANGLES, this->_entries[i]._indexCount, GL_UNSIGNED_INT, 0);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(8);
glDisableVertexAttribArray(2);

我的问题是没有显示纹理。仅分配纹理第一个像素的颜色(在我的例子中是蓝色)。我已经使用 gDEBugger 调试了整个过程,我可以看出 VertexBuffer 似乎设置正确,纹理图像加载正确,但不会显示。

我已经尝试搜索和尝试调试的不同方面,包括:

  • 确保 glEnable(GL_TEXTURE_2D) 已设置;
  • 添加 glEnable(GL_LIGHTING) 和 glDisable(GL_LIGHTING);
  • 使用 glDisable(GL_DEPTH_TEST); glDisable(GL_BLEND);和 glDisable(GL_SCISSOR_TEST);在绑定(bind)纹理之前
  • 获取错误
  • 搜索奇怪的邮件
  • 仔细检查内存中的顶点缓冲区(逐字逐句检查元素并交叉比较 *.dae 文件中的内容)

编辑

我正在为此使用 CG 着色器。这是顶点程序:

struct vsOutput {


 float4 position : POSITION;
  float2 texCoord : TEXCOORD0;
  float3 color    : COLOR;
};

vsOutput VS_Main(      float4 position : POSITION,
                           float2 texCoord : TEXCOORD0,
                           float3 color    : COLOR,
                           uniform float4x4 ModelViewProj
                           )
{
  vsOutput OUT;

  OUT.position = mul(ModelViewProj, position);
  OUT.texCoord = texCoord;
  OUT.color = color;

  return OUT;   
}

和片段程序:

struct fsOutput {
  float4 color : COLOR;
};

fsOutput FS_Main(
    float2 texCoord : TEXCOORD0,
    uniform sampler2D decal : TEX0
)
{ 
    fsOutput OUT;
    OUT.color = tex2D(decal,texCoord);
    return OUT;
}

如有必要,我可以添加有关我的项目的更多详细信息,但根据我的追踪,问题似乎出在顶点缓冲区的渲染中。

编辑

我还发现,因为我使用的是 CG,所以 vertexAttribPointers 应该不同(8 代表 TEXCOORD0)所以我基于此更改了一般描述。

解决方案编辑

非常感谢评论中的几个小伙伴,他们打开了我的思路,让我从不同的角度看待问题,并实际做了更多的阅读。这是解决方案:

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(8);
    glEnableVertexAttribArray(2);

    glBindBuffer(GL_ARRAY_BUFFER, this->_entries[i].VB);
    glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, sizeof(RomCommon::Vertex3D), 0); // Vertices
    glVertexAttribPointer(8,2, GL_FLOAT, GL_FALSE, sizeof(RomCommon::Vertex3D), (const GLvoid*)sizeof(RomCommon::Vector3D)); // Supposed texture position
    glVertexAttribPointer(2,3, GL_FLOAT, GL_FALSE, sizeof(RomCommon::Vertex3D),  (const GLvoid*)(sizeof(RomCommon::Vector2D) + sizeof(RomCommon::Vector3D))); // Supposed normal position
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->_entries[i].IB);


/* Solution start */
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, sizeof(RomCommon::Vertex3D), (const GLvoid*)12);
        glBindTexture(GL_TEXTURE_2D, 3);
        glDrawElements(GL_TRIANGLES, this->_entries[i]._indexCount, GL_UNSIGNED_INT, 0);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
/* Solution end */


    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(8);
    glDisableVertexAttribArray(2);

我的问题是我只将纹理坐标传递给着色器,但没有将其推送到 OpenGL 状态机(如果这是一个很好的表达方式)。 我认为这与我使用 CG 着色器而不是 GLSL 着色器这一事实有关,尽管我可能是错的。该代码大致基于我阅读的一些示例和解释,但它们都是基于 GLSL 着色器并且它们正在工作,因此是 dediction。 不管怎样,现在已经解决了。

最佳答案

glBindTexture(GL_TEXTURE_2D, 3);

我对在 OpenGL 上强制使用纹理对象 ID 持谨慎态度。从技术上讲,它应该可以工作,但使用 glGenTextures()获取纹理 ID 是一个更好的主意。

关于c++ - OpenGL 绑定(bind)纹理到顶点缓冲对象(使用 CG 着色器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14583709/

相关文章:

c++ - CEIL 对于精确的整数除法来说太高了

c++ - Qt OpenGL 渲染文本问题 QGLWidget

css - Roboto Condensed google 字体在所有浏览器中都会回落

java - 按下一个键后从迭代器中删除对象

java - glTranslatef 不随 z 轴移动

java - 如何将 OpenGL 上下文放入我当前的线程中,未找到 opengl 上下文

python - 以不同格式导出数据集

c++ - 鉴于 C++ 中没有反射,我怎样才能使它更健壮?

c++ - 遍历 BST 时出现 Stackoverflow 异常

c++ - 函数重载