Added a ScreenCoords class with some util functions to clean up the Renderer class.

This commit is contained in:
Tleety
2015-12-03 18:14:52 +01:00
parent 6a24ed534a
commit 1c06c06816
5 changed files with 85 additions and 54 deletions
+3 -48
View File
@@ -161,16 +161,15 @@ void Renderer::InputUpdate(double dt)
static double mousePosX, mousePosY;
glfwGetCursorPos(m_Window, &mousePosX, &mousePosY);
if (glfwGetMouseButton(m_Window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS) {
glm::vec3 data = GetClickedPixelData(mousePosX, m_Resolution.Height - mousePosY);
glm::vec3 data = ScreenCoords::ToPixelData(mousePosX, m_Resolution.Height - mousePosY, m_PickingBuffer, m_DepthBuffer);
glm::vec2 color = glm::vec2(data);
float depth = data.z;
glm::vec3 viewPos = ScreenCoordsToWorldPos(glm::vec2(mousePosX, m_Resolution.Height - mousePosY), depth);
glm::vec3 viewPos = ScreenCoords::ToWorldPos(mousePosX, m_Resolution.Height - mousePosY, depth, m_Resolution, m_Camera->ProjectionMatrix(), m_Camera->ViewMatrix());
// glm::vec3 worldPos = glm::vec3(glm::inverse(m_Camera->ViewMatrix()) * glm::vec4(viewPos, 1.f));
//printf("R: %f, G: %f, Depth: %f\n", color.r, color.g, depth);
printf("view: x: %f, y: %f z: %f, Length: %f\n", viewPos.x, viewPos.y, viewPos.z, glm::length(viewPos));
//printf("view: x: %f, y: %f z: %f, Length: %f\n\n", viewPos.x, viewPos.y, viewPos.z, glm::length(viewPos));
if (color != glm::vec2(0, 0)) {
const Model* pickModel = m_PickingColorsToModels[color];
@@ -200,33 +199,6 @@ void Renderer::InputUpdate(double dt)
m_Camera->SetPosition(m_Position);
}
// Utility functions should be moved to a better place
glm::vec3 Renderer::ScreenCoordsToWorldPos(glm::vec2 screenCoord, float depth)
{
glm::vec4 pos;
float near = m_Camera->NearClip();
float far = m_Camera->FarClip();
glm::vec3 ndc;
ndc.x = screenCoord.x / m_Resolution.Width;
ndc.y = screenCoord.y / m_Resolution.Height;
glm::vec4 clipSpace = glm::vec4(glm::vec2(ndc.x, ndc.y) * 2.0f - 1.0f, (depth) * 2.0f - 1.0f, 1.0f);
glm::vec4 EyeSpace = glm::inverse(m_Camera->ProjectionMatrix()) * clipSpace;
glm::vec4 WorldSpace = glm::inverse(m_Camera->ViewMatrix()) * EyeSpace;
WorldSpace = glm::vec4(glm::vec3(WorldSpace) / WorldSpace.w, 1.f);
return glm::vec3(WorldSpace);
}
//EntityID Renderer::ScreenCoordsToEntityID(glm::vec2 screenCoord, float depth)
//{
//
//}
void Renderer::Update(double dt)
{
InputUpdate(dt);
@@ -352,23 +324,6 @@ void Renderer::DrawScreenQuad(GLuint textureToDraw)
}
// Will return a vec3 where RG is the PickColor and B is the Depth
glm::vec3 Renderer::GetClickedPixelData(float x, float y)
{
glBindFramebuffer(GL_FRAMEBUFFER, m_PickingBuffer);
glm::vec2 pixelData;
glReadPixels(x, y, 1, 1, GL_RG, GL_FLOAT, &pixelData);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, m_DepthBuffer);
float depthData;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depthData);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return glm::vec3(pixelData, depthData);
}
void Renderer::InitializeTextures()
{
m_ErrorTexture=ResourceManager::Load<Texture>("Textures/Core/ErrorTexture.png");
@@ -0,0 +1,50 @@
#include "Rendering/Util/ScreenCoords.h"
glm::vec3 ScreenCoords::ToWorldPos(float x, float y, float depth, float screenWidth, float screenHeight, glm::mat4 cameraProjectionMat, glm::mat4 cameraViewMat)
{
glm::vec3 ndc;
ndc.x = (x / screenWidth)*2.f - 1.f;
ndc.y = (y / screenHeight)*2.f - 1.f;
ndc.z = depth*2.f - 1.f;
glm::vec4 clipSpace = glm::vec4(ndc, 1.0f);
glm::vec4 EyeSpace = glm::inverse(cameraProjectionMat) * clipSpace;
glm::vec4 WorldSpace = glm::inverse(cameraViewMat) * EyeSpace;
WorldSpace = glm::vec4(glm::vec3(WorldSpace) / WorldSpace.w, 1.f);
return glm::vec3(WorldSpace);
}
glm::vec3 ScreenCoords::ToWorldPos(glm::vec2 screenCoord, float depth, Rectangle resolution, glm::mat4 cameraProjectionMat, glm::mat4 cameraViewMat)
{
return ToWorldPos(screenCoord.x, screenCoord.y, depth, resolution.Width, resolution.Height, cameraProjectionMat, cameraViewMat);
}
glm::vec3 ScreenCoords::ToWorldPos(float x, float y, float depth, Rectangle resolution, glm::mat4 cameraProjectionMat, glm::mat4 cameraViewMat)
{
return ToWorldPos(x, y, depth, resolution.Width, resolution.Height, cameraProjectionMat, cameraViewMat);
}
glm::vec3 ScreenCoords::ToWorldPos(glm::vec2 screenCoord, float depth, float screenWidth, float screenHeight, glm::mat4 cameraProjectionMat, glm::mat4 cameraViewMat)
{
return ToWorldPos(screenCoord.x, screenCoord.y, depth, screenWidth, screenHeight, cameraProjectionMat, cameraViewMat);
}
glm::vec3 ScreenCoords::ToPixelData(float x, float y, GLuint PickDataBuffer, GLuint DepthBuffer)
{
glBindFramebuffer(GL_FRAMEBUFFER, PickDataBuffer);
glm::vec2 pixelData;
glReadPixels(x, y, 1, 1, GL_RG, GL_FLOAT, &pixelData);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, DepthBuffer);
float depthData;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depthData);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return glm::vec3(pixelData, depthData);
}
glm::vec3 ScreenCoords::ToPixelData(glm::vec2 screenCoord, GLuint PickDataBuffer, GLuint DepthBuffer)
{
return ToPixelData(screenCoord.x, screenCoord.y, PickDataBuffer, DepthBuffer);
}