Browse Source

fix bug with starting camera xform

master
cinnaboot 8 years ago
parent
commit
ee37f93c4b
  1. 4
      data/scene_schema.json
  2. 10
      data/test_scene.json
  3. 28
      src/camera.cpp
  4. 15
      src/camera.h
  5. 29
      src/gooey.cpp
  6. 3
      src/hexgame.cpp
  7. 2
      src/scene_loader.cpp

4
data/scene_schema.json

@ -41,9 +41,9 @@
"properties": { "properties": {
"position": { "$ref": "#/definitions/vector3" }, "position": { "$ref": "#/definitions/vector3" },
"target": { "$ref": "#/definitions/vector3" }, "target": { "$ref": "#/definitions/vector3" },
"up": { "$ref": "#/definitions/vector3" } "world_up": { "$ref": "#/definitions/vector3" }
}, },
"required": ["position", "target", "up"] "required": ["position", "target", "world_up"]
}, },
"hex_grid" : { "hex_grid" : {

10
data/test_scene.json

@ -6,14 +6,14 @@
"z" : 100 "z" : 100
}, },
"target" : { "target" : {
"x" : 640, "x" : -140,
"y" : 500, "y" : 50,
"z" : 0 "z" : 0
}, },
"up" : { "world_up" : {
"x" : 0, "x" : 0,
"y" : 1, "y" : 0,
"z" : 0 "z" : 1
} }
}, },

28
src/camera.cpp

