void HierarchicalRenderable::afterDraw() { //After the instance has been drawn using do_draw(), //we loop over its children and draw them. //The subtlety is that these children can be drawn with different shaderProgram, //therefore we shall NOT forget to : //-Bind their respective shaderProgram //-Send projection and view matrix to the GPU //-Draw the object ;) //-Unbind their respective shaderProgram. for(size_t i=0; i<m_children.size(); ++i) { // this affectation here is a little hack we use to keep the source code simple. // As we go through the hierarchy for the drawing, we will need to have access to the camera, in order // to get the projection and the view matrices. The non root hierarchical renderables has not been added // to the viewer, thus they do not have the field m_viewer correctly setted. This is why we perform this // affectation here: we are then sure this field is up-to-date when a do_draw() method is called. m_children[i]->m_viewer = m_viewer; m_children[i]->bindShaderProgram(); glcheck(glUniformMatrix4fv(m_children[i]->projectionLocation(), 1, GL_FALSE, glm::value_ptr(m_viewer->getCamera().projectionMatrix()))); glcheck(glUniformMatrix4fv(m_children[i]->viewLocation(), 1, GL_FALSE, glm::value_ptr(m_viewer->getCamera().viewMatrix()))); m_children[i]->draw(); m_children[i]->unbindShaderProgram(); } }
Skybox::~Skybox() { glcheck(glDeleteBuffers(1, &m_pBuffer)); glcheck(glDeleteBuffers(1, &m_tBuffer)); glcheck(glDeleteBuffers(1, &m_nBuffer)); glcheck(glDeleteTextures(1, &m_texId)); }
bool Material::sendToGPU(const ShaderProgramPtr& program, const MaterialPtr &material) { bool success = true; int location = -1; if(program==nullptr || material==nullptr) { return false; } location = program->getUniformLocation("material.ambient"); if(location!=ShaderProgram::null_location) { glcheck(glUniform3fv(location, 1, glm::value_ptr(material->ambient()))); } else { success = false; } location = program->getUniformLocation("material.diffuse"); if(location!=ShaderProgram::null_location) { glcheck(glUniform3fv(location, 1, glm::value_ptr(material->diffuse()))); } else { success = false; } location = program->getUniformLocation("material.specular"); if(location!=ShaderProgram::null_location) { glcheck(glUniform3fv(location, 1, glm::value_ptr(material->specular()))); } else { success = false; } location = program->getUniformLocation("material.shininess"); if(location!=ShaderProgram::null_location) { glcheck(glUniform1f(location, material->shininess())); } else { success = false; } return success; }
static void initializeGL() { //Initialize GLEW glewExperimental = GL_TRUE; GLenum err = glewInit(); if( GLEW_OK != err ) LOG( error, "[GLEW] " << glewGetErrorString(err) ); LOG( info, "[GLEW] using version " << glewGetString( GLEW_VERSION ) ); //Initialize OpenGL context glcheck(glClearColor(0.8f,0.8f,0.8f,1.0f)); glcheck(glEnable(GL_DEPTH_TEST)); glcheck(glDepthFunc(GL_LESS)); glcheck(glEnable(GL_VERTEX_PROGRAM_POINT_SIZE)); glcheck(glEnable(GL_TEXTURE_2D)); }
LakeRenderable::~LakeRenderable() { /* * Disabling the GPU buffers used while rendering. */ glcheck(glDeleteBuffers(1, &m_pBuffer)); }
void Viewer::handleEvent() { sf::Event event; while(m_window.pollEvent(event)) { switch(event.type) { case sf::Event::Closed: m_applicationRunning=false; break; case sf::Event::Resized: m_window.setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height))); m_camera.setRatio( (float)(m_window.getSize().x)/(float)(m_window.getSize().y) ); m_tengine.setWindowDimensions( m_window.getSize().x, m_window.getSize().y ); glcheck(glViewport(0, 0, event.size.width, event.size.height)); break; case sf::Event::KeyPressed: keyPressedEvent(event); break; case sf::Event::KeyReleased: keyReleasedEvent(event); break; case sf::Event::MouseWheelMoved: mouseWheelEvent(event); break; case sf::Event::MouseButtonPressed: mousePressEvent(event); break; case sf::Event::MouseButtonReleased: mouseReleaseEvent(event); break; case sf::Event::MouseMoved: mouseMoveEvent(event); break; default: break; } } if( m_camera.getMouseBehavior() == Camera::SPACESHIP_BEHAVIOR && (keyboard.forward || keyboard.backward || keyboard.left || keyboard.right ) ) { float speed = keyboard.speed * Duration(clock::now() - m_lastEventHandleTime).count(); if( keyboard.fast ) speed *= float(15.0); else if ( keyboard.slow ) speed *= float(0.5); glm::vec3 shift = speed * normalize( keyboard.direction ); m_camera.setPosition( m_camera.getPosition() + shift.x * m_camera.getRight() + shift.y * m_camera.getUp() + shift.z * m_camera.getForward() ); } m_lastEventHandleTime = clock::now(); }
ControlledForceFieldRenderable::ControlledForceFieldRenderable(ShaderProgramPtr program,ConstantForceFieldPtr forceField) : HierarchicalRenderable(program), m_force(forceField), m_pBuffer(0), m_cBuffer(0), m_nBuffer(0) { glm::vec3 initial_direction(1,0,0); m_status = ControlledForceFieldStatus(initial_direction); //Create geometric data to display an arrow representing the movement of the particle const std::vector<ParticlePtr>& particles = m_force->getParticles(); m_positions.clear(); m_colors.clear(); m_normals.clear(); for (ParticlePtr p : particles) { m_positions.push_back(p->getPosition()); m_positions.push_back(p->getPosition() + m_status.movement); m_colors.push_back(glm::vec4(1.0,0.0,0.0,1.0)); m_colors.push_back(glm::vec4(1.0,0.0,0.0,1.0)); m_normals.push_back(glm::vec3(1.0,0.0,0.0)); m_normals.push_back(glm::vec3(1.0,0.0,0.0)); } //Create buffers glGenBuffers(1, &m_pBuffer); //vertices glGenBuffers(1, &m_cBuffer); //colors glGenBuffers(1, &m_nBuffer); //normals //Activate buffer and send data to the graphics card glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_positions.size()*sizeof(glm::vec3), m_positions.data(), GL_STATIC_DRAW)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_colors.size()*sizeof(glm::vec4), m_colors.data(), GL_STATIC_DRAW)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_nBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_normals.size()*sizeof(glm::vec3), m_normals.data(), GL_STATIC_DRAW)); }
PointLightRenderable::PointLightRenderable(ShaderProgramPtr shaderProgram, PointLightPtr light) : HierarchicalRenderable(shaderProgram), m_light(light), m_pBuffer(0), m_cBuffer(0), m_nBuffer(0) { std::vector<glm::vec3> tmp_x, tmp_n; unsigned int strips=20, slices=20; glm::mat4 transformation(1.0); teachers::getUnitSphere(tmp_x, tmp_n, strips, slices); m_positions.insert(m_positions.end(), tmp_x.begin(), tmp_x.end()); m_normals.insert(m_normals.end(), tmp_n.begin(), tmp_n.end()); m_colors.resize(m_positions.size(), glm::vec4(light->diffuse(),1.0)); transformation = glm::translate(glm::mat4(1.0), m_light->position()); setParentTransform(transformation); //Create buffers glGenBuffers(1, &m_pBuffer); //vertices glGenBuffers(1, &m_cBuffer); //colors glGenBuffers(1, &m_nBuffer); //normals //Activate buffer and send data to the graphics card glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_positions.size()*sizeof(glm::vec3), m_positions.data(), GL_STATIC_DRAW)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_colors.size()*sizeof(glm::vec4), m_colors.data(), GL_STATIC_DRAW)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_nBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_normals.size()*sizeof(glm::vec3), m_normals.data(), GL_STATIC_DRAW)); }
void Viewer::draw() { glcheck(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); for( ShaderProgramPtr prog : m_programs ) { prog->bind(); DirectionalLight::sendToGPU( prog, m_directionalLight); SpotLight::sendToGPU( prog, m_spotLights); PointLight::sendToGPU( prog, m_pointLights); } for(RenderablePtr r : m_renderables) { if( r->getShaderProgram() ) { r->bindShaderProgram(); glcheck(glUniformMatrix4fv(r->projectionLocation(), 1, GL_FALSE, glm::value_ptr(m_camera.projectionMatrix()))); glcheck(glUniformMatrix4fv(r->viewLocation(), 1, GL_FALSE, glm::value_ptr(m_camera.viewMatrix()))); } r->draw(); r->unbindShaderProgram(); } //Refresh the viewer.m_window if( clock::now() < m_modeInformationTextDisappearanceTime ) { m_tengine.render( m_modeInformationText, glm::vec2(10, m_window.getSize().y - 30), glm::vec3(0.1, 0.1, 0.1) ); } { std::ostringstream ss; ss << "FPS: " << std::setprecision( 2 ) << std::fixed << m_fpsCounter.getFPS(); m_tengine.render( ss.str(), glm::vec2(m_window.getSize().x - 200, m_window.getSize().y - 30), glm::vec3(0.1,0.1,0.1) ); } if( m_helpDisplayed ) m_tengine.render( g_help_message, glm::vec2(100, 650), glm::vec3{.0, .1, .2}); }
ConeRenderable::ConeRenderable(ShaderProgramPtr shaderProgram) : Renderable(shaderProgram), m_pBuffer(0), m_cBuffer(0), m_nBuffer(0) { unsigned int strips=1; unsigned int slices=50; getUnitCone(m_positions, m_normals, strips, slices); m_colors.resize(m_positions.size(), glm::vec4(1.0,0.0,0.0,1.0)); for(size_t i=0; i<m_colors.size(); ++i) for(size_t j=0; j<3; ++j) m_colors[i][j] = m_normals[i][j]; //Create buffers glGenBuffers(1, &m_pBuffer); //vertices glGenBuffers(1, &m_cBuffer); //colors glGenBuffers(1, &m_nBuffer); //normals //Activate buffer and send data to the graphics card glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_positions.size()*sizeof(glm::vec3), m_positions.data(), GL_STATIC_DRAW)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_colors.size()*sizeof(glm::vec4), m_colors.data(), GL_STATIC_DRAW)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_nBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_normals.size()*sizeof(glm::vec3), m_normals.data(), GL_STATIC_DRAW)); }
void LightedCylinderRenderable::do_draw() { //Location int positionLocation = m_shaderProgram->getAttributeLocation("vPosition"); int colorLocation = m_shaderProgram->getAttributeLocation("vColor"); int normalLocation = m_shaderProgram->getAttributeLocation("vNormal"); int modelLocation = m_shaderProgram->getUniformLocation("modelMat"); int nitLocation = m_shaderProgram->getUniformLocation("NIT"); //Send material uniform to GPU Material::sendToGPU(m_shaderProgram, m_material); //Send data to GPU if(modelLocation != ShaderProgram::null_location) { glcheck(glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(getModelMatrix()))); } if(positionLocation != ShaderProgram::null_location) { //Activate location glcheck(glEnableVertexAttribArray(positionLocation)); //Bind buffer glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); //Specify internal format glcheck(glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if(colorLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(colorLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer)); glcheck(glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if(normalLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(normalLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_nBuffer)); glcheck(glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if( nitLocation != ShaderProgram::null_location ) { glcheck(glUniformMatrix3fv( nitLocation, 1, GL_FALSE, glm::value_ptr(glm::transpose(glm::inverse(glm::mat3(getModelMatrix())))))); } //Draw triangles elements glcheck(glDrawArrays(GL_TRIANGLES,0, m_positions.size())); if(positionLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(positionLocation)); } if(colorLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(colorLocation)); } if(normalLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(normalLocation)); } }
PointLightRenderable::~PointLightRenderable() { glcheck(glDeleteBuffers(1, &m_pBuffer)); glcheck(glDeleteBuffers(1, &m_cBuffer)); glcheck(glDeleteBuffers(1, &m_nBuffer)); }
ConeRenderable::~ConeRenderable() { glcheck(glDeleteBuffers(1, &m_pBuffer)); glcheck(glDeleteBuffers(1, &m_cBuffer)); glcheck(glDeleteBuffers(1, &m_nBuffer)); }
void LakeRenderable::do_draw() { /* * Gathering uniforms and attributes locations. */ // Position. int positionLocation = m_shaderProgram->getAttributeLocation("coord"); // Model matrix. int modelLocation = m_shaderProgram->getUniformLocation("modelMat"); // The texture scale parameter. int scaleTextureLocation = m_shaderProgram->getUniformLocation("scaleTexture"); // Texture. int lakeTextureLocation = m_shaderProgram->getUniformLocation("seaTex"); /* * Sending material uniform to the GPU. */ Material::sendToGPU(m_shaderProgram, m_material); /* * Binding position buffer. */ if(positionLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(positionLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); glcheck(glVertexAttribPointer( positionLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 ) ); } /* * Sending the model matrix (uniform). */ if(modelLocation != ShaderProgram::null_location) { glcheck(glUniformMatrix4fv( modelLocation, 1, GL_FALSE, glm::value_ptr(getModelMatrix()) ) ); } /* * Sending the texture scale parameter (uniform). */ if (scaleTextureLocation != ShaderProgram::null_location) { glcheck(glUniform1f(scaleTextureLocation, 20.0f)); } /* * Binding and sending the lake texture. */ if (lakeTextureLocation != ShaderProgram::null_location) { glcheck(glActiveTexture(GL_TEXTURE0)); glcheck(glBindTexture(GL_TEXTURE_2D, m_textureId)); glcheck(glUniform1i(lakeTextureLocation, 0)); } /* * Drawing the triangles in order to render the scene. */ glcheck(glDrawArrays(GL_TRIANGLES, 0, m_positions.size())); /* * Releasing the texture. */ glcheck(glBindTexture(GL_TEXTURE_2D, 0)); /* * Disabling the buffers. */ if(positionLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(positionLocation)); } }
ControlledForceFieldRenderable::~ControlledForceFieldRenderable() { glcheck(glDeleteBuffers(1, &m_pBuffer)); glcheck(glDeleteBuffers(1, &m_cBuffer)); glcheck(glDeleteBuffers(1, &m_nBuffer)); }
void ParticleRenderable::do_draw() { //Update the parent and local transform matrix to position the geometric data according to the particle's data. const float& pRadius = m_particle->getRadius(); const glm::vec3& pPosition = m_particle->getPosition(); float toRotate = m_particle->getAngle(); glm::mat4 scale = glm::scale(glm::mat4(1.0), glm::vec3(pRadius)); glm::mat4 translate = glm::translate(glm::mat4(1.0), glm::vec3(pPosition)); glm::mat4 rotate = glm::rotate(glm::mat4(1.0), toRotate, glm::vec3(0,0,1)); setLocalTransform(translate*scale*rotate); //Draw geometric data int positionLocation = m_shaderProgram->getAttributeLocation("vPosition"); int colorLocation = m_shaderProgram->getAttributeLocation("vColor"); int normalLocation = m_shaderProgram->getAttributeLocation("vNormal"); int modelLocation = m_shaderProgram->getUniformLocation("modelMat"); if (modelLocation != ShaderProgram::null_location) { glcheck(glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(getModelMatrix()))); } if (positionLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(positionLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); glcheck(glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if (colorLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(colorLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer)); glcheck(glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if (normalLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(normalLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_nBuffer)); glcheck(glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } //Draw triangles elements glcheck(glDrawArrays(GL_TRIANGLES,0, m_positions.size())); if (positionLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(positionLocation)); } if (colorLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(colorLocation)); } if (normalLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(normalLocation)); } }
LightedCylinderRenderable::~LightedCylinderRenderable() { glcheck(glDeleteBuffers(1, &m_pBuffer)); glcheck(glDeleteBuffers(1, &m_cBuffer)); glcheck(glDeleteBuffers(1, &m_nBuffer)); }
void SphereRenderable::do_draw() { int positionLocation = m_shaderProgram->getAttributeLocation("vPosition"); int colorLocation = m_shaderProgram->getAttributeLocation("vColor"); int normalLocation = m_shaderProgram->getAttributeLocation("vNormal"); int modelLocation = m_shaderProgram->getUniformLocation("modelMat"); if(modelLocation != ShaderProgram::null_location) { glcheck(glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(getModelMatrix()))); } if(positionLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(positionLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); glcheck(glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if(colorLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(colorLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer)); glcheck(glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if(normalLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(normalLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_nBuffer)); glcheck(glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } //Draw triangles elements glcheck(glDrawArrays(GL_TRIANGLES,0, m_positions.size())); if(positionLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(positionLocation)); } if(colorLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(colorLocation)); } if(normalLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(normalLocation)); } }
ParticleRenderable::ParticleRenderable(ShaderProgramPtr shaderProgram, ParticlePtr particle) : HierarchicalRenderable(shaderProgram), m_particle(particle), m_pBuffer(0), m_cBuffer(0), m_nBuffer(0) { double radius = 1.0; int thetaStep = 40; int phiStep = 10; glm::vec3 center(0.0, 0.0, 0.0); for (int i = 0; i < thetaStep; ++i) { for(int j = 0; j < phiStep; ++j) { double curr_theta = i*(2.0*M_PI / (double) thetaStep); double curr_phi = j*(M_PI / (double)phiStep); double next_theta = (i+1) * (2.0 * M_PI / (double) thetaStep); double next_phi = (j+1) * (M_PI / (double) phiStep); glm::vec3 faceNormal(1, 0, 0); std::array<glm::vec3,3> vTriangles; vTriangles[0] = center + glm::vec3(radius * cos(curr_theta) * sin(curr_phi), radius * sin(curr_theta) * sin(curr_phi), radius * cos(curr_phi)); vTriangles[1] = center + glm::vec3(radius * cos(next_theta) * sin(curr_phi), radius * sin(next_theta) * sin(curr_phi), radius * cos(curr_phi)); vTriangles[2] = center + glm::vec3(radius * cos(next_theta) * sin(next_phi), radius * sin(next_theta) * sin(next_phi), radius * cos(next_phi)); faceNormal = -glm::normalize(glm::cross(vTriangles[1]-vTriangles[0], vTriangles[2]-vTriangles[0])); m_positions.push_back(vTriangles[0]); m_positions.push_back(vTriangles[1]); m_positions.push_back(vTriangles[2]); m_normals.push_back(faceNormal); m_normals.push_back(faceNormal); m_normals.push_back(faceNormal); m_colors.push_back(glm::vec4(1.0,1.0,1.0,1.0)); m_colors.push_back(glm::vec4(1.0,1.0,1.0,1.0)); m_colors.push_back(glm::vec4(1.0,1.0,1.0,1.0)); vTriangles[0] = center + glm::vec3(radius * cos(curr_theta) * sin(curr_phi), radius * sin(curr_theta) * sin(curr_phi), radius * cos(curr_phi)); vTriangles[1] = center + glm::vec3(radius * cos(next_theta) * sin(next_phi), radius * sin(next_theta) * sin(next_phi), radius * cos(next_phi)); vTriangles[2] = center + glm::vec3(radius * cos(curr_theta) * sin(next_phi), radius * sin(curr_theta) * sin(next_phi), radius * cos(next_phi)); faceNormal = -glm::normalize(glm::cross( vTriangles[1]-vTriangles[0], vTriangles[2]-vTriangles[0] )); m_positions.push_back(vTriangles[0]); m_positions.push_back(vTriangles[1]); m_positions.push_back(vTriangles[2]); m_normals.push_back(faceNormal); m_normals.push_back(faceNormal); m_normals.push_back(faceNormal); m_colors.push_back(glm::vec4(1.0,1.0,1.0,1.0)); m_colors.push_back(glm::vec4(1.0,1.0,1.0,1.0)); m_colors.push_back(glm::vec4(1.0,1.0,1.0,1.0)); } } //Create buffers glGenBuffers(1, &m_pBuffer); //vertices glGenBuffers(1, &m_cBuffer); //colors glGenBuffers(1, &m_nBuffer); //normals //Activate buffer and send data to the graphics card glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_positions.size()*sizeof(glm::vec3), m_positions.data(), GL_STATIC_DRAW)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_colors.size()*sizeof(glm::vec4), m_colors.data(), GL_STATIC_DRAW)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_nBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_normals.size()*sizeof(glm::vec3), m_normals.data(), GL_STATIC_DRAW)); }
void LegRenderable::do_draw() { //Update the parent and local transform matrix to position the geometric data according to the particle's data. float thighAngle = 4 * 3.14 / 3.0; float toRotateBody = 0.5 * m_particle->getBodyAngle(); glm::mat4 ParentTransform; if (m_isLeft) { ParentTransform = glm::scale(glm::mat4(), glm::vec3(1.5, 1.5, 1.6)); ParentTransform = glm::translate(ParentTransform, glm::vec3(-0.4, -0.3, 2)); ParentTransform = glm::rotate(ParentTransform, -thighAngle, glm::vec3(0, 1, 0.0)); glm::mat4 rotate = glm::rotate(glm::mat4(1.0), 0.0f, glm::vec3(0,1,0)); if (toRotateBody < 0) { rotate = glm::rotate(glm::mat4(1.0), toRotateBody, glm::vec3(0,1,0)); } ParentTransform = ParentTransform * rotate; } else { ParentTransform = glm::scale(glm::mat4(), glm::vec3(1.5, 1.5, 1.6)); ParentTransform = glm::translate(ParentTransform, glm::vec3(-0.4, 0.3, 2)); ParentTransform = glm::rotate(ParentTransform, -thighAngle, glm::vec3(0, 1, 0.0)); glm::mat4 rotate = glm::rotate(glm::mat4(1.0), 0.0f, glm::vec3(0,1,0)); if (toRotateBody > 0) { rotate = glm::rotate(glm::mat4(1.0), -toRotateBody, glm::vec3(0,1,0)); } ParentTransform = ParentTransform * rotate; } setLocalTransform(getLocalTransform()); setParentTransform(ParentTransform); //Draw geometric data int positionLocation = m_shaderProgram->getAttributeLocation("vPosition"); int colorLocation = m_shaderProgram->getAttributeLocation("vColor"); int normalLocation = m_shaderProgram->getAttributeLocation("vNormal"); int modelLocation = m_shaderProgram->getUniformLocation("modelMat"); if (modelLocation != ShaderProgram::null_location) { glcheck(glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(getModelMatrix()))); } if (positionLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(positionLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); glcheck(glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if (colorLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(colorLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer)); glcheck(glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if (normalLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(normalLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_nBuffer)); glcheck(glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } //Draw triangles elements glcheck(glDrawArrays(GL_TRIANGLES,0, m_positions.size())); if (positionLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(positionLocation)); } if (colorLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(colorLocation)); } if (normalLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(normalLocation)); } }
void ControlledForceFieldRenderable::do_draw() { //Update vertices positions from particle's positions const std::vector<ParticlePtr>& particles = m_force->getParticles(); m_positions.clear(); m_colors.clear(); m_normals.clear(); //Display an arrow representing the movement of the particle for (ParticlePtr p : particles) { p->setAngle(m_status.angle); p->setBodyAngle(m_status.bodyAngle); p->setBraking(m_status.braking); /*m_positions.push_back(p->getPosition()); m_positions.push_back(p->getPosition() + 2.0f* m_status.movement); m_colors.push_back(glm::vec4(1.0,0.0,0.0,1.0)); m_colors.push_back(glm::vec4(1.0,0.0,0.0,1.0)); m_normals.push_back(glm::vec3(1.0,0.0,0.0)); m_normals.push_back(glm::vec3(1.0,0.0,0.0));*/ } //Update data on the GPU glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); glcheck(glBufferData(GL_ARRAY_BUFFER, m_positions.size()*sizeof(glm::vec3), m_positions.data(), GL_STATIC_DRAW)); //Draw geometric data int positionLocation = m_shaderProgram->getAttributeLocation("vPosition"); int colorLocation = m_shaderProgram->getAttributeLocation("vColor"); int normalLocation = m_shaderProgram->getAttributeLocation("vNormal"); int modelLocation = m_shaderProgram->getUniformLocation("modelMat"); if (modelLocation != ShaderProgram::null_location) { glcheck(glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(getModelMatrix()))); } if (positionLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(positionLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); glcheck(glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if (colorLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(colorLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer)); glcheck(glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if (normalLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(normalLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_nBuffer)); glcheck(glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } //Draw elements glLineWidth(3.0); glcheck(glDrawArrays(GL_LINES,0, m_positions.size())); glLineWidth(1.0); if (positionLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(positionLocation)); } if (colorLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(colorLocation)); } if (normalLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(normalLocation)); } }
LakeRenderable::LakeRenderable( ShaderProgramPtr shaderProgram, std::list< std::pair< std::vector<int>, std::vector<glm::vec3> > >& lakesTriangles ) : HierarchicalRenderable(shaderProgram), m_positions(0) { /* * Pushing the triangles into the positions buffer. */ for ( auto globalIt = lakesTriangles.begin(); globalIt != lakesTriangles.end(); globalIt++ ) { /* * So as to set the altitude of the lake coherently, we compute the * average altitude of the vertices constituting the connexe lakes. */ float averageAltitude = 0.0; for ( auto localIt = globalIt->second.begin(); localIt != globalIt->second.end(); localIt++ ) { averageAltitude += localIt->z; } averageAltitude /= (float)(globalIt->second.size()); /* * If the average altitude is below the sea altitude, we slightly * increase it. */ //if (averageAltitude < 0.0) { // averageAltitude = 0.3; //} for ( auto localIt = globalIt->second.begin(); localIt != globalIt->second.end(); localIt++ ) { m_positions.push_back(glm::vec3(localIt->x, localIt->y, averageAltitude)); } } /* * Creating the position buffer. */ glGenBuffers(1, &m_pBuffer); /* * Activating the buffer and sending data to the GPU. */ glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); glcheck(glBufferData( GL_ARRAY_BUFFER, m_positions.size()*sizeof(glm::vec3), m_positions.data(), GL_STATIC_DRAW ) ); /* * Creating the texture associated with lakes. */ std::vector<std::string> filenames; std::string name= "../textures/shutter_texture_water_lake"; std::string extension = ".png"; int minRes = 2; int maxRes = 8; for (int i = minRes; i <= maxRes; ++i) { filenames.push_back(name + std::to_string(i) + extension); } sendMipMapTextures(filenames, &m_textureId); }