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

made Camera class, more functionality moved to sim

parent c7f73d72
No related branches found
No related tags found
No related merge requests found
......@@ -17,6 +17,9 @@ add_library(${THIS_TARGET_NAME}
./include/gfx/ebo.hpp
./src/ebo.cpp
./include/gfx/camera.hpp
./src/camera.cpp
# ./include/osi/opengl.hpp
# ./include/osi/gui.hpp
)
......
#ifndef CAMERA_INCLUDED
#define CAMERA_INCLUDED
#include<glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
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);
const float speed = 0.05f; //TO DO: change
float pitch = 0.0f;
float yaw = -90.0f;
public:
void forward();
void backward();
void left();
void right();
void up();
void down();
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();
//void setUpVec();
};
#endif
\ No newline at end of file
......@@ -13,7 +13,7 @@
Shader create_shader_program();
void get_vertices(float*& vertices, unsigned int &size);
void get_indices(unsigned int*& indices, unsigned int &size);
bool create_buffer(unsigned int &VAO, unsigned int &VBO, unsigned int &EBO);
void gfx_draw(Shader &myShader, VAO &VAO);
bool create_buffer(VAO &VAO, VBO &VBO, EBO &EBO);
void gfx_draw(Shader &myShader, VAO &VAO, Camera camera);
#endif
\ No newline at end of file
#include <gfx/camera.hpp>
void Camera::forward()
{
posVec += speed * frontVec;
}
void Camera::backward()
{
posVec -= speed * frontVec;
}
void Camera::left()
{
posVec -= glm::normalize(glm::cross(frontVec, upVec)) * speed;
}
void Camera::right()
{
posVec += glm::normalize(glm::cross(frontVec, upVec)) * speed;
}
void Camera::up()
{
posVec += speed * upVec;
}
void Camera::down()
{
posVec -= speed * upVec;
}
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()
bool create_buffer(unsigned int &VAO, unsigned int &VBO, unsigned int &EBO)
{
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
/** VERTEX INPUT **/
// TRIANGLE
float vertices_traingle[] = {
-0.5f, -0.5f, 0.0f, // left
0.5f, -0.5f, 0.0f, // right
0.0f, 0.5f, 0.0f // top
};
// RECTANGLE
float vertices[] = {
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
// CUBE
float vertices_cube[] =
{
// positions // texture coords
//front
0.2f, 0.2f, 0.0f, // top right
0.2f, -0.2f, 0.0f, // bottom right
-0.2f, -0.2f, 0.0f, // bottom left
-0.2f, 0.2f, 0.0f, // top left
//back
0.2f, 0.2f, -0.4f, // top right
0.2f, -0.2f, -0.4f, // bottom right
-0.2f, -0.2f, -0.4f, // bottom left
-0.2f, 0.2f, -0.4f // top left
};
unsigned int indices_cube[] =
{
// front
0, 1, 3,
1, 2, 3,
// back
4, 5, 7,
5, 6, 7,
// right
0, 1, 4,
1, 4, 5,
// left
2, 3, 7,
2, 6, 7,
// top
0, 3, 4,
3, 4, 7,
// bottom
1, 2, 5,
2, 5, 6
};
/* Order of upcoming buffer creation and binding is important. */
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_cube), vertices_cube, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices_cube), indices_cube, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0); // IS THIS REQUIRED?
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
return true;
}
MOUSE AND KEYBOARD
// GLOBAL VARIABLES
// glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
// glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
// glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
// float pitch;
// float yaw = -90.0f;
// int prev_mouse_x;
// int prev_mouse_y;
// const uint8_t* keyboard;
// void process_input()
// {
// SDL_PumpEvents();
// keyboard = SDL_GetKeyboardState(NULL);
// const float cameraSpeed = 0.05f; // adjust accordingly
// if (keyboard[SDL_SCANCODE_W])
// cameraPos += cameraSpeed * cameraFront;
// if (keyboard[SDL_SCANCODE_S])
// cameraPos -= cameraSpeed * cameraFront;
// if (keyboard[SDL_SCANCODE_A])
// cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
// if (keyboard[SDL_SCANCODE_D])
// cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
// if (keyboard[SDL_SCANCODE_R])
// cameraPos += cameraSpeed * cameraUp;
// if (keyboard[SDL_SCANCODE_F])
// cameraPos -= cameraSpeed * cameraUp;
// }
// void process_mouse()
// {
// //SDL_SetWindowGrab(window, SDL_TRUE); -- currently possible in run.cpp
// SDL_PumpEvents();
// SDL_Event event;
// SDL_PollEvent(&event);
// int curr_mouse_x;
// int curr_mouse_y;
// unsigned int mouse = SDL_GetMouseState(&curr_mouse_x, &curr_mouse_y);
// if (!(mouse & SDL_BUTTON_LMASK))
// {
// prev_mouse_x = curr_mouse_x;
// prev_mouse_y = curr_mouse_y;
// return;
// }
// float xoffset = static_cast<float>(curr_mouse_x - prev_mouse_x);
// float yoffset = static_cast<float>(prev_mouse_y - curr_mouse_y);
// prev_mouse_x = curr_mouse_x;
// prev_mouse_y = curr_mouse_y;
// const float sensitivity = 0.1f;
// xoffset *= sensitivity;
// yoffset *= sensitivity;
// yaw += xoffset;
// pitch += yoffset;
// if(pitch > 89.0f)
// {
// pitch = 89.0f;
// }
// if(pitch < -89.0f)
// {
// pitch = -89.0f;
// }
// 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));
// cameraFront = glm::normalize(front);
// }
\ No newline at end of file
......@@ -93,95 +93,12 @@ void get_indices(unsigned int*& indices, unsigned int &size)
size = sizeof(indices);
}
bool create_buffer(unsigned int &VAO, unsigned int &VBO, unsigned int &EBO)
bool create_buffer(VAO &VAO, VBO &VBO, EBO &EBO)
{
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
/** VERTEX INPUT **/
// TRIANGLE
float vertices_traingle[] = {
-0.5f, -0.5f, 0.0f, // left
0.5f, -0.5f, 0.0f, // right
0.0f, 0.5f, 0.0f // top
};
// RECTANGLE
float vertices[] = {
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
// CUBE
float vertices_cube[] =
{
// positions // texture coords
//front
0.2f, 0.2f, 0.0f, // top right
0.2f, -0.2f, 0.0f, // bottom right
-0.2f, -0.2f, 0.0f, // bottom left
-0.2f, 0.2f, 0.0f, // top left
//back
0.2f, 0.2f, -0.4f, // top right
0.2f, -0.2f, -0.4f, // bottom right
-0.2f, -0.2f, -0.4f, // bottom left
-0.2f, 0.2f, -0.4f // top left
};
unsigned int indices_cube[] =
{
// front
0, 1, 3,
1, 2, 3,
// back
4, 5, 7,
5, 6, 7,
// right
0, 1, 4,
1, 4, 5,
// left
2, 3, 7,
2, 6, 7,
// top
0, 3, 4,
3, 4, 7,
// bottom
1, 2, 5,
2, 5, 6
};
/* Order of upcoming buffer creation and binding is important. */
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_cube), vertices_cube, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices_cube), indices_cube, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0); // IS THIS REQUIRED?
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
VAO.LinkVBO(VBO);
VAO.Unbind(); // CUBE WOULD NOT SPAWN WITHOUT THIS WHYYY
//VBO.Unbind();
//EBO.Unbind();
return true;
}
......@@ -197,86 +114,7 @@ void send_matrices_to_shader(Shader &myShader, glm::mat4 &model, glm::mat4 &view
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
}
// GLOBAL VARIABLES
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
float pitch;
float yaw = -90.0f;
int prev_mouse_x;
int prev_mouse_y;
const Uint8* keyboard;
void process_input()
{
SDL_PumpEvents();
keyboard = SDL_GetKeyboardState(NULL);
const float cameraSpeed = 0.05f; // adjust accordingly
if (keyboard[SDL_SCANCODE_W])
cameraPos += cameraSpeed * cameraFront;
if (keyboard[SDL_SCANCODE_S])
cameraPos -= cameraSpeed * cameraFront;
if (keyboard[SDL_SCANCODE_A])
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keyboard[SDL_SCANCODE_D])
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keyboard[SDL_SCANCODE_R])
cameraPos += cameraSpeed * cameraUp;
if (keyboard[SDL_SCANCODE_F])
cameraPos -= cameraSpeed * cameraUp;
}
void process_mouse()
{
//SDL_SetWindowGrab(window, SDL_TRUE); -- currently possible in run.cpp
SDL_PumpEvents();
SDL_Event event;
SDL_PollEvent(&event);
int curr_mouse_x;
int curr_mouse_y;
unsigned int mouse = SDL_GetMouseState(&curr_mouse_x, &curr_mouse_y);
if (!(mouse & SDL_BUTTON_LMASK))
{
prev_mouse_x = curr_mouse_x;
prev_mouse_y = curr_mouse_y;
return;
}
float xoffset = static_cast<float>(curr_mouse_x - prev_mouse_x);
float yoffset = static_cast<float>(prev_mouse_y - curr_mouse_y);
prev_mouse_x = curr_mouse_x;
prev_mouse_y = curr_mouse_y;
const float sensitivity = 0.1f;
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if(pitch > 89.0f)
{
pitch = 89.0f;
}
if(pitch < -89.0f)
{
pitch = -89.0f;
}
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));
cameraFront = glm::normalize(front);
}
void gfx_draw(Shader &myShader, VAO &myVAO)
void gfx_draw(Shader &myShader, VAO &VAO, Camera camera)
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
......@@ -286,7 +124,6 @@ void gfx_draw(Shader &myShader, VAO &myVAO)
// glm::mat4 transform = glm::mat4(1.0f);
// transform = glm::rotate(transform, glm::radians(90.0f), glm::vec3(0.0, 0.0, 1.0));
// transform = glm::scale(transform, glm::vec3(0.5, 0.5, 0.5));
// unsigned int transformLoc = glGetUniformLocation(myShader.ID, "transform");
// glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
......@@ -303,10 +140,7 @@ void gfx_draw(Shader &myShader, VAO &myVAO)
// glm::vec3(0.0f, 0.0f, 0.0f),
// glm::vec3(0.0f, 1.0f, 0.0f));
process_input();
process_mouse();
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
view = glm::lookAt(camera.getPosVec(), camera.getPosVec() + camera.getFrontVec(), camera.getUpVec());
glm::mat4 projection;
projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);
......@@ -315,11 +149,8 @@ void gfx_draw(Shader &myShader, VAO &myVAO)
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int fill_mode = keyboard[SDL_SCANCODE_X] ? GL_FILL : GL_LINE;
glPolygonMode(GL_FRONT_AND_BACK, fill_mode);
myVAO.Bind();
VAO.Bind();
//glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
//glBindVertexArray(0);
......
......@@ -6,11 +6,13 @@
#include <osi/opengl.hpp>
#include <osi/gui.hpp>
#include <SDL2/SDL.h>
#include <glad/glad.h>
#include <gfx/vao.hpp>
#include <gfx/vbo.hpp>
#include <gfx/ebo.hpp>
#include <gfx/camera.hpp>
#include <gfx/shader.hpp>
#include <gfx/render.hpp>
#include <iostream>
......@@ -24,6 +26,10 @@ struct Simulator : public osi::Simulator
void update() override;
void present() override;
void process_input();
void process_mouse();
private:
//unsigned int VBO, VAO, EBO; //predelat na vlastni tridy + procedural gen
......@@ -32,6 +38,11 @@ struct Simulator : public osi::Simulator
VBO myVBO;
EBO myEBO;
Camera camera;
int prev_mouse_x;
int prev_mouse_y;
const uint8_t* keyboard;
Shader myShader;
};
......
......@@ -47,24 +47,12 @@ Simulator::Simulator()
, myEBO{EBO{}}
, myShader{create_shader_program()}
{
// unsigned int v_size;
// unsigned int i_size;
// float* vertices;
// unsigned int* indices;
// get_vertices(vertices, v_size);
// get_indices(indices, i_size);
//myVAO.Bind();
// myVBO = VBO{vertices_cube, sizeof(vertices_cube)};
// myEBO = EBO{indices_cube, sizeof(indices_cube)};
myVBO.SendData(vertices_cube, sizeof(vertices_cube));
myEBO.SendData(indices_cube, sizeof(indices_cube));
myVAO.LinkVBO(myVBO);
myVAO.Unbind(); // CUBE WOULD NOT SPAWN WITHOUT THIS WHYYY
myVBO.Unbind();
myEBO.Unbind();
//create_buffer(myVAO, myVBO, myEBO); //bool
create_buffer(myVAO, myVBO, myEBO); //bool
}
Simulator::~Simulator()
......@@ -77,6 +65,8 @@ Simulator::~Simulator()
void Simulator::update()
{
// Update the state (i.e., transform objects, create/destroy objects, ... )
process_input();
process_mouse();
}
void Simulator::present()
......@@ -88,7 +78,64 @@ void Simulator::present()
ImGui::Text("Hello, world!");
ImGui::End();
gfx_draw(myShader, myVAO);
gfx_draw(myShader, myVAO, camera);
}
void Simulator::process_input()
{
SDL_PumpEvents();
keyboard = SDL_GetKeyboardState(NULL);
const float cameraSpeed = 0.05f; // adjust accordingly
if (keyboard[SDL_SCANCODE_W])
camera.forward();
if (keyboard[SDL_SCANCODE_S])
camera.backward();
if (keyboard[SDL_SCANCODE_A])
camera.left();
if (keyboard[SDL_SCANCODE_D])
camera.right();
if (keyboard[SDL_SCANCODE_R])
camera.up();
if (keyboard[SDL_SCANCODE_F])
camera.down();
int fill_mode = keyboard[SDL_SCANCODE_X] ? GL_FILL : GL_LINE;
glPolygonMode(GL_FRONT_AND_BACK, fill_mode);
}
void Simulator::process_mouse()
{
//SDL_SetWindowGrab(window, SDL_TRUE); -- currently possible in run.cpp
SDL_PumpEvents();
SDL_Event event;
SDL_PollEvent(&event);
int curr_mouse_x;
int curr_mouse_y;
unsigned int mouse = SDL_GetMouseState(&curr_mouse_x, &curr_mouse_y);
if (!(mouse & SDL_BUTTON_LMASK))
{
prev_mouse_x = curr_mouse_x;
prev_mouse_y = curr_mouse_y;
return;
}
float xoffset = static_cast<float>(curr_mouse_x - prev_mouse_x);
float yoffset = static_cast<float>(prev_mouse_y - curr_mouse_y);
prev_mouse_x = curr_mouse_x;
prev_mouse_y = curr_mouse_y;
const float sensitivity = 0.1f;
xoffset *= sensitivity;
yoffset *= sensitivity;
camera.setYaw(xoffset);
camera.setPitch(yoffset);
camera.setFrontVec();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment