gpt4 book ai didi

c - OpenGL 4.1 : Vertex Array Object draws nothing

转载 作者:行者123 更新时间:2023-11-30 19:11:53 26 4
gpt4 key购买 nike

我正在尝试使用顶点数组对象在 OpenGL 4.1 中绘制一些顶点和索引:

float vertices[] = {-0.8, -0.8, 0, 0.8, -0.8, 0, 0, 0.8, 0};
unsigned int indices[] = { 0, 1, 2 };

设置 VAO 并使用 glDrawElements() 绘制它后,我得到一个空屏幕。我还尝试使用一对简单的着色器,仅返回原始顶点位置和硬编码颜色,但也没有运气。

以下是设置 VBO 和 VAO 的代码:

unsigned int vertexArray;
glGenVertexArrays(1, &vertexArray);
glBindVertexArray(vertexArray);

unsigned int vertexBufferID;
unsigned int indexBufferID;

glGenBuffers(1, &vertexBufferID);
glGenBuffers(1, &indexBufferID);

glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, 4 * vertexCount, vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 4 * vertexCount, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * indexCount, indices, GL_STATIC_DRAW);

我使用以下方法绘制 VAO:

glBindVertexArray(vertexArray);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)0);

以下是所有 OpenGL 调用的跟踪:

// Setup code

glViewport(0,0,1024,768)
glGetString(GL_RENDERER)="GeForce GT 540M/PCIe/SSE2"
glGetString(GL_VENDOR)="NVIDIA Corporation"
glGetString(GL_VERSION)="4.1.0 NVIDIA 359.46"
glGetString(GL_SHADING_LANGUAGE_VERSION)="4.10 NVIDIA via Cg compil..."
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
glGenVertexArrays(1,000000000015F9B4)
glBindVertexArray(1)
glGenBuffers(1,000000000015F9D4)
glGenBuffers(1,000000000015F9F4)
glBindBuffer(GL_ARRAY_BUFFER,1)
glBufferData(GL_ARRAY_BUFFER,36,000000000015FA48,GL_STATIC_DRAW)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0,3,GL_FLOAT,false,36,0000000000000000)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,2)
glBufferData(GL_ELEMENT_ARRAY_BUFFER,12,000000000015FA88,GL_STATIC_DRAW)
glClearColor(0.300000,0.300000,0.400000,1.000000)

// End of setup, here's the code rendering two frames (they just repeat):

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
glBindVertexArray(1)
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0000000000000000)
glGetError()=GL_NO_ERROR
wglSwapBuffers(0000000069011A82)=true

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
glBindVertexArray(1)
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0000000000000000)
glGetError()=GL_NO_ERROR
wglSwapBuffers(0000000069011A82)=true

据我所知,我正在做几乎每个教程都说我应该做的事情,但由于某种原因,我的屏幕上没有出现任何内容。我错过了什么?

最佳答案

您的数据紧密地封装在数组中,因此 glVertexAttribPointer 中的 stride 参数应为 0。顺便说一句,如果您声明静态数组,则应使用 sizeof传递数组的大小而不是依赖于实现的东西,例如 4 * vertexCount:

glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

关于c - OpenGL 4.1 : Vertex Array Object draws nothing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38717414/

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