diff --git a/src/controls/include/controls/control.hpp b/src/controls/include/controls/control.hpp index 6d15654b9154cf7274d40f504e2da916d936ce40..d5ddda2b566ac835c57b1dedeb66baf121ea644e 100644 --- a/src/controls/include/controls/control.hpp +++ b/src/controls/include/controls/control.hpp @@ -42,7 +42,8 @@ public: void setPosY(float coord) { bound_frame->setPosVec(glm::vec3(bound_frame->getPosition().x, coord, bound_frame->getPosition().z)); } void setPosZ(float coord) { bound_frame->setPosVec(glm::vec3(bound_frame->getPosition().x, bound_frame->getPosition().y, coord)); } - void keyboardMove(const std::unordered_set<std::string> &pressed_keys); + void keyboardMove(const std::unordered_set<std::string> &held_keys, + const std::unordered_set<std::string> &pressed_keys); void forward(); void backward(); void left(); @@ -50,6 +51,9 @@ public: void up(); void down(); void rotate(glm::vec3 rotational_difference); + + void stepForward(float step_size); + void stepBackward(float step_size); }; #endif \ No newline at end of file diff --git a/src/controls/src/control.cpp b/src/controls/src/control.cpp index 71c295fca53894ad6dc470ea2cc990c6b933b4f9..26ebd0b0f12fcc3167d87087c1f220326a2a59ce 100644 --- a/src/controls/src/control.cpp +++ b/src/controls/src/control.cpp @@ -15,20 +15,26 @@ void Controller::setup(float dt, frame_ptr frame) setStep(dt); } -void Controller::keyboardMove(const std::unordered_set<std::string> &pressed_keys) +void Controller::keyboardMove(const std::unordered_set<std::string> &held_keys, + const std::unordered_set<std::string> &pressed_keys) { - if (pressed_keys.contains("W")) + if (held_keys.contains("W")) forward(); - if (pressed_keys.contains("S")) + if (held_keys.contains("S")) backward(); - if (pressed_keys.contains("A")) + if (held_keys.contains("A")) left(); - if (pressed_keys.contains("D")) + if (held_keys.contains("D")) right(); - if (pressed_keys.contains("R")) + if (held_keys.contains("R")) up(); - if (pressed_keys.contains("F")) + if (held_keys.contains("F")) down(); + + if (pressed_keys.contains("P")) + stepForward(1.0f); + if (pressed_keys.contains("O")) + stepBackward(1.0f); } void Controller::forward() @@ -60,3 +66,12 @@ void Controller::rotate(glm::vec3 rotational_difference) { bound_frame->rotateRotationQuat(rotational_difference); } + +void Controller::stepForward(float step_size) +{ + bound_frame->translatePosVec(step_size * getFrontVec()); +} +void Controller::stepBackward(float step_size) +{ + bound_frame->translatePosVec(-step_size * getFrontVec()); +} \ No newline at end of file diff --git a/src/controls/src/control_hub.cpp b/src/controls/src/control_hub.cpp index 3b240db2a3e4ca6c54813086b664d24cf3d62b38..9c4d86ee66a322e873f1414113eafa802aa8cfee 100644 --- a/src/controls/src/control_hub.cpp +++ b/src/controls/src/control_hub.cpp @@ -36,13 +36,13 @@ void ControlHub::processInput(const scene_ptr &scene, const osi::Keyboard &keybo for (auto &controller : obj_controls) { controller->setup(timer.dt(), scene->getSelection()->getFrame()); - controller->keyboardMove(keyboard.down()); + controller->keyboardMove(keyboard.down(), keyboard.just_released()); } } else { active_camera_type->setStep(timer.dt()); - active_camera_type->keyboardMove(keyboard.down()); + active_camera_type->keyboardMove(keyboard.down(), keyboard.just_pressed()); } if (keyboard.just_pressed().contains("C")) diff --git a/src/edit/include/edit/editor.hpp b/src/edit/include/edit/editor.hpp index bdff623a5a6fadb699030a77fdd6c75388891b01..4b7ce927a481b5a9b225cc980f479b3fbbb09fa0 100644 --- a/src/edit/include/edit/editor.hpp +++ b/src/edit/include/edit/editor.hpp @@ -69,6 +69,7 @@ public: void setRunning(bool _editor_running) { editor_running = _editor_running; } state &getControlState() { return control_state; } + const Level &getLevel() { return build_level; } bool isObjectMoving() const { return object_moving; } void setObjectMoving(bool _object_moving) { object_moving = _object_moving; } @@ -78,6 +79,7 @@ public: void doObjectAction(scene_ptr scene, ctrlhub_ptr controls); void updateCamera(scene_ptr scene, ctrlhub_ptr controls); + void changeLevel(scene_ptr scene, ctrlhub_ptr controls); void selectObject(scene_ptr scene, ctrlhub_ptr controls); void rotateObject(scene_ptr scene, ctrlhub_ptr controls); diff --git a/src/edit/include/edit/scene.hpp b/src/edit/include/edit/scene.hpp index b806063802bb140a90dcdcd6c0265e8666372fc6..59695d428b920ab6e124b3b5208c605c807efde8 100644 --- a/src/edit/include/edit/scene.hpp +++ b/src/edit/include/edit/scene.hpp @@ -68,7 +68,7 @@ public: void addCameraNode(float FOV, frame_ptr frame); void addCameraNode(float FOV, Frame frame); - void addCameraNode(float FOV, glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f)); + void addCameraNode(float FOV, glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0f)); void addMesh(node_ptr node, const std::string &file_name); void addMesh(node_ptr node, mesh_ptr mesh); diff --git a/src/edit/src/editor.cpp b/src/edit/src/editor.cpp index 44c7653a108cfee466ee204f345d57326fd43180..b79d270bb5384555a23ab18faac2a2d2092e2ef5 100644 --- a/src/edit/src/editor.cpp +++ b/src/edit/src/editor.cpp @@ -31,10 +31,10 @@ void Editor::updateCamera(scene_ptr scene, ctrlhub_ptr controls) && !keyboard.just_pressed().contains("L") ) return; - std::array<float, 3> levels = build_level.getAll(); - std::array<glm::vec3, 3> rotations = { glm::vec3(0.0f, 90.0f, 0.0f), // L(x-depth) - glm::vec3(-89.0f, 0.0f, 0.0f), // I (y-depth) // FIX - should be -90.0f - glm::vec3(0.0f, 00.0f, 0.0f) }; // K (z-depth) + std::array<float, 3> cam_levels = build_level.getAll(); + std::array<glm::vec3, 3> rotations = { glm::vec3(0.0f, -90.0f, 0.0f), // L(x-depth) + glm::vec3(-90.0f, 0.0f, 0.0f), // I (y-depth) // FIX - should be -90.0f + glm::vec3(0.0f, 180.0f, 0.0f) }; // K (z-depth) std::uint8_t new_active = 0; if (keyboard.just_pressed().contains("I")) @@ -46,10 +46,18 @@ void Editor::updateCamera(scene_ptr scene, ctrlhub_ptr controls) if (keyboard.just_pressed().contains("L")) new_active = 0; - levels[new_active] += 5.0f; + cam_levels[new_active] += new_active == 1 ? 5.0f : -5.0f; // +5 for up-down, else -5 build_level.setActiveIndex(new_active); controls->getActiveCamCtrl()->setRotation(rotations[new_active]); - controls->getActiveCamCtrl()->setPos(levels[0], levels[1], levels[2]); + controls->getActiveCamCtrl()->setPos(cam_levels[0], cam_levels[1], cam_levels[2]); +} +void Editor::changeLevel(scene_ptr scene, ctrlhub_ptr controls) +{ + if (keyboard.just_pressed().contains("P")) + ++build_level; + + if (keyboard.just_pressed().contains("O")) + --build_level; } void Editor::selectObject(scene_ptr scene, ctrlhub_ptr controls) { diff --git a/src/edit/src/scene.cpp b/src/edit/src/scene.cpp index d22c1cec5c51b7164f87fa024d223f1c9d801351..be535437fa1ab8ed2a07807289fd4ec8e7984f59 100644 --- a/src/edit/src/scene.cpp +++ b/src/edit/src/scene.cpp @@ -75,7 +75,7 @@ void Scene::createScene() // addMeshNode("./data/models/final_giraffe.obj", glm::vec3(-5, 1, 3)); /* ADD CAMERA */ - addCameraNode(60.0f, glm::vec3(0.0f, 1.0f, 6.0f)); + addCameraNode(60.0f, glm::vec3(0.0f, 1.0f, -6.0f), glm::vec3(0.0f, 180.0f, 0.0f)); } void Scene::removeActiveCamera(camera_ptr camera) // TO DO: fix @@ -164,9 +164,9 @@ void Scene::addCameraNode(float FOV, Frame frame) scene.back()->addObject(std::make_shared<Camera>(FOV, scene.back()->getFrame())); } -void Scene::addCameraNode(float FOV, glm::vec3 position) +void Scene::addCameraNode(float FOV, glm::vec3 position, glm::vec3 rotation) { - scene.emplace_back(std::make_shared<Node>(std::make_shared<Frame>(position))); + scene.emplace_back(std::make_shared<Node>(std::make_shared<Frame>(position, rotation))); scene.back()->addObject(std::make_shared<Camera>(FOV, scene.back()->getFrame())); } diff --git a/src/gfx/include/gfx/render.hpp b/src/gfx/include/gfx/render.hpp index 7276519135f1d2cc6d0dc702668ddbf0aa000ab7..45842c75c08543b670c919d6845f4b784afb776d 100644 --- a/src/gfx/include/gfx/render.hpp +++ b/src/gfx/include/gfx/render.hpp @@ -3,6 +3,7 @@ #include <common/frame.hpp> #include <common/node.hpp> +#include <edit/editor.hpp> #include <gfx/camera.hpp> #include <gfx/light.hpp> #include <gfx/mesh.hpp> @@ -28,6 +29,7 @@ glm::mat4 get_projection_matrix(camera_ptr camera); void gfx_draw(std::map<std::string, shader_ptr> &my_shaders, std::vector<light_ptr> lights, camera_ptr camera, - const std::vector<node_ptr> &scene); + const std::vector<node_ptr> &scene, + const edit::Level &build_level); #endif \ No newline at end of file diff --git a/src/gfx/src/render.cpp b/src/gfx/src/render.cpp index 4d808ebce035f94a24c4e0148dd112dc3ec5c4c9..2825e3955c4d62bba122aa08d9297b0ab62301bf 100644 --- a/src/gfx/src/render.cpp +++ b/src/gfx/src/render.cpp @@ -81,10 +81,18 @@ glm::mat4 get_projection_matrix(camera_ptr camera) 100.0f); } +bool node_in_active(camera_ptr camera, frame_ptr frame, const edit::Level &build_level) // will be more complicated in the future +{ + if (camera->isPerspective()) + return true; + return frame->getPosition()[build_level.getActiveIndex()] == build_level.getActive(); +} + void draw_objects(std::map<std::string, shader_ptr> &my_shaders, std::vector<light_ptr> lights, camera_ptr camera, const std::vector<node_ptr> &scene, - glm::mat4 &view, glm::mat4 &projection) + glm::mat4 &view, glm::mat4 &projection, + const edit::Level &build_level) { for (auto &node : scene) { @@ -115,21 +123,24 @@ void draw_objects(std::map<std::string, shader_ptr> &my_shaders, std::vector<lig obj->getVAO()->bind(); - glDrawElements(obj->getDrawMode(), + glPolygonMode(GL_FRONT_AND_BACK, node_in_active(camera, node->getFrame(), build_level) ? GL_FILL : GL_LINE); + + glDrawElements(obj->getDrawMode() , static_cast<GLsizei>(obj->getIndices().size()), GL_UNSIGNED_INT, 0); ASSUMPTION(glGetError() == GL_NO_ERROR); } - draw_objects(my_shaders, lights, camera, node->getChildren(), view, projection); + draw_objects(my_shaders, lights, camera, node->getChildren(), view, projection, build_level); } } void gfx_draw(std::map<std::string, shader_ptr> &my_shaders, std::vector<light_ptr> lights, camera_ptr camera, - const std::vector<node_ptr> &scene) + const std::vector<node_ptr> &scene, + const edit::Level &build_level) { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); @@ -160,5 +171,5 @@ void gfx_draw(std::map<std::string, shader_ptr> &my_shaders, ASSUMPTION(glGetError() == GL_NO_ERROR); /* DRAW OBJECTS */ - draw_objects(my_shaders, lights, camera, scene, view, projection); + draw_objects(my_shaders, lights, camera, scene, view, projection, build_level); } diff --git a/src/studio/src/simulator.cpp b/src/studio/src/simulator.cpp index e6e3d1b43d8b22b66a4eb0909545ac88ea4175ec..49a338c17447331a49c52d3f3b8f84c637cb6496 100644 --- a/src/studio/src/simulator.cpp +++ b/src/studio/src/simulator.cpp @@ -39,6 +39,10 @@ void Simulator::update() editor->updateCamera(scene, controls); editor->doObjectAction(scene, controls); + editor->changeLevel(scene, controls); + + auto cam_frame = scene->getActiveCameras()[0].lock()->getFrame()->getPosition(); + std::cout << "Camera coords: X " << cam_frame.x << " | Y " << cam_frame.y << " | Z " << cam_frame.z << std::endl; /* Rotation around x-axis demo */ // controls->rotate_obj(glm::vec3(1.0f, 0.0f, 0.0f)); @@ -57,7 +61,7 @@ void Simulator::present() { active_cam.lock()->setWindowSize(window().size()); } - gfx_draw(my_shaders, scene->getLights(), active_cam.lock(), scene->getScene()); + gfx_draw(my_shaders, scene->getLights(), active_cam.lock(), scene->getScene(), editor->getLevel()); } }