【问题标题】:Rewrite old OpenGL to OpenGL 4.5 with DSA?使用 DSA 将旧的 OpenGL 重写为 OpenGL 4.5?
【发布时间】:2019-04-22 15:23:46
【问题描述】:

我多次尝试重写以下代码,但都失败了,我知道我必须使用 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);

【问题讨论】:

    标签: c opengl opengl-4


    【解决方案1】:

    你不会“替换”glBindBuffer;你根本不使用它。这就像 DSA 的全部意义:不必绑定对象只是为了修改它们。

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

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

    相当于您尝试做的事情是:

    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);
    

    【讨论】:

    • 非常感谢,但与 glVertexArrayVertexBuffer(phong.vao, 0);论点不是太少了吗?
    • @Afendas:已修复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2012-01-07
    相关资源
    最近更新 更多