Fixed perspective camera gedöns

simple_math_single_header
Martin Felis 2018-02-28 21:11:51 +01:00
parent c86f535571
commit e2671e8de4
5 changed files with 36 additions and 23 deletions

View File

@ -91,8 +91,9 @@ inline Matrix44f Ortho(float left, float right,
inline Matrix44f Perspective(float fovy, float aspect, inline Matrix44f Perspective(float fovy, float aspect,
float near, float far) { float near, float far) {
float x = fovy / 2.0f; float x = (fovy * M_PI / 180.0) / 2.0f;
float f = cos(x) / sin(x); float f = cos(x) / sin(x);
return Matrix44f( return Matrix44f(
f / aspect, 0.0f, 0.0f, 0.0f, f / aspect, 0.0f, 0.0f, 0.0f,
0.0f, f, 0.0f, 0.0f, 0.0f, f, 0.0f, 0.0f,
@ -109,11 +110,11 @@ inline Matrix44f LookAt(
Vector3f s = d.cross(up.normalized()); Vector3f s = d.cross(up.normalized());
Vector3f u = s.cross(d); Vector3f u = s.cross(d);
return Matrix44f( return TranslateMat44(-eye[0], -eye[1], -eye[2]) * Matrix44f(
s[0], u[0], -d[0], 0.0f, s[0], u[0], -d[0], 0.0f,
s[1], u[1], -d[1], 0.0f, s[1], u[1], -d[1], 0.0f,
s[2], u[2], -d[2], 0.0f, s[2], u[2], -d[2], 0.0f,
-eye[0], -eye[1], -eye[2], 1.0f 0.0f, 0.0f, 0.0f, 1.0f
); );
} }

View File

@ -140,7 +140,7 @@ int main(void)
ImGuiIO& io = ImGui::GetIO(); (void)io; ImGuiIO& io = ImGui::GetIO(); (void)io;
GuiInputState gui_input_state; GuiInputState gui_input_state;
gGuiInputState = &gui_input_state; gGuiInputState = &gui_input_state;
ImGui_ImplGlfwGL3_Init(gWindow, false); ImGui_ImplGlfwGL3_Init(gWindow, true);
// Timer // Timer
Timer timer; Timer timer;
@ -175,6 +175,7 @@ int main(void)
// Start the imgui frame such that widgets can be submitted // Start the imgui frame such that widgets can be submitted
handle_mouse(); handle_mouse();
glfwGetWindowSize(gWindow, &width, &height); glfwGetWindowSize(gWindow, &width, &height);
glViewport(0, 0, width, height);
glfwPollEvents(); glfwPollEvents();
ImGui_ImplGlfwGL3_NewFrame(); ImGui_ImplGlfwGL3_NewFrame();

View File

@ -166,13 +166,17 @@ void Camera::UpdateMatrices() {
} }
void Camera::DrawGui() { void Camera::DrawGui() {
ImGui::SliderFloat3("Eye", mEye.data(), -10.0f, 10.0f); ImGui::Text("Width %3.4f, Height %3.4f", mWidth, mHeight);
ImGui::SliderFloat3("Poi", mPoi.data(), -10.0f, 10.0f);
ImGui::SliderFloat3("Up", mUp.data(), -10.0f, 10.0f); ImGui::InputFloat3("Eye", mEye.data(), -10.0f, 10.0f);
ImGui::SliderFloat3("EyeS", mEye.data(), -10.0f, 10.0f);
ImGui::InputFloat3("Poi", mPoi.data(), -10.0f, 10.0f);
ImGui::InputFloat3("Up", mUp.data(), -10.0f, 10.0f);
ImGui::Checkbox("Orthographic", &mIsOrthographic); ImGui::Checkbox("Orthographic", &mIsOrthographic);
ImGui::SliderFloat("Fov", &mFov, 5, 160); ImGui::SliderFloat("Fov", &mFov, 5, 160);
ImGui::SliderFloat("Near", &mNear, -10, 10); ImGui::SliderFloat("Near", &mNear, -10, 10);
ImGui::SliderFloat("Far", &mFar, -20, 20); ImGui::SliderFloat("Far", &mFar, -20, 50);
if (ImGui::Button("Reset")) { if (ImGui::Button("Reset")) {
*this = Camera(); *this = Camera();
} }
@ -305,11 +309,15 @@ void Renderer::Shutdown() {
void Renderer::RenderGl() { void Renderer::RenderGl() {
int width, height; mSceneAreaWidth = mSceneAreaWidth < 1 ? 1 : mSceneAreaWidth;
glfwGetWindowSize(gWindow, &width, &height); mSceneAreaHeight = mSceneAreaHeight < 1 ? 1 : mSceneAreaHeight;
if (width != mWidth || height != mHeight) if (mSceneAreaWidth != mRenderTarget.mWidth || mSceneAreaHeight != mRenderTarget.mHeight) {
Resize(width, height); mRenderTarget.Resize(mSceneAreaWidth, mSceneAreaHeight);
mCamera.mWidth = mSceneAreaWidth;
mCamera.mHeight = mSceneAreaHeight;
}
glViewport(0, 0, mCamera.mWidth, mCamera.mHeight);
mCamera.UpdateMatrices(); mCamera.UpdateMatrices();
glEnable(GL_LINE_SMOOTH); glEnable(GL_LINE_SMOOTH);
@ -378,7 +386,7 @@ void Renderer::RenderGl() {
// Coordinate System: VertexArrayMesh // Coordinate System: VertexArrayMesh
model_view_projection = model_view_projection =
TranslateMat44(1.25f, 0.0f, 0.0f) TranslateMat44(0.0f, 0.0f, 0.0f)
* mCamera.mViewMatrix * mCamera.mViewMatrix
* mCamera.mProjectionMatrix; * mCamera.mProjectionMatrix;
glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data()); glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data());
@ -453,6 +461,8 @@ void Renderer::RenderGui() {
ImGui::Text("Scene"); ImGui::Text("Scene");
const ImVec2 content_avail = ImGui::GetContentRegionAvail(); const ImVec2 content_avail = ImGui::GetContentRegionAvail();
mSceneAreaWidth = content_avail.x;
mSceneAreaHeight = content_avail.y;
ImGui::Image((void*) texture, ImGui::Image((void*) texture,
content_avail, content_avail,
@ -460,9 +470,12 @@ void Renderer::RenderGui() {
ImVec2(1.0f, 0.0f) ImVec2(1.0f, 0.0f)
); );
} }
ImGui::EndDock(); ImGui::EndDock();
if (ImGui::BeginDock("Render Settings")) { if (ImGui::BeginDock("Render Settings")) {
ImGui::Text("Render Target");
ImGui::Text("Width %d, Height %d", mRenderTarget.mWidth, mRenderTarget.mHeight);
ImGui::Text("Camera"); ImGui::Text("Camera");
mCamera.DrawGui(); mCamera.DrawGui();
@ -477,11 +490,3 @@ void Renderer::RenderGui() {
ImGui::EndDock(); ImGui::EndDock();
} }
void Renderer::Resize (int width, int height) {
mWidth = width;
mHeight = height;
mRenderTarget.Resize(mWidth, mHeight);
mCamera.mWidth = mWidth;
mCamera.mHeight = mHeight;
glViewport(0, 0, mWidth, mHeight);
}

View File

@ -110,6 +110,8 @@ struct Renderer {
bool mInitialized = false; bool mInitialized = false;
uint32_t mWidth = 1; uint32_t mWidth = 1;
uint32_t mHeight = 1; uint32_t mHeight = 1;
uint32_t mSceneAreaWidth = 1;
uint32_t mSceneAreaHeight = 1;
Camera mCamera; Camera mCamera;
Mesh mMesh; Mesh mMesh;
@ -147,5 +149,4 @@ struct Renderer {
void Shutdown(); void Shutdown();
void RenderGl(); void RenderGl();
void RenderGui(); void RenderGui();
void Resize (int width, int height);
}; };

View File

@ -179,14 +179,19 @@ void RenderTarget::Cleanup() {
glDeleteTextures(1, &mLinearizedDepthTexture); glDeleteTextures(1, &mLinearizedDepthTexture);
mLinearizedDepthTexture = -1; mLinearizedDepthTexture = -1;
} }
mWidth = -1;
mHeight = -1;
} }
void RenderTarget::Resize(int width, int height) { void RenderTarget::Resize(int width, int height) {
if (width == mWidth || height == mHeight) if (width == mWidth && height == mHeight)
return; return;
Cleanup(); Cleanup();
gLog("Resizing RenderTarget");
mWidth = width; mWidth = width;
mHeight = height; mHeight = height;