gpt4 book ai didi

c - OpenGL 3.3 核心配置文件 - glDrawArrays 无声地失败

转载 作者:太空宇宙 更新时间:2023-11-04 08:02:40 24 4
gpt4 key购买 nike

我正在使用 OpenGL 3.3 和 EGL 1.4。我用来构建和运行我的代码 (C) 的笔记本电脑使用英特尔的 HD 3000 集成 GPU 并运行 linux。因此,我使用 Mesa 3D 库。在我的程序中,我在 X11 窗口中创建并绑定(bind)了一个 OpenGL 3.3 核心配置文件兼容上下文,由稍后返回的 glGetString(GL_VERSION) 字符串“3.3 (Core Profile) Mesa 13.0.6”确认.

接下来,我使用 EGL 加载 OpenGL 规范的各种函数(我选择不使用 GLEW 等库)。完成此操作后,我开始设置 OpenGL 状态以渲染普通三角形。我初始化了一个 float 组,它们构成了三角形的 3 个硬编码顶点。然后,我初始化并绑定(bind)一个顶点数组对象,接着初始化并绑定(bind)一个顶点缓冲区对象。最后,我将我的顶点数组传递给 glBufferData,以将顶点数据与顶点缓冲区对象链接起来。

unsigned int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
unsigned int vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

然后,我初始化了一个基本的顶点和片段着色器。都编译成功,我的着色器程序链接成功,验证成功。

#version 330 core
layout (location = 0) in vec3 p;
void main(){
gl_Position = vec4(p, 0.0);
}

#version 330 core
out vec4 color;
void main(){
color = vec4(0.0, 0.0, 0.0, 1.0);
}

然后我继续告诉 GL 如何解释我的顶点数据;

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
glEnableVertexAttribArray(0);

最后,我进入程序的主循环,负责绘制和交换帧缓冲区,其中 bufferloop 是 struct timespec bufferloop = (struct timespec){0, 250000};

while(app.running){
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
eglSwapBuffers(egldisplay, eglsurface);
nanosleep(&bufferloop, (void *)0);
}

我得到的只是一个白屏;我的三角形无处可寻。我终生无法确定问题出在哪里。我已将 printf("%d\n", glGetError()); 粘贴到我的所有代码中,但未出现任何错误。glDrawArrays(GL_TRIANGLES, 0, 3); 毫无征兆地失败了。

最佳答案

在你的顶点着色器中,w 组件应该是 1 而不是 0。OpenGL 红皮书 here 中有很好的描述。描述了如何使用 w。简而言之:

OpenGL commands usually deal with two- and three-dimensional vertices, but in fact all are treated internally as three-dimensional homogeneous vertices comprising four coordinates. Every column vector (x, y, z, w)T represents a homogeneous vertex if at least one of its elements is nonzero.

As long as w is nonzero, the homogeneous vertex (x, y, z, w)T corresponds to the three-dimensional point (x/w, y/w, z/w)T. If w = 0.0, it corresponds to no euclidean point, but rather to some idealized "point at infinity." To understand this point at infinity, consider the point (1, 2, 0, 0), and note that the sequence of points (1, 2, 0, 1), (1, 2, 0, 0.01), and (1, 2.0, 0.0, 0.0001), corresponds to the euclidean points (1, 2), (100, 200), and (10000, 20000). This sequence represents points rapidly moving toward infinity along the line 2x = y. Thus, you can think of (1, 2, 0, 0) as the point at infinity in the direction of that line.

关于c - OpenGL 3.3 核心配置文件 - glDrawArrays 无声地失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45003134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com