c - 使用 DSA 将旧 OpenGL 重写为 OpenGL 4.5?

标签 c opengl opengl-4

我多次尝试重写以下代码,但失败了,我知道我必须使用 glCreateBuffers、glVertexArrayElementBuffer、glVertexArrayVertexBuffer、glnamedBufferData、glBindVertexArray 等函数,但我在替换 glBindBuffer、glVertexAttribPointer、glEnableVertexAttribArray 和 glGetAttribLocation 部分时遇到问题。

这是我尝试实现的以下代码:

glCreateBuffers(1, &phong.vbo);  
glNamedBufferData(phong.vbo,sizeof(modelVertices),modelVertices,GL_STATIC_DRAW);
glCreateBuffers(1, &phong.ebo);  glNamedBufferData(phong.ebo,sizeof(modelIndices),modelIndices,GL_STATIC_DRAW); 
glCreateVertexArrays(1, &phong.vao);
glBindVertexArray(phong.vao); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, phong.ebo);   
glBindBuffer(GL_ARRAY_BUFFER, phong.vbo);
const GLint possitionAttribute = glGetAttribLocation(phong.program, "position");
glVertexAttribPointer((GLuint) possitionAttribute, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 6, (const GLvoid *) 0 );
glEnableVertexAttribArray((GLuint) possitionAttribute);

最佳答案

你不“替换”glBindBuffer;你只是不使用它。这就像 DSA 的全部要点:不必仅仅为了修改对象而绑定(bind)对象。

glGetAttribLocation 已经是一个 DSA 函数(第一个参数是它作用的对象),因此您可以继续使用它(或者更好的是,停止询问着色器和 assign position a specific location within the shader )。

glVertexArray* 函数都操作 VAO、附加缓冲区并定义顶点格式。您遇到的“问题”是您已经习惯了糟糕的 glVertexAttribPointer API,该 API 已被 with a much better API in 4.3 取代。 。而且 DSA 不提供可怕的旧 API 的 DSA 版本。因此您需要使用单独的属性格式 API,以 DSA 形式。

与您尝试执行的操作等效的是:

glCreateVertexArrays(1, &phong.vao);

//Setup vertex format.
auto positionAttribute = glGetAttribLocation(phong.program, "position");
glEnableVertexArrayAttrib(phong.vao, positionAttribute);
glVertexArrayAttribFormat(phong.vao, positionAttribute, 3, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(phong.vao, positionAttribute, 0);

//Attach a buffer to be read from.
//0 is the *binding index*, not the attribute index.
glVertexArrayVertexBuffer(phong.vao, 0, phong.vbo, 0, sizeof(GLfloat) * 6);

//Index buffer
glVertexArrayElementBuffer(phong.vao, phong.ebo);

关于c - 使用 DSA 将旧 OpenGL 重写为 OpenGL 4.5?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55796756/

相关文章:

c - C 中的 char **ptr 和 char *ptr[] 有什么区别?

c++ - OpenGL屏幕坐标到世界坐标

opengl - 使用 premake4 构建项目文件 - 如何?

c++ - 如何在 OpenGL 中实现绘画(支持图层)?

c - 扫描抛物线函数ax ^ 2 + bx + c的a,b和c值的数组

C程序接受两个字符

c++ - 如何在 OpenGL 中创建网格并用线条绘制

c++ - 在 OpenGL 4 中绘制一个简单的矩形

c - 结构中的二维数组 - C -