From fe7afa17c62f3a5aa05a1bd9c2a0b9d24aa76f05 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Sun, 13 Mar 2022 11:46:55 -0400 Subject: [PATCH] store Transform structure on Camera instead of RenderState --- include/camera.h | 13 +++++++++---- include/shader.h | 12 ------------ include/tangerine.h | 5 ----- src/camera.cpp | 27 ++++++++++++++++++--------- 4 files changed, 27 insertions(+), 30 deletions(-) diff --git a/include/camera.h b/include/camera.h index 44de9c8..8e27e4b 100644 --- a/include/camera.h +++ b/include/camera.h @@ -4,6 +4,7 @@ #include #include "types.h" +#include "shader.h" using glm::ivec2; using glm::vec2; @@ -11,6 +12,13 @@ using glm::vec3; using glm::mat4; +struct Transforms +{ + mat4 view; + mat4 projection; + mat4 normal; +}; + struct Camera { float hAngle; @@ -23,10 +31,7 @@ struct Camera vec3 target; vec3 world_up; - mat4 model; - mat4 view; - mat4 projection; - mat4 MVP; + Transforms xforms; }; diff --git a/include/shader.h b/include/shader.h index 63eba43..2d5cadb 100644 --- a/include/shader.h +++ b/include/shader.h @@ -156,18 +156,6 @@ struct GLMesh struct Animation; -struct Transforms -{ - glm::mat4 view_xform; - glm::mat4 proj_xform; - glm::mat4 normal_xform; -}; - - -const float DEFAULT_FOV = 60.f; -const float NEAR_CLIP_PLANE = 5.f; -const float DEFAULT_ASPECT_RATIO = 16.f / 9.f; - GLContext* initGLContext(MemoryArena* arena, u32 max_shaders, diff --git a/include/tangerine.h b/include/tangerine.h index 5c3ce6b..1b16c3f 100644 --- a/include/tangerine.h +++ b/include/tangerine.h @@ -165,7 +165,6 @@ struct RenderState { bool running; GLClearColor clear_col; - Transforms* xforms; // NOTE: would be part of camera in libTangerine SDLHandles handles; GLContext* gl_ctx; Assets assets; @@ -179,11 +178,7 @@ struct RenderState // could have render groups, lights, camera // could match up with gltf scene graph where everyting is part of a node LightsBuffer* lights_buf; - - // FIXME: re-integrating missing libTangerine render_state properties Camera* camera; - glm::vec2 viewport_dims; - /// }; diff --git a/src/camera.cpp b/src/camera.cpp index 50aecb9..493c87e 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -19,7 +19,7 @@ using glm::mat4; #define ROTATE_SPEED 0.005f #define CAMERA_Z_CLAMP_ANGLE 85.f #define FOV 60.f -#define NEAR_CLIP_PLANE 20.f +#define NEAR_CLIP_PLANE 5.f // forward declarations @@ -39,7 +39,7 @@ cameraInitPerspective(Camera* cam, cam->position = position; cam->target = target; cam->world_up = world_up; - cam->projection = glm::infinitePerspective(glm::radians(FOV), + cam->xforms.projection = glm::infinitePerspective(glm::radians(FOV), aspect_ratio, NEAR_CLIP_PLANE); @@ -53,10 +53,8 @@ cameraInitPerspective(Camera* cam, glm::pow(cam->forward.x, 2)); cam->vAngle = glm::atan(cam->forward.z, len); - cam->view = + cam->xforms.view= glm::lookAt(cam->position, cam->position + cam->forward, cam->up); - cam->model = mat4(1.0f); - cam->MVP = cam->projection * cam->view * cam->model; } // TODO: re-add orthographic camera @@ -80,6 +78,7 @@ cameraInitOrthographic(/*camera& cam, */) vec2 cameraUnproject(Camera& cam, int x, int y, int vp_width, int vp_height) { +#if 0 // NOTE: using depth buffer may not be as accurate as doing ray-cast GLfloat depth; glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth); @@ -88,11 +87,14 @@ cameraUnproject(Camera& cam, int x, int y, int vp_width, int vp_height) vec3 vU = glm::unProject(wincoord, cam.view, cam.projection, viewport); return vec2(vU.x, vU.y); +#endif + return vec2(); } vec3 cameraCreateRay(Camera& cam, ivec2 vp_coords, ivec2 vp_dims) { +#if 0 // NOTE: http://antongerdelan.net/opengl/raycasting.html float x = 2.f * vp_coords.x / vp_dims.x - 1.f; float y = 2.f * vp_coords.y / vp_dims.y - 1.f; @@ -102,6 +104,8 @@ cameraCreateRay(Camera& cam, ivec2 vp_coords, ivec2 vp_dims) vec4 ray_world = glm::normalize(glm::inverse(cam.view) * ray_eye); return vec3(ray_world.x, ray_world.y, ray_world.z); +#endif + return vec3(); } bool @@ -162,8 +166,9 @@ cameraMove(Camera& cam, p += (v * MOVE_SPEED); vec3 diff = old - p; - cam.view = glm::translate(cam.view, diff); - cam.MVP = cam.projection * cam.view * cam.model; + cam.xforms.view = glm::translate(cam.xforms.view, diff); + //cam.view = glm::translate(cam.view, diff); + //cam.MVP = cam.projection * cam.view * cam.model; } void @@ -189,13 +194,16 @@ cameraRotate(Camera& cam, i32 xrel, i32 yrel) cam.left = glm::normalize(glm::cross(cam.forward, cam.world_up)); cam.up = glm::normalize(glm::cross(cam.left, cam.forward)); - cam.view = glm::lookAt(cam.position, cam.position + cam.forward, cam.up); - cam.MVP = cam.projection * cam.view * cam.model; + cam.xforms.view = glm::lookAt(cam.position, + cam.position + cam.forward, + cam.up); + //cam.MVP = cam.projection * cam.view * cam.model; } void cameraRoll(Camera& cam, bool CW, bool CCW) { +#if 0 if ((!CW && !CCW) || (CW && CCW)) return; @@ -208,6 +216,7 @@ cameraRoll(Camera& cam, bool CW, bool CCW) cam.up = vec3(v.x, v.y, v.z); cam.view *= m; cam.MVP = cam.projection * cam.view * cam.model; +#endif } // internal