void Graphics3D::render(const Matrix &view, const Matrix &proj, const Light *lighting, int flags) { if (!getIsVisible()) return; if (!isValid()) return; if (lighting == nullptr) { lighting = defaultLight; } Matrix modl = referenceFrame->getModelMatrix(); OpenGLShaderProgram *shaderProgram = shader->getShader(); shaderProgram->use(); OpenGLShaderProgram::Uniform modlMatrix(*shaderProgram, "M"); OpenGLShaderProgram::Uniform viewMatrix(*shaderProgram, "V"); OpenGLShaderProgram::Uniform projMatrix(*shaderProgram, "P"); OpenGLShaderProgram::Uniform selectionHandle(*shaderProgram, "selectionHandle"); modlMatrix.setMatrix4(modl.mat, 1, false); viewMatrix.setMatrix4(view.mat, 1, false); projMatrix.setMatrix4(proj.mat, 1, false); selectionHandle.set(flags ? uniqueHandle : 0); geometry->activate(shaderProgram); material->activate(shaderProgram); lighting->activate(shaderProgram); geometry->drawElements(); geometry->deactivate(shaderProgram); material->deactivate(shaderProgram); lighting->deactivate(shaderProgram); }
glm::mat4 Camera::projectionMatrix() const { //return glm::ortho(-1.0f*m_sideRatio, 1.0f*m_sideRatio, -1.0f, 1.0f, 0.001f, 100000.0f); if (m_cameraNode.expired()) return glm::perspective<float>(M_PI / 3.0f, m_sideRatio, 0.001f, 100000.0f); auto realNode = m_cameraNode.lock(); return realNode->projMatrix(m_sideRatio); }
void cameraRecalculateMatrices(Camera* camera) { camera->perspectiveMatrix = projMatrix(camera->FOV, camera->aspectRatio, camera->nearPlane, camera->farPlane); Vec3 negCamPos; vec3Scale(&negCamPos, &camera->position, -1.0f); Mat4 translation = translate(negCamPos); Quaternion q = quaternion(-camera->rotation.w,camera->rotation.x,camera->rotation.y,camera->rotation.z); Mat4 rotato; mat4FromQuaternion(&rotato, &q); mat4Mul(&camera->transformMatrix, &rotato, &translation); }
void MeshRenderer :: computeDepthBuffer() { ntk::TimeCount tc_depth("computeDepthBuffer", 1); glReadPixels(0, 0, m_depth_buffer.cols, m_depth_buffer.rows, GL_DEPTH_COMPONENT, GL_FLOAT, m_depth_buffer.data); tc_depth.elapsedMsecs("-- glReadPixels: "); cv::Mat1f flipped; flip(m_depth_buffer, flipped, 0); m_depth_buffer = flipped; // FIXME: this is very slow !!! // gluUnproject is not necessary, or at least one // could invert the projection Matrix only once. cv::Mat_<GLdouble> modelMatrix(4,4); setIdentity(modelMatrix); cv::Mat_<GLdouble> projMatrix(4,4); glGetDoublev(GL_PROJECTION_MATRIX,projMatrix[0]); // projMatrix = projMatrix.inv(); int viewport[4]; glGetIntegerv(GL_VIEWPORT,viewport); GLdouble objx, objy, objz; for (int r = 0; r < m_depth_buffer.rows; ++r) for (int c = 0; c < m_depth_buffer.cols; ++c) { double depth = m_depth_buffer(r,c); if (ntk::flt_eq(depth,1) || ntk::flt_eq(depth,0)) { m_depth_buffer(r,c) = 0; continue; } gluUnProject(c, r, depth, modelMatrix[0], projMatrix[0], viewport,&objx, &objy, &objz); // double winz = (2.0*depth)-1; // double objz = (projMatrix(2,3)) / (winz * projMatrix(3,2) + projMatrix(3,3)); // double objz = ; m_depth_buffer(r,c) = -objz; } tc_depth.elapsedMsecs(" -- after unprojecting points: "); }
void OgreImGui::renderQueueEnded(Ogre::uint8 queueGroupId, const Ogre::String& invocation,bool& repeatThisInvocation) { if ((queueGroupId != Ogre::RENDER_QUEUE_OVERLAY) || (invocation == "SHADOWS")) { return; } Ogre::RenderSystem* renderSys = Ogre::Root::getSingletonPtr()->getRenderSystem(); Ogre::Viewport* vp = renderSys->_getViewport(); if ((vp == nullptr) || (!vp->getTarget()->isPrimary()) || mFrameEnded) { return; } mFrameEnded = true; ImGui::Render(); this->updateVertexData(); ImGuiIO& io = ImGui::GetIO(); // Construct projection matrix, taking texel offset corrections in account (important for DirectX9) // See also: // - OGRE-API specific hint: http://www.ogre3d.org/forums/viewtopic.php?f=5&p=536881#p536881 // - IMGUI Dx9 demo solution: https://github.com/ocornut/imgui/blob/master/examples/directx9_example/imgui_impl_dx9.cpp#L127-L138 const float texelOffsetX = renderSys->getHorizontalTexelOffset(); const float texelOffsetY = renderSys->getVerticalTexelOffset(); const float L = texelOffsetX; const float R = io.DisplaySize.x + texelOffsetX; const float T = texelOffsetY; const float B = io.DisplaySize.y + texelOffsetY; Ogre::Matrix4 projMatrix( 2.0f/(R-L), 0.0f, 0.0f, (L+R)/(L-R), 0.0f, -2.0f/(B-T), 0.0f, (T+B)/(B-T), 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); mPass->getVertexProgramParameters()->setNamedConstant("ProjectionMatrix", projMatrix); for(std::list<ImGUIRenderable*>::iterator it = mRenderables.begin(); it!=mRenderables.end(); ++it) { mSceneMgr->_injectRenderWithPass(mPass, (*it), false, false, nullptr); } }
void OgreImGui::Render() { // Construct projection matrix, taking texel offset corrections in account (important for DirectX9) // See also: // - OGRE-API specific hint: http://www.ogre3d.org/forums/viewtopic.php?f=5&p=536881#p536881 // - IMGUI Dx9 demo solution: https://github.com/ocornut/imgui/blob/master/examples/directx9_example/imgui_impl_dx9.cpp#L127-L138 ImGuiIO& io = ImGui::GetIO(); Ogre::RenderSystem* renderSys = Ogre::Root::getSingletonPtr()->getRenderSystem(); const float texelOffsetX = renderSys->getHorizontalTexelOffset(); const float texelOffsetY = renderSys->getVerticalTexelOffset(); const float L = texelOffsetX; const float R = io.DisplaySize.x + texelOffsetX; const float T = texelOffsetY; const float B = io.DisplaySize.y + texelOffsetY; Ogre::Matrix4 projMatrix( 2.0f/(R-L), 0.0f, 0.0f, (L+R)/(L-R), 0.0f, -2.0f/(B-T), 0.0f, (T+B)/(B-T), 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); mPass->getVertexProgramParameters()->setNamedConstant("ProjectionMatrix", projMatrix); // Instruct ImGui to Render() and process the resulting CmdList-s /// Adopted from https://bitbucket.org/ChaosCreator/imgui-ogre2.1-binding /// ... Commentary on OGRE forums: http://www.ogre3d.org/forums/viewtopic.php?f=5&t=89081#p531059 ImGui::Render(); ImDrawData* draw_data = ImGui::GetDrawData(); Ogre::Viewport* vp = renderSys->_getViewport(); int vpWidth = vp->getActualWidth(); int vpHeight = vp->getActualHeight(); for (int i = 0; i < draw_data->CmdListsCount; ++i) { const ImDrawList* draw_list = draw_data->CmdLists[i]; unsigned int startIdx = 0; for (int j = 0; j < draw_list->CmdBuffer.Size; ++j) { // Create a renderable and fill it's buffers ImGUIRenderable renderable; const ImDrawCmd *drawCmd = &draw_list->CmdBuffer[j]; renderable.updateVertexData(draw_list->VtxBuffer.Data, &draw_list->IdxBuffer.Data[startIdx], draw_list->VtxBuffer.Size, drawCmd->ElemCount); // Set scissoring int scLeft = static_cast<int>(drawCmd->ClipRect.x); // Obtain bounds int scTop = static_cast<int>(drawCmd->ClipRect.y); int scRight = static_cast<int>(drawCmd->ClipRect.z); int scBottom = static_cast<int>(drawCmd->ClipRect.w); scLeft = scLeft < 0 ? 0 : (scLeft > vpWidth ? vpWidth : scLeft); // Clamp bounds to viewport dimensions scRight = scRight < 0 ? 0 : (scRight > vpWidth ? vpWidth : scRight); scTop = scTop < 0 ? 0 : (scTop > vpHeight ? vpHeight : scTop); scBottom = scBottom < 0 ? 0 : (scBottom > vpHeight ? vpHeight : scBottom); renderSys->setScissorTest(true, scLeft, scTop, scRight, scBottom); // Render! mSceneMgr->_injectRenderWithPass(mPass, &renderable, false, false, nullptr); // Update counts startIdx += drawCmd->ElemCount; } } renderSys->setScissorTest(false); }
Ibl::Matrix44f Camera::viewProjMatrix() const { return viewMatrix() * projMatrix(); }
void Camera::cacheCameraTransforms() const { _cameraTransformCache->set(viewMatrix(), projMatrix(), viewProjMatrix(), translation(), zNear(), zFar(), 1.0f); }
void Frustum::Update() { projMatrix.Zero(); float invWidth = 1 / (nearRight - nearLeft); float invHeight = 1 / (nearTop - nearBottom); float invNearFarDist = 1 / (farDist - nearDist); if (projectionType == FRUSTUM_PERSPECTIVE) { float tanThetaY = std::tan(fovy*0.5f); float tanThetaX = aspectRatio * tanThetaY; float halfWidth = nearDist * std::tan(tanThetaX); float halfHeight = nearDist * std::tan(tanThetaY); // NB: This creates 'uniform' perspective projection matrix, // which depth range [-1,1], right-handed rules projMatrix(0, 0) = 2.0f * nearDist * invWidth; projMatrix(0, 2) = (nearRight + nearLeft) * invWidth; projMatrix(1, 1) = 2.0f * nearDist * invHeight; projMatrix(1, 2) = (nearTop + nearBottom) * invHeight; projMatrix(2, 2) = -(farDist + nearDist) * invNearFarDist; projMatrix(2, 3) = -2.0f * farDist * nearDist * invNearFarDist; projMatrix(3, 2) = -1.0f; } else if (projectionType == FRUSTUM_ORTHOGRAPHIC) { projMatrix(0, 0) = 2 * invWidth; projMatrix(0, 3) = -(nearRight + nearLeft) * invWidth; projMatrix(1, 1) = 2 * invHeight; projMatrix(1, 3) = -(nearTop + nearBottom) * invHeight; projMatrix(2, 2) = -2.0f * invNearFarDist; projMatrix(2, 3) = -(farDist + nearDist) * invNearFarDist; projMatrix(3, 3) = 1.0f; } }