Refactor events.
This commit is contained in:
@@ -9,5 +9,6 @@ ColumnLimit: '120'
|
||||
IndentWidth: '4'
|
||||
Language: Cpp
|
||||
PointerAlignment: Right
|
||||
SortIncludes: false
|
||||
|
||||
...
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "Camera.h"
|
||||
#include "eventActions.h"
|
||||
#include "definitions/eventActions.h"
|
||||
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
@@ -40,43 +40,44 @@ void Camera::lookForward()
|
||||
viewMatrix = glm::lookAt(position, position + frontVec, upVec);
|
||||
}
|
||||
|
||||
void Camera::updatePositionFromKeyboardInput(bool *cameraActionRegister, float deltaTime)
|
||||
void Camera::updatePositionFromKeyboardInput(const CameraActionMap &cameraActionMap, float deltaTime)
|
||||
{
|
||||
glm::vec3 frontVecWithoutY = glm::vec3(frontVec.x, 0.0f, frontVec.z);
|
||||
|
||||
glm::vec3 deltaPos = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
if (cameraActionRegister[cameraForward]) {
|
||||
if (cameraActionMap.at(CameraAction::Forward)) {
|
||||
deltaPos += speed * deltaTime * glm::normalize(frontVecWithoutY);
|
||||
}
|
||||
if (cameraActionRegister[cameraBackward]) {
|
||||
if (cameraActionMap.at(CameraAction::Backward)) {
|
||||
deltaPos -= speed * deltaTime * glm::normalize(frontVecWithoutY);
|
||||
}
|
||||
if (cameraActionRegister[cameraLeft]) {
|
||||
if (cameraActionMap.at(CameraAction::Left)) {
|
||||
deltaPos -= speed * deltaTime * glm::normalize(glm::cross(frontVec, upVec));
|
||||
}
|
||||
if (cameraActionRegister[cameraRight]) {
|
||||
if (cameraActionMap.at(CameraAction::Right)) {
|
||||
deltaPos += speed * deltaTime * glm::normalize(glm::cross(frontVec, upVec));
|
||||
}
|
||||
if (cameraActionRegister[cameraUp]) {
|
||||
if (cameraActionMap.at(CameraAction::Up)) {
|
||||
deltaPos += speed * deltaTime * upVec;
|
||||
}
|
||||
if (cameraActionRegister[cameraDown]) {
|
||||
if (cameraActionMap.at(CameraAction::Down)) {
|
||||
deltaPos -= speed * deltaTime * upVec;
|
||||
}
|
||||
|
||||
position += deltaPos;
|
||||
}
|
||||
|
||||
void Camera::updateDirectionFromMouseInput(double *cameraMouseActionRegister)
|
||||
void Camera::updateDirectionFromMouseInput(const CameraMouseActionMap &cameraMouseActionMap)
|
||||
{
|
||||
|
||||
if (cameraMouseActionRegister[cameraMouseDeltaX] == 0 && cameraMouseActionRegister[cameraMouseDeltaY] == 0) {
|
||||
if (cameraMouseActionMap.at(CameraMouseAction::DeltaX) == 0 &&
|
||||
cameraMouseActionMap.at(CameraMouseAction::DeltaY) == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
yaw += cameraMouseActionRegister[cameraMouseDeltaX];
|
||||
pitch += cameraMouseActionRegister[cameraMouseDeltaY];
|
||||
yaw += cameraMouseActionMap.at(CameraMouseAction::DeltaX);
|
||||
pitch += cameraMouseActionMap.at(CameraMouseAction::DeltaY);
|
||||
|
||||
if (pitch > 89.0f) {
|
||||
pitch = 89.0f;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "EventHandler.h"
|
||||
#include "ShaderProgram.h"
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
@@ -11,8 +12,8 @@ public:
|
||||
|
||||
void updateVPM();
|
||||
void updateAspectRatio(float aspectRatio);
|
||||
void updatePositionFromKeyboardInput(bool *actionCameraRegister, float deltaTime);
|
||||
void updateDirectionFromMouseInput(double *cameraMouseActionRegister);
|
||||
void updatePositionFromKeyboardInput(const CameraActionMap &cameraActionMap, float deltaTime);
|
||||
void updateDirectionFromMouseInput(const CameraMouseActionMap &cameraMouseActionMap);
|
||||
|
||||
void translate(glm::vec3 translateVector);
|
||||
|
||||
|
||||
@@ -167,17 +167,17 @@ void Controller::run()
|
||||
// --- Check events, handle input ---
|
||||
gameEventHandler->handleEvents();
|
||||
|
||||
camera->updatePositionFromKeyboardInput(gameEventHandler->getCameraActionRegister(), deltaTime);
|
||||
camera->updatePositionFromKeyboardInput(gameEventHandler->getCameraActionMap(), deltaTime);
|
||||
if (gameWindow->getMouseIsCatched()) {
|
||||
camera->updateDirectionFromMouseInput(gameEventHandler->getCursorDelta());
|
||||
camera->updateDirectionFromMouseInput(gameEventHandler->getCameraMouseActionMap());
|
||||
}
|
||||
|
||||
menu->writeWindowActions(gameEventHandler->getWindowActionRegister());
|
||||
gameWindow->handleWindowActionRegister(gameEventHandler->getWindowActionRegister());
|
||||
menu->writeWindowActions(gameEventHandler->getWindowActionMap());
|
||||
gameWindow->handleWindowActionMap(gameEventHandler->getWindowActionMap());
|
||||
|
||||
// Handle widget pressed event only when a screen is currently active
|
||||
if (menu->getActiveScreen())
|
||||
menu->handleMouseButtonActionRegister(gameEventHandler->getMouseButtonActionRegister(), gameWindow);
|
||||
menu->handleMouseButtonActionMap(gameEventHandler->getMouseButtonActionMap(), gameWindow);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "Camera.h"
|
||||
#include "Entity.h"
|
||||
#include "EventHandler.h"
|
||||
@@ -11,7 +9,6 @@
|
||||
#include "ShaderProgram.h"
|
||||
#include "Window.h"
|
||||
#include "World.h"
|
||||
#include "defines.h"
|
||||
|
||||
class Controller
|
||||
{
|
||||
|
||||
@@ -2,10 +2,20 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
double EventHandler::cameraMouseActionRegister[CAMERA_MOUSE_ACTION_NUM_ITEMS] = {0.0, 0.0};
|
||||
bool EventHandler::cameraActionRegister[CAMERA_ACTION_NUM_ITEMS] = {0};
|
||||
bool EventHandler::mouseButtonActionRegister[MOUSE_BUTTON_ACTION_NUM_ITEMS] = {0};
|
||||
bool EventHandler::windowActionRegister[WINDOW_ACTION_NUM_ITEMS] = {0};
|
||||
CameraActionMap EventHandler::cameraActionMap = {{CameraAction::Forward, false}, {CameraAction::Backward, false},
|
||||
{CameraAction::Up, false}, {CameraAction::Down, false},
|
||||
{CameraAction::Left, false}, {CameraAction::Right, false}};
|
||||
|
||||
CameraMouseActionMap EventHandler::cameraMouseActionMap = {{CameraMouseAction::DeltaX, 0.0},
|
||||
{CameraMouseAction::DeltaY, 0.0}};
|
||||
|
||||
WindowActionMap EventHandler::windowActionMap = {{WindowAction::WireFrameToggle, false},
|
||||
{WindowAction::MouseCatchToggle, false},
|
||||
{WindowAction::WindowShouldClose, false}};
|
||||
|
||||
MouseButtonActionMap EventHandler::mouseButtonActionMap = {{MouseButtonAction::LeftClicked, false},
|
||||
{MouseButtonAction::RightClicked, false},
|
||||
{MouseButtonAction::MiddleClicked, false}};
|
||||
|
||||
bool EventHandler::firstMouseInput = 1;
|
||||
float EventHandler::mouseSensitivity = 0.15f;
|
||||
@@ -25,10 +35,17 @@ void EventHandler::handleEvents()
|
||||
|
||||
void EventHandler::clearActionRegisters()
|
||||
{
|
||||
// std::fill_n(cameraActionRegister, CAMERA_ACTION_NUM_ITEMS, 0);
|
||||
std::fill_n(cameraMouseActionRegister, CAMERA_MOUSE_ACTION_NUM_ITEMS, 0.0);
|
||||
std::fill_n(windowActionRegister, WINDOW_ACTION_NUM_ITEMS, 0);
|
||||
std::fill_n(mouseButtonActionRegister, MOUSE_BUTTON_ACTION_NUM_ITEMS, 0);
|
||||
for (auto &element : cameraMouseActionMap) {
|
||||
element.second = 0.0;
|
||||
}
|
||||
|
||||
for (auto &element : windowActionMap) {
|
||||
element.second = 0.0;
|
||||
}
|
||||
|
||||
for (auto &element : mouseButtonActionMap) {
|
||||
element.second = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
void EventHandler::key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
@@ -39,55 +56,55 @@ void EventHandler::key_callback(GLFWwindow *window, int key, int scancode, int a
|
||||
(void)mods;
|
||||
|
||||
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
|
||||
windowActionRegister[windowActions::windowShouldClose] = 1;
|
||||
windowActionMap[WindowAction::WindowShouldClose] = 1;
|
||||
}
|
||||
|
||||
if (key == GLFW_KEY_O && action == GLFW_PRESS) {
|
||||
windowActionRegister[windowActions::wireFrameToggle] = 1;
|
||||
windowActionMap[WindowAction::WireFrameToggle] = 1;
|
||||
}
|
||||
if (key == GLFW_KEY_LEFT_CONTROL && action == GLFW_PRESS) {
|
||||
windowActionRegister[windowActions::mouseCatchToggle] = 1;
|
||||
windowActionMap[WindowAction::MouseCatchToggle] = 1;
|
||||
firstMouseInput = 1;
|
||||
}
|
||||
|
||||
// Movement press
|
||||
if (key == GLFW_KEY_W && action == GLFW_PRESS) {
|
||||
cameraActionRegister[cameraActions::cameraForward] = 1;
|
||||
cameraActionMap[CameraAction::Forward] = 1;
|
||||
}
|
||||
if (key == GLFW_KEY_S && action == GLFW_PRESS) {
|
||||
cameraActionRegister[cameraActions::cameraBackward] = 1;
|
||||
cameraActionMap[CameraAction::Backward] = 1;
|
||||
}
|
||||
if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) {
|
||||
cameraActionRegister[cameraActions::cameraUp] = 1;
|
||||
cameraActionMap[CameraAction::Up] = 1;
|
||||
}
|
||||
if (key == GLFW_KEY_LEFT_SHIFT && action == GLFW_PRESS) {
|
||||
cameraActionRegister[cameraActions::cameraDown] = 1;
|
||||
cameraActionMap[CameraAction::Down] = 1;
|
||||
}
|
||||
if (key == GLFW_KEY_A && action == GLFW_PRESS) {
|
||||
cameraActionRegister[cameraActions::cameraLeft] = 1;
|
||||
cameraActionMap[CameraAction::Left] = 1;
|
||||
}
|
||||
if (key == GLFW_KEY_D && action == GLFW_PRESS) {
|
||||
cameraActionRegister[cameraActions::cameraRight] = 1;
|
||||
cameraActionMap[CameraAction::Right] = 1;
|
||||
}
|
||||
|
||||
// Movement release
|
||||
if (key == GLFW_KEY_W && action == GLFW_RELEASE) {
|
||||
cameraActionRegister[cameraActions::cameraForward] = 0;
|
||||
cameraActionMap[CameraAction::Forward] = 0;
|
||||
}
|
||||
if (key == GLFW_KEY_S && action == GLFW_RELEASE) {
|
||||
cameraActionRegister[cameraActions::cameraBackward] = 0;
|
||||
cameraActionMap[CameraAction::Backward] = 0;
|
||||
}
|
||||
if (key == GLFW_KEY_SPACE && action == GLFW_RELEASE) {
|
||||
cameraActionRegister[cameraActions::cameraUp] = 0;
|
||||
cameraActionMap[CameraAction::Up] = 0;
|
||||
}
|
||||
if (key == GLFW_KEY_LEFT_SHIFT && action == GLFW_RELEASE) {
|
||||
cameraActionRegister[cameraActions::cameraDown] = 0;
|
||||
cameraActionMap[CameraAction::Down] = 0;
|
||||
}
|
||||
if (key == GLFW_KEY_A && action == GLFW_RELEASE) {
|
||||
cameraActionRegister[cameraActions::cameraLeft] = 0;
|
||||
cameraActionMap[CameraAction::Left] = 0;
|
||||
}
|
||||
if (key == GLFW_KEY_D && action == GLFW_RELEASE) {
|
||||
cameraActionRegister[cameraActions::cameraRight] = 0;
|
||||
cameraActionMap[CameraAction::Right] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,8 +131,8 @@ void EventHandler::mouse_callback(GLFWwindow *window, double xpos, double ypos)
|
||||
deltaCursorPosX *= mouseSensitivity;
|
||||
deltaCursorPosY *= mouseSensitivity;
|
||||
|
||||
cameraMouseActionRegister[cameraMouseActions::cameraMouseDeltaX] += deltaCursorPosX;
|
||||
cameraMouseActionRegister[cameraMouseActions::cameraMouseDeltaY] += deltaCursorPosY;
|
||||
cameraMouseActionMap[CameraMouseAction::DeltaX] += deltaCursorPosX;
|
||||
cameraMouseActionMap[CameraMouseAction::DeltaY] += deltaCursorPosY;
|
||||
}
|
||||
|
||||
void EventHandler::mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
|
||||
@@ -124,31 +141,31 @@ void EventHandler::mouse_button_callback(GLFWwindow *window, int button, int act
|
||||
(void)mods;
|
||||
|
||||
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
|
||||
mouseButtonActionRegister[mouseButtonActions::leftClicked] = true;
|
||||
mouseButtonActionMap[MouseButtonAction::LeftClicked] = true;
|
||||
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
|
||||
mouseButtonActionRegister[mouseButtonActions::rightClicked] = true;
|
||||
mouseButtonActionMap[MouseButtonAction::RightClicked] = true;
|
||||
if (button == GLFW_MOUSE_BUTTON_MIDDLE && action == GLFW_PRESS)
|
||||
mouseButtonActionRegister[mouseButtonActions::middleClicked] = true;
|
||||
mouseButtonActionMap[MouseButtonAction::MiddleClicked] = true;
|
||||
}
|
||||
|
||||
bool *EventHandler::getCameraActionRegister()
|
||||
const CameraActionMap &EventHandler::getCameraActionMap() const
|
||||
{
|
||||
return cameraActionRegister;
|
||||
return cameraActionMap;
|
||||
}
|
||||
|
||||
bool *EventHandler::getWindowActionRegister()
|
||||
WindowActionMap &EventHandler::getWindowActionMap() const
|
||||
{
|
||||
return windowActionRegister;
|
||||
return windowActionMap;
|
||||
}
|
||||
|
||||
bool *EventHandler::getMouseButtonActionRegister()
|
||||
const MouseButtonActionMap &EventHandler::getMouseButtonActionMap() const
|
||||
{
|
||||
return mouseButtonActionRegister;
|
||||
return mouseButtonActionMap;
|
||||
}
|
||||
|
||||
double *EventHandler::getCursorDelta()
|
||||
const CameraMouseActionMap &EventHandler::getCameraMouseActionMap() const
|
||||
{
|
||||
return cameraMouseActionRegister;
|
||||
return cameraMouseActionMap;
|
||||
}
|
||||
|
||||
void EventHandler::setFirstMouseInput(bool val)
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "eventActions.h"
|
||||
#include "definitions/enumHash.h"
|
||||
#include "definitions/eventActions.h"
|
||||
|
||||
typedef std::unordered_map<CameraAction, bool, EnumClassHash> CameraActionMap;
|
||||
typedef std::unordered_map<CameraMouseAction, double, EnumClassHash> CameraMouseActionMap;
|
||||
typedef std::unordered_map<WindowAction, bool, EnumClassHash> WindowActionMap;
|
||||
typedef std::unordered_map<MouseButtonAction, bool, EnumClassHash> MouseButtonActionMap;
|
||||
|
||||
class EventHandler
|
||||
{
|
||||
@@ -12,10 +19,10 @@ public:
|
||||
|
||||
void handleEvents();
|
||||
|
||||
bool *getCameraActionRegister();
|
||||
bool *getWindowActionRegister();
|
||||
bool *getMouseButtonActionRegister();
|
||||
double *getCursorDelta();
|
||||
WindowActionMap &getWindowActionMap() const;
|
||||
const CameraActionMap &getCameraActionMap() const;
|
||||
const MouseButtonActionMap &getMouseButtonActionMap() const;
|
||||
const CameraMouseActionMap &getCameraMouseActionMap() const;
|
||||
void setFirstMouseInput(bool val);
|
||||
|
||||
private:
|
||||
@@ -26,10 +33,10 @@ private:
|
||||
static void mouse_callback(GLFWwindow *window, double xpos, double ypos);
|
||||
|
||||
private:
|
||||
static bool cameraActionRegister[CAMERA_ACTION_NUM_ITEMS];
|
||||
static double cameraMouseActionRegister[CAMERA_MOUSE_ACTION_NUM_ITEMS];
|
||||
static bool windowActionRegister[WINDOW_ACTION_NUM_ITEMS];
|
||||
static bool mouseButtonActionRegister[MOUSE_BUTTON_ACTION_NUM_ITEMS];
|
||||
static CameraActionMap cameraActionMap;
|
||||
static CameraMouseActionMap cameraMouseActionMap;
|
||||
static WindowActionMap windowActionMap;
|
||||
static MouseButtonActionMap mouseButtonActionMap;
|
||||
|
||||
GLFWwindow *window;
|
||||
|
||||
|
||||
@@ -268,8 +268,7 @@ std::vector<Widget *> JsonParser::getWidgetsFromScreen(const Json::Value &screen
|
||||
const Json::Value currentWidgetPosition = currentWidgetJson["position"];
|
||||
const Json::Value currentWidgetDimensions = currentWidgetJson["dimensions"];
|
||||
std::string name = currentWidgetJson["unique_name"].asString();
|
||||
Texture *currentWidgetTexture =
|
||||
new Texture(currentWidgetTextureJson.asString().c_str(), textureType::texture_diffuse);
|
||||
Texture *currentWidgetTexture = new Texture(currentWidgetTextureJson.asString().c_str(), TextureType::Diffuse);
|
||||
Widget *currentWidget =
|
||||
new Widget(name, currentWidgetTexture, currentWidgetPosition[0].asFloat(),
|
||||
currentWidgetPosition[1].asFloat(), currentWidgetDimensions[0].asFloat(),
|
||||
|
||||
10
src/Menu.cpp
10
src/Menu.cpp
@@ -1,7 +1,7 @@
|
||||
#include "Menu.h"
|
||||
#include "Helper.h"
|
||||
#include "JsonParser.h"
|
||||
#include "eventActions.h"
|
||||
#include "definitions/eventActions.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
@@ -56,9 +56,9 @@ void Menu::resetActiveScreen()
|
||||
activeScreen = nullptr;
|
||||
}
|
||||
|
||||
void Menu::handleMouseButtonActionRegister(bool *mouseButtonActionRegister, Window *window)
|
||||
void Menu::handleMouseButtonActionMap(const MouseButtonActionMap &mouseButtonActionMap, Window *window)
|
||||
{
|
||||
if (mouseButtonActionRegister[mouseButtonActions::leftClicked]) {
|
||||
if (mouseButtonActionMap.at(MouseButtonAction::LeftClicked)) {
|
||||
auto widgets = activeScreen->getWidgets();
|
||||
for (auto it = widgets.begin(); it != widgets.end(); it++) {
|
||||
if ((*it)->isHovered(window)) {
|
||||
@@ -72,10 +72,10 @@ void Menu::handleMouseButtonActionRegister(bool *mouseButtonActionRegister, Wind
|
||||
}
|
||||
}
|
||||
|
||||
void Menu::writeWindowActions(bool *windowActionRegister)
|
||||
void Menu::writeWindowActions(WindowActionMap &windowActionMap)
|
||||
{
|
||||
if (shouldExit)
|
||||
windowActionRegister[windowActions::windowShouldClose] = true;
|
||||
windowActionMap[WindowAction::WindowShouldClose] = true;
|
||||
}
|
||||
|
||||
void Menu::onPlayPressed()
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "Framebuffer.h"
|
||||
#include "JsonParser.h"
|
||||
#include "Screen.h"
|
||||
#include "eventActions.h"
|
||||
#include "EventHandler.h"
|
||||
|
||||
class Menu
|
||||
{
|
||||
@@ -18,11 +18,11 @@ public:
|
||||
void showScreenByName(const std::string &name);
|
||||
|
||||
Screen *getActiveScreen() const;
|
||||
void writeWindowActions(bool *windowActionRegister);
|
||||
void writeWindowActions(WindowActionMap &windowActionMap);
|
||||
|
||||
void resetActiveScreen();
|
||||
|
||||
void handleMouseButtonActionRegister(bool *mouseButtonActionRegister, Window *window);
|
||||
void handleMouseButtonActionMap(const MouseButtonActionMap &mouseButtonActionMap, Window *window);
|
||||
|
||||
void onPlayPressed();
|
||||
void onExitPressed();
|
||||
|
||||
12
src/Mesh.cpp
12
src/Mesh.cpp
@@ -4,23 +4,21 @@ Mesh::Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices, std::vec
|
||||
: numElements(indices.size()), textures(textures),
|
||||
vertexArray(static_cast<void *>(vertices.data()), static_cast<void *>(indices.data()), vertices.size(),
|
||||
indices.size())
|
||||
{
|
||||
// Empty...
|
||||
}
|
||||
{}
|
||||
|
||||
void Mesh::draw(ShaderProgram *shaderProgram)
|
||||
{
|
||||
uint8_t typeNumberCount[TEXTURE_TYPE_NUM_ITEMS]{0};
|
||||
uint8_t typeNumberCount[static_cast<int>(TextureType::TEXTURE_TYPE_NUM_ITEMS)]{0};
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
// Bind all textures in order to its texture unit
|
||||
for (auto it = textures.begin(); it != textures.end(); it++) {
|
||||
const int i = it - textures.begin();
|
||||
|
||||
uint8_t currentTextureType = (*it)->getTextureType();
|
||||
TextureType currentTextureType = (*it)->getTextureType();
|
||||
|
||||
(*it)->bind(i, shaderProgram, typeNumberCount[currentTextureType]);
|
||||
(*it)->bind(i, shaderProgram, typeNumberCount[static_cast<int>(currentTextureType)]);
|
||||
|
||||
typeNumberCount[currentTextureType] += 1;
|
||||
typeNumberCount[static_cast<int>(currentTextureType)] += 1;
|
||||
}
|
||||
|
||||
// Draw elements
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "ShaderProgram.h"
|
||||
#include "Texture.h"
|
||||
#include "VertexArray.h"
|
||||
#include "defines.h"
|
||||
#include "definitions/models.h"
|
||||
|
||||
class Mesh
|
||||
{
|
||||
|
||||
@@ -59,9 +59,9 @@ void Model::loadModel(const std::string &pathToModel)
|
||||
uint32_t numTextures;
|
||||
input.read((char *)&numTextures, sizeof(uint32_t));
|
||||
|
||||
std::vector<uint32_t> textureTypes;
|
||||
std::vector<TextureType> textureTypes;
|
||||
for (unsigned int i = 0; i < numTextures; i++) {
|
||||
uint32_t currentTextureType;
|
||||
TextureType currentTextureType;
|
||||
input.read((char *)¤tTextureType, sizeof(uint32_t));
|
||||
textureTypes.push_back(currentTextureType);
|
||||
}
|
||||
@@ -93,7 +93,7 @@ void Model::loadModel(const std::string &pathToModel)
|
||||
// When there is no normal map bound, please use fallback texture
|
||||
bool hasNormalMap = false;
|
||||
for (auto it = textureTypes.begin(); it != textureTypes.end(); it++) {
|
||||
if (*it == textureType::texture_normal)
|
||||
if (*it == TextureType::Normal)
|
||||
hasNormalMap = true;
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ void Model::loadModel(const std::string &pathToModel)
|
||||
TexturePrototype texture_prototype;
|
||||
|
||||
texture_prototype.texturePath = "data/res/models/tex/fallback_normal.png";
|
||||
texture_prototype.textureType = textureType::texture_normal;
|
||||
texture_prototype.textureType = TextureType::Normal;
|
||||
|
||||
modelTexturePrototypes.push_back(texture_prototype);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
struct TexturePrototype
|
||||
{
|
||||
uint32_t textureType;
|
||||
TextureType textureType;
|
||||
std::string texturePath;
|
||||
};
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <iostream>
|
||||
#include <stb/stb_image.h>
|
||||
|
||||
Texture::Texture(const std::string &texturePath, uint8_t textureType)
|
||||
Texture::Texture(const std::string &texturePath, TextureType textureType)
|
||||
: texturePath(texturePath), textureType(textureType)
|
||||
{
|
||||
stbi_set_flip_vertically_on_load(1);
|
||||
@@ -15,10 +15,10 @@ Texture::Texture(const std::string &texturePath, uint8_t textureType)
|
||||
internalFormat = GL_RED;
|
||||
dataFormat = GL_RED;
|
||||
} else if (numComponents == 3) {
|
||||
internalFormat = (textureType == texture_diffuse) ? GL_SRGB8 : GL_RGB8;
|
||||
internalFormat = (textureType == TextureType::Diffuse) ? GL_SRGB8 : GL_RGB8;
|
||||
dataFormat = GL_RGB;
|
||||
} else if (numComponents == 4) {
|
||||
internalFormat = (textureType == texture_diffuse) ? GL_SRGB8_ALPHA8 : GL_RGBA8;
|
||||
internalFormat = (textureType == TextureType::Diffuse) ? GL_SRGB8_ALPHA8 : GL_RGBA8;
|
||||
dataFormat = GL_RGBA;
|
||||
}
|
||||
|
||||
@@ -57,19 +57,19 @@ void Texture::bind(uint8_t textureUnit, ShaderProgram *shaderProgram, uint8_t te
|
||||
|
||||
switch (textureType) {
|
||||
|
||||
case texture_diffuse:
|
||||
case TextureType::Diffuse:
|
||||
uniformName += "diffuse" + std::to_string(textureTypeNum);
|
||||
break;
|
||||
case texture_specular:
|
||||
case TextureType::Specular:
|
||||
uniformName += "specular" + std::to_string(textureTypeNum);
|
||||
break;
|
||||
case texture_normal:
|
||||
case TextureType::Normal:
|
||||
uniformName += "normal" + std::to_string(textureTypeNum);
|
||||
break;
|
||||
case texture_height:
|
||||
case TextureType::Height:
|
||||
uniformName += "height" + std::to_string(textureTypeNum);
|
||||
break;
|
||||
case texture_gloss:
|
||||
case TextureType::Gloss:
|
||||
uniformName += "gloss" + std::to_string(textureTypeNum);
|
||||
break;
|
||||
}
|
||||
@@ -87,7 +87,7 @@ void Texture::unbind()
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
uint8_t Texture::getTextureType()
|
||||
TextureType Texture::getTextureType()
|
||||
{
|
||||
return textureType;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "ShaderProgram.h"
|
||||
|
||||
#include "defines.h"
|
||||
#include "definitions/models.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <glad/glad.h>
|
||||
@@ -24,13 +24,13 @@ enum cubeMapFaces
|
||||
class Texture
|
||||
{
|
||||
public:
|
||||
Texture(const std::string &texturePath, uint8_t textureType);
|
||||
Texture(const std::string &texturePath, TextureType textureType);
|
||||
~Texture();
|
||||
|
||||
void bind(uint8_t textureUnit, ShaderProgram *shaderProgram, uint8_t textureTypeNum);
|
||||
void unbind();
|
||||
|
||||
uint8_t getTextureType();
|
||||
TextureType getTextureType();
|
||||
std::string getPath();
|
||||
GLuint getTextureId();
|
||||
|
||||
@@ -43,7 +43,7 @@ private:
|
||||
|
||||
GLuint textureId;
|
||||
|
||||
uint8_t textureType;
|
||||
TextureType textureType;
|
||||
};
|
||||
|
||||
class CubeMap
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "VertexArray.h"
|
||||
#include "defines.h"
|
||||
#include "definitions/models.h"
|
||||
|
||||
VertexArray::VertexArray(void *vertexData, void *indexData, uint32_t numVertices, uint32_t numIndices)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include "defines.h"
|
||||
#include "definitions/models.h"
|
||||
|
||||
class VertexArray
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "Mesh.h"
|
||||
#include "Texture.h"
|
||||
#include "Window.h"
|
||||
#include "eventActions.h"
|
||||
#include "definitions/eventActions.h"
|
||||
|
||||
class Menu;
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
#include "Helper.h"
|
||||
#include "Window.h"
|
||||
#include "defines.h"
|
||||
#include "eventActions.h"
|
||||
#include "definitions/eventActions.h"
|
||||
#include "definitions/window.h"
|
||||
|
||||
Window::Window()
|
||||
{
|
||||
@@ -111,10 +111,10 @@ void Window::setCatchedCursor(bool value)
|
||||
}
|
||||
}
|
||||
|
||||
void Window::handleWindowActionRegister(bool *windowActionRegister)
|
||||
void Window::handleWindowActionMap(WindowActionMap &windowActionMap)
|
||||
{
|
||||
if (windowActionRegister[windowActions::wireFrameToggle]) {
|
||||
windowActionRegister[windowActions::wireFrameToggle] = 0;
|
||||
if (windowActionMap.at(WindowAction::WireFrameToggle)) {
|
||||
windowActionMap[WindowAction::WireFrameToggle] = false;
|
||||
wireFrameMode = !wireFrameMode;
|
||||
if (wireFrameMode) {
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
@@ -123,13 +123,13 @@ void Window::handleWindowActionRegister(bool *windowActionRegister)
|
||||
}
|
||||
}
|
||||
|
||||
if (windowActionRegister[windowActions::mouseCatchToggle]) {
|
||||
windowActionRegister[windowActions::mouseCatchToggle] = 0;
|
||||
if (windowActionMap.at(WindowAction::MouseCatchToggle)) {
|
||||
windowActionMap[WindowAction::MouseCatchToggle] = false;
|
||||
mouseCatched = !mouseCatched;
|
||||
setCatchedCursor(mouseCatched);
|
||||
}
|
||||
|
||||
if (windowActionRegister[windowActions::windowShouldClose]) {
|
||||
if (windowActionMap.at(WindowAction::WindowShouldClose)) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "EventHandler.h"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
class Window
|
||||
@@ -18,7 +20,7 @@ public:
|
||||
bool isWindowResized();
|
||||
void updateWindowDimensions();
|
||||
|
||||
void handleWindowActionRegister(bool *windowActionRegister);
|
||||
void handleWindowActionMap(WindowActionMap &windowActionMap);
|
||||
|
||||
private:
|
||||
static void glfw_error_callback(int error, const char *description);
|
||||
|
||||
@@ -136,7 +136,7 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg
|
||||
shaderProgram->setUniform("u_directionalLightViewProjMatrix", directionalLightViewProjectionMatrix);
|
||||
|
||||
// Send shadowMap to basic shader
|
||||
int textureUnit = TEXTURE_TYPE_NUM_ITEMS * 2;
|
||||
int textureUnit = static_cast<int>(TextureType::TEXTURE_TYPE_NUM_ITEMS) * 2;
|
||||
shaderProgram->setUniform("u_texture_directionalShadowMap", (int)textureUnit);
|
||||
glActiveTexture(GL_TEXTURE0 + textureUnit);
|
||||
glBindTexture(GL_TEXTURE_2D, depthMapDirectionalFBO.getDepthMap());
|
||||
@@ -190,7 +190,7 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg
|
||||
|
||||
shaderProgram->setUniform("pointShadowDepthMapFarPlane", far_plane_point);
|
||||
|
||||
textureUnit = TEXTURE_TYPE_NUM_ITEMS * 2 + i + 1;
|
||||
textureUnit = static_cast<int>(TextureType::TEXTURE_TYPE_NUM_ITEMS) * 2 + i + 1;
|
||||
shaderProgram->setUniform("u_texture_pointShadowMap0", (int)textureUnit);
|
||||
glActiveTexture(GL_TEXTURE0 + textureUnit);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, depthMapPointFBO[i]->getCubeMapId());
|
||||
|
||||
11
src/definitions/enumHash.h
Normal file
11
src/definitions/enumHash.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
struct EnumClassHash
|
||||
{
|
||||
template <typename T> std::size_t operator()(T t) const
|
||||
{
|
||||
return static_cast<std::size_t>(t);
|
||||
}
|
||||
};
|
||||
35
src/definitions/eventActions.h
Normal file
35
src/definitions/eventActions.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
enum class CameraAction
|
||||
{
|
||||
Up,
|
||||
Down,
|
||||
Forward,
|
||||
Backward,
|
||||
Left,
|
||||
Right,
|
||||
CAMERA_ACTION_NUM_ITEMS
|
||||
};
|
||||
|
||||
enum class CameraMouseAction
|
||||
{
|
||||
DeltaX,
|
||||
DeltaY,
|
||||
CAMERA_MOUSE_ACTION_NUM_ITEMS
|
||||
};
|
||||
|
||||
enum class WindowAction
|
||||
{
|
||||
WireFrameToggle,
|
||||
MouseCatchToggle,
|
||||
WindowShouldClose,
|
||||
WINDOW_ACTION_NUM_ITEMS
|
||||
};
|
||||
|
||||
enum class MouseButtonAction
|
||||
{
|
||||
LeftClicked,
|
||||
RightClicked,
|
||||
MiddleClicked,
|
||||
MOUSE_BUTTON_ACTION_NUM_ITEMS
|
||||
};
|
||||
31
src/definitions/models.h
Normal file
31
src/definitions/models.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
enum class TextureType
|
||||
{
|
||||
Diffuse,
|
||||
Specular,
|
||||
Normal,
|
||||
Height,
|
||||
Gloss,
|
||||
TEXTURE_TYPE_NUM_ITEMS
|
||||
};
|
||||
|
||||
struct Vertex
|
||||
{
|
||||
// Postition
|
||||
glm::vec3 position;
|
||||
|
||||
// UV Texture Mapping
|
||||
glm::vec2 textureCoords;
|
||||
|
||||
// Normal vector
|
||||
glm::vec3 normalVec;
|
||||
|
||||
// Tangent vector
|
||||
glm::vec3 tangentVec;
|
||||
|
||||
// Bittangent vector
|
||||
glm::vec3 bitangentVec;
|
||||
};
|
||||
4
src/definitions/window.h
Normal file
4
src/definitions/window.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
#define INIT_WINDOW_WIDTH 1280
|
||||
#define INIT_WINDOW_HEIGHT 720
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glad/glad.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "Controller.h"
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
|
||||
#include "../src/defines.h"
|
||||
#include "../src/definitions/models.h"
|
||||
#include "primitiveModel.h"
|
||||
|
||||
void processNode(aiNode *node, const aiScene *scene, Model *model);
|
||||
@@ -196,18 +196,22 @@ Mesh processMesh(aiMesh *mesh, const aiScene *scene, Model *model) {
|
||||
// Material
|
||||
if(mesh->mMaterialIndex > 0) {
|
||||
aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
|
||||
|
||||
std::vector<Texture> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, texture_diffuse, ¤tMesh, model);
|
||||
|
||||
std::vector<Texture> diffuseMaps = loadMaterialTextures(
|
||||
material, aiTextureType_DIFFUSE, static_cast<int>(TextureType::Diffuse), ¤tMesh, model);
|
||||
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
|
||||
|
||||
std::vector<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, texture_specular, ¤tMesh, model);
|
||||
std::vector<Texture> specularMaps = loadMaterialTextures(
|
||||
material, aiTextureType_SPECULAR, static_cast<int>(TextureType::Specular), ¤tMesh, model);
|
||||
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
|
||||
|
||||
std::vector<Texture> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, texture_normal, ¤tMesh, model);
|
||||
std::vector<Texture> normalMaps = loadMaterialTextures(
|
||||
material, aiTextureType_HEIGHT, static_cast<int>(TextureType::Normal), ¤tMesh, model);
|
||||
textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
|
||||
|
||||
// Not entirely sure if aiTextureType_HEIGHT is correct
|
||||
std::vector<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, texture_height, ¤tMesh, model);
|
||||
std::vector<Texture> heightMaps = loadMaterialTextures(
|
||||
material, aiTextureType_HEIGHT, static_cast<int>(TextureType::Height), ¤tMesh, model);
|
||||
textures.insert(textures.end(), heightMaps.begin(), heightMaps.end());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user