C++ C++;OpenGL纹理立方体缺少三角形

C++ C++;OpenGL纹理立方体缺少三角形,c++,opengl,3d,textures,glfw,C++,Opengl,3d,Textures,Glfw,在我走向OpenGL的过程中,当我遇到这个问题时,我正试图制作一个纹理立方体: 只渲染了我的多维数据集的一部分。应该有12个三角形,但只渲染其中的3个。我看过很多教程,但我似乎找不到代码之间的问题/主要区别。这是我的: ... int main(int argc, char* argv[]) { if (!setupGLFW()) return 1; setupApple(); glfwWindowHint(GLFW_SAMPLES, 4); GLFWwin

在我走向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;
} 
正如您所知,
setupGLFW()
setupApple()
setupGLEW()
getprogrammfromfiles()
calculateMVP()
loadBMP()
、或我的顶点和片段着色器都不在不同的文件中。这编译得很好,因为我从
myglutils.h
中包括
GLFW
GLEW
这是
uvtemplate.bmp
,它是一幅512x512图像:


如果你们能帮助我,我将不胜感激。谢谢

问题在于调用
glBufferData()
。您可以使用浮点设置大小,但必须使用
sizeof(GLfloat)
以字节为单位指定大小。这就是为什么OpenGL没有接收所有数据

void glBufferData
    (
    GLenum target,
    GLsizeiptr size,
    const GLvoid* data,
    GLenum usage
    );
因此,您需要替换:

glBufferData(GL_ARRAY_BUFFER, vertices * 3, points, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertices * 2, textureCoords, GL_STATIC_DRAW);
与:


哇!我不敢相信我犯了那个错误!我在OpenGL中的所有其他程序都是这样做的,我不知道为什么我的大脑会滑到那里。非常感谢!
glBufferData(GL_ARRAY_BUFFER, vertices * 3 * sizeof(GLfloat), points, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertices * 2 * sizeof(GLfloat), textureCoords, GL_STATIC_DRAW);