Re-enabled camera movement

simple_math_single_header
Martin Felis 2018-03-09 16:44:40 +01:00
parent 69471bba84
commit e9c7a66d42
4 changed files with 25 additions and 190 deletions

View File

@ -107,8 +107,8 @@ inline Matrix44f LookAt(
const Vector3f& poi,
const Vector3f& up) {
Vector3f d = (poi - eye).normalized();
Vector3f s = d.cross(up.normalized());
Vector3f u = s.cross(d);
Vector3f s = d.cross(up.normalized()).normalized();
Vector3f u = s.cross(d).normalized();
return TranslateMat44(-eye[0], -eye[1], -eye[2]) * Matrix44f(
s[0], u[0], -d[0], 0.0f,

View File

@ -152,8 +152,8 @@ int main(void)
printf("Initializing ModuleManager...\n");
RuntimeModuleManager module_manager;
module_manager.RegisterModule("src/modules/libRenderModule.so");
module_manager.RegisterModule("src/modules/libTestModule.so");
// module_manager.RegisterModule("src/modules/libCharacterModule.so");
// module_manager.RegisterModule("src/modules/libTestModule.so");
// Setup global variables
gModuleManager = &module_manager;

View File

@ -7,10 +7,10 @@ ADD_LIBRARY (RenderModule SHARED
RenderUtils.cc
)
# ADD_LIBRARY (TestModule SHARED
# TestModule.cc
# )
#
ADD_LIBRARY (TestModule SHARED
TestModule.cc
)
# ADD_LIBRARY (CharacterModule SHARED
# CharacterModule.cc
# )

View File

@ -1,39 +1,24 @@
#include "RuntimeModule.h"
#include "Globals.h"
#include "3rdparty/ocornut-imgui/imgui.h"
#include <GL/gl3w.h> // This example is using gl3w to access OpenGL functions (because it is small). You may use glew/glad/glLoadGen/etc. whatever already works for you.
#include "imgui/imgui.h"
#include <bx/math.h>
#include <GLFW/glfw3.h>
#include "SimpleMath/SimpleMath.h"
#include "SimpleMath/SimpleMathMap.h"
#include "SimpleMath/SimpleMathGL.h"
#include "RuntimeModuleManager.h"
#include "Serializer.h"
#include "Timer.h"
#include "modules/RenderModule.h"
#include "modules/CharacterModule.h"
#include <iostream>
#include <sstream>
using namespace std;
bool fps_camera = true;
// Boilerplate for the module reload stuff
struct module_state {
bool fps_camera;
float camera_theta;
float camera_phi;
bool modules_window_visible = false;
bool imgui_demo_window_visible = false;
bool character_properties_window_visible = false;
int modules_window_selected_index = -1;
CharacterEntity* character = nullptr;
};
void handle_mouse (struct module_state *state) {
@ -42,18 +27,18 @@ void handle_mouse (struct module_state *state) {
}
if (glfwGetMouseButton(gWindow, 1)) {
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
} else {
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
Camera *active_camera = &gRenderer->cameras[gRenderer->activeCameraIndex];
assert (active_camera != nullptr);
Matrix44f camera_view_matrix = SimpleMath::Map<Matrix44f>(active_camera->mtxView, 4, 4);
Camera *camera = &gRenderer->mCamera;
assert (camera != nullptr);
const Matrix44f& camera_view_matrix = camera->mViewMatrix;
Matrix33f camera_rot_inv = camera_view_matrix.block<3,3>(0,0).transpose();
Vector3f eye = active_camera->eye;
Vector3f poi = active_camera->poi;
Vector3f eye = camera->mEye;
Vector3f poi = camera->mPoi;
if (glfwGetMouseButton(gWindow, 1)) {
Vector3f view_dir;
@ -69,10 +54,10 @@ void handle_mouse (struct module_state *state) {
0.f, 1.f, 0.f);
poi = eye + rot_matrix_x * rot_matrix_y * view_dir;
active_camera->poi = poi;
camera->mPoi = poi;
}
active_camera->updateMatrices();
camera->UpdateMatrices();
}
void handle_keyboard (struct module_state *state, float dt) {
@ -80,15 +65,9 @@ void handle_keyboard (struct module_state *state, float dt) {
return;
}
if (glfwGetMouseButton(gWindow, 1)) {
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
} else {
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
Camera *active_camera = &gRenderer->cameras[gRenderer->activeCameraIndex];
assert (active_camera != nullptr);
Matrix44f camera_view_matrix = SimpleMath::Map<Matrix44f>(active_camera->mtxView, 4, 4);
Camera *camera = &gRenderer->mCamera;
assert (camera != nullptr);
const Matrix44f& camera_view_matrix = camera->mViewMatrix;
Matrix33f camera_rot_inv = camera_view_matrix.block<3,3>(0,0).transpose();
Vector3f forward = camera_rot_inv.transpose() * Vector3f (0.f, 0.f, 1.f);
@ -96,8 +75,8 @@ void handle_keyboard (struct module_state *state, float dt) {
if (glfwGetMouseButton(gWindow, 1)) {
// Right mouse button pressed, move the camera
Vector3f eye = active_camera->eye;
Vector3f poi = active_camera->poi;
Vector3f eye = camera->mEye;
Vector3f poi = camera->mPoi;
Vector3f direction (0.f, 0.f, 0.f);
@ -128,37 +107,9 @@ void handle_keyboard (struct module_state *state, float dt) {
eye += direction * 5.f * gTimer->mFrameTime;
poi += direction * 5.f * gTimer->mFrameTime;
active_camera->eye = eye;
active_camera->poi = poi;
} else if (state->character != nullptr) {
// Movement of the character
CharacterController& controller = state->character->mController;
controller.reset();
Vector3f forward_plane = Vector3f (0.0f, 1.0f, 0.0f).cross(right);
// Reset the character control state:
if (glfwGetKey(gWindow, GLFW_KEY_W) == GLFW_PRESS) {
controller.mDirection += forward_plane;
}
if (glfwGetKey(gWindow, GLFW_KEY_S) == GLFW_PRESS) {
controller.mDirection -= forward_plane;
}
if (glfwGetKey(gWindow, GLFW_KEY_D) == GLFW_PRESS) {
controller.mDirection += right;
}
if (glfwGetKey(gWindow, GLFW_KEY_A) == GLFW_PRESS) {
controller.mDirection -= right;
}
if (glfwGetKey(gWindow, GLFW_KEY_SPACE) == GLFW_PRESS) {
controller.mState[CharacterController::ControlStateJump] = true;
}
}
camera->mEye = eye;
camera->mPoi = poi;
}
// handle pause
if (glfwGetKey(gWindow, GLFW_KEY_P) == GLFW_PRESS) {
@ -166,18 +117,9 @@ void handle_keyboard (struct module_state *state, float dt) {
}
}
void update_character(module_state* state, float dt) {
if (state->character != nullptr) {
state->character->Update(dt);
}
}
static struct module_state *module_init() {
gLog ("%s %s called", __FILE__, __FUNCTION__);
module_state *state = (module_state*) malloc(sizeof(*state));
state->modules_window_selected_index = -1;
fps_camera = true;
return state;
}
@ -186,12 +128,6 @@ template <typename Serializer>
static void module_serialize (
struct module_state *state,
Serializer* serializer) {
SerializeVec3(*serializer, "protot.TestModule.entity.mPosition", state->character->mPosition);
SerializeVec3(*serializer, "protot.TestModule.entity.mVelocity", state->character->mVelocity);
SerializeBool(*serializer, "protot.TestModule.character_window.visible", state->character_properties_window_visible);
SerializeBool(*serializer, "protot.TestModule.modules_window.visible", state->modules_window_visible);
SerializeBool(*serializer, "protot.TestModule.imgui_demo_window_visible", state->imgui_demo_window_visible);
SerializeInt(*serializer, "protot.TestModule.modules_window.selection_index", state->modules_window_selected_index);
}
static void module_finalize(struct module_state *state) {
@ -202,11 +138,6 @@ static void module_finalize(struct module_state *state) {
static void module_reload(struct module_state *state, void* read_serializer) {
gLog ("%s %s called (state %p)", __FILE__, __FUNCTION__, state);
gLog ("Creating render entity");
state->character = new CharacterEntity;
state->character->mPosition = Vector3f (0.f, 0.f, 0.f);
// load the state of the entity
if (read_serializer != nullptr) {
module_serialize(state, static_cast<ReadSerializer*>(read_serializer));
@ -221,111 +152,15 @@ static void module_unload(struct module_state *state, void* write_serializer) {
module_serialize(state, static_cast<WriteSerializer*>(write_serializer));
}
// clean up
state->character->mEntity = nullptr;
delete state->character;
gLog ("Cleanup complete");
}
void ShowModulesWindow(struct module_state *state) {
// ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4 (0.5f, 0.5f, 0.5f, 0.8f));
// if (ImGui::BeginDock("Modules")) {
// ImGui::Columns(2);
int selected = state->modules_window_selected_index;
for (int i = 0; i < gModuleManager->mModules.size(); i++) {
ImGuiTreeNodeFlags node_flags =
ImGuiTreeNodeFlags_Leaf
| ((i == selected) ? ImGuiTreeNodeFlags_Selected : 0)
;
bool node_open = ImGui::TreeNodeEx(
gModuleManager->mModules[i]->name.c_str(),
node_flags);
if (ImGui::IsItemClicked()) {
selected = i;
}
if (node_open) {
ImGui::TreePop();
}
}
state->modules_window_selected_index = selected;
ImGui::Separator();
RuntimeModule* selected_module = nullptr;
if (selected != -1) {
selected_module = gModuleManager->mModules[selected];
}
if (selected_module) {
static char time_buf[32];
memset (time_buf, 0, 32);
ImGui::LabelText("File", "%s", selected_module->name.c_str());
ImGui::LabelText("Handle", "0x%p", selected_module->handle);
ImGui::LabelText("id", "%ld", selected_module->id);
ImGui::LabelText("mtime", "%ld", selected_module->mtime);
ImGui::LabelText("mtimensec", "%ld", selected_module->mtimensec);
// ImGui::LabelText("mtime", "%s", ctime((time_t*)&selected_module->mtime));
// cout << "time_buf = " << ctime((time_t*)&selected_module->mtime) << endl;
if (ImGui::Button ("Force Reload")) {
selected_module->mtime = 0;
selected_module->id = 0;
}
}
// }
// ImGui::EndDock();
// ImGui::PopStyleColor ();
}
static bool module_step(struct module_state *state, float dt) {
if (gRenderer == nullptr)
return false;
bool enabled = true;
static bool imgui_demo_window_visible = false;
ImGui::BeginMainMenuBar();
if (ImGui::BeginMenu("Dialogs"))
{
ImGui::Checkbox("Modules", &state->modules_window_visible);
ImGui::Checkbox("ImGui Demo", &state->imgui_demo_window_visible);
gLog ("show demo: %d", state->imgui_demo_window_visible == true);
ImGui::Checkbox("Character", &state->character_properties_window_visible);
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
if (state->modules_window_visible) {
ShowModulesWindow(state);
}
if (state->character_properties_window_visible && state->character != nullptr) {
ShowCharacterPropertiesWindow(state->character);
}
if (state->imgui_demo_window_visible) {
ImGui::ShowDemoWindow();
}
handle_mouse(state);
handle_keyboard(state, dt);
update_character(state, dt);
gRenderer->drawDebugAxes (
Vector3f (0.f, 0.f, 0.f),
Matrix33f::Identity(),
1.0f);
return true;
}