Browse Source

store Transform structure on Camera instead of RenderState

remotes/bxxa/master
cinnaboot 4 years ago
parent
commit
fe7afa17c6
  1. 13
      include/camera.h
  2. 12
      include/shader.h
  3. 5
      include/tangerine.h
  4. 27
      src/camera.cpp

13
include/camera.h

@ -4,6 +4,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "types.h" #include "types.h"
#include "shader.h"
using glm::ivec2; using glm::ivec2;
using glm::vec2; using glm::vec2;
@ -11,6 +12,13 @@ using glm::vec3;
using glm::mat4; using glm::mat4;
struct Transforms
{
mat4 view;
mat4 projection;
mat4 normal;
};
struct Camera struct Camera
{ {
float hAngle; float hAngle;
@ -23,10 +31,7 @@ struct Camera
vec3 target; vec3 target;
vec3 world_up; vec3 world_up;
mat4 model; Transforms xforms;
mat4 view;
mat4 projection;
mat4 MVP;
}; };

12
include/shader.h

@ -156,18 +156,6 @@ struct GLMesh
struct Animation; 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, GLContext* initGLContext(MemoryArena* arena,
u32 max_shaders, u32 max_shaders,

5
include/tangerine.h

@ -165,7 +165,6 @@ struct RenderState
{ {
bool running; bool running;
GLClearColor clear_col; GLClearColor clear_col;
Transforms* xforms; // NOTE: would be part of camera in libTangerine
SDLHandles handles; SDLHandles handles;
GLContext* gl_ctx; GLContext* gl_ctx;
Assets assets; Assets assets;
@ -179,11 +178,7 @@ struct RenderState
// could have render groups, lights, camera // could have render groups, lights, camera
// could match up with gltf scene graph where everyting is part of a node // could match up with gltf scene graph where everyting is part of a node
LightsBuffer* lights_buf; LightsBuffer* lights_buf;
// FIXME: re-integrating missing libTangerine render_state properties
Camera* camera; Camera* camera;
glm::vec2 viewport_dims;
///
}; };

27
src/camera.cpp

@ -19,7 +19,7 @@ using glm::mat4;
#define ROTATE_SPEED 0.005f #define ROTATE_SPEED 0.005f
#define CAMERA_Z_CLAMP_ANGLE 85.f #define CAMERA_Z_CLAMP_ANGLE 85.f
#define FOV 60.f #define FOV 60.f
#define NEAR_CLIP_PLANE 20.f #define NEAR_CLIP_PLANE 5.f
// forward declarations // forward declarations
@ -39,7 +39,7 @@ cameraInitPerspective(Camera* cam,
cam->position = position; cam->position = position;
cam->target = target; cam->target = target;
cam->world_up = world_up; cam->world_up = world_up;
cam->projection = glm::infinitePerspective(glm::radians(FOV), cam->xforms.projection = glm::infinitePerspective(glm::radians(FOV),
aspect_ratio, aspect_ratio,
NEAR_CLIP_PLANE); NEAR_CLIP_PLANE);
@ -53,10 +53,8 @@ cameraInitPerspective(Camera* cam,
glm::pow(cam->forward.x, 2)); glm::pow(cam->forward.x, 2));
cam->vAngle = glm::atan(cam->forward.z, len); cam->vAngle = glm::atan(cam->forward.z, len);
cam->view = cam->xforms.view=
glm::lookAt(cam->position, cam->position + cam->forward, cam->up); 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 // TODO: re-add orthographic camera
@ -80,6 +78,7 @@ cameraInitOrthographic(/*camera& cam, */)
vec2 vec2
cameraUnproject(Camera& cam, int x, int y, int vp_width, int vp_height) 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 // NOTE: using depth buffer may not be as accurate as doing ray-cast
GLfloat depth; GLfloat depth;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &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); vec3 vU = glm::unProject(wincoord, cam.view, cam.projection, viewport);
return vec2(vU.x, vU.y); return vec2(vU.x, vU.y);
#endif
return vec2();
} }
vec3 vec3
cameraCreateRay(Camera& cam, ivec2 vp_coords, ivec2 vp_dims) cameraCreateRay(Camera& cam, ivec2 vp_coords, ivec2 vp_dims)
{ {
#if 0
// NOTE: http://antongerdelan.net/opengl/raycasting.html // NOTE: http://antongerdelan.net/opengl/raycasting.html
float x = 2.f * vp_coords.x / vp_dims.x - 1.f; float x = 2.f * vp_coords.x / vp_dims.x - 1.f;
float y = 2.f * vp_coords.y / vp_dims.y - 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); vec4 ray_world = glm::normalize(glm::inverse(cam.view) * ray_eye);
return vec3(ray_world.x, ray_world.y, ray_world.z); return vec3(ray_world.x, ray_world.y, ray_world.z);
#endif
return vec3();
} }
bool bool
@ -162,8 +166,9 @@ cameraMove(Camera& cam,
p += (v * MOVE_SPEED); p += (v * MOVE_SPEED);
vec3 diff = old - p; vec3 diff = old - p;
cam.view = glm::translate(cam.view, diff); cam.xforms.view = glm::translate(cam.xforms.view, diff);
cam.MVP = cam.projection * cam.view * cam.model; //cam.view = glm::translate(cam.view, diff);
//cam.MVP = cam.projection * cam.view * cam.model;
} }
void void
@ -189,13 +194,16 @@ cameraRotate(Camera& cam, i32 xrel, i32 yrel)
cam.left = glm::normalize(glm::cross(cam.forward, cam.world_up)); cam.left = glm::normalize(glm::cross(cam.forward, cam.world_up));
cam.up = glm::normalize(glm::cross(cam.left, cam.forward)); cam.up = glm::normalize(glm::cross(cam.left, cam.forward));
cam.view = glm::lookAt(cam.position, cam.position + cam.forward, cam.up); cam.xforms.view = glm::lookAt(cam.position,
cam.MVP = cam.projection * cam.view * cam.model; cam.position + cam.forward,
cam.up);
//cam.MVP = cam.projection * cam.view * cam.model;
} }
void void
cameraRoll(Camera& cam, bool CW, bool CCW) cameraRoll(Camera& cam, bool CW, bool CCW)
{ {
#if 0
if ((!CW && !CCW) || (CW && CCW)) if ((!CW && !CCW) || (CW && CCW))
return; return;
@ -208,6 +216,7 @@ cameraRoll(Camera& cam, bool CW, bool CCW)
cam.up = vec3(v.x, v.y, v.z); cam.up = vec3(v.x, v.y, v.z);
cam.view *= m; cam.view *= m;
cam.MVP = cam.projection * cam.view * cam.model; cam.MVP = cam.projection * cam.view * cam.model;
#endif
} }
// internal // internal

Loading…
Cancel
Save