예제 #1
0
void Vao::initialize(RenderState& rs, ShaderProgram& _program, const VertexOffsets& _vertexOffsets,
                     VertexLayout& _layout, GLuint _vertexBuffer, GLuint _indexBuffer) {

    m_glVAOs.resize(_vertexOffsets.size());

    GL::genVertexArrays(m_glVAOs.size(), m_glVAOs.data());

    fastmap<std::string, GLuint> locations;

    // FIXME (use a bindAttrib instead of getLocation) to make those locations shader independent
    for (auto& attrib : _layout.getAttribs()) {
        GLint location = _program.getAttribLocation(attrib.name);
        locations[attrib.name] = location;
    }

    rs.vertexBuffer(_vertexBuffer);

    int vertexOffset = 0;
    for (size_t i = 0; i < _vertexOffsets.size(); ++i) {
        auto vertexIndexOffset = _vertexOffsets[i];
        int nVerts = vertexIndexOffset.second;
        GL::bindVertexArray(m_glVAOs[i]);

        // ELEMENT_ARRAY_BUFFER must be bound after bindVertexArray to be used by VAO
        if (_indexBuffer != 0) {
            rs.indexBufferUnset(_indexBuffer);
            rs.indexBuffer(_indexBuffer);
        }

        // Enable vertex layout on the specified locations
        _layout.enable(locations, vertexOffset * _layout.getStride());

        vertexOffset += nVerts;
    }

    GL::bindVertexArray(0);

    rs.vertexBuffer(0);
    rs.indexBuffer(0);
}
예제 #2
0
파일: vao.cpp 프로젝트: ForeverDavid/oglw
void Vao::init(GLuint _vertexBuffer, GLuint _indexBuffer,
    VertexLayout& _layout,
    const std::unordered_map<std::string, GLuint>&
    _locations)
{
    generate();

    // Bind the vertex array for initialization
    bind();

    // Bind the vertex and index buffer
    if (_vertexBuffer) {
        GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer));
    }

    if (_indexBuffer) {
        GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer));
    }

    _layout.enable(_locations);

    // Make sure VAO is not modified outside
    unbind();
}