OpenGL Cube没有正确纹理化

时间:2016-12-13 19:10:03

标签: c++ opengl textures vbo vao

我正在尝试使用VBO / VAO构建3D立方体。然而,我只是设法在立方体上正确纹理4/6面。我尝试了多种解决方案(从多次定义顶点到尝试立方体贴图 - 当使用讲师的解决方案时,只会导致立方体因某种原因完全消失)

最初,我可以正确地对前脸,后脸,上下脸进行纹理处理。但经过几个小时的纠缠,我没有设法纹理正面,背面,左面和右面(但不是顶部或底部)。

如果有人可以帮助我(并且善意地解释纹理坐标实际上是如何工作的 - 我在2D Quad上得到了想法,但不是在3D VBO上 - 或者更好的质量教程等:) :)

以下是代码;

VBO设置

//每顶点位置向量

static float pyramidVertices[] =
{
    //Front
    0.0f, 0.0f, 0.0f, 1.0f, //BtmLeft
    1.0f, 0.0f, 0.0f, 1.0f, //BtmRight
    1.0f, 1.0f, 0.0f, 1.0f, //TopRight
    0.0f, 1.0f, 0.0f, 1.0f, //TopLeft
    //Back
    0.0f, 1.0f, -1.0f, 1.0f,    //TopLeft
    1.0f, 1.0f, -1.0f, 1.0f,    //TopRight
    1.0f, 0.0f, -1.0f, 1.0f,    //BottomRight
    0.0f, 0.0f, -1.0f, 1.0f //BottomLeft
};

#pragma region Pyramid Data
// Per-vertex colours (RGBA) floating point values
static float        pyramidColours[32] =
{
    1.0f, 0.0f, 0.0f, 1.0f,
    0.0f, 1.0f, 0.0f, 1.0f,
    0.0f, 0.0f, 1.0f, 1.0f,
    1.0f, 0.0f, 1.0f, 1.0f,
    0.0f, 1.0f, 1.0f, 1.0f,
    0.0f, 0.0f, 1.0f, 1.0f,
    1.0f, 0.0f, 1.0f, 1.0f,
    1.0f, 0.0f, 0.0f, 1.0f
};

// 5 faces each with 3 vertices (each face forms a triangle)
static unsigned short       pyramidVertexIndices[] =
{
    //Front
    0, 1, 2,
    2, 3, 0,
    //Left
    0, 3, 7,
    7, 3, 4,
    //Back
    4, 5, 6,
    6, 7, 4, 
    //Top
    4, 3, 5,
    5, 3, 2,
    //Right
    2, 1, 5,
    5, 1, 6,
    //Bottom
    6, 1, 7, 
    7, 1, 0
};

#pragma endregion
static float pyramidTexCoordArray[] =
{
    0.0f, 0.0f,
    1.0f, 0.0f,
    1.0f, 1.0f,
    0.0f, 1.0f,
    1.0f, 1.0f,
    0.0f, 1.0f,
    0.0f, 0.0f,
    1.0f, 0.0f
};

初始化

void init(void) {

    // Request an OpenGL 4.3 context with the Compatibility profile
    glutInitContextVersion(4, 3);
    glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);

    // Setup OpenGL Display mode - include MSAA x4
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);

    [...]
    [...]
    [...]

    texturedQuad = new CGTexturedQuad(wstring(L"Common\\Resources\\Textures\\bumblebee.png"));
    pyramidTexture = TextureLoader::fiLoadTexture(wstring(L"Common\\Resources\\Textures\\VBO\\sandstone.png"));
    exampleModel = new CGModel();
    importGSF(L"Common\\Resources\\Models\\dropship.gsf", exampleModel);

    // Setup VAO for pyramid object
    glGenVertexArrays(1, &pyramidVAO);
    glBindVertexArray(pyramidVAO);

    // Setup VBO for vertex position data
    glGenBuffers(1, &pyramidVertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, pyramidVertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(pyramidVertices), pyramidVertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0); // attribute 0 gets data from bound VBO (so assign vertex position buffer to attribute 0)

    // Setup VBO for vertex colour data
    glGenBuffers(1, &pyramidColourBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, pyramidColourBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(pyramidColours), pyramidColours, GL_STATIC_DRAW);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_TRUE, 0, (const GLvoid*)0); // attribute 1 gets colour data

    glGenBuffers(1, &pyramidTexCoordBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, pyramidTexCoordBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(pyramidTexCoordArray), pyramidTexCoordArray, GL_STATIC_DRAW);
    glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0);

    // Enable vertex position and colour attribute arrays
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(3);

    // Setup VBO for face index array
    glGenBuffers(1, &pyramidIndexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pyramidIndexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(pyramidVertexIndices), pyramidVertexIndices, GL_STATIC_DRAW);

    // Unbind pyramid VAO (or bind another VAO for another object / effect)
    // If we didn't do this, we may alter the bindings created above.
    glBindVertexArray(0);


    glEnable(GL_NORMALIZE); // If we scale objects, ensure normal vectors are re-normalised to length 1.0 to keep lighting calculations correct (see lecture notes)
    //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Best colour interpolation results


    // Setup GL_LIGHT0
    glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);     // Setup ambient light
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);     // Setup diffuse light
    glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);   // Setup specular light

    glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, ca);
    glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, la);
    glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, qa);

    glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 15.0f);
    glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0.0);

    // OpenGL provides a global ambient light component - we don't want this so set to zero
    GLfloat global_ambient[] = { 0.15f, 0.15f, 0.15f, 1.0f };
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
    //
    // Load the shader we'll use for the pyramid object
    //

    err = ShaderLoader::createShaderProgram(string("Common\\Resources\\Shaders\\basic_texture.vs"), string("Common\\Resources\\Shaders\\basic_texture.fs"), &basicShader);
}

显示功能

void display(void) {

    // Clear the screen
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Set viewport to the client area of the current window
    glViewport(0, 0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));

    // Get view-projection transform as a GUMatrix4
    GUMatrix4 T = mainCamera->projectionTransform() * mainCamera->viewTransform();

    if (principleAxes)
        principleAxes->render(T);

    if (texturedQuad)
        texturedQuad->render(T * GUMatrix4::translationMatrix(0.5f, 0.5f, 0.0f));


    // Fixed function rendering (Compatability profile only) - use this since CGImport is written against OpenGL 2.1
    glUseProgram(0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMultMatrixf((const float*)mainCamera->projectionTransform().M);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glMultMatrixf((const float*)mainCamera->viewTransform().M);
    glMultMatrixf((const float*)GUMatrix4::translationMatrix(0.0f, -0.15f, 0.0f).M);

    glEnable(GL_TEXTURE_2D);

    glPolygonMode(GL_FRONT, GL_FILL);

    if (exampleModel)
        exampleModel->renderTexturedModel();

    glDisable(GL_TEXTURE_2D);

    //Define position and direction (so appear at fixed point in scene)
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightDirection);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);

    // enable texturing
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    //
    // Pyramid VBO rendering
    //

    // Use basic shader for rendering pyramid (we'll look at this in more detail next week)
    glUseProgram(basicShader);

    static GLint mvpLocationPyramid = glGetUniformLocation(basicShader, "mvpMatrix");

    glUniformMatrix4fv(mvpLocationPyramid, 1, GL_FALSE, (const GLfloat*)&(T.M));

    GUMatrix4 pyramidModelTransform = GUMatrix4::translationMatrix(-0.0f, 0.0f, 0.0f) * GUMatrix4::scaleMatrix(2.0f, 2.0f, 2.0f);
    GUMatrix4 mvpPyramid = T * pyramidModelTransform;
    glUniformMatrix4fv(mvpLocationPyramid, 1, GL_FALSE, (const GLfloat*)&(mvpPyramid.M));

    // Bind VAO that contains all relevant pyramid VBO buffer and attribute pointer bindings
    glBindVertexArray(pyramidVAO);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, pyramidTexture);

    // Draw pyramid
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, (const GLvoid*)0);

    // Unbind pyramid VAO (or bind another VAO)
    glBindVertexArray(0);


    glutSwapBuffers();
}

片段着色器

#version 330

uniform sampler2D texture;

in vec2 texCoord;

layout (location=0) out vec4 fragColour;

void main(void) {

    vec4 texColor = texture2D(texture, texCoord);
    fragColour = texColor;
}

顶点着色器

#version 330

uniform mat4 mvpMatrix;

layout (location=0) in vec4 vertexPos;
layout (location=3) in vec2 vertexTexCoord;

out vec2 texCoord;

void main(void) {

    mat4 M;
    M[0] = vec4(1.0);

    ivec2 a = ivec2(1, 2);
    //vec3 b = vec3(2.0, 4.0, 1.0) + a;

    texCoord = vertexTexCoord;
    gl_Position = mvpMatrix * vertexPos;
}

导致多维数据集 (忽略灰色位,它只是导入的3D模型“dropship.gsf”)

1 个答案:

答案 0 :(得分:3)

这是预期的。问题是每个顶点只能有一个纹理坐标。为了解决这个问题,复制顶点,这样就可以将不同的纹理坐标分配给共享相同位置的不同顶点。

我不使用8个顶点,而是使用24个顶点:每个面都有4个顶点。通过不共享面之间的顶点,您可以独立地纹理每个面。

你可以在技术上仍然共享顶点的一些,但24非常小,如果你想稍后添加它们,它可以让你获得正常向量。对于具有UV贴图的平滑模型,通常会有一个接缝(或几个接缝),其中顶点是重复的,这可以在UV绘图过程中在3D编辑器中创建。

P.S。将变量命名为“金字塔”而不是“立方体”有点令人困惑。