尝试显示纹理,但仅使用OpenGL获得纯色

时间:2019-02-15 16:19:27

标签: c++ qt opengl

我的目标是显示一个具有从png文件(DarkDirt.png)提取的纹理的多维数据集。问题是,我只能得到纯褐色,并且形成立方体的每个三角形的照明都在变化。

现在,我一直在阅读,我认为这可能与将索引设置为1有关,但是我没有获得任何成功。我尝试使用UV值,但是它只会改变照明的行为方式,不会解决任何问题。所以现在我在想也许我的顶点(代码中的顶点)是错误的?

c ++代码,数组:

const GLuint NumVertices = 24;
const GLuint NumTriCubes = 12;
const GLuint NumNormals = 6;
const GLuint NumUvs = 4;

GLfloat uvs[NumUvs][2] = {
    { 0, 0 },
    { 1, 0 },
    { 0, 1 },
    { 1, 1 },
};

GLfloat vertices[NumVertices][3] = {
    //Front
    { -0.5f, -0.5f, 0.5f }, //0
    { 0.5f, -0.5f, 0.5f }, //1
    { 0.5f, 0.5f, 0.5f }, //2
    { -0.5f, 0.5f, 0.5f }, //3

    //Right
    { 0.5f, -0.5f, 0.5f }, //4
    { 0.5f, -0.5f, -0.5f }, //5
    { 0.5f, 0.5f, -0.5f }, //6
    { 0.5f, 0.5f, 0.5f }, //7

    //Back
    { -0.5f, -0.5f, -0.5f },  //8
    { -0.5f, 0.5f, -0.5f }, //9
    { 0.5f, 0.5f, -0.5f }, //10
    { 0.5f, -0.5f, -0.5f }, //11

    //Left
    { -0.5f, -0.5f, 0.5f }, //12
    { -0.5f, 0.5f, 0.5f }, //13
    { -0.5f, 0.5f, -0.5f }, //14
    { -0.5f, -0.5f, -0.5f }, //15

    //Upper
    { -0.5f, 0.5f, 0.5f }, //16
    { 0.5f, 0.5f, 0.5f }, //17
    { 0.5f, 0.5f, -0.5f }, //18
    { -0.5f, 0.5f, -0.5f }, //19

    //Bottom
    { -0.5f, -0.5f, 0.5f }, //20
    { -0.5f, -0.5f, -0.5f }, //21
    { 0.5f, -0.5f, -0.5f }, //22
    { 0.5f, -0.5f, 0.5f }, //23

};

GLuint vertexIndices[NumTriCubes][3] = {
    //Front
    { 0, 1, 2 },
    { 0, 2, 3 },

    //Right
    { 4, 5, 6 },
    { 4, 6, 7 },

    //Back
    { 8, 9, 10 },
    { 8, 10, 11 },

    //Left
    { 12, 13, 14 },
    { 12, 14, 15 },

    //Upper
    { 16, 17, 18 },
    { 16, 18, 19 },

    //Bottom
    { 20, 21, 22 },
    { 20, 22, 23 }
};

GLfloat normals[NumVertices][3] = {

    //Front
    { 0.0f, 0.0f, 1.0f},
    { 0.0f, 0.0f, 1.0f},
    { 0.0f, 0.0f, 1.0f},
    { 0.0f, 0.0f, 1.0f},

    //Right
    { 1.0f, 0.0f, 0.0f},
    { 1.0f, 0.0f, 0.0f},
    { 1.0f, 0.0f, 0.0f},
    { 1.0f, 0.0f, 0.0f},

    //Back
    { 0.0f, 0.0f, -1.0f},
    { 0.0f, 0.0f, -1.0f},
    { 0.0f, 0.0f, -1.0f},
    { 0.0f, 0.0f, -1.0f},

    //Left
    { -1.0f, 0.0f, 0.0f},
    { -1.0f, 0.0f, 0.0f},
    { -1.0f, 0.0f, 0.0f},
    { -1.0f, 0.0f, 0.0f},

    //Upper
    { 0.0f, 1.0f, 0.0f},
    { 0.0f, 1.0f, 0.0f},
    { 0.0f, 1.0f, 0.0f},
    { 0.0f, 1.0f, 0.0f},

    //Bottom
    { 0.0f, -1.0f, 0.0f},
    { 0.0f, -1.0f, 0.0f},
    { 0.0f, -1.0f, 0.0f},
    { 0.0f, -1.0f, 0.0f}

};


c ++代码,我将纹理绑定到draw()函数中:

    m_texture->bind();

c ++代码,初始化:

    glGenBuffers(NumBuffers, m_Buffers);
    glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices)+ sizeof(uvs) + sizeof(normals), nullptr, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
    glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(uvs), uvs);
    glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(uvs), sizeof(normals), normals);

...

    glEnableVertexAttribArray(GLuint(m_vNormalLocation));
    glVertexAttribPointer(GLuint(m_vNormalLocation), 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(vertices)));

    glVertexAttribPointer(GLuint(m_vUVLocation), 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(vertices)));
    glEnableVertexAttribArray(GLuint(m_vUVLocation));

...
    m_texture = new QOpenGLTexture(QImage("DarkDirt.png").mirrored());
    m_texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
    m_texture->setMagnificationFilter(QOpenGLTexture::Linear);

我试图将纹理显示在立方体的每一侧。因此它会给我一个“污垢立方体”,例如在我的世界中。但是,是的,现在它只显示为一个实心的棕色立方体,周围到处都是照明。任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

每个顶点坐标都需要一个纹理坐标属性。由于您有24个(4 * 6)顶点坐标,因此也需要24个纹理坐标属性:

const GLuint NumUvs = 24;
GLfloat uvs[NumUvs][2] = {
    { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 },
    { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 },
    { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 },
    { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 },
    { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 },
    { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 }
};

法向矢量偏移量的计算错误:

glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(uvs), uvs);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(uvs), sizeof(normals), normals);

glVertexAttribPointer(GLuint(m_vNormalLocation), 3, GL_FLOAT, GL_FALSE, 0,
      BUFFER_OFFSET(sizeof(vertices)));

必须为:

glVertexAttribPointer(GLuint(m_vNormalLocation), 3, GL_FLOAT, GL_FALSE, 0, 
    BUFFER_OFFSET(sizeof(vertices) + sizeof(uvs)));

似乎从未在程序中设置纹理坐标(m_vUVLocation)的属性索引。

您可以通过QOpenGLShaderProgram::attributeLocation获取属性的属性索引:

m_vUVLocation = m_programRender->attributeLocation("vUV");

glGetAttribLocation的本机OpenGL:

m_vUVLocation = glGetAttribLocation(m_programRender->programId(), "vUV");

注意,也可以使用GLSL Layout Qualifiers在着色器代码中定义属性位置:

例如顶点着色器:

#version 400 core

layout(location = 0) in vec4 vPosition;
layout(location = 1) in vec4 vColor;
layout(location = 2) in vec3 vNormal;
layout(location = 3) in vec2 vUV;

在这种情况下,m_vUVLocation的值为3。