void VertexLayout::enable(RenderState& rs, ShaderProgram& _program, size_t _byteOffset, void* _ptr) { GLuint glProgram = _program.getGlProgram(); // Enable all attributes for this layout for (auto& attrib : m_attribs) { GLint location = _program.getAttribLocation(attrib.name); if (location != -1) { auto& loc = rs.attributeBindings[location]; // Track currently enabled attribs by the program to which they are bound if (loc != glProgram) { GL::enableVertexAttribArray(location); loc = glProgram; } void* data = (unsigned char*)_ptr + attrib.offset + _byteOffset; GL::vertexAttribPointer(location, attrib.size, attrib.type, attrib.normalized, m_stride, data); } } // Disable previously bound and now-unneeded attributes for (size_t i = 0; i < RenderState::MAX_ATTRIBUTES; ++i) { GLuint& boundProgram = rs.attributeBindings[i]; if (boundProgram != glProgram && boundProgram != 0) { GL::disableVertexAttribArray(i); boundProgram = 0; } } }
void VertexLayout::enable(ShaderProgram& _program, size_t _byteOffset, void* _ptr) { GLuint glProgram = _program.getGlProgram(); // Enable all attributes for this layout for (auto& attrib : m_attribs) { GLint location = _program.getAttribLocation(attrib.name); if (location != -1) { auto& loc = s_enabledAttribs[location]; // Track currently enabled attribs by the program to which they are bound if (loc != glProgram) { glEnableVertexAttribArray(location); loc = glProgram; } void* data = _ptr ? _ptr : ((unsigned char*) attrib.offset) + _byteOffset; glVertexAttribPointer(location, attrib.size, attrib.type, attrib.normalized, m_stride, data); } } // Disable previously bound and now-unneeded attributes for (auto& locationProgramPair : s_enabledAttribs) { const GLint& location = locationProgramPair.first; GLuint& boundProgram = locationProgramPair.second; if (boundProgram != glProgram && boundProgram != 0) { glDisableVertexAttribArray(location); boundProgram = 0; } } }
TEST_F(TransformFeedbackTest, test) { std::ifstream vsFile("data/transform_feedback.vert"); std::ifstream gsFile("data/transform_feedback.geom"); Shader vertex(GL_VERTEX_SHADER); Shader geometry(GL_GEOMETRY_SHADER); ShaderProgram shader; Query query(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN); VAO vao; VBO vbo; TFBO tfbo; const GLchar* feedbackVaryings[] = { "outValue" }; GLfloat data[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }; GLfloat feedback[15]; GLuint primitives; ASSERT_TRUE(vertex.compile(vsFile)); ASSERT_TRUE(geometry.compile(gsFile)); shader.attach(vertex); shader.attach(geometry); shader.setTransformFeedbackVaryings(1, feedbackVaryings, GL_INTERLEAVED_ATTRIBS); ASSERT_TRUE(shader.link()); shader.use(); shader.printDebug(); vao.bind(); vbo.bind(); vbo.setData(sizeof(data), data, GL_STATIC_DRAW); GLint inputAttrib = shader.getAttribLocation("inValue"); vao.enableAttrib(inputAttrib); glVertexAttribPointer(inputAttrib, 1, GL_FLOAT, GL_FALSE, 0, 0); tfbo.setData(sizeof(data) * 3, nullptr, GL_STATIC_READ); tfbo.bindBufferBase(0); mogl::enable(GL_RASTERIZER_DISCARD); query.begin(); TransformFeedback::begin(GL_TRIANGLES); glDrawArrays(GL_POINTS, 0, 5); TransformFeedback::end(); query.end(); mogl::disable(GL_RASTERIZER_DISCARD); mogl::Sync::flush(); primitives = query.get<GLuint>(GL_QUERY_RESULT); tfbo.getSubData(0, sizeof(feedback), feedback); std::cout << primitives << " primitives written" << std::endl; for (unsigned int i = 0; i < 15; ++i) std::cout << feedback[i] << std::endl; }
void Cube::uploadVaoData(const ShaderProgram &shader) { glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); GLint positionAttribLocation = shader.getAttribLocation( "position" ); glEnableVertexAttribArray(positionAttribLocation); glVertexAttribPointer(positionAttribLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr); //-- Unbind target, and restore default values: glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); CHECK_GL_ERRORS; }
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); }
// Draw the skybox using the skybox shader void Skybox::draw(ShaderProgram& s){ GLint posAttrib = s.getAttribLocation( "position" ); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_CUBE_MAP,tex); GLint cube = s.getUniformLocation("cubeMap"); glUniform1i(cube,0); glEnableVertexAttribArray(posAttrib); glBindBuffer(GL_ARRAY_BUFFER, vbo); glVertexAttribPointer( posAttrib, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 ); glDrawArrays (GL_TRIANGLES, 0, 36); glBindBuffer(GL_ARRAY_BUFFER,0); }
Model::Model(std::string filename, ShaderProgram &s, std::string positionName, std::string normalName, std::string uvName) { loadModel(filename, s.getAttribLocation(positionName), s.getAttribLocation(normalName), s.getAttribLocation(uvName)); }