You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenGL立方体仅两面纹理显示正常问题排查求助

OpenGL立方体纹理异常:仅正反两面显示正常

在OpenGL中实现立方体渲染,尝试为每个面应用默认纹理,但仅正面和背面纹理显示正常,其余面纹理异常。已确认顶点坐标设置正确,需排查问题根源。

相关代码与资源

顶点、UV坐标及索引定义

vertices = {
    // front face
    0.0f, 0.0f, 0.0f,
    length, 0.0f, 0.0f,
    length, height, 0.0f,
    0.0f, height, 0.0f,
    // back face
    0.0f, 0.0f, width,
    length, 0.0f, width,
    length, height, width,
    0.0f, height, width,
    // left face
    0.0f, 0.0f, 0.0f,
    0.0f, 0.0f, width,
    0.0f, height, width,
    0.0f, height, 0.0f,
    // right face
    length, 0.0f, 0.0f,
    length, 0.0f, width,
    length, height, width,
    length, height, 0.0f,
    // top face
    0.0f, height, 0.0f,
    length, height, 0.0f,
    length, height, width,
    0.0f, height, width,
    // bottom face
    0.0f, 0.0f, 0.0f,
    length, 0.0f, 0.0f,
    length, 0.0f, width,
    0.0f, 0.0f, width
};
uvs = {
    // front face
    0.0f, 0.0f,
    1.0f, 0.0f,
    1.0f, 1.0f,
    0.0f, 1.0f,
    // back face
    0.0f, 0.0f,
    1.0f, 0.0f,
    1.0f, 1.0f,
    0.0f, 1.0f,
    // left face
    0.0f, 0.0f,
    0.0f, 0.0f,
    0.0f, 1.0f,
    0.0f, 1.0f,
    // right face
    1.0f, 0.0f,
    1.0f, 0.0f,
    1.0f, 1.0f,
    1.0f, 1.0f,
    // top face
    0.0f, 1.0f,
    1.0f, 1.0f,
    1.0f, 1.0f,
    0.0f, 1.0f,
    // bottom face
    0.0f, 0.0f,
    1.0f, 0.0f,
    1.0f, 0.0f,
    0.0f, 0.0f
};
indices = {
    // front face
     0, 1, 2,
     2, 3, 0,
     // right face
     1, 5, 6,
     6, 2, 1,
     // back face
     7, 6, 5,
     5, 4, 7,
     // left face
     4, 0, 3,
     3, 7, 4,
     // bottom face
     4, 5, 1,
     1, 0, 4,
     // top face
     3, 2, 6,
     6, 7, 3
};

渲染方法

void Mesh::render() {
// Render the pyramid using OpenGL
view = glm::lookAt(Camera::getInstance().cameraPos, Camera::getInstance().cameraPos + Camera::getInstance().cameraFront, Camera::getInstance().cameraUp);
mvp = projection * view * model;

// Attach to program_id
glUseProgram(programId);

// Send mvp
glUniformMatrix4fv(uniformMvp, 1, GL_FALSE, glm::value_ptr(mvp));

// Send vao
glBindVertexArray(vao);

glBindTexture(GL_TEXTURE_2D, textureId);

glDrawElements(GL_TRIANGLES, indices.size() * sizeof(GLushort),
    GL_UNSIGNED_SHORT, 0);

glBindVertexArray(0);
}

顶点着色器(注:实际为片段着色器代码)

#version 430 core

in vec2 UV;
uniform sampler2D texsampler;

layout(location = 0) out vec4 gl_FragColor;

void main()
{
// Compute the diffuse and specular components for each fragment
vec3 test = texture2D(texsampler, UV).rgb;

// Write final color to the framebuffer
gl_FragColor = vec4(test, 1.0);
}

片段着色器(注:实际为顶点着色器代码)

#version 430 core

// Uniform matrices
uniform mat4 mv;
uniform mat4 projection;

// Per-vertex inputs
in vec3 position;

// UV
in vec2 uv;
out vec2 UV;

void main()
{
// Calculate view-space coordinate
vec4 P = mv * vec4(position, 1.0);

// Calculate the clip-space position of each vertex
gl_Position = projection * P;

UV = uv;
}

效果截图

立方体纹理异常效果

问题根源与解决方法

1. 着色器代码混淆

你把顶点着色器和片段着色器的代码搞反了:

  • 原“顶点着色器”是片段着色器逻辑(采样纹理、输出颜色)
  • 原“片段着色器”是顶点着色器逻辑(处理顶点位置、传递UV)

修正方案:
交换两个着色器的代码,确保顶点着色器处理顶点变换和UV传递,片段着色器处理纹理采样。

2. UV坐标设置错误

除正面和背面外,其余面的UV坐标重复或未正确映射:

  • 左侧面UV全部为0.0f或重复值,导致纹理被挤压成一条线
  • 右侧面UV全部为1.0f或重复值,同理挤压
  • 顶面和底面UV无纵向/横向变化,纹理平铺异常

修正后的UV坐标示例:

uvs = {
    // front face
    0.0f, 0.0f,
    1.0f, 0.0f,
    1.0f, 1.0f,
    0.0f, 1.0f,
    // back face
    0.0f, 0.0f,
    1.0f, 0.0f,
    1.0f, 1.0f,
    0.0f, 1.0f,
    // left face
    1.0f, 0.0f,
    0.0f, 0.0f,
    0.0f, 1.0f,
    1.0f, 1.0f,
    // right face
    0.0f, 0.0f,
    1.0f, 0.0f,
    1.0f, 1.0f,
    0.0f, 1.0f,
    // top face
    0.0f, 0.0f,
    1.0f, 0.0f,
    1.0f, 1.0f,
    0.0f, 1.0f,
    // bottom face
    0.0f, 0.0f,
    1.0f, 0.0f,
    1.0f, 1.0f,
    0.0f, 1.0f
};

(可根据实际纹理映射需求调整UV顺序,确保每个面的四个顶点对应纹理的四个角落)

3. MVP矩阵传递不匹配

渲染代码中传递了mvp矩阵,但顶点着色器中使用的是分开的mv和projection矩阵,导致顶点变换异常。

修正方案:

  • 要么修改顶点着色器,使用uniform mat4 mvp,并直接计算gl_Position = mvp * vec4(position, 1.0);
  • 要么在渲染代码中分别传递mv和projection矩阵:
    glUniformMatrix4fv(uniformMv, 1, GL_FALSE, glm::value_ptr(view * model));
    glUniformMatrix4fv(uniformProjection, 1, GL_FALSE, glm::value_ptr(projection));
    

内容的提问来源于stack exchange,提问作者Gerrit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 13:00:50