立方体没有按预期使用openGL出现

Posted

技术标签:

【中文标题】立方体没有按预期使用openGL出现【英文标题】:Cube not coming out as expected using openGL 【发布时间】:2018-06-21 04:53:29 【问题描述】:

我的立方体不是以立方体的形式出现,而是如下所示:

我似乎无法弄清楚为什么它没有呈现为立方体我想知道这里是否有人有任何指针。

我的代码如下:

#define GLEW_STATIC

#include <GL/glew.h>
#include <GLFW/glfw3.h> // glfw manages the window
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <stdio.h>
#include <chrono>
#include <math.h>

#define GLSL(version , shader) "#version " #version " core \n" #shader

int main() 

        glfwInit();

        // setup glfw
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

        glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

        // create window
        GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr);
        glfwMakeContextCurrent(window);

        // window complete OpenGL code below ---------------------------------------

        // initialise glew
        glewExperimental = GL_TRUE;
        glewInit();

        glEnable(GL_DEPTH_TEST);

        GLuint elements[] = 
                0, 1, 2
        ;

        // create the shape of triangle 

        GLfloat vertices[] = 
    -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
     0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
     0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
     0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
    -0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
    -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,

    -0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
     0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
     0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
     0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
    -0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
    -0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,

    -0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
    -0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
    -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
    -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
    -0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
    -0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,

     0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
     0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
     0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
     0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
     0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
     0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,

    -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
     0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
     0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
     0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
    -0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
    -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,

    -0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
     0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
     0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
     0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
    -0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
    -0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f
        ;

        const GLchar* vertexSource = GLSL(150,
                in vec3 position;
                in vec3 color;
                in vec2 texcoord;
                out vec3 Color;
                out vec2 Texcoord;
                uniform mat4 model;
                uniform mat4 view;
                uniform mat4 proj;
                void main()
                
                        Color = color;
                        Texcoord = texcoord;
                        gl_Position =  proj * view * model * vec4(position, 1.0);
                
        );

        const GLchar* fragmentSource = GLSL(150,
                in vec3 Color;
                out vec4 outColor;
                void main()
                
                        outColor = vec4(Color, 1.0);
                
        );

        GLuint vao;
        glGenVertexArrays(1 ,&vao);
        glBindVertexArray(vao);

        GLuint vbo;
        glGenBuffers(1, &vbo);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

        GLuint ebo;
        glGenBuffers(1, &ebo);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

        // create our shaders
        GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertexShader, 1, &vertexSource, NULL);
        glCompileShader(vertexShader);

        GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
        glCompileShader(fragmentShader);

        GLuint shaderProgram = glCreateProgram();
        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);
        glBindFragDataLocation(shaderProgram, 0, "outColor");
        glLinkProgram(shaderProgram);
        glUseProgram(shaderProgram);


        //GLuint uniColor = glGetUniformLocation(shaderProgram, "triangleColor");
        GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
        glEnableVertexAttribArray(posAttrib);
        glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE,5 * sizeof(float), 0);

        GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
        glEnableVertexAttribArray(colAttrib);
        glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
        auto t_start = std::chrono::high_resolution_clock::now();

        GLint uniTrans = glGetUniformLocation(shaderProgram, "model");
        GLint uniView = glGetUniformLocation(shaderProgram, "view");

        glm::mat4 view = glm::lookAt(
                glm::vec3(1.2f, 1.2f, 1.2f),
                glm::vec3(0.0f, 0.0f, 0.0f),
                glm::vec3(0.0f, 0.0f, 1.0f)
        );

        glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
        GLint uniProj = glGetUniformLocation(shaderProgram, "proj");
        glm::mat4 proj = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 1.0f, 10.0f);
        glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));


        while(!glfwWindowShouldClose(window))
        

                auto t_now = std::chrono::high_resolution_clock::now();
                float time = std::chrono::duration_cast<std::chrono::duration<float> >(t_now - t_start).count();

                glUniform3f(colAttrib, (sin(time * 4.0f) + 1.0f) / 2.0f, 0.0f, 0.0f);
                glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                glm::mat4 model = glm::mat4(1.0f);
                model = glm::rotate(
                                model,
                                time * glm::radians(180.0f),
                                glm::vec3(0.0f, 0.0f, 1.0f)
                                );

                glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));

                glDrawArrays(GL_TRIANGLES, 0, 36);

                glfwPollEvents();
                glfwSwapBuffers(window);

                if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) 
                        glfwSetWindowShouldClose(window, GL_TRUE);
                
        

        glDeleteProgram(shaderProgram);
        glDeleteShader(fragmentShader);
        glDeleteShader(vertexShader);

        glDeleteBuffers(1, &vbo);
        glDeleteBuffers(1, &ebo);
        glDeleteVertexArrays(1, &vao);
        glDeleteVertexArrays(1, &vao);

        glfwTerminate();

        return 0;

【问题讨论】:

ericlippert.com/2014/03/21/find-a-simpler-problem 在这种情况下,我建议先尝试制作一个三角形,然后制作一个正方形,然后制作三个正方形制作一个开放棱镜。 【参考方案1】:

您的glVertexAttribPointer() 调用暗示数据布局与vertices 中的内容不匹配。

指定与您的数据匹配的布局(四分量顶点 (XYZW) + 四分量颜色 (RGBA)):

glVertexAttribPointer
    (
    posAttrib,
    4, // four-component vertices (XYZW)
    GL_FLOAT,
    GL_FALSE,
    8 * sizeof(float), // a new vertex every 8 floats
    0 // first vertex starts zero floats in
    );

glVertexAttribPointer
    (
    colAttrib,
    4, // four-component colors (RGBA)
    GL_FLOAT,
    GL_FALSE,
    8 * sizeof(float), // a new color every 8 floats
    (void*)(4 * sizeof(float)) // first color starts 4 floats in
    );

【讨论】:

成功了!非常感谢,我已经为此苦苦思索了好几个小时! @BenMoore:感谢的修订版-1 minimal reproducible example!可悲的是,它们既有用又稀有!

以上是关于立方体没有按预期使用openGL出现的主要内容,如果未能解决你的问题,请参考以下文章

纹理不适用于我在 C++ 和 OpenGL 中的立方体

OpenGL立方体贴图面顺序和采样问题

OpenGL 深度/剔除问题

openGL按照载体自身坐标系旋转

奇怪的 OpenGL 立方体

Opengl 3d立方体不渲染