OpenGL正在绘制UV贴图而不是网格

OpenGL is drawing the UV map instead of the mesh

本文关键字:网格 绘制 UV OpenGL      更新时间:2023-10-16

我正在关注一个关于OpenGL开发的YouTube教程系列。他已经在github上上传了代码。但是,下载的代码似乎绘制了UV贴图而不是网格本身。

我试图隔离问题,我认为问题出在网格.cpp。我可能是错的。也许问题出在 glDrawElementsBaseVertex 行中,因为我们没有指定指向索引数组的指针。

void Mesh::Draw()
{
    glBindVertexArray(m_vertexArrayObject);
    glDrawElementsBaseVertex(GL_TRIANGLES, m_numIndices, GL_UNSIGNED_INT, 0, 0);
    glBindVertexArray(0);
}

我尝试交换位置和纹理坐标,但这似乎不起作用。

    glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(model.positions[0]) * model.positions.size(), &model.positions[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[TEXCOORD_VB]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(model.texCoords[0]) * model.texCoords.size(), &model.texCoords[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);

由于我是OpenGL的新手,因此无法解决此问题。我已经问过YouTuber,但我还没有得到答复。

我的显卡可能与OpenGL 120有一些兼容性问题,所以我将其更改为330并进行了一些更改,一切开始正常工作。以下是我所做的更改。

basicShader.vs

#version 330
layout(location = 0) in vec3 position;
layout(location = 1) in  vec2 texCoord;
layout(location = 2) in  vec3 normal;
out vec2 texCoord0;
out vec3 normal0;
uniform mat4 MVP;
uniform mat4 Normal;
void main()
{
    gl_Position = MVP * vec4(position, 1.0);
    texCoord0 = texCoord;
    normal0 = (Normal * vec4(normal, 0.0)).xyz;
}

basicShader.fs

#version 330
out vec4 color;
in vec2 texCoord0;
in vec3 normal0;
uniform sampler2D sampler;
uniform vec3 lightDirection;
void main()
{
    color = texture(sampler, texCoord0) * 
        clamp(dot(-lightDirection, normal0), 0.0, 1.0);
}