ADD: Apply View matrices
This commit is contained in:
parent
0871202db9
commit
3e37c53779
@ -47,7 +47,11 @@ add_executable(Dicewars_Siedler src/main.cpp
|
||||
src/engine/renderer/model/TexturedModel.h
|
||||
src/engine/toolbox/MathUtils.h
|
||||
src/engine/layer/entities/Entity.cpp
|
||||
src/engine/layer/entities/Entity.h)
|
||||
src/engine/layer/entities/Entity.h
|
||||
src/engine/layer/entities/Camera.cpp
|
||||
src/engine/layer/entities/Camera.h
|
||||
src/engine/platform/glfw/InputManager.cpp
|
||||
src/engine/platform/glfw/InputManager.h)
|
||||
|
||||
target_include_directories(Dicewars_Siedler PRIVATE
|
||||
lib/glfw/include
|
||||
|
||||
@ -7,9 +7,10 @@ out vec2 pass_textureCoords;
|
||||
|
||||
uniform mat4 transformationMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projectionMatrix * transformationMatrix * vec4(position, 1.0f);
|
||||
gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position, 1.0f);
|
||||
pass_textureCoords = textureCoords;
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
#include "GameLayer.h"
|
||||
|
||||
#include "../platform/glfw/InputManager.h"
|
||||
#include "../renderer/Renderer.h"
|
||||
#include "../renderer/model/TexturedModel.h"
|
||||
#include "../renderer/textures/ModelTexture.h"
|
||||
@ -37,6 +38,7 @@ void GameLayer::onAttach()
|
||||
ModelTexture texture = ModelTexture(loader.loadTexture("assets/textures/texture.png"));
|
||||
texturedModel = TexturedModel(std::make_shared<RawModel>(model), std::make_shared<ModelTexture>(texture));
|
||||
entity = std::make_unique<Entity>(Entity(std::make_shared<TexturedModel>(texturedModel), glm::vec3(0,0,-1), 0,0,0, 1.f));
|
||||
camera = std::make_unique<Camera>();
|
||||
}
|
||||
|
||||
void GameLayer::onDetach()
|
||||
@ -46,8 +48,14 @@ void GameLayer::onDetach()
|
||||
|
||||
void GameLayer::onUpdate()
|
||||
{
|
||||
entity->increasePosition(0,0,-0.002f);
|
||||
renderer.renderFrame(*entity);
|
||||
glm::vec3 moveDir = glm::vec3(0,0,0);
|
||||
if (InputManager::isKeyPressed(GLFW_KEY_W)) moveDir.z -= 1.0f;
|
||||
if (InputManager::isKeyPressed(GLFW_KEY_S)) moveDir.z += 1.0f;
|
||||
if (InputManager::isKeyPressed(GLFW_KEY_A)) moveDir.x -= 1.0f;
|
||||
if (InputManager::isKeyPressed(GLFW_KEY_D)) moveDir.x += 1.0f;
|
||||
|
||||
camera->move(moveDir, 0.02f);
|
||||
renderer.renderFrame(*entity, *camera);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
#include "../renderer/Renderer.h"
|
||||
#include "../renderer/loader/Loader.h"
|
||||
#include "../renderer/model/TexturedModel.h"
|
||||
#include "entities/Camera.h"
|
||||
|
||||
|
||||
class GameLayer: public Layer {
|
||||
@ -23,6 +24,7 @@ private:
|
||||
Loader loader;
|
||||
TexturedModel texturedModel;
|
||||
std::unique_ptr<Entity> entity;
|
||||
std::unique_ptr<Camera> camera;
|
||||
Renderer renderer;
|
||||
};
|
||||
|
||||
|
||||
5
src/engine/layer/entities/Camera.cpp
Normal file
5
src/engine/layer/entities/Camera.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by sebastian on 07.02.26.
|
||||
//
|
||||
|
||||
#include "Camera.h"
|
||||
40
src/engine/layer/entities/Camera.h
Normal file
40
src/engine/layer/entities/Camera.h
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// Created by sebastian on 07.02.26.
|
||||
//
|
||||
|
||||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
#include <cstdio>
|
||||
|
||||
#include "glm/vec3.hpp"
|
||||
|
||||
|
||||
class Camera {
|
||||
private:
|
||||
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
float pitch = 0.0f;
|
||||
float yaw = 0.0f;
|
||||
float roll = 0.0f;
|
||||
|
||||
public:
|
||||
Camera() = default;
|
||||
void move(const glm::vec3 moveDir, const float moveSpeed) {
|
||||
if (moveDir != glm::vec3(0.0f)) {
|
||||
printf("moveDir: %f %f %f\n", moveDir.x, moveDir.y, moveDir.z);
|
||||
}
|
||||
position += moveDir * moveSpeed;
|
||||
}
|
||||
[[nodiscard]] glm::vec3 getPosition() const {return position;}
|
||||
void setPosition(glm::vec3 position) {this->position = position;}
|
||||
[[nodiscard]] float getPitch() const {return pitch;}
|
||||
void setPitch(float pitch) {this->pitch = pitch;}
|
||||
[[nodiscard]] float getYaw() const {return yaw;}
|
||||
void setYaw(float yaw) {this->yaw = yaw;}
|
||||
[[nodiscard]] float getRoll() const {return roll;}
|
||||
void setRoll(const float roll) {this->roll = roll;}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //CAMERA_H
|
||||
30
src/engine/platform/glfw/InputManager.cpp
Normal file
30
src/engine/platform/glfw/InputManager.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by sebastian on 07.02.26.
|
||||
//
|
||||
|
||||
#include "InputManager.h"
|
||||
|
||||
#include "../../core/Application.h"
|
||||
#include "GLFW/glfw3.h"
|
||||
|
||||
bool InputManager::isKeyPressed(int keycode) {
|
||||
auto window = static_cast<GLFWwindow*>(Application::getInstance().getWindow().GetNativeWindow());
|
||||
return glfwGetKey(window, keycode) == GLFW_PRESS;
|
||||
}
|
||||
|
||||
bool InputManager::isMouseButtonPressed(int button) {
|
||||
auto window = static_cast<GLFWwindow*>(Application::getInstance().getWindow().GetNativeWindow());
|
||||
return glfwGetMouseButton(window, button) == GLFW_PRESS;
|
||||
}
|
||||
|
||||
glm::vec2 InputManager::getMouseDelta() {
|
||||
static double lastX = 0, lastY = 0;
|
||||
double xpos, ypos;
|
||||
auto window = static_cast<GLFWwindow*>(Application::getInstance().getWindow().GetNativeWindow());
|
||||
glfwGetCursorPos(window, &xpos, &ypos);
|
||||
glm::vec2 delta(xpos - lastX, ypos - lastY);
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
return delta;
|
||||
}
|
||||
|
||||
20
src/engine/platform/glfw/InputManager.h
Normal file
20
src/engine/platform/glfw/InputManager.h
Normal file
@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by sebastian on 07.02.26.
|
||||
//
|
||||
|
||||
#ifndef INPUTMANAGER_H
|
||||
#define INPUTMANAGER_H
|
||||
#include "glm/vec2.hpp"
|
||||
|
||||
|
||||
class InputManager {
|
||||
public:
|
||||
static bool isKeyPressed(int keycode);
|
||||
static bool isMouseButtonPressed(int button);
|
||||
glm::vec2 getMouseDelta();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //INPUTMANAGER_H
|
||||
@ -10,13 +10,18 @@
|
||||
#include "glm/ext/matrix_clip_space.hpp"
|
||||
#include "model/TexturedModel.h"
|
||||
|
||||
void Renderer::prepare() {
|
||||
void Renderer::prepare(const Camera& camera) {
|
||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
const glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera);
|
||||
staticShader.start();
|
||||
staticShader.loadViewMatrix(viewMatrix);
|
||||
staticShader.stop();
|
||||
}
|
||||
|
||||
void Renderer::renderFrame(const Entity &entity) {
|
||||
prepare();
|
||||
void Renderer::renderFrame(const Entity &entity, const Camera& camera) {
|
||||
prepare(camera);
|
||||
staticShader.start();
|
||||
renderRawModel(entity);
|
||||
staticShader.stop();
|
||||
|
||||
@ -10,6 +10,8 @@
|
||||
#include "shaders/StaticShader.h"
|
||||
|
||||
|
||||
class Camera;
|
||||
|
||||
class Renderer {
|
||||
public:
|
||||
Renderer() : projectionMatrix(createProjectionMatrix()) {
|
||||
@ -17,8 +19,8 @@ public:
|
||||
staticShader.loadProjectionMatrix(projectionMatrix);
|
||||
staticShader.stop();
|
||||
};
|
||||
void prepare();
|
||||
void renderFrame(const Entity &entity);
|
||||
void prepare(const Camera& camera);
|
||||
void renderFrame(const ::Entity &entity, const Camera& camera);
|
||||
private:
|
||||
void renderRawModel(const Entity &entity);
|
||||
StaticShader staticShader;
|
||||
|
||||
@ -17,6 +17,10 @@ void StaticShader::loadProjectionMatrix(glm::mat4 matrix) {
|
||||
loadMatrix(location_projectionMatrix, matrix);
|
||||
}
|
||||
|
||||
void StaticShader::loadViewMatrix(glm::mat4 matrix) {
|
||||
loadMatrix(location_viewMatrix, matrix);
|
||||
}
|
||||
|
||||
void StaticShader::bindAttributes() const {
|
||||
bindAttribute(0, "position");
|
||||
bindAttribute(1, "textureCoords");
|
||||
@ -25,6 +29,7 @@ void StaticShader::bindAttributes() const {
|
||||
void StaticShader::getAllUniformLocations() {
|
||||
location_transformationMatrix = getUniformLocation("transformationMatrix");
|
||||
location_projectionMatrix = getUniformLocation("projectionMatrix");
|
||||
location_viewMatrix = getUniformLocation("viewMatrix");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -13,12 +13,14 @@ public:
|
||||
StaticShader();
|
||||
void loadTransformationMatrix(glm::mat4 matrix);
|
||||
void loadProjectionMatrix(glm::mat4 matrix);
|
||||
void loadViewMatrix(glm::mat4 matrix);
|
||||
private:
|
||||
inline static const std::string VERTEX_FILE = "assets/shaders/vertexShader.glsl";
|
||||
inline static const std::string FRAGMENT_FILE = "assets/shaders/fragmentShader.glsl";
|
||||
|
||||
int location_transformationMatrix;
|
||||
int location_projectionMatrix;
|
||||
int location_viewMatrix;
|
||||
|
||||
protected:
|
||||
void bindAttributes() const override;
|
||||
|
||||
@ -4,10 +4,14 @@
|
||||
|
||||
#ifndef MATHUTILS_H
|
||||
#define MATHUTILS_H
|
||||
#include "../layer/entities/Camera.h"
|
||||
#include "glm/mat4x4.hpp"
|
||||
#include "glm/vec3.hpp"
|
||||
#include "glm/ext/matrix_transform.hpp"
|
||||
|
||||
|
||||
class Camera;
|
||||
|
||||
namespace MathUtils {
|
||||
inline static glm::mat4 createTransformationMatrix(const glm::vec3 translation, const float rx, const float ry, const float rz, const float scale) {
|
||||
auto matrix = glm::mat4(1.0f);
|
||||
@ -18,6 +22,19 @@ namespace MathUtils {
|
||||
matrix = glm::scale(matrix, glm::vec3(scale));
|
||||
return matrix;
|
||||
}
|
||||
|
||||
inline static glm::mat4 createViewMatrix(const Camera& camera) {
|
||||
const glm::vec3& pos = camera.getPosition();
|
||||
|
||||
glm::vec3 front;
|
||||
front.x = sin(glm::radians(camera.getYaw())) * cos(glm::radians(camera.getPitch()));
|
||||
front.y = sin(glm::radians(camera.getPitch()));
|
||||
front.z = -cos(glm::radians(camera.getYaw())) * cos(glm::radians(camera.getPitch()));
|
||||
front = glm::normalize(front);
|
||||
|
||||
glm::vec3 up(0.0f, 1.0f, 0.0f);
|
||||
return glm::lookAt(pos, pos + front, up);
|
||||
}
|
||||
}
|
||||
|
||||
#endif //MATHUTILS_H
|
||||
|
||||
Loading…
Reference in New Issue
Block a user