OpenGL无法渲染,可能是由于我找不到的一些简单错误

OpenGL not rendering, likely due to a few simple mistakes I couldn't find

本文关键字:找不到 于我 错误 简单 OpenGL      更新时间:2023-10-16

所以这段代码应该渲染一个矩形(最终我希望它渲染一个立方体,但在没有能够首先绘制矩形的情况下尝试这样做是没有意义的)。

我有代码来检查GL错误,虽然有一个,但这个问题的答案让我相信它是不相关的(它是GL_INVALID_ENUM,在glewInit();之后立即抛出)。

下面是我的代码: 主:

#include <glm/glm.hpp>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <thread>
#define GLEW_STATIC
#include <GL/glew.h>
#include <engine.hpp>
GLuint VertexBuffer, VertexArray, ElementBuffer, ShaderProgram;
GLfloat vertices[] = {
         -0.5, 0.5, 0.5, // Front Top Left      - 0
         0.5,  0.5, 0.5, // Front Top Right     - 1
         0.5, -0.5, 0.5, // Front Bottom Right  - 2
         -0.5,-0.5, 0.5, // Front Bottom Left   - 3
         -0.5, 0.5,-0.5, // Back Top Left       - 4
         0.5,  0.5,-0.5, // Back Top Right      - 5
         0.5, -0.5,-0.5, // Back Bottom Right   - 6
         -0.5,-0.5,-0.5, // Back Bottom Left    - 7
};
GLuint elements[]{
    0,1,2,
    2,3,0
};
int main()
{
    CheckForGLErrors(__func__, __LINE__);
    /*Initialization*/
    GLFWwindow* window = InitializeContext();
    CheckForGLErrors(__func__, __LINE__);
    //Create our element buffer object using the elements array
    ElementBuffer = InitializeElementBuffer(elements, sizeof(elements)/sizeof(GLuint));
    CheckForGLErrors(__func__, __LINE__);
    //Load and compile the shaders
    ShaderProgram = LoadShaders("vertexshader.cpp", "fragmentshader.cpp");
    CheckForGLErrors(__func__, __LINE__);
    //Create our vertex attribute array
    VertexArray = CreateVertexArray();
    CheckForGLErrors(__func__, __LINE__);
    //Create our vertex  buffer object using the vertices array
    VertexBuffer = InitializeVertexBuffer(vertices, sizeof(vertices)/sizeof(GLfloat));
    CheckForGLErrors(__func__, __LINE__);
    //Assign our array the appropriate structure
    InitializeVertexArray(ShaderProgram);
    CheckForGLErrors(__func__, __LINE__);


    while(!glfwWindowShouldClose(window))
    {
        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS){
            glfwSetWindowShouldClose(window, GL_TRUE);
        }
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        CheckForGLErrors(__func__, __LINE__);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    CheckForGLErrors(__func__, __LINE__);
    glfwTerminate();
}
  • 引擎(pastebin链接,因为SO似乎不希望我在我的帖子中有那么多文本)更新以修复user3256930指出的缺陷
  • engine_shaders.hpp(同样的原因再次粘贴)

我很乐意按要求提供更多的信息。

注意:我以前已经能够绘制三角形n的东西,但我的旧代码没有提供给我洞察力,为什么这是失败的

当你将数组作为函数参数传递时,它将其视为指针,并且你不能在函数中使用sizeof来获得数组的完整大小。在这种情况下,您还需要将数组的大小作为函数参数传递。当你调用glBufferData时,size参数期望以字节为单位的数组大小,而不是数组中元素的数量。