Skip to content
Snippets Groups Projects
Commit 7ba6aefe authored by Martin Štourač's avatar Martin Štourač
Browse files

Frame and Controller classes incorporated

parent d08230a7
No related branches found
No related tags found
No related merge requests found
Showing
with 304 additions and 179 deletions
set(THIS_TARGET_NAME gfx) set(THIS_TARGET_NAME gfx)
add_library(${THIS_TARGET_NAME} add_library(${THIS_TARGET_NAME}
#./include/gfx/file.hpp
./include/gfx/shader.hpp ./include/gfx/shader.hpp
./src/shader.cpp ./src/shader.cpp
...@@ -23,6 +22,18 @@ add_library(${THIS_TARGET_NAME} ...@@ -23,6 +22,18 @@ add_library(${THIS_TARGET_NAME}
./include/gfx/object.hpp ./include/gfx/object.hpp
./src/object.cpp ./src/object.cpp
./include/gfx/frame.hpp
./src/frame.cpp
./include/gfx/control.hpp
./src/control.cpp
./include/gfx/cam_control.hpp
./src/cam_control.cpp
./include/gfx/obj_control.hpp
./src/obj_control.cpp
# ./include/osi/opengl.hpp # ./include/osi/opengl.hpp
# ./include/osi/gui.hpp # ./include/osi/gui.hpp
) )
......
#ifndef CAM_CONTROL_INCLUDED
#define CAM_CONTROL_INCLUDED
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <gfx/control.hpp>
class CameraController : public Controller
{
float pitch = 0.0f;
float yaw = -90.0f;
glm::vec2 sens;
float sens_multiplier = 400.0f;
public:
using Controller::Controller;
float getPitch() { return pitch; }
float getYaw() { return yaw; }
void setPitch(float yoffset);
void setYaw(float xoffset);
void setFrontVec() override;
void turn(float xoffset, float yoffset);
glm::vec2 getSens() { return sens; }
void setSens(float x_size, float y_size);
void setSensMultiplier(float multiplier) { sens_multiplier = multiplier; }
};
#endif
\ No newline at end of file
...@@ -8,57 +8,16 @@ ...@@ -8,57 +8,16 @@
class Camera class Camera
{ {
glm::vec3 posVec = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 frontVec = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 upVec = glm::vec3(0.0f, 1.0f, 0.0f);
float FOV = 45.0f; float FOV = 45.0f;
float speed = 2.5f;
float step = 0;
glm::vec2 sens;
float sens_multiplier = 400.0f;
float pitch = 0.0f;
float yaw = -90.0f;
glm::u32vec2 window_size = glm::u32vec2{0, 0}; glm::u32vec2 window_size = glm::u32vec2{0, 0};
public: public:
Camera(glm::vec3 const &pos, glm::vec3 const &direction, float fov); Camera(float fov);
void forward();
void backward();
void left();
void right();
void up();
void down();
float getFOV(); float getFOV();
void setFOV(float fov); void setFOV(float fov);
float getPitch();
float getYaw();
void setPitch(float yoffset);
void setYaw(float xoffset);
glm::vec3 getFrontVec();
glm::vec3 getPosVec();
glm::vec3 getUpVec();
void setFrontVec();
//void setPosVec(); TO DO
//void setUpVec(); TO DO
void turn(float xoffset, float yoffset);
void setStep(float dt);
void setSpeed(float multiplier);
glm::vec2 getSens();
void setSens(float x_size, float y_size);
void setSensMultiplier(float multiplier);
glm::vec2 getWindowSize(); glm::vec2 getWindowSize();
void setWindowSize(glm::u32vec2 size); void setWindowSize(glm::u32vec2 size);
}; };
......
#ifndef CONTROL_INCLUDED
#define CONTROL_INCLUDED
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <gfx/frame.hpp>
#include <unordered_set>
#include <string>
class Controller
{
protected:
glm::vec3 frontVec = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 upVec = glm::vec3(0.0f, 1.0f, 0.0f);
float speed = 2.5f;
float step = 0;
Frame &bound_frame;
public:
Controller(Frame &frame) : bound_frame{frame} {}
glm::vec3 getFrontVec() { return frontVec; }
glm::vec3 getUpVec() { return upVec; }
virtual void setFrontVec() = 0;
//void setUpVec(); TO DO
void setStep(float dt);
void setSpeed(float new_speed);
void teleport(glm::vec3 new_position) { bound_frame.setPositionVec(new_position); }
void move(const std::unordered_set<std::string> &pressed_keys);
void forward();
void backward();
void left();
void right();
void up();
void down();
};
#endif
\ No newline at end of file
#ifndef FRAME_INCLUDED
#define FRAME_INCLUDED
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
class Frame
{
glm::vec3 positionVec = glm::vec3(0.0f, 0.0f, 0.0f);
glm::quat rotationQuat;
public:
Frame(glm::vec3 position, glm::vec3 rotation)
: positionVec{position}, rotationQuat{glm::quat(rotation)} {}
glm::vec3 getPosition() { return positionVec; }
glm::quat getRotation() { return rotationQuat; }
glm::mat4 getRotationMat() { return glm::toMat4(rotationQuat); }
void setPositionVec(glm::vec3 new_position) { positionVec = new_position; }
void setrotationQuat(glm::vec3 new_rotation) { rotationQuat = glm::quat(new_rotation); }
};
#endif
\ No newline at end of file
#ifndef OBJ_CONTROL_INCLUDED
#define OBJ_CONTROL_INCLUDED
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <gfx/control.hpp>
#include <gfx/frame.hpp>
#include <unordered_set>
#include <string>
class ObjectController : public Controller
{
// QUESTION: set automatically relative to camera or axis movement?
// Currently: axis
// glm::vec3 frontVec = glm::vec3(0.0f, 0.0f, -1.0f);
// glm::vec3 upVec = glm::vec3(0.0f, 1.0f, 0.0f);
public:
using Controller::Controller;
void setFrontVec() override;
//void rotate() TO DO
};
#endif
\ No newline at end of file
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
class Object class Object
{ {
glm::vec3 positionVec = { 0.0f, 0.0f, 0.0f };
std::vector<float> vertices; std::vector<float> vertices;
std::vector<unsigned int> indices; std::vector<unsigned int> indices;
...@@ -22,13 +21,9 @@ class Object ...@@ -22,13 +21,9 @@ class Object
bool buffer_object(); bool buffer_object();
public: public:
Object(glm::vec3 const &position, Object(std::vector<float> const &_vertices,
std::vector<float> const &_vertices,
std::vector<unsigned int> const &_indices); std::vector<unsigned int> const &_indices);
glm::vec3 const &getPosition() const { return positionVec; }
void setPosition(glm::vec3 const &new_pos) { positionVec = new_pos; }
std::vector<float> const &getVertices() const { return vertices; } std::vector<float> const &getVertices() const { return vertices; }
void setVertices(std::vector<float> const &new_vertices) { vertices = new_vertices; } void setVertices(std::vector<float> const &new_vertices) { vertices = new_vertices; }
......
...@@ -12,6 +12,9 @@ ...@@ -12,6 +12,9 @@
#include <vector> #include <vector>
void send_matrices_to_shader(Shader &myShader, glm::mat4 &model, glm::mat4 &view, glm::mat4 &projection); void send_matrices_to_shader(Shader &myShader, glm::mat4 &model, glm::mat4 &view, glm::mat4 &projection);
void gfx_draw(Shader &myShader, Camera &camera, Object &object); void gfx_draw(Shader &myShader,
Camera &camera, Object &object,
Frame &cam_frame, Frame &obj_frame,
CameraController &cam_control, ObjectController &obj_control);
#endif #endif
\ No newline at end of file
#include <gfx/cam_control.hpp>
void CameraController::setPitch(float yoffset)
{
pitch += yoffset;
if(pitch > 89.0f)
{
pitch = 89.0f;
}
if(pitch < -89.0f)
{
pitch = -89.0f;
}
}
void CameraController::setYaw(float xoffset) { yaw += xoffset; }
void CameraController::setFrontVec()
{
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
frontVec = glm::normalize(front);
}
void CameraController::turn(float xoffset, float yoffset)
{
setYaw(xoffset);
setPitch(yoffset);
setFrontVec();
}
void CameraController::setSens(float x_size, float y_size)
{
sens.x = sens_multiplier * x_size;
sens.y = sens_multiplier * y_size;
}
#include <gfx/camera.hpp> #include <gfx/camera.hpp>
Camera::Camera(glm::vec3 const &pos, glm::vec3 const &direction, float fov) : Camera::Camera(float fov) : FOV{fov} {}
posVec{pos}, frontVec{direction}, FOV{fov} {}
void Camera::forward()
{
posVec += step * frontVec;
}
void Camera::backward()
{
posVec -= step * frontVec;
}
void Camera::left()
{
posVec -= glm::normalize(glm::cross(frontVec, upVec)) * step;
}
void Camera::right()
{
posVec += glm::normalize(glm::cross(frontVec, upVec)) * step;
}
void Camera::up()
{
posVec += step * upVec;
}
void Camera::down()
{
posVec -= step * upVec;
}
float Camera::getFOV() { return FOV; } float Camera::getFOV() { return FOV; }
void Camera::setFOV(float fov) { FOV = fov <= 120 ? fov : 120; } void Camera::setFOV(float fov) { FOV = fov <= 120 ? fov : 120; }
float Camera::getPitch() { return pitch; }
float Camera::getYaw() { return yaw; }
void Camera::setPitch(float yoffset)
{
pitch += yoffset;
if(pitch > 89.0f)
{
pitch = 89.0f;
}
if(pitch < -89.0f)
{
pitch = -89.0f;
}
}
void Camera::setYaw(float xoffset) { yaw += xoffset; }
glm::vec3 Camera::getFrontVec() { return frontVec; }
glm::vec3 Camera::getPosVec() { return posVec; }
glm::vec3 Camera::getUpVec() { return upVec; }
void Camera::setFrontVec()
{
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
frontVec = glm::normalize(front);
}
// void Camera::setPosVec()
// void Camera::setUpVec()
void Camera::turn(float xoffset, float yoffset)
{
setYaw(xoffset);
setPitch(yoffset);
setFrontVec();
}
void Camera::setStep(float dt)
{
step = speed * dt;
}
void Camera::setSpeed(float multiplier)
{
speed = multiplier;
}
glm::vec2 Camera::getSens()
{
return sens;
}
void Camera::setSens(float x_size, float y_size)
{
sens.x = sens_multiplier * x_size;
sens.y = sens_multiplier * y_size;
}
void Camera::setSensMultiplier(float multiplier)
{
sens_multiplier = multiplier;
}
glm::vec2 Camera::getWindowSize() glm::vec2 Camera::getWindowSize()
{ {
return window_size; return window_size;
......
#include <gfx/control.hpp>
void Controller::setStep(float dt)
{
step = speed * dt;
}
void Controller::setSpeed(float new_speed)
{
speed = new_speed;
}
void Controller::move(const std::unordered_set<std::string> &pressed_keys)
{
if (pressed_keys.contains("W"))
forward();
if (pressed_keys.contains("S"))
backward();
if (pressed_keys.contains("A"))
left();
if (pressed_keys.contains("D"))
right();
if (pressed_keys.contains("R"))
up();
if (pressed_keys.contains("F"))
down();
}
void Controller::forward()
{
bound_frame.setPositionVec(bound_frame.getPosition() + step * frontVec);
}
void Controller::backward()
{
bound_frame.setPositionVec(bound_frame.getPosition() - step * frontVec);
}
void Controller::left()
{
bound_frame.setPositionVec(bound_frame.getPosition() - step * glm::normalize(glm::cross(frontVec, upVec)));
}
void Controller::right()
{
bound_frame.setPositionVec(bound_frame.getPosition() + step * glm::normalize(glm::cross(frontVec, upVec)));
}
void Controller::up()
{
bound_frame.setPositionVec(bound_frame.getPosition() + step * upVec);
}
void Controller::down()
{
bound_frame.setPositionVec(bound_frame.getPosition() - step * upVec);
}
\ No newline at end of file
#include <gfx/frame.hpp>
#include <gfx/obj_control.hpp>
void ObjectController::setFrontVec() // temporary implementation
{
frontVec = glm::vec3(0.0f, 0.0f, -1.0f);
}
\ No newline at end of file
#include <gfx/object.hpp> #include <gfx/object.hpp>
#include <vector> #include <vector>
Object::Object(glm::vec3 const &position, std::vector<float> const &_vertices, std::vector<unsigned int> const &_indices) : Object::Object(std::vector<float> const &_vertices,
positionVec{position}, vertices{_vertices}, indices{_indices} std::vector<unsigned int> const &_indices)
: vertices{_vertices}, indices{_indices}
{ {
buffer_object(); buffer_object();
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <gfx/ebo.hpp> #include <gfx/ebo.hpp>
#include <gfx/camera.hpp> #include <gfx/camera.hpp>
#include <gfx/object.hpp> #include <gfx/object.hpp>
#include <gfx/frame.hpp>
#include <gfx/render.hpp> // Order important for compile #include <gfx/render.hpp> // Order important for compile
#include <gfx/shader.hpp> #include <gfx/shader.hpp>
#include <iostream> #include <iostream>
...@@ -37,7 +38,10 @@ void send_matrices_to_shader(Shader &myShader, glm::mat4 &model, glm::mat4 &view ...@@ -37,7 +38,10 @@ void send_matrices_to_shader(Shader &myShader, glm::mat4 &model, glm::mat4 &view
ASSUMPTION(glGetError() == GL_NO_ERROR); ASSUMPTION(glGetError() == GL_NO_ERROR);
} }
void gfx_draw(Shader &myShader, Camera &camera, Object &object) void gfx_draw(Shader &myShader,
Camera &camera, Object &object,
Frame &cam_frame, Frame &obj_frame,
CameraController &cam_control, ObjectController &obj_control)
{ {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
...@@ -53,7 +57,8 @@ void gfx_draw(Shader &myShader, Camera &camera, Object &object) ...@@ -53,7 +57,8 @@ void gfx_draw(Shader &myShader, Camera &camera, Object &object)
glm::mat4 model = glm::mat4(1.0f); glm::mat4 model = glm::mat4(1.0f);
//model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f)); //model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::translate(model, object.getPosition()); model *= obj_frame.getRotationMat();
model = glm::translate(model, obj_frame.getPosition());
//model = glm::rotate(model, ((float)SDL_GetTicks() / 1000) * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f)); //model = glm::rotate(model, ((float)SDL_GetTicks() / 1000) * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
// // glm::mat4 view = glm::mat4(1.0f); // // glm::mat4 view = glm::mat4(1.0f);
...@@ -65,7 +70,7 @@ void gfx_draw(Shader &myShader, Camera &camera, Object &object) ...@@ -65,7 +70,7 @@ void gfx_draw(Shader &myShader, Camera &camera, Object &object)
// glm::vec3(0.0f, 0.0f, 0.0f), // glm::vec3(0.0f, 0.0f, 0.0f),
// glm::vec3(0.0f, 1.0f, 0.0f)); // glm::vec3(0.0f, 1.0f, 0.0f));
view = glm::lookAt(camera.getPosVec(), camera.getPosVec() + camera.getFrontVec(), camera.getUpVec()); view = glm::lookAt(cam_frame.getPosition(), cam_frame.getPosition() + cam_control.getFrontVec(), cam_control.getUpVec());
glm::mat4 projection; glm::mat4 projection;
//projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); //projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);
...@@ -89,5 +94,5 @@ void gfx_draw(Shader &myShader, Camera &camera, Object &object) ...@@ -89,5 +94,5 @@ void gfx_draw(Shader &myShader, Camera &camera, Object &object)
GL_UNSIGNED_INT, GL_UNSIGNED_INT,
0); 0);
ASSUMPTION(glGetError() == GL_NO_ERROR); ASSUMPTION(glGetError() == GL_NO_ERROR);
//glBindVertexArray(0); //object.getVAO().Unbind();
} }
...@@ -14,6 +14,9 @@ ...@@ -14,6 +14,9 @@
#include <gfx/ebo.hpp> #include <gfx/ebo.hpp>
#include <gfx/camera.hpp> #include <gfx/camera.hpp>
#include <gfx/object.hpp> #include <gfx/object.hpp>
#include <gfx/frame.hpp>
#include <gfx/cam_control.hpp>
#include <gfx/obj_control.hpp>
#include <gfx/shader.hpp> #include <gfx/shader.hpp>
#include <gfx/render.hpp> #include <gfx/render.hpp>
#include <iostream> #include <iostream>
...@@ -31,12 +34,15 @@ struct Simulator : public osi::Simulator ...@@ -31,12 +34,15 @@ struct Simulator : public osi::Simulator
void process_mouse(); void process_mouse();
private: private:
VAO myVAO;
VBO myVBO;
EBO myEBO;
Camera camera; Camera camera;
Object object; // TO DO: vector of objects Object object; // TO DO: vector of objects
Frame cam_frame;
Frame obj_frame;
CameraController cam_control;
ObjectController obj_control;
Shader myShader; Shader myShader;
}; };
......
...@@ -12,8 +12,8 @@ ...@@ -12,8 +12,8 @@
namespace studio { namespace studio {
// OBJECT // OBJECT
glm::vec3 obj_position = { 1.0f, -1.0f, 0.0f }; glm::vec3 obj_position = { 1.0f, -1.0f, 0.0f }; // frame
// rotation ? TO DO // rotation ? TO DO // frame
std::vector<float> obj_vertices = std::vector<float> obj_vertices =
{ {
// positions // positions
...@@ -57,9 +57,8 @@ Simulator::Simulator() ...@@ -57,9 +57,8 @@ Simulator::Simulator()
// camera frame, object frame, camera, object // camera frame, object frame, camera, object
, myShader{"./data/shaders/basicshader.vert", , myShader{"./data/shaders/basicshader.vert",
"./data/shaders/basicshader.frag"} "./data/shaders/basicshader.frag"}
, camera{{0.0f, 0.0f, 3.0f}, {0.0f, 1.0f, 0.0f}, 60.0f} , camera{60.0f}
, object{{ 0.0f, 0.0f, 0.0f }, , object{{
{
0, 0, 0, 1, -0.2f, 0, 1, 0.2f, 0, 0, 0, 0, 1, -0.2f, 0, 1, 0.2f, 0,
0, 0, 0, -0.2f, 2, 0, 0.2f, 2, 0, 0, 0, 0, -0.2f, 2, 0, 0.2f, 2, 0,
0, 0, 0, -0.2f, 0, 3, 0.2f, 0, 3, 0, 0, 0, -0.2f, 0, 3, 0.2f, 0, 3,
...@@ -69,6 +68,12 @@ Simulator::Simulator() ...@@ -69,6 +68,12 @@ Simulator::Simulator()
3, 4, 5, 3, 4, 5,
6, 7, 8 6, 7, 8
} }
}
, cam_frame{{0.0f, 0.0f, 3.0f}, {0.0f, 90.0f, 0.0f}}
, obj_frame{{ 0.0f, 0.0f, 0.0f }, { -50.0f, 0.0f, 0.0f }}
//, cam_control{cam_frame}
, obj_control{obj_frame}
, cam_control{cam_frame}
// , object{{ 1.0f, -1.0f, 0.0f }, // , object{{ 1.0f, -1.0f, 0.0f },
// { // {
// // positions // // positions
...@@ -96,8 +101,7 @@ Simulator::Simulator() ...@@ -96,8 +101,7 @@ Simulator::Simulator()
// 0, 3, 4, 3, 4, 7, // 0, 3, 4, 3, 4, 7,
// // bottom // // bottom
// 1, 2, 5, 2, 5, 6 // 1, 2, 5, 2, 5, 6
// } // }}
}
{ {
camera.setWindowSize(window().size()); camera.setWindowSize(window().size());
} }
...@@ -126,7 +130,7 @@ void Simulator::present() ...@@ -126,7 +130,7 @@ void Simulator::present()
camera.setWindowSize(window().size()); camera.setWindowSize(window().size());
} }
gfx_draw(myShader, camera, object); gfx_draw(myShader, camera, object, cam_frame, obj_frame, cam_control, obj_control);
} }
void Simulator::process_input() void Simulator::process_input()
...@@ -134,21 +138,16 @@ void Simulator::process_input() ...@@ -134,21 +138,16 @@ void Simulator::process_input()
if (!window().has_keyboard_focus()) if (!window().has_keyboard_focus())
return; return;
camera.setStep(timer().dt()); if (keyboard().down().contains("O"))
{
// Issues not occuring anymore obj_control.setStep(timer().dt());
if (keyboard().down().contains("W")) obj_control.move(keyboard().down());
camera.forward(); }
if (keyboard().down().contains("S")) else
camera.backward(); {
if (keyboard().down().contains("A")) cam_control.setStep(timer().dt());
camera.left(); cam_control.move(keyboard().down());
if (keyboard().down().contains("D")) }
camera.right();
if (keyboard().down().contains("R"))
camera.up();
if (keyboard().down().contains("F"))
camera.down();
int fill_mode = keyboard().down().contains("X") ? GL_FILL : GL_LINE; // TO DO: change int fill_mode = keyboard().down().contains("X") ? GL_FILL : GL_LINE; // TO DO: change
glPolygonMode(GL_FRONT_AND_BACK, fill_mode); glPolygonMode(GL_FRONT_AND_BACK, fill_mode);
...@@ -170,13 +169,13 @@ void Simulator::process_mouse() ...@@ -170,13 +169,13 @@ void Simulator::process_mouse()
xoffset = static_cast<float>(offset.x); xoffset = static_cast<float>(offset.x);
yoffset = static_cast<float>(-offset.y); yoffset = static_cast<float>(-offset.y);
camera.setSens(window().pixel_size_in_meters().x, // TO DO: if window.is_resized()? cam_control.setSens(window().pixel_size_in_meters().x, // TO DO: if window.is_resized()?
window().pixel_size_in_meters().y); window().pixel_size_in_meters().y);
xoffset *= camera.getSens().x; xoffset *= cam_control.getSens().x;
yoffset *= camera.getSens().y; yoffset *= cam_control.getSens().y;
} }
camera.turn(xoffset, yoffset); cam_control.turn(xoffset, yoffset);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment