From ee37f93c4be48131285b569103c09e8d49619a03 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Fri, 21 Dec 2018 16:59:25 -0500 Subject: [PATCH] fix bug with starting camera xform --- data/scene_schema.json | 4 ++-- data/test_scene.json | 10 +++++----- src/camera.cpp | 28 +++++++++++++--------------- src/camera.h | 15 +++++---------- src/gooey.cpp | 29 ++++++++++++++++++----------- src/hexgame.cpp | 3 ++- src/scene_loader.cpp | 2 +- 7 files changed, 46 insertions(+), 45 deletions(-) diff --git a/data/scene_schema.json b/data/scene_schema.json index 60b1f1a..589c3af 100644 --- a/data/scene_schema.json +++ b/data/scene_schema.json @@ -41,9 +41,9 @@ "properties": { "position": { "$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" : { diff --git a/data/test_scene.json b/data/test_scene.json index 109810f..383ab87 100644 --- a/data/test_scene.json +++ b/data/test_scene.json @@ -6,14 +6,14 @@ "z" : 100 }, "target" : { - "x" : 640, - "y" : 500, + "x" : -140, + "y" : 50, "z" : 0 }, - "up" : { + "world_up" : { "x" : 0, - "y" : 1, - "z" : 0 + "y" : 0, + "z" : 1 } }, diff --git a/src/camera.cpp b/src/camera.cpp index 7b50a0a..dfa4b6f 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -3,30 +3,29 @@ #include "camera.h" +// TODO: add these props to scene json #define MOVE_SPEED 5.f #define ROTATE_SPEED 0.005f #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 -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.target = target; - cam.up = up; + cam.world_up = world_up; cam.projection = glm::infinitePerspective(glm::radians(FOV), ASPECT_RATIO, NEAR_CLIP_PLANE); - cam.hAngle = 0; - cam.vAngle = glm::atan((cam.target.z - cam.position.z) / (cam.target.y - cam.position.y)); + cam.forward = glm::normalize(target - position); + 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 - // camera position also changes with change to window size - 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.hAngle = glm::atan(cam.forward.x, cam.forward.y); + cam.vAngle = glm::atan(cam.forward.z, cam.forward.y); cam.view = glm::lookAt(cam.position, cam.position + cam.forward, cam.up); cam.model = glm::mat4(1.0f); @@ -119,8 +118,7 @@ cameraRotate(camera& cam, int32 xrel, int32 yrel) ); glm::normalize(cam.forward); - cam.up = glm::vec3(0,0,1); - cam.left = glm::normalize(glm::cross(cam.forward, cam.up)); + 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); diff --git a/src/camera.h b/src/camera.h index 004dd70..143194a 100644 --- a/src/camera.h +++ b/src/camera.h @@ -4,23 +4,18 @@ #include #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 { - glm::vec3 position; float hAngle; float vAngle; - glm::vec3 target; + + glm::vec3 position; glm::vec3 forward; glm::vec3 up; glm::vec3 left; + glm::vec3 target; + glm::vec3 world_up; glm::mat4 model; glm::mat4 view; @@ -36,7 +31,7 @@ enum projection_type 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 cameraRotate(camera& cam, int32 xrel, int32 yrel); void cameraRoll(camera& cam, bool CW, bool CCW); diff --git a/src/gooey.cpp b/src/gooey.cpp index 27dca26..64a0f77 100644 --- a/src/gooey.cpp +++ b/src/gooey.cpp @@ -86,19 +86,26 @@ gooRender(SDL_Handles &handles, hexgrid* grid, bool &is_debug, camera* cam) ImGui::Text("is_selecting"); ImGui::SameLine(); ImGui::TextUnformatted(grid->is_selecting ? "true" : "false"); - if (ImGui::CollapsingHeader("Camera Position", ImGuiTreeNodeFlags_DefaultOpen)) { - 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", ImGuiTreeNodeFlags_DefaultOpen)) { + ImGui::Indent(); + if (ImGui::CollapsingHeader("Camera Position", ImGuiTreeNodeFlags_DefaultOpen)) { + 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) { Hex ch = grid->current_hex->hex; ImGui::Text("current_hex: %i, %i, %i", ch.q, ch.r, ch.s); diff --git a/src/hexgame.cpp b/src/hexgame.cpp index 7183a0a..8c30069 100644 --- a/src/hexgame.cpp +++ b/src/hexgame.cpp @@ -2,9 +2,9 @@ * TODO: * - renderer * - 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 * - 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 * - may be fixed?? need to test by examining camera position between frames * and compare the distance @@ -23,6 +23,7 @@ * - use a storage pool for assimp meshes allowing reuse across entities * - replace remaining calls to malloc/free with safe(r) function in util.h * - 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 * - check for memory leaks w/ valgrind ******************************************************************************/ diff --git a/src/scene_loader.cpp b/src/scene_loader.cpp index 413c899..08b82af 100644 --- a/src/scene_loader.cpp +++ b/src/scene_loader.cpp @@ -105,7 +105,7 @@ slParseCamera(slSceneDoc* sd, camera& cam) cameraInitPerspective(cam, parseVec3(json_cam["position"]), parseVec3(json_cam["target"]), - parseVec3(json_cam["up"]) + parseVec3(json_cam["world_up"]) ); }