Model::Model(Geometry* geometry, Textures* textures, ShaderProgram* shaderProgram) : m_geometry(geometry), m_shaderProgram(shaderProgram), m_textures(textures) { glGenVertexArrays ( 1, &m_vaoId ); glBindVertexArray ( m_vaoId ); // bind all geometry buffers for(int i=0; i < m_geometry->size(); ++i) { m_geometry->get(i)->bind(); m_geometry->get(i)->bufferData(); } // Set up the Vertex attribute pointer for the vVertex attribute for( auto attr : m_shaderProgram->attributeLocList ) { GeometryBuffer* buff = m_geometry->get( attr.first ); buff->bind(); glEnableVertexAttribArray( attr.second.index ); glVertexAttribPointer( attr.second.index, // Attribute location attr.second.size, // Number of elements per vertex, here (x,y,z), so 3 attr.second.type, // Data type of each element attr.second.normalized,// Normalised? attr.second.stride, // Stride attr.second.pointer // Offset ); } // add uniforms for every texture for (int i = 0; i <m_textures->size(); ++i) { const std::string textureUniformName = m_textures->textureName( i ); if ( textureUniformName != "" ) { m_shaderProgram->addUniform( textureUniformName ); } } glBindVertexArray ( 0 ); }
void Model::draw(Camera * camera) { shaderProgram()->setUniformMatrix4fv("projection", camera->projectionMatrix()); shaderProgram()->setUniformMatrix4fv("view", camera->transformationMatrix()); shaderProgram()->setUniformMatrix4fv("model", transformationMatrix()); bindTextures(); shaderProgram()->use(); if (isWireframeMode()) { // wireframe mode glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); } glBindVertexArray ( m_vaoId ); if (geometry()->isExist("index")) { GeometryBuffer *indices = geometry()->get("index"); indices->bind(); glDrawElements(GL_TRIANGLES, indices->size(), GL_UNSIGNED_INT, 0); } else { glDrawArrays ( GL_TRIANGLES, 0, geometry()->get("vertex")->size() ); } glBindVertexArray ( 0 ); if (isWireframeMode()) { // wireframe mode glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); } shaderProgram()->disable(); unbindTextures(); }