void cube::visit(float dt) { if (!visible) return; cube_shader.use(); set_pl_uniforms(); auto camera = get_default_camera(); glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)get_width() / get_height(), 0.1f, 100.f); glm::mat4 view = glm::lookAt(camera.pos, camera.pos + camera.front, camera.up); auto model = glm::mat4(); model = glm::translate(model, pos); model = glm::scale(model, scale); model = glm::rotate(model, glm::radians(rotation), glm::vec3(1.0)); glm::mat4 mvp = projection * view * model; glm::mat4 mv = view * model; glUniformMatrix4fv(cube_uniforms.mvp_loc, 1, GL_FALSE, glm::value_ptr(mvp)); glUniformMatrix4fv(cube_uniforms.mv_loc, 1, GL_FALSE, glm::value_ptr(mv)); glUniformMatrix4fv(cube_uniforms.m_loc, 1, GL_FALSE, glm::value_ptr(model)); glUniform3f(cube_uniforms.view_loc, camera.pos.x, camera.pos.y, camera.pos.z); d_map[0].bind(d_loc[0], 0); s_map[0].bind(s_loc[0], 1); draw(); }
void DisplayDeviceOpenGL::render(const Renderable* r) const { if(!r->isEnabled()) { // Renderable item not enabled then early return. return; } StencilScopePtr stencil_scope; if(r->hasClipSettings()) { ModelManager2D mm(r->getPosition().x, r->getPosition().y); auto clip_shape = r->getStencilMask(); bool cam_set = false; if(clip_shape->getCamera() == nullptr && r->getCamera() != nullptr) { cam_set = true; clip_shape->setCamera(r->getCamera()); } stencil_scope.reset(new StencilScopeOGL(r->getStencilSettings())); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glDepthMask(GL_FALSE); glClear(GL_STENCIL_BUFFER_BIT); render(clip_shape.get()); stencil_scope->applyNewSettings(keep_stencil_settings); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_TRUE); if(cam_set) { clip_shape->setCamera(nullptr); } } auto shader = r->getShader(); shader->makeActive(); BlendEquationScopeOGL be_scope(*r); BlendModeScopeOGL bm_scope(*r); // apply lighting/depth check/depth write here. bool use_lighting = r->isLightingStateSet() ? r->useLighting() : false; // Set the depth enable. if(r->isDepthEnableStateSet()) { if(get_current_depth_enable() != r->isDepthEnabled()) { if(r->isDepthEnabled()) { glEnable(GL_DEPTH_TEST); } else { glDisable(GL_DEPTH_TEST); } get_current_depth_enable() = r->isDepthEnabled(); } } else { // We assume that depth is disabled if not specified. if(get_current_depth_enable() == true) { glDisable(GL_DEPTH_TEST); get_current_depth_enable() = false; } } glm::mat4 pmat(1.0f); glm::mat4 vmat(1.0f); if(r->getCamera()) { // set camera here. pmat = r->getCamera()->getProjectionMat(); vmat = r->getCamera()->getViewMat(); } else if(get_default_camera() != nullptr) { pmat = get_default_camera()->getProjectionMat(); vmat = get_default_camera()->getViewMat(); } if(use_lighting) { for(auto lp : r->getLights()) { /// xxx need to set lights here. } } if(r->getRenderTarget()) { r->getRenderTarget()->apply(); } if(shader->getPUniform() != ShaderProgram::INVALID_UNIFORM) { shader->setUniformValue(shader->getPUniform(), glm::value_ptr(pmat)); } if(shader->getMvUniform() != ShaderProgram::INVALID_UNIFORM) { glm::mat4 mvmat = vmat; if(is_global_model_matrix_valid() && !r->ignoreGlobalModelMatrix()) { mvmat *= get_global_model_matrix() * r->getModelMatrix(); } else { mvmat *= r->getModelMatrix(); } shader->setUniformValue(shader->getMvUniform(), glm::value_ptr(mvmat)); } if(shader->getMvpUniform() != ShaderProgram::INVALID_UNIFORM) { glm::mat4 pvmat(1.0f); if(is_global_model_matrix_valid() && !r->ignoreGlobalModelMatrix()) { pvmat = pmat * vmat * get_global_model_matrix() * r->getModelMatrix(); } else { pvmat = pmat * vmat * r->getModelMatrix(); } shader->setUniformValue(shader->getMvpUniform(), glm::value_ptr(pvmat)); } if(shader->getColorUniform() != ShaderProgram::INVALID_UNIFORM) { if(r->isColorSet()) { shader->setUniformValue(shader->getColorUniform(), r->getColor().asFloatVector()); } else { shader->setUniformValue(shader->getColorUniform(), ColorScope::getCurrentColor().asFloatVector()); } } shader->setUniformsForTexture(r->getTexture()); // XXX we should make this either or with setting the mvp/color uniforms above. auto uniform_draw_fn = shader->getUniformDrawFunction(); if(uniform_draw_fn) { uniform_draw_fn(shader); } // Loop through uniform render variables and set them. /*for(auto& urv : r->UniformRenderVariables()) { for(auto& rvd : urv->VariableDescritionList()) { auto rvdd = std::dynamic_pointer_cast<RenderVariableDeviceData>(rvd->GetDisplayData()); ASSERT_LOG(rvdd != nullptr, "Unable to cast DeviceData to RenderVariableDeviceData."); shader->SetUniformValue(rvdd->GetActiveMapIterator(), urv->Value()); } }*/ // Need to figure the interaction with shaders. /// XXX Need to create a mapping between attributes and the index value below. for(auto as : r->getAttributeSet()) { //ASSERT_LOG(as->getCount() > 0, "No (or negative) number of vertices in attribute set. " << as->getCount()); if((!as->isMultiDrawEnabled() && as->getCount() <= 0) || (as->isMultiDrawEnabled() && as->getMultiDrawCount() <= 0)) { //LOG_WARN("No (or negative) number of vertices in attribute set. " << as->getCount()); continue; } GLenum draw_mode = convert_drawing_mode(as->getDrawMode()); // apply blend, if any, from attribute set. BlendEquationScopeOGL be_scope(*as); BlendModeScopeOGL bm_scope(*as); if(shader->getColorUniform() != ShaderProgram::INVALID_UNIFORM && as->isColorSet()) { shader->setUniformValue(shader->getColorUniform(), as->getColor().asFloatVector()); } for(auto& attr : as->getAttributes()) { if(attr->isEnabled()) { shader->applyAttribute(attr); } } if(as->isInstanced()) { if(as->isIndexed()) { as->bindIndex(); // XXX as->GetIndexArray() should be as->GetIndexArray()+as->GetOffset() glDrawElementsInstanced(draw_mode, static_cast<GLsizei>(as->getCount()), convert_index_type(as->getIndexType()), as->getIndexArray(), as->getInstanceCount()); as->unbindIndex(); } else { glDrawArraysInstanced(draw_mode, static_cast<GLint>(as->getOffset()), static_cast<GLsizei>(as->getCount()), as->getInstanceCount()); } } else { if(as->isIndexed()) { as->bindIndex(); // XXX as->GetIndexArray() should be as->GetIndexArray()+as->GetOffset() glDrawElements(draw_mode, static_cast<GLsizei>(as->getCount()), convert_index_type(as->getIndexType()), as->getIndexArray()); as->unbindIndex(); } else { if(as->isMultiDrawEnabled()) { glMultiDrawArrays(draw_mode, as->getMultiOffsetArray().data(), as->getMultiCountArray().data(), as->getMultiDrawCount()); } else { glDrawArrays(draw_mode, static_cast<GLint>(as->getOffset()), static_cast<GLsizei>(as->getCount())); } } } shader->cleanUpAfterDraw(); glBindBuffer(GL_ARRAY_BUFFER, 0); } if(r->getRenderTarget()) { r->getRenderTarget()->unapply(); } }
CameraPtr DisplayDeviceOpenGL::getDefaultCamera() const { return get_default_camera(); }
CameraPtr DisplayDeviceOpenGL::setDefaultCamera(const CameraPtr& cam) { auto old_cam = get_default_camera(); get_default_camera() = cam; return old_cam; }