OpenGL无法渲染,可能是由于我找不到的一些简单错误
OpenGL not rendering, likely due to a few simple mistakes I couldn't find
所以这段代码应该渲染一个矩形(最终我希望它渲染一个立方体,但在没有能够首先绘制矩形的情况下尝试这样做是没有意义的)。
我有代码来检查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参数期望以字节为单位的数组大小,而不是数组中元素的数量。
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 我在c ++矩阵计算器程序中找不到错误
- CMake:在其他目录中找不到我的头文件
- 错误:当我从"WinDbg"打开可执行文件时,找不到符号文件。默认导出 ntdll 的符号.dll
- 为什么我的静态分析器找不到潜在的未初始化读取?
- 找不到键盘,即使我包括键盘。
- 给定一个 3 x 3 矩阵或任何 n x n 矩阵,为什么我的以下函数找不到对角线差异?
- 包含文件 <sched_yield.h>在我的 RHEL 5 上找不到
- 我在代码中找不到错误以找到最大的素因数?
- 我想使用 "cout" 命令慢慢打印文本,但我真的找不到任何解决方案
- 链接器找不到库,即使我用 -L 指定了它的位置
- 我在QChartView中找不到缩放图形的鼠标滚轮滚动事件
- CMake GoogleTests在我的测试文件中找不到导入的头文件
- 在我的应用逻辑中找不到错误
- 在 Ubuntu 18.04 上,我编译的 cpp 可执行文件找不到 VTK7 依赖项
- 为什么在qt c++中找不到我的插槽?
- 我找不到瓦尔格林德告诉我的记忆链接
- 我找不到导致我的数组在运行两次案例并退出后损坏的错误
- C++创建进程 - 系统错误#2找不到文件 - 我的文件路径有什么问题?