@ -3,30 +3,29 @@
#include "camera.h" #include "camera.h"
// TODO: add these props to scene json
#define MOVE_SPEED 5.f #define MOVE_SPEED 5.f
#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 ASPECT_RATIO 16.f/9.f
#define NEAR_CLIP_PLANE 0.1f
void void
cameraInitPerspective(camera& cam, glm::vec3 position, glm::vec3 target, glm::vec3 up) cameraInitPerspective(camera& cam, glm::vec3 position, glm::vec3 target, glm::vec3 world_up)
{ {
cam.position = position; cam.position = position;
cam.target = target; cam.target = target;
cam.up = up; cam.world_up = world_up;
cam.projection = glm::infinitePerspective(glm::radians(FOV), ASPECT_RATIO, NEAR_CLIP_PLANE); cam.projection = glm::infinitePerspective(glm::radians(FOV), ASPECT_RATIO, NEAR_CLIP_PLANE);
cam.hAngle = 0; cam.forward = glm::normalize(target - position);
cam.vAngle = glm::atan((cam.target.z - cam.position.z) / (cam.target.y - cam.position.y)); cam.left = glm::normalize(glm::cross(cam.world_up, cam.forward));
cam.up = glm::normalize(glm::cross(cam.forward, cam.left));
// FIXME: bug, camera always starts with forward pointing at positive 'z' axis cam.hAngle = glm::atan(cam.forward.x, cam.forward.y);
// camera position also changes with change to window size cam.vAngle = glm::atan(cam.forward.z, cam.forward.y);
cam.forward = glm::normalize(glm::vec3(
glm::cos(cam.vAngle) * glm::sin(cam.hAngle),
glm::cos(cam.vAngle) * glm::cos(cam.hAngle),
glm::sin(cam.vAngle)
));
cam.left = glm::normalize(glm::cross(cam.up, cam.forward));
cam.up = glm::normalize(glm::cross(cam.forward, cam.left)); // get better "up" vector
cam.view = glm::lookAt(cam.position, cam.position + cam.forward, cam.up); cam.view = glm::lookAt(cam.position, cam.position + cam.forward, cam.up);
cam.model = glm::mat4(1.0f); cam.model = glm::mat4(1.0f);
@ -119,8 +118,7 @@ cameraRotate(camera& cam, int32 xrel, int32 yrel)
); );
glm::normalize(cam.forward); glm::normalize(cam.forward);
cam.up = glm::vec3(0,0,1); cam.left = glm::normalize(glm::cross(cam.forward, cam.world_up));
cam.left = glm::normalize(glm::cross(cam.forward, cam.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.view = glm::lookAt(cam.position, cam.position + cam.forward, cam.up);

15
src/camera.h

@ -4,23 +4,18 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include "util.h" #include "util.h"
// TODO: move other camera functions here from renderer.h and pass in camera references
// TODO: add these props to scene json
#define FOV 60.f
#define ASPECT_RATIO 16.f/9.f
#define NEAR_CLIP_PLANE 0.1f
struct camera struct camera
{ {
glm::vec3 position;
float hAngle; float hAngle;
float vAngle; float vAngle;
glm::vec3 target;
glm::vec3 position;
glm::vec3 forward; glm::vec3 forward;
glm::vec3 up; glm::vec3 up;
glm::vec3 left; glm::vec3 left;
glm::vec3 target;
glm::vec3 world_up;
glm::mat4 model; glm::mat4 model;
glm::mat4 view; glm::mat4 view;
@ -36,7 +31,7 @@ enum projection_type
v2f cameraUnproject(camera& cam, int x, int y, int vp_width, int vp_height); v2f cameraUnproject(camera& cam, int x, int y, int vp_width, int vp_height);
void cameraInitPerspective(camera& cam, glm::vec3 position, glm::vec3 target, glm::vec3 up); void cameraInitPerspective(camera& cam, glm::vec3 position, glm::vec3 target, glm::vec3 world_up);
void cameraMove(camera& cam, bool up, bool left, bool down, bool right, bool forward, bool backward); void cameraMove(camera& cam, bool up, bool left, bool down, bool right, bool forward, bool backward);
void cameraRotate(camera& cam, int32 xrel, int32 yrel); void cameraRotate(camera& cam, int32 xrel, int32 yrel);
void cameraRoll(camera& cam, bool CW, bool CCW); void cameraRoll(camera& cam, bool CW, bool CCW);

29
src/gooey.cpp

@ -86,19 +86,26 @@ gooRender(SDL_Handles &handles, hexgrid* grid, bool &is_debug, camera* cam)
ImGui::Text("is_selecting"); ImGui::Text("is_selecting");
ImGui::SameLine(); ImGui::TextUnformatted(grid->is_selecting ? "true" : "false"); ImGui::SameLine(); ImGui::TextUnformatted(grid->is_selecting ? "true" : "false");
if (ImGui::CollapsingHeader("Camera Position", ImGuiTreeNodeFlags_DefaultOpen)) { if (ImGui::CollapsingHeader("Camera", ImGuiTreeNodeFlags_DefaultOpen)) {
ImGui::Text("x: %f", cam->position.x); ImGui::Indent();
ImGui::Text("y: %f", cam->position.y); if (ImGui::CollapsingHeader("Camera Position", ImGuiTreeNodeFlags_DefaultOpen)) {
ImGui::Text("z: %f", cam->position.z); ImGui::Text("x: %f", cam->position.x);
ImGui::Text("y: %f", cam->position.y);
ImGui::Text("z: %f", cam->position.z);
}
if (ImGui::CollapsingHeader("Camera Forward", ImGuiTreeNodeFlags_DefaultOpen)) {
ImGui::Text("x: %f", cam->forward.x);
ImGui::Text("y: %f", cam->forward.y);
ImGui::Text("z: %f", cam->forward.z);
}
ImGui::Spacing();
ImGui::Text("hAngle: %f", cam->hAngle);
ImGui::Text("vAngle: %f", cam->vAngle);
ImGui::Unindent();
} }
#if 0
// testing SDL_image
SDL_Surface* image = handles.texSurfaces[0];
ImGui::Image((void*)(intptr_t) image->userdata, ImVec2(image->w, image->h));
#endif
//ImGui::ShowMetricsWindow();
if (grid->current_hex) { if (grid->current_hex) {
Hex ch = grid->current_hex->hex; Hex ch = grid->current_hex->hex;
ImGui::Text("current_hex: %i, %i, %i", ch.q, ch.r, ch.s); ImGui::Text("current_hex: %i, %i, %i", ch.q, ch.r, ch.s);

3
src/hexgame.cpp

@ -2,9 +2,9 @@
* TODO: * TODO:
* - renderer * - renderer
* - clean up main(), split out initialization and scene loading to new functions * - clean up main(), split out initialization and scene loading to new functions
* - move input handling out to new file, controller?
* - check over renderer, camera, gooey init after changes * - check over renderer, camera, gooey init after changes
* - pass in frame time to camera movement functions to decouple speed from framerate * - pass in frame time to camera movement functions to decouple speed from framerate
* - attempt to fix bug with starting camera xform
* - test camera speed when moving with composite vector * - test camera speed when moving with composite vector
* - may be fixed?? need to test by examining camera position between frames * - may be fixed?? need to test by examining camera position between frames
* and compare the distance * and compare the distance
@ -23,6 +23,7 @@
* - use a storage pool for assimp meshes allowing reuse across entities * - use a storage pool for assimp meshes allowing reuse across entities
* - replace remaining calls to malloc/free with safe(r) function in util.h * - replace remaining calls to malloc/free with safe(r) function in util.h
* - add cpu perforcmace counters in render loop * - add cpu perforcmace counters in render loop
* - test YAML for scene loading https://yaml.org/
* - maybe try deferred rendering at some point to use a ton of lights * - maybe try deferred rendering at some point to use a ton of lights
* - check for memory leaks w/ valgrind * - check for memory leaks w/ valgrind
******************************************************************************/ ******************************************************************************/

2
src/scene_loader.cpp

@ -105,7 +105,7 @@ slParseCamera(slSceneDoc* sd, camera& cam)
cameraInitPerspective(cam, cameraInitPerspective(cam,
parseVec3(json_cam["position"]), parseVec3(json_cam["position"]),
parseVec3(json_cam["target"]), parseVec3(json_cam["target"]),
parseVec3(json_cam["up"]) parseVec3(json_cam["world_up"])
); );
} }

Loading…
Cancel
Save