C++ OpenGL Textured Cube Missing Triangles

时间:2022-09-10 22:02:41

On my journey towards being competent at OpenGL, I was trying to make a textured cube when I came upon this problem: C++ OpenGL Textured Cube Missing Triangles Only part of my cube is being rendered. There should be 12 triangles but only 3 of them are being rendered. I've looked at many tutorials, but I can't seem to find the problem/main difference between our code. Here's mine:

在我有能力胜任OpenGL的过程中,当我遇到这个问题时,我试图制作一个有纹理的立方体:只有我的立方体的一部分被渲染。应该有12个三角形,但只有3个正在渲染。我看了很多教程,但我似乎无法找到我们的代码之间的问题/主要区别。这是我的:

...

int main(int argc, char* argv[]) {
    if (!setupGLFW()) return 1;
    setupApple();

    glfwWindowHint(GLFW_SAMPLES, 4);

    GLFWwindow* window = glfwCreateWindow(WIN_WIDTH, WIN_HEIGHT, "Textured Cube", NULL, NULL);
    if (!window) {
        log_msg(LOG_ERROR, "Could not open a GLFW3 window!\n");

        return 1;
    }

    glfwMakeContextCurrent(window);

    if (!setupGLEW()) return 1;

    glClearColor(0.5, 0.5, 0.5, 1.0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    static const int vertices = 12 * 3;
    static const GLfloat points[] = {
        -1.0, -1.0, -1.0,
        -1.0, -1.0,  1.0,
        -1.0,  1.0,  1.0,
         1.0,  1.0, -1.0,
        -1.0, -1.0, -1.0,
        -1.0,  1.0, -1.0,
         1.0, -1.0,  1.0,
        -1.0, -1.0, -1.0,
         1.0, -1.0, -1.0,
         1.0,  1.0, -1.0,
         1.0, -1.0, -1.0,
        -1.0, -1.0, -1.0,
        -1.0, -1.0, -1.0,
        -1.0,  1.0,  1.0,
        -1.0,  1.0, -1.0,
         1.0, -1.0,  1.0,
        -1.0, -1.0,  1.0,
        -1.0, -1.0, -1.0,
        -1.0,  1.0,  1.0,
        -1.0, -1.0,  1.0,
         1.0, -1.0,  1.0,
         1.0,  1.0,  1.0,
         1.0, -1.0, -1.0,
         1.0,  1.0, -1.0,
         1.0, -1.0, -1.0,
         1.0,  1.0,  1.0,
         1.0, -1.0,  1.0,
         1.0,  1.0,  1.0,
         1.0,  1.0, -1.0,
        -1.0,  1.0, -1.0,
         1.0,  1.0,  1.0,
        -1.0,  1.0, -1.0,
        -1.0,  1.0,  1.0,
         1.0,  1.0,  1.0,
        -1.0,  1.0,  1.0,
         1.0, -1.0,  1.0
    };


    static const GLfloat textureCoords[] = {
        0, 0,
        1, 0,
        0, 1,
        0, 1,
        1, 0,
        1, 1,
        0, 0,
        1, 0,
        0, 1,
        0, 1,
        1, 0,
        1, 1,
        0, 0,
        1, 0,
        0, 1,
        0, 1,
        1, 0,
        1, 1,
        0, 0,
        1, 0,
        0, 1,
        0, 1,
        1, 0,
        1, 1,
        0, 0,
        1, 0,
        0, 1,
        0, 1,
        1, 0,
        1, 1,
        0, 0,
        1, 0,
        0, 1,
        0, 1,
        1, 0,
        1, 1,
    };

    GLuint pointBuffer;
    glGenBuffers(1, &pointBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, pointBuffer);
    glBufferData(GL_ARRAY_BUFFER, vertices * 3, points, GL_STATIC_DRAW);

    GLuint textureBuffer;
    glGenBuffers(1, &textureBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, textureBuffer);
    glBufferData(GL_ARRAY_BUFFER, vertices * 2, textureCoords, GL_STATIC_DRAW);

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    glBindBuffer(GL_ARRAY_BUFFER, pointBuffer);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glBindBuffer(GL_ARRAY_BUFFER, textureBuffer);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    GLuint program = getProgramFromFiles("text_cube.vs.glsl", "text_cube.fs.glsl");
    glm::mat4 MVP = calculateMVP();
    GLuint texture = loadBMP("uvtemplate.bmp");

    if (!program) {
        log_msg(LOG_ERROR, "There was a problem opening shader.\n");

        // clean up
        return 1;
    }

    if (!texture) {
        log_msg(LOG_ERROR, "There was a problem opening shader.\n");

        // clean up
        return 1;
    }

    glUseProgram(program);

    GLuint MVPID = glGetUniformLocation(program, "MVP");
    GLuint textureID = glGetUniformLocation(program, "cube_texture");

    glUniformMatrix4fv(MVPID, 1, GL_FALSE, &MVP[0][0]);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glUniform1i(textureID, 0);

    while (!glfwWindowShouldClose(window) && glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glDrawArrays(GL_TRIANGLES, 0, vertices);

        glfwPollEvents();
        glfwSwapBuffers(window);
    }

    // clean up
    return 0;
} 

Just so you know, the error is not with setupGLFW(), setupApple(), setupGLEW(), getProgramFromFiles(), calculateMVP(), loadBMP(), or my vertex and fragment shaders, which are in different files. This compiles fine because I include GLFW and GLEW from myglutils.h This is uvtemplate.bmp, its a 512x512 image:
C++ OpenGL Textured Cube Missing Triangles
If you guys could help me, it would be greatly appreciated. Thanks!

您知道,错误不在于setupGLFW(),setupApple(),setupGLEW(),getProgramFromFiles(),calculateMVP(),loadBMP()或我的顶点和片段着色器,它们位于不同的文件中。这个编译很好,因为我包括来自myglutils.h的GLFW和GLEW。这是uvtemplate.bmp,它是一个512x512图像:如果你们可以帮助我,我将不胜感激。谢谢!

1 个解决方案

#1


1  

The Problem is the call to glBufferData(). You set the size in floats but you have to specify it in bytes usig sizeof(GLfloat). That's why OpenGL isn't receiving all of your data.

问题是对glBufferData()的调用。您可以在浮点数中设置大小,但必须以字节usig sizeof(GLfloat)指定它。这就是OpenGL没有收到所有数据的原因。

void glBufferData( GLenum target, GLsizeiptr size, const GLvoid * data, GLenum usage);

void glBufferData(GLenum target,GLsizeiptr size,const GLvoid * data,GLenum usage);

So you need to replace
glBufferData(GL_ARRAY_BUFFER, vertices * 3, points, GL_STATIC_DRAW);
and
glBufferData(GL_ARRAY_BUFFER, vertices * 2, textureCoords, GL_STATIC_DRAW);
with
glBufferData(GL_ARRAY_BUFFER, vertices * 3 * sizeof(GLfloat), points, GL_STATIC_DRAW);
and
glBufferData(GL_ARRAY_BUFFER, vertices * 2 * sizeof(GLfloat), textureCoords, GL_STATIC_DRAW);

所以你需要替换glBufferData(GL_ARRAY_BUFFER,顶点* 3,点,GL_STATIC_DRAW);和glBufferData(GL_ARRAY_BUFFER,顶点* 2,textureCoords,GL_STATIC_DRAW); with glBufferData(GL_ARRAY_BUFFER,vertices * 3 * sizeof(GLfloat),points,GL_STATIC_DRAW);和glBufferData(GL_ARRAY_BUFFER,顶点* 2 * sizeof(GLfloat),textureCoords,GL_STATIC_DRAW);

#1


1  

The Problem is the call to glBufferData(). You set the size in floats but you have to specify it in bytes usig sizeof(GLfloat). That's why OpenGL isn't receiving all of your data.

问题是对glBufferData()的调用。您可以在浮点数中设置大小,但必须以字节usig sizeof(GLfloat)指定它。这就是OpenGL没有收到所有数据的原因。

void glBufferData( GLenum target, GLsizeiptr size, const GLvoid * data, GLenum usage);

void glBufferData(GLenum target,GLsizeiptr size,const GLvoid * data,GLenum usage);

So you need to replace
glBufferData(GL_ARRAY_BUFFER, vertices * 3, points, GL_STATIC_DRAW);
and
glBufferData(GL_ARRAY_BUFFER, vertices * 2, textureCoords, GL_STATIC_DRAW);
with
glBufferData(GL_ARRAY_BUFFER, vertices * 3 * sizeof(GLfloat), points, GL_STATIC_DRAW);
and
glBufferData(GL_ARRAY_BUFFER, vertices * 2 * sizeof(GLfloat), textureCoords, GL_STATIC_DRAW);

所以你需要替换glBufferData(GL_ARRAY_BUFFER,顶点* 3,点,GL_STATIC_DRAW);和glBufferData(GL_ARRAY_BUFFER,顶点* 2,textureCoords,GL_STATIC_DRAW); with glBufferData(GL_ARRAY_BUFFER,vertices * 3 * sizeof(GLfloat),points,GL_STATIC_DRAW);和glBufferData(GL_ARRAY_BUFFER,顶点* 2 * sizeof(GLfloat),textureCoords,GL_STATIC_DRAW);