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,
float near, float far) {
float x = fovy / 2.0f;
float x = (fovy * M_PI / 180.0) / 2.0f;
float f = cos(x) / sin(x);
return Matrix44f(
f / aspect, 0.0f, 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 u = s.cross(d);
return Matrix44f(
return TranslateMat44(-eye[0], -eye[1], -eye[2]) * Matrix44f(
s[0], u[0], -d[0], 0.0f,
s[1], u[1], -d[1], 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;
GuiInputState gui_input_state;
gGuiInputState = &gui_input_state;
ImGui_ImplGlfwGL3_Init(gWindow, false);
ImGui_ImplGlfwGL3_Init(gWindow, true);
// Timer
Timer timer;
@ -175,6 +175,7 @@ int main(void)
// Start the imgui frame such that widgets can be submitted
handle_mouse();
glfwGetWindowSize(gWindow, &width, &height);
glViewport(0, 0, width, height);
glfwPollEvents();
ImGui_ImplGlfwGL3_NewFrame();

View File

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

View File

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