Add m_ to member variables.
This commit is contained in:
@@ -12,7 +12,7 @@ add_executable(Fall-Fever
|
||||
Entity.cpp
|
||||
Light.cpp
|
||||
World.cpp
|
||||
Framebuffer.cpp
|
||||
FrameBuffer.cpp
|
||||
Widget.cpp
|
||||
Screen.cpp
|
||||
Menu.cpp
|
||||
|
||||
@@ -6,66 +6,66 @@
|
||||
|
||||
Camera::Camera(float fov, float aspectRatio)
|
||||
{
|
||||
this->fov = fov;
|
||||
viewMatrix = glm::mat4(1.0f);
|
||||
this->m_fov = fov;
|
||||
m_viewMatrix = glm::mat4(1.0f);
|
||||
updateAspectRatio(aspectRatio);
|
||||
updateVPM();
|
||||
}
|
||||
|
||||
void Camera::updateVPM()
|
||||
{
|
||||
viewProjectionMatrix = projectionMatrix * viewMatrix;
|
||||
m_viewProjectionMatrix = m_projectionMatrix * m_viewMatrix;
|
||||
}
|
||||
|
||||
void Camera::updateAspectRatio(float aspectRatio)
|
||||
{
|
||||
// projectionMatrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.f, 100.0f);
|
||||
projectionMatrix = glm::perspective(fov / 2.0f, aspectRatio, 0.1f, 1000.0f);
|
||||
// m_projectionMatrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.f, 100.0f);
|
||||
m_projectionMatrix = glm::perspective(m_fov / 2.0f, aspectRatio, 0.1f, 1000.0f);
|
||||
updateVPM();
|
||||
}
|
||||
|
||||
void Camera::translate(glm::vec3 translateVector)
|
||||
{
|
||||
position += translateVector;
|
||||
viewMatrix = glm::translate(viewMatrix, translateVector * -1.0f);
|
||||
m_position += translateVector;
|
||||
m_viewMatrix = glm::translate(m_viewMatrix, translateVector * -1.0f);
|
||||
}
|
||||
|
||||
void Camera::lookAtTarget(glm::vec3 target)
|
||||
{
|
||||
viewMatrix = glm::lookAt(position, target, upVec);
|
||||
m_viewMatrix = glm::lookAt(m_position, target, m_upVec);
|
||||
}
|
||||
|
||||
void Camera::lookForward()
|
||||
{
|
||||
viewMatrix = glm::lookAt(position, position + frontVec, upVec);
|
||||
m_viewMatrix = glm::lookAt(m_position, m_position + m_frontVec, m_upVec);
|
||||
}
|
||||
|
||||
void Camera::updatePositionFromKeyboardInput(const CameraActionMap &cameraActionMap, float deltaTime)
|
||||
{
|
||||
glm::vec3 frontVecWithoutY = glm::vec3(frontVec.x, 0.0f, frontVec.z);
|
||||
glm::vec3 frontVecWithoutY = glm::vec3(m_frontVec.x, 0.0f, m_frontVec.z);
|
||||
|
||||
glm::vec3 deltaPos = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
if (cameraActionMap.at(CameraAction::Forward)) {
|
||||
deltaPos += speed * deltaTime * glm::normalize(frontVecWithoutY);
|
||||
deltaPos += m_speed * deltaTime * glm::normalize(frontVecWithoutY);
|
||||
}
|
||||
if (cameraActionMap.at(CameraAction::Backward)) {
|
||||
deltaPos -= speed * deltaTime * glm::normalize(frontVecWithoutY);
|
||||
deltaPos -= m_speed * deltaTime * glm::normalize(frontVecWithoutY);
|
||||
}
|
||||
if (cameraActionMap.at(CameraAction::Left)) {
|
||||
deltaPos -= speed * deltaTime * glm::normalize(glm::cross(frontVec, upVec));
|
||||
deltaPos -= m_speed * deltaTime * glm::normalize(glm::cross(m_frontVec, m_upVec));
|
||||
}
|
||||
if (cameraActionMap.at(CameraAction::Right)) {
|
||||
deltaPos += speed * deltaTime * glm::normalize(glm::cross(frontVec, upVec));
|
||||
deltaPos += m_speed * deltaTime * glm::normalize(glm::cross(m_frontVec, m_upVec));
|
||||
}
|
||||
if (cameraActionMap.at(CameraAction::Up)) {
|
||||
deltaPos += speed * deltaTime * upVec;
|
||||
deltaPos += m_speed * deltaTime * m_upVec;
|
||||
}
|
||||
if (cameraActionMap.at(CameraAction::Down)) {
|
||||
deltaPos -= speed * deltaTime * upVec;
|
||||
deltaPos -= m_speed * deltaTime * m_upVec;
|
||||
}
|
||||
|
||||
position += deltaPos;
|
||||
m_position += deltaPos;
|
||||
}
|
||||
|
||||
void Camera::updateDirectionFromMouseInput(const CameraMouseActionMap &cameraMouseActionMap)
|
||||
@@ -76,49 +76,49 @@ void Camera::updateDirectionFromMouseInput(const CameraMouseActionMap &cameraMou
|
||||
return;
|
||||
}
|
||||
|
||||
yaw += cameraMouseActionMap.at(CameraMouseAction::DeltaX);
|
||||
pitch += cameraMouseActionMap.at(CameraMouseAction::DeltaY);
|
||||
m_yaw += cameraMouseActionMap.at(CameraMouseAction::DeltaX);
|
||||
m_pitch += cameraMouseActionMap.at(CameraMouseAction::DeltaY);
|
||||
|
||||
if (pitch > 89.0f) {
|
||||
pitch = 89.0f;
|
||||
if (m_pitch > 89.0f) {
|
||||
m_pitch = 89.0f;
|
||||
}
|
||||
if (pitch < -89.0f) {
|
||||
pitch = -89.0f;
|
||||
if (m_pitch < -89.0f) {
|
||||
m_pitch = -89.0f;
|
||||
}
|
||||
|
||||
glm::vec3 direction;
|
||||
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
direction.y = sin(glm::radians(pitch));
|
||||
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
frontVec = glm::normalize(direction);
|
||||
direction.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
|
||||
direction.y = sin(glm::radians(m_pitch));
|
||||
direction.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
|
||||
m_frontVec = glm::normalize(direction);
|
||||
}
|
||||
|
||||
void Camera::setPosition(glm::vec3 position)
|
||||
{
|
||||
this->position = position;
|
||||
this->m_position = position;
|
||||
}
|
||||
|
||||
glm::mat4 Camera::getView()
|
||||
{
|
||||
return viewMatrix;
|
||||
return m_viewMatrix;
|
||||
}
|
||||
|
||||
glm::mat4 Camera::getProj()
|
||||
{
|
||||
return projectionMatrix;
|
||||
return m_projectionMatrix;
|
||||
}
|
||||
|
||||
glm::mat4 Camera::getViewProj()
|
||||
{
|
||||
return viewProjectionMatrix;
|
||||
return m_viewProjectionMatrix;
|
||||
}
|
||||
|
||||
glm::vec3 Camera::getPosition()
|
||||
{
|
||||
return position;
|
||||
return m_position;
|
||||
}
|
||||
|
||||
glm::vec3 Camera::getDirection()
|
||||
{
|
||||
return frontVec;
|
||||
return m_frontVec;
|
||||
}
|
||||
|
||||
20
src/Camera.h
20
src/Camera.h
@@ -29,18 +29,18 @@ public:
|
||||
void setPosition(glm::vec3 position);
|
||||
|
||||
private:
|
||||
glm::mat4 viewMatrix;
|
||||
glm::mat4 projectionMatrix;
|
||||
glm::mat4 viewProjectionMatrix;
|
||||
glm::mat4 m_viewMatrix;
|
||||
glm::mat4 m_projectionMatrix;
|
||||
glm::mat4 m_viewProjectionMatrix;
|
||||
|
||||
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 frontVec = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||
glm::vec3 upVec = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||
glm::vec3 m_position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 m_frontVec = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||
glm::vec3 m_upVec = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||
|
||||
float pitch = 0.0f;
|
||||
float yaw = -90.0f;
|
||||
float m_pitch = 0.0f;
|
||||
float m_yaw = -90.0f;
|
||||
|
||||
float speed = 2.0f;
|
||||
float m_speed = 2.0f;
|
||||
|
||||
float fov;
|
||||
float m_fov;
|
||||
};
|
||||
|
||||
@@ -25,28 +25,28 @@
|
||||
|
||||
Controller::Controller()
|
||||
{
|
||||
gameWindow = new Window();
|
||||
gameEventHandler = new EventHandler(gameWindow->getGLFWwindow());
|
||||
m_gameWindow = new Window();
|
||||
m_gameEventHandler = new EventHandler(m_gameWindow->getGLFWwindow());
|
||||
|
||||
camera = new Camera(90.0f, gameWindow->getWindowAspectRatio());
|
||||
m_camera = new Camera(90.0f, m_gameWindow->getWindowAspectRatio());
|
||||
|
||||
JsonParser shaderParser("data/shaderPrograms.json");
|
||||
shaderPrograms = shaderParser.getShaderPrograms();
|
||||
m_shaderPrograms = shaderParser.getShaderPrograms();
|
||||
|
||||
pp_framebuffer = new Framebuffer(gameWindow->getWindowWidth(), gameWindow->getWindowHeight(),
|
||||
getShaderProgramByName("postProcessingProgram"));
|
||||
m_postProcessFrameBuffer = new FrameBuffer(m_gameWindow->getWindowWidth(), m_gameWindow->getWindowHeight(),
|
||||
getShaderProgramByName("postProcessingProgram"));
|
||||
|
||||
menu = new Menu(pp_framebuffer, getShaderProgramByName("menuProgram"));
|
||||
m_menu = new Menu(m_postProcessFrameBuffer, getShaderProgramByName("menuProgram"));
|
||||
|
||||
// Show loading screen...
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
menu->showScreenByName("loadingScreen");
|
||||
glfwSwapBuffers(gameWindow->getGLFWwindow());
|
||||
m_menu->showScreenByName("loadingScreen");
|
||||
glfwSwapBuffers(m_gameWindow->getGLFWwindow());
|
||||
|
||||
// Show main menu when loading is finished...
|
||||
menu->showScreenByName("mainMenuScreen");
|
||||
m_menu->showScreenByName("mainMenuScreen");
|
||||
|
||||
world = new World(shaderPrograms);
|
||||
m_world = new World(m_shaderPrograms);
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Setup Dear ImGui context
|
||||
@@ -55,7 +55,7 @@ Controller::Controller()
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
(void)io;
|
||||
// Setup Platform/Renderer bindings
|
||||
ImGui_ImplGlfw_InitForOpenGL(gameWindow->getGLFWwindow(), true);
|
||||
ImGui_ImplGlfw_InitForOpenGL(m_gameWindow->getGLFWwindow(), true);
|
||||
ImGui_ImplOpenGL3_Init("#version 150");
|
||||
// Setup Dear ImGui style
|
||||
ImGui::StyleColorsDark();
|
||||
@@ -70,32 +70,32 @@ Controller::~Controller()
|
||||
ImGui::DestroyContext();
|
||||
#endif
|
||||
|
||||
for (auto it = shaderPrograms.begin(); it != shaderPrograms.end(); it++) {
|
||||
for (auto it = m_shaderPrograms.begin(); it != m_shaderPrograms.end(); it++) {
|
||||
delete *it;
|
||||
}
|
||||
|
||||
delete world;
|
||||
delete camera;
|
||||
delete menu;
|
||||
delete pp_framebuffer;
|
||||
delete gameEventHandler;
|
||||
delete gameWindow;
|
||||
delete m_world;
|
||||
delete m_camera;
|
||||
delete m_menu;
|
||||
delete m_postProcessFrameBuffer;
|
||||
delete m_gameEventHandler;
|
||||
delete m_gameWindow;
|
||||
}
|
||||
|
||||
void Controller::run()
|
||||
{
|
||||
updateExposure(getShaderProgramByName("postProcessingProgram"));
|
||||
|
||||
Entity *lightSource = world->getEntityByName("light");
|
||||
Entity *lightSource = m_world->getEntityByName("light");
|
||||
lightSource->setScale(0.1f);
|
||||
lightSource->setRotation(glm::vec3(0.f));
|
||||
lightSource->setPosition(glm::vec3(-2.f, 1.5f, 2.f));
|
||||
lightSource->setIsLightSource(true);
|
||||
|
||||
camera->translate(glm::vec3(0.0f, 1.5f, 5.0f));
|
||||
m_camera->translate(glm::vec3(0.0f, 1.5f, 5.0f));
|
||||
|
||||
// This is the game loop
|
||||
while (!glfwWindowShouldClose(gameWindow->getGLFWwindow())) {
|
||||
while (!glfwWindowShouldClose(m_gameWindow->getGLFWwindow())) {
|
||||
|
||||
// --- Timing ---
|
||||
limit_framerate();
|
||||
@@ -106,15 +106,15 @@ void Controller::run()
|
||||
if (rotateLightSource) {
|
||||
float radius = 4.0;
|
||||
glm::vec3 newPos = glm::vec3(-cos(glfwGetTime() * 0.5), 0.5f, sin(glfwGetTime() * 0.5)) * radius;
|
||||
world->getEntityByName("light")->setPosition(newPos);
|
||||
m_world->getEntityByName("light")->setPosition(newPos);
|
||||
}
|
||||
if (rotateEntity) {
|
||||
world->getEntityById(0)->rotate(glm::vec3(0.0f, 1.0f, 0.0f), -0.2f * deltaTime);
|
||||
m_world->getEntityById(0)->rotate(glm::vec3(0.0f, 1.0f, 0.0f), -0.2f * m_deltaTime);
|
||||
}
|
||||
static glm::vec3 lightColor = glm::vec3(1.f);
|
||||
static float intensity = 7.5f;
|
||||
world->updatePointLight(0, true, world->getEntityByName("light")->getPosition(), lightColor, intensity);
|
||||
world->updateDirectionalLight(true, world->getDirectionalLight()->getDirection(), lightColor);
|
||||
m_world->updatePointLight(0, true, m_world->getEntityByName("light")->getPosition(), lightColor, intensity);
|
||||
m_world->updateDirectionalLight(true, m_world->getDirectionalLight()->getDirection(), lightColor);
|
||||
getShaderProgramByName("lightProgram")->bind();
|
||||
getShaderProgramByName("lightProgram")->setUniform("v_lightColor", lightColor * 100.0f);
|
||||
getShaderProgramByName("lightProgram")->unbind();
|
||||
@@ -129,55 +129,55 @@ void Controller::run()
|
||||
getShaderProgramByName("defaultProgram")->unbind();
|
||||
if (drawShadows || firstRun) {
|
||||
firstRun = false;
|
||||
world->calculateShadows(getShaderProgramByName("directionalShadowDepthProgram"),
|
||||
getShaderProgramByName("pointShadowDepthProgram"));
|
||||
m_world->calculateShadows(getShaderProgramByName("directionalShadowDepthProgram"),
|
||||
getShaderProgramByName("pointShadowDepthProgram"));
|
||||
}
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
auto activeScreen = menu->getActiveScreen();
|
||||
auto activeScreen = m_menu->getActiveScreen();
|
||||
if (activeScreen) {
|
||||
activeScreen->draw();
|
||||
} else {
|
||||
pp_framebuffer->bind();
|
||||
m_postProcessFrameBuffer->bind();
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
camera->lookForward();
|
||||
camera->updateVPM();
|
||||
m_camera->lookForward();
|
||||
m_camera->updateVPM();
|
||||
|
||||
world->getSkybox()->draw(camera->getView(), camera->getProj());
|
||||
world->draw(camera->getViewProj(), camera->getPosition());
|
||||
m_world->getSkybox()->draw(m_camera->getView(), m_camera->getProj());
|
||||
m_world->draw(m_camera->getViewProj(), m_camera->getPosition());
|
||||
|
||||
pp_framebuffer->unbind();
|
||||
pp_framebuffer->render();
|
||||
m_postProcessFrameBuffer->unbind();
|
||||
m_postProcessFrameBuffer->render();
|
||||
|
||||
#ifdef _DEBUG
|
||||
renderImGui(world, &lightColor, &rotateEntity, &rotateLightSource,
|
||||
renderImGui(m_world, &lightColor, &rotateEntity, &rotateLightSource,
|
||||
getShaderProgramByName("postProcessingProgram"), &intensity, &drawShadows);
|
||||
#endif
|
||||
}
|
||||
glfwSwapBuffers(gameWindow->getGLFWwindow());
|
||||
glfwSwapBuffers(m_gameWindow->getGLFWwindow());
|
||||
|
||||
// Update window size
|
||||
if (gameWindow->isWindowResized()) {
|
||||
gameWindow->updateWindowDimensions();
|
||||
if (m_gameWindow->isWindowResized()) {
|
||||
m_gameWindow->updateWindowDimensions();
|
||||
updateWindowDimensions();
|
||||
}
|
||||
|
||||
// --- Check events, handle input ---
|
||||
gameEventHandler->handleEvents();
|
||||
m_gameEventHandler->handleEvents();
|
||||
|
||||
camera->updatePositionFromKeyboardInput(gameEventHandler->getCameraActionMap(), deltaTime);
|
||||
if (gameWindow->getMouseIsCatched()) {
|
||||
camera->updateDirectionFromMouseInput(gameEventHandler->getCameraMouseActionMap());
|
||||
m_camera->updatePositionFromKeyboardInput(m_gameEventHandler->getCameraActionMap(), m_deltaTime);
|
||||
if (m_gameWindow->getMouseIsCatched()) {
|
||||
m_camera->updateDirectionFromMouseInput(m_gameEventHandler->getCameraMouseActionMap());
|
||||
}
|
||||
|
||||
menu->writeWindowActions(gameEventHandler->getWindowActionMap());
|
||||
gameWindow->handleWindowActionMap(gameEventHandler->getWindowActionMap());
|
||||
m_menu->writeWindowActions(m_gameEventHandler->getWindowActionMap());
|
||||
m_gameWindow->handleWindowActionMap(m_gameEventHandler->getWindowActionMap());
|
||||
|
||||
// Handle widget pressed event only when a screen is currently active
|
||||
if (menu->getActiveScreen())
|
||||
menu->handleMouseButtonActionMap(gameEventHandler->getMouseButtonActionMap(), gameWindow);
|
||||
if (m_menu->getActiveScreen())
|
||||
m_menu->handleMouseButtonActionMap(m_gameEventHandler->getMouseButtonActionMap(), m_gameWindow);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,34 +188,34 @@ void Controller::limit_framerate()
|
||||
|
||||
lastTime = glfwGetTime() - startingTime;
|
||||
|
||||
double frameTime = 1 / (double)MAX_FPS;
|
||||
double frameTime = 1 / (double)m_MAX_FPS;
|
||||
if (frameTime > lastTime) {
|
||||
Helper::sleep((frameTime - lastTime) * 1000000);
|
||||
}
|
||||
|
||||
deltaTime = glfwGetTime() - startingTime;
|
||||
m_deltaTime = glfwGetTime() - startingTime;
|
||||
|
||||
startingTime = glfwGetTime();
|
||||
}
|
||||
|
||||
void Controller::updateWindowDimensions()
|
||||
{
|
||||
camera->updateAspectRatio(gameWindow->getWindowAspectRatio());
|
||||
gameEventHandler->setFirstMouseInput(1);
|
||||
m_camera->updateAspectRatio(m_gameWindow->getWindowAspectRatio());
|
||||
m_gameEventHandler->setFirstMouseInput(1);
|
||||
|
||||
pp_framebuffer->changeDimensions(gameWindow->getWindowWidth(), gameWindow->getWindowHeight());
|
||||
m_postProcessFrameBuffer->changeDimensions(m_gameWindow->getWindowWidth(), m_gameWindow->getWindowHeight());
|
||||
}
|
||||
|
||||
void Controller::updateExposure(ShaderProgram *shaderProgram)
|
||||
{
|
||||
shaderProgram->bind();
|
||||
shaderProgram->setUniform("u_exposure", exposure);
|
||||
shaderProgram->setUniform("u_exposure", m_exposure);
|
||||
shaderProgram->unbind();
|
||||
}
|
||||
|
||||
ShaderProgram *Controller::getShaderProgramByName(const std::string &name)
|
||||
{
|
||||
for (auto it = shaderPrograms.begin(); it != shaderPrograms.end(); it++) {
|
||||
for (auto it = m_shaderPrograms.begin(); it != m_shaderPrograms.end(); it++) {
|
||||
if ((*it)->getUniqueName() == name) {
|
||||
return *it;
|
||||
}
|
||||
@@ -237,7 +237,7 @@ ShaderProgram *Controller::getShaderProgramByName(std::vector<ShaderProgram *> s
|
||||
|
||||
void Controller::setMaxFps(uint16_t fps)
|
||||
{
|
||||
MAX_FPS = fps;
|
||||
m_MAX_FPS = fps;
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
@@ -281,7 +281,7 @@ void Controller::renderImGui(World *world, glm::vec3 *lightColor, bool *rotateEn
|
||||
lightColor->z = color[2];
|
||||
|
||||
ImGui::Text("\nMiscellaneous");
|
||||
ImGui::SliderFloat("Exposure", &exposure, 0, 5.0f);
|
||||
ImGui::SliderFloat("Exposure", &m_exposure, 0, 5.0f);
|
||||
|
||||
ImGui::Checkbox("Draw Shadows", drawShadows);
|
||||
ImGui::Checkbox("Rotate Lightsource", rotateLightSource);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "Camera.h"
|
||||
#include "Entity.h"
|
||||
#include "EventHandler.h"
|
||||
#include "Framebuffer.h"
|
||||
#include "FrameBuffer.h"
|
||||
#include "Light.h"
|
||||
#include "Menu.h"
|
||||
#include "ShaderProgram.h"
|
||||
@@ -33,21 +33,20 @@ private:
|
||||
void renderImGui(World *world, glm::vec3 *lightColor, bool *rotateEntity, bool *rotateLightSource,
|
||||
ShaderProgram *postProcessingProgram, float *intensity, bool *drawShadows);
|
||||
|
||||
private:
|
||||
Window *gameWindow;
|
||||
EventHandler *gameEventHandler;
|
||||
Window *m_gameWindow;
|
||||
EventHandler *m_gameEventHandler;
|
||||
|
||||
World *world;
|
||||
World *m_world;
|
||||
|
||||
Camera *camera;
|
||||
Menu *menu;
|
||||
Camera *m_camera;
|
||||
Menu *m_menu;
|
||||
|
||||
std::vector<ShaderProgram *> shaderPrograms;
|
||||
std::vector<ShaderProgram *> m_shaderPrograms;
|
||||
|
||||
Framebuffer *pp_framebuffer;
|
||||
FrameBuffer *m_postProcessFrameBuffer;
|
||||
|
||||
uint16_t MAX_FPS = 60;
|
||||
double deltaTime;
|
||||
uint16_t m_MAX_FPS = 60;
|
||||
double m_deltaTime;
|
||||
|
||||
float exposure = 1.0f;
|
||||
float m_exposure = 1.0f;
|
||||
};
|
||||
|
||||
@@ -3,43 +3,43 @@
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
uint32_t Entity::id_counter = 0;
|
||||
uint32_t Entity::s_idCounter = 0;
|
||||
|
||||
Entity::Entity(std::string name, Model *model, ShaderProgram *shaderProgram)
|
||||
: unique_name(name), model(model), shaderProgram(shaderProgram)
|
||||
Entity::Entity(const std::string &name, Model *model, ShaderProgram *shaderProgram)
|
||||
: m_uniqueName(name), m_model(model), m_shaderProgram(shaderProgram)
|
||||
{
|
||||
id = id_counter++;
|
||||
m_id = s_idCounter++;
|
||||
}
|
||||
|
||||
void Entity::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition)
|
||||
{
|
||||
shaderProgram->bind();
|
||||
m_shaderProgram->bind();
|
||||
|
||||
glm::mat4 modelViewProj = viewProjMatrix * modelMatrix;
|
||||
shaderProgram->setUniform("u_modelViewProjMatrix", modelViewProj);
|
||||
shaderProgram->setUniform("u_modelMatrix", modelMatrix);
|
||||
glm::mat4 modelViewProj = viewProjMatrix * m_modelMatrix;
|
||||
m_shaderProgram->setUniform("u_modelViewProjMatrix", modelViewProj);
|
||||
m_shaderProgram->setUniform("u_modelMatrix", m_modelMatrix);
|
||||
|
||||
glm::mat3 normalMatrix = glm::mat3(modelMatrix);
|
||||
glm::mat3 normalMatrix = glm::mat3(m_modelMatrix);
|
||||
normalMatrix = glm::transpose(glm::inverse(normalMatrix));
|
||||
shaderProgram->setUniform("u_normalMatrix", normalMatrix);
|
||||
m_shaderProgram->setUniform("u_normalMatrix", normalMatrix);
|
||||
|
||||
shaderProgram->setUniform("u_viewPosition", viewPosition);
|
||||
m_shaderProgram->setUniform("u_viewPosition", viewPosition);
|
||||
|
||||
// Draw the model
|
||||
model->draw(shaderProgram);
|
||||
m_model->draw(m_shaderProgram);
|
||||
|
||||
shaderProgram->unbind();
|
||||
m_shaderProgram->unbind();
|
||||
}
|
||||
|
||||
void Entity::drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram *p_shaderProgram)
|
||||
{
|
||||
p_shaderProgram->bind();
|
||||
|
||||
glm::mat4 modelViewProj = viewProjMatrix * modelMatrix;
|
||||
glm::mat4 modelViewProj = viewProjMatrix * m_modelMatrix;
|
||||
p_shaderProgram->setUniform("u_modelViewProjMatrix", modelViewProj);
|
||||
|
||||
// Draw the model
|
||||
model->drawWithoutTextures();
|
||||
m_model->drawWithoutTextures();
|
||||
|
||||
p_shaderProgram->unbind();
|
||||
}
|
||||
@@ -48,48 +48,48 @@ void Entity::drawPointShadows(ShaderProgram *p_shaderProgram)
|
||||
{
|
||||
p_shaderProgram->bind();
|
||||
|
||||
p_shaderProgram->setUniform("u_modelMatrix", modelMatrix);
|
||||
p_shaderProgram->setUniform("u_modelMatrix", m_modelMatrix);
|
||||
|
||||
// Draw the model
|
||||
model->drawWithoutTextures();
|
||||
m_model->drawWithoutTextures();
|
||||
|
||||
p_shaderProgram->unbind();
|
||||
}
|
||||
|
||||
void Entity::translate(glm::vec3 vector)
|
||||
{
|
||||
position += vector;
|
||||
m_position += vector;
|
||||
updateModelMatrix();
|
||||
}
|
||||
|
||||
void Entity::rotate(glm::vec3 axis, float radians)
|
||||
{
|
||||
glm::quat rotation = glm::angleAxis(radians, axis);
|
||||
quaternion = rotation * quaternion;
|
||||
m_quaternion = rotation * m_quaternion;
|
||||
updateModelMatrix();
|
||||
}
|
||||
|
||||
void Entity::setPosition(glm::vec3 position)
|
||||
{
|
||||
this->position = position;
|
||||
this->m_position = position;
|
||||
updateModelMatrix();
|
||||
}
|
||||
|
||||
void Entity::setRotation(glm::vec3 eulerAngles)
|
||||
{
|
||||
quaternion = glm::quat(eulerAngles);
|
||||
m_quaternion = glm::quat(eulerAngles);
|
||||
updateModelMatrix();
|
||||
}
|
||||
|
||||
void Entity::setRotation(glm::vec3 axis, float radians)
|
||||
{
|
||||
quaternion = glm::angleAxis(radians, axis);
|
||||
m_quaternion = glm::angleAxis(radians, axis);
|
||||
updateModelMatrix();
|
||||
}
|
||||
|
||||
void Entity::setScale(float scaleFactor)
|
||||
{
|
||||
modelScale = scaleFactor;
|
||||
m_modelScale = scaleFactor;
|
||||
updateModelMatrix();
|
||||
}
|
||||
|
||||
@@ -99,55 +99,55 @@ void Entity::updateModelMatrix()
|
||||
// First scaling, then rotation, then translation
|
||||
|
||||
// Translate
|
||||
glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), position);
|
||||
glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), m_position);
|
||||
|
||||
// Rotate
|
||||
glm::mat4 rotationMatrix = glm::toMat4(quaternion);
|
||||
glm::mat4 rotationMatrix = glm::toMat4(m_quaternion);
|
||||
|
||||
// Scale
|
||||
glm::mat4 scaleMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(modelScale, modelScale, modelScale));
|
||||
glm::mat4 scaleMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(m_modelScale, m_modelScale, m_modelScale));
|
||||
|
||||
modelMatrix = translationMatrix * rotationMatrix * scaleMatrix;
|
||||
m_modelMatrix = translationMatrix * rotationMatrix * scaleMatrix;
|
||||
}
|
||||
|
||||
void Entity::setIsLightSource(bool temp)
|
||||
{
|
||||
isLightSource = temp;
|
||||
m_isLightSource = temp;
|
||||
}
|
||||
|
||||
void Entity::setId(uint32_t id)
|
||||
{
|
||||
this->id = id;
|
||||
this->m_id = id;
|
||||
}
|
||||
|
||||
uint32_t Entity::getId()
|
||||
{
|
||||
return id;
|
||||
return m_id;
|
||||
}
|
||||
|
||||
std::string Entity::getUniqueName()
|
||||
{
|
||||
return unique_name;
|
||||
return m_uniqueName;
|
||||
}
|
||||
|
||||
glm::vec3 Entity::getPosition()
|
||||
{
|
||||
return position;
|
||||
return m_position;
|
||||
}
|
||||
|
||||
glm::mat4 Entity::getModelMatrix()
|
||||
{
|
||||
return modelMatrix;
|
||||
return m_modelMatrix;
|
||||
}
|
||||
|
||||
bool Entity::getIsLightSource()
|
||||
{
|
||||
return isLightSource;
|
||||
return m_isLightSource;
|
||||
}
|
||||
|
||||
Skybox::Skybox(Model *cubeModel, ShaderProgram *shaderProgram, const char *texturePseudoPath)
|
||||
: cubeModel(cubeModel), shaderProgram(shaderProgram), cubeMap(texturePseudoPath),
|
||||
vertexArray(cubeModel->getMesh(0)->getVertexArray())
|
||||
: m_cubeModel(cubeModel), m_shaderProgram(shaderProgram), m_cubeMap(texturePseudoPath),
|
||||
m_vertexArray(cubeModel->getMesh(0)->getVertexArray())
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
@@ -160,18 +160,18 @@ void Skybox::draw(glm::mat4 viewMatrix, glm::mat4 projectionMatrix)
|
||||
glDisable(GL_CULL_FACE);
|
||||
|
||||
glDepthMask(GL_FALSE);
|
||||
shaderProgram->bind();
|
||||
m_shaderProgram->bind();
|
||||
|
||||
// Delete any translation from the skybox cube
|
||||
glm::mat4 viewProjectionMatrix = projectionMatrix * glm::mat4(glm::mat3(viewMatrix));
|
||||
|
||||
shaderProgram->setUniform("u_viewProjectionMatrix", viewProjectionMatrix);
|
||||
m_shaderProgram->setUniform("u_viewProjectionMatrix", viewProjectionMatrix);
|
||||
|
||||
cubeMap.bind(shaderProgram);
|
||||
cubeModel->getMesh(0)->drawWithoutTextures();
|
||||
cubeMap.unbind();
|
||||
m_cubeMap.bind(m_shaderProgram);
|
||||
m_cubeModel->getMesh(0)->drawWithoutTextures();
|
||||
m_cubeMap.unbind();
|
||||
|
||||
shaderProgram->unbind();
|
||||
m_shaderProgram->unbind();
|
||||
glDepthMask(GL_TRUE);
|
||||
|
||||
// Restore face culling
|
||||
|
||||
33
src/Entity.h
33
src/Entity.h
@@ -10,7 +10,7 @@
|
||||
class Entity
|
||||
{
|
||||
public:
|
||||
Entity(std::string name, Model *model, ShaderProgram *shaderProgram);
|
||||
Entity(const std::string &name, Model *model, ShaderProgram *shaderProgram);
|
||||
~Entity() = default;
|
||||
|
||||
void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition);
|
||||
@@ -36,23 +36,22 @@ public:
|
||||
private:
|
||||
void updateModelMatrix();
|
||||
|
||||
private:
|
||||
uint32_t id;
|
||||
static uint32_t id_counter;
|
||||
std::string unique_name;
|
||||
uint32_t m_id;
|
||||
static uint32_t s_idCounter;
|
||||
std::string m_uniqueName;
|
||||
|
||||
Model *model;
|
||||
ShaderProgram *shaderProgram;
|
||||
Model *m_model;
|
||||
ShaderProgram *m_shaderProgram;
|
||||
|
||||
bool isLightSource = false;
|
||||
bool m_isLightSource = false;
|
||||
|
||||
glm::mat4 modelMatrix = glm::mat4(1.0f);
|
||||
glm::mat4 m_modelMatrix = glm::mat4(1.0f);
|
||||
|
||||
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::quat quaternion;
|
||||
glm::vec3 velocity = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 m_position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 m_velocity = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::quat m_quaternion;
|
||||
|
||||
float modelScale = 1.0f;
|
||||
float m_modelScale = 1.0f;
|
||||
};
|
||||
|
||||
class Skybox
|
||||
@@ -64,10 +63,10 @@ public:
|
||||
void draw(glm::mat4 viewMatrix, glm::mat4 projectionMatrix);
|
||||
|
||||
private:
|
||||
Model *cubeModel;
|
||||
ShaderProgram *shaderProgram;
|
||||
Model *m_cubeModel;
|
||||
ShaderProgram *m_shaderProgram;
|
||||
|
||||
CubeMap cubeMap;
|
||||
CubeMap m_cubeMap;
|
||||
|
||||
VertexArray *vertexArray;
|
||||
VertexArray *m_vertexArray;
|
||||
};
|
||||
|
||||
@@ -2,29 +2,29 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
CameraActionMap EventHandler::cameraActionMap = {{CameraAction::Forward, false}, {CameraAction::Backward, false},
|
||||
{CameraAction::Up, false}, {CameraAction::Down, false},
|
||||
{CameraAction::Left, false}, {CameraAction::Right, false}};
|
||||
CameraActionMap EventHandler::s_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}};
|
||||
CameraMouseActionMap EventHandler::s_cameraMouseActionMap = {{CameraMouseAction::DeltaX, 0.0},
|
||||
{CameraMouseAction::DeltaY, 0.0}};
|
||||
|
||||
WindowActionMap EventHandler::windowActionMap = {{WindowAction::WireFrameToggle, false},
|
||||
{WindowAction::MouseCatchToggle, false},
|
||||
{WindowAction::WindowShouldClose, false}};
|
||||
WindowActionMap EventHandler::s_windowActionMap = {{WindowAction::WireFrameToggle, false},
|
||||
{WindowAction::MouseCatchToggle, false},
|
||||
{WindowAction::WindowShouldClose, false}};
|
||||
|
||||
MouseButtonActionMap EventHandler::mouseButtonActionMap = {{MouseButtonAction::LeftClicked, false},
|
||||
{MouseButtonAction::RightClicked, false},
|
||||
{MouseButtonAction::MiddleClicked, false}};
|
||||
MouseButtonActionMap EventHandler::s_mouseButtonActionMap = {{MouseButtonAction::LeftClicked, false},
|
||||
{MouseButtonAction::RightClicked, false},
|
||||
{MouseButtonAction::MiddleClicked, false}};
|
||||
|
||||
bool EventHandler::firstMouseInput = 1;
|
||||
float EventHandler::mouseSensitivity = 0.15f;
|
||||
bool EventHandler::s_firstMouseInput = 1;
|
||||
float EventHandler::s_mouseSensitivity = 0.15f;
|
||||
|
||||
EventHandler::EventHandler(GLFWwindow *p_window) : window(p_window)
|
||||
EventHandler::EventHandler(GLFWwindow *p_window) : m_window(p_window)
|
||||
{
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetMouseButtonCallback(window, mouse_button_callback);
|
||||
glfwSetKeyCallback(m_window, key_callback);
|
||||
glfwSetCursorPosCallback(m_window, mouse_callback);
|
||||
glfwSetMouseButtonCallback(m_window, mouse_button_callback);
|
||||
}
|
||||
|
||||
void EventHandler::handleEvents()
|
||||
@@ -35,15 +35,15 @@ void EventHandler::handleEvents()
|
||||
|
||||
void EventHandler::clearActionRegisters()
|
||||
{
|
||||
for (auto &element : cameraMouseActionMap) {
|
||||
for (auto &element : s_cameraMouseActionMap) {
|
||||
element.second = 0.0;
|
||||
}
|
||||
|
||||
for (auto &element : windowActionMap) {
|
||||
for (auto &element : s_windowActionMap) {
|
||||
element.second = 0.0;
|
||||
}
|
||||
|
||||
for (auto &element : mouseButtonActionMap) {
|
||||
for (auto &element : s_mouseButtonActionMap) {
|
||||
element.second = 0.0;
|
||||
}
|
||||
}
|
||||
@@ -56,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) {
|
||||
windowActionMap[WindowAction::WindowShouldClose] = 1;
|
||||
s_windowActionMap[WindowAction::WindowShouldClose] = 1;
|
||||
}
|
||||
|
||||
if (key == GLFW_KEY_O && action == GLFW_PRESS) {
|
||||
windowActionMap[WindowAction::WireFrameToggle] = 1;
|
||||
s_windowActionMap[WindowAction::WireFrameToggle] = 1;
|
||||
}
|
||||
if (key == GLFW_KEY_LEFT_CONTROL && action == GLFW_PRESS) {
|
||||
windowActionMap[WindowAction::MouseCatchToggle] = 1;
|
||||
firstMouseInput = 1;
|
||||
s_windowActionMap[WindowAction::MouseCatchToggle] = 1;
|
||||
s_firstMouseInput = 1;
|
||||
}
|
||||
|
||||
// Movement press
|
||||
if (key == GLFW_KEY_W && action == GLFW_PRESS) {
|
||||
cameraActionMap[CameraAction::Forward] = 1;
|
||||
s_cameraActionMap[CameraAction::Forward] = 1;
|
||||
}
|
||||
if (key == GLFW_KEY_S && action == GLFW_PRESS) {
|
||||
cameraActionMap[CameraAction::Backward] = 1;
|
||||
s_cameraActionMap[CameraAction::Backward] = 1;
|
||||
}
|
||||
if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) {
|
||||
cameraActionMap[CameraAction::Up] = 1;
|
||||
s_cameraActionMap[CameraAction::Up] = 1;
|
||||
}
|
||||
if (key == GLFW_KEY_LEFT_SHIFT && action == GLFW_PRESS) {
|
||||
cameraActionMap[CameraAction::Down] = 1;
|
||||
s_cameraActionMap[CameraAction::Down] = 1;
|
||||
}
|
||||
if (key == GLFW_KEY_A && action == GLFW_PRESS) {
|
||||
cameraActionMap[CameraAction::Left] = 1;
|
||||
s_cameraActionMap[CameraAction::Left] = 1;
|
||||
}
|
||||
if (key == GLFW_KEY_D && action == GLFW_PRESS) {
|
||||
cameraActionMap[CameraAction::Right] = 1;
|
||||
s_cameraActionMap[CameraAction::Right] = 1;
|
||||
}
|
||||
|
||||
// Movement release
|
||||
if (key == GLFW_KEY_W && action == GLFW_RELEASE) {
|
||||
cameraActionMap[CameraAction::Forward] = 0;
|
||||
s_cameraActionMap[CameraAction::Forward] = 0;
|
||||
}
|
||||
if (key == GLFW_KEY_S && action == GLFW_RELEASE) {
|
||||
cameraActionMap[CameraAction::Backward] = 0;
|
||||
s_cameraActionMap[CameraAction::Backward] = 0;
|
||||
}
|
||||
if (key == GLFW_KEY_SPACE && action == GLFW_RELEASE) {
|
||||
cameraActionMap[CameraAction::Up] = 0;
|
||||
s_cameraActionMap[CameraAction::Up] = 0;
|
||||
}
|
||||
if (key == GLFW_KEY_LEFT_SHIFT && action == GLFW_RELEASE) {
|
||||
cameraActionMap[CameraAction::Down] = 0;
|
||||
s_cameraActionMap[CameraAction::Down] = 0;
|
||||
}
|
||||
if (key == GLFW_KEY_A && action == GLFW_RELEASE) {
|
||||
cameraActionMap[CameraAction::Left] = 0;
|
||||
s_cameraActionMap[CameraAction::Left] = 0;
|
||||
}
|
||||
if (key == GLFW_KEY_D && action == GLFW_RELEASE) {
|
||||
cameraActionMap[CameraAction::Right] = 0;
|
||||
s_cameraActionMap[CameraAction::Right] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,17 +122,17 @@ void EventHandler::mouse_callback(GLFWwindow *window, double xpos, double ypos)
|
||||
lastCursorPosY = ypos;
|
||||
|
||||
// Check if this is the first VALID mouse event after window being resized
|
||||
if (firstMouseInput && !(deltaCursorPosX == 0 && deltaCursorPosY == 0)) {
|
||||
firstMouseInput = 0;
|
||||
if (s_firstMouseInput && !(deltaCursorPosX == 0 && deltaCursorPosY == 0)) {
|
||||
s_firstMouseInput = 0;
|
||||
deltaCursorPosX = 0.0;
|
||||
deltaCursorPosY = 0.0;
|
||||
}
|
||||
|
||||
deltaCursorPosX *= mouseSensitivity;
|
||||
deltaCursorPosY *= mouseSensitivity;
|
||||
deltaCursorPosX *= s_mouseSensitivity;
|
||||
deltaCursorPosY *= s_mouseSensitivity;
|
||||
|
||||
cameraMouseActionMap[CameraMouseAction::DeltaX] += deltaCursorPosX;
|
||||
cameraMouseActionMap[CameraMouseAction::DeltaY] += deltaCursorPosY;
|
||||
s_cameraMouseActionMap[CameraMouseAction::DeltaX] += deltaCursorPosX;
|
||||
s_cameraMouseActionMap[CameraMouseAction::DeltaY] += deltaCursorPosY;
|
||||
}
|
||||
|
||||
void EventHandler::mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
|
||||
@@ -141,34 +141,34 @@ void EventHandler::mouse_button_callback(GLFWwindow *window, int button, int act
|
||||
(void)mods;
|
||||
|
||||
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
|
||||
mouseButtonActionMap[MouseButtonAction::LeftClicked] = true;
|
||||
s_mouseButtonActionMap[MouseButtonAction::LeftClicked] = true;
|
||||
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
|
||||
mouseButtonActionMap[MouseButtonAction::RightClicked] = true;
|
||||
s_mouseButtonActionMap[MouseButtonAction::RightClicked] = true;
|
||||
if (button == GLFW_MOUSE_BUTTON_MIDDLE && action == GLFW_PRESS)
|
||||
mouseButtonActionMap[MouseButtonAction::MiddleClicked] = true;
|
||||
s_mouseButtonActionMap[MouseButtonAction::MiddleClicked] = true;
|
||||
}
|
||||
|
||||
const CameraActionMap &EventHandler::getCameraActionMap() const
|
||||
{
|
||||
return cameraActionMap;
|
||||
return s_cameraActionMap;
|
||||
}
|
||||
|
||||
WindowActionMap &EventHandler::getWindowActionMap() const
|
||||
{
|
||||
return windowActionMap;
|
||||
return s_windowActionMap;
|
||||
}
|
||||
|
||||
const MouseButtonActionMap &EventHandler::getMouseButtonActionMap() const
|
||||
{
|
||||
return mouseButtonActionMap;
|
||||
return s_mouseButtonActionMap;
|
||||
}
|
||||
|
||||
const CameraMouseActionMap &EventHandler::getCameraMouseActionMap() const
|
||||
{
|
||||
return cameraMouseActionMap;
|
||||
return s_cameraMouseActionMap;
|
||||
}
|
||||
|
||||
void EventHandler::setFirstMouseInput(bool val)
|
||||
{
|
||||
firstMouseInput = val;
|
||||
s_firstMouseInput = val;
|
||||
}
|
||||
|
||||
@@ -32,14 +32,13 @@ private:
|
||||
static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);
|
||||
static void mouse_callback(GLFWwindow *window, double xpos, double ypos);
|
||||
|
||||
private:
|
||||
static CameraActionMap cameraActionMap;
|
||||
static CameraMouseActionMap cameraMouseActionMap;
|
||||
static WindowActionMap windowActionMap;
|
||||
static MouseButtonActionMap mouseButtonActionMap;
|
||||
static CameraActionMap s_cameraActionMap;
|
||||
static CameraMouseActionMap s_cameraMouseActionMap;
|
||||
static WindowActionMap s_windowActionMap;
|
||||
static MouseButtonActionMap s_mouseButtonActionMap;
|
||||
|
||||
GLFWwindow *window;
|
||||
static float s_mouseSensitivity;
|
||||
static bool s_firstMouseInput;
|
||||
|
||||
static float mouseSensitivity;
|
||||
static bool firstMouseInput;
|
||||
GLFWwindow *m_window;
|
||||
};
|
||||
|
||||
@@ -1,45 +1,45 @@
|
||||
#include "Framebuffer.h"
|
||||
#include "FrameBuffer.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
|
||||
Framebuffer::Framebuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram) : shaderProgram(shaderProgram)
|
||||
FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram) : m_shaderProgram(shaderProgram)
|
||||
{
|
||||
glGenFramebuffers(1, &FBO);
|
||||
glGenFramebuffers(1, &m_FBO);
|
||||
|
||||
generateTextures(width, height);
|
||||
setExposureCorrection(true);
|
||||
}
|
||||
|
||||
Framebuffer::~Framebuffer()
|
||||
FrameBuffer::~FrameBuffer()
|
||||
{
|
||||
glDeleteFramebuffers(1, &FBO);
|
||||
glDeleteTextures(1, &colorBuffer);
|
||||
glDeleteRenderbuffers(1, &depthStencilBuffer);
|
||||
glDeleteFramebuffers(1, &m_FBO);
|
||||
glDeleteTextures(1, &m_colorBuffer);
|
||||
glDeleteRenderbuffers(1, &m_depthStencilBuffer);
|
||||
}
|
||||
|
||||
void Framebuffer::bind()
|
||||
void FrameBuffer::bind()
|
||||
{
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
|
||||
}
|
||||
|
||||
void Framebuffer::unbind()
|
||||
void FrameBuffer::unbind()
|
||||
{
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
void Framebuffer::render()
|
||||
void FrameBuffer::render()
|
||||
{
|
||||
// Disable wireframe mode
|
||||
GLint wireframe;
|
||||
glGetIntegerv(GL_POLYGON_MODE, &wireframe);
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
|
||||
shaderProgram->bind();
|
||||
m_shaderProgram->bind();
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, getTextureId());
|
||||
|
||||
GLint location = glGetUniformLocation(shaderProgram->getShaderProgramId(), "u_texture");
|
||||
GLint location = glGetUniformLocation(m_shaderProgram->getShaderProgramId(), "u_texture");
|
||||
glUniform1i(location, 0);
|
||||
|
||||
// A VAO is necessary although no data is stored in it
|
||||
@@ -50,66 +50,66 @@ void Framebuffer::render()
|
||||
glBindVertexArray(0);
|
||||
|
||||
glPolygonMode(GL_FRONT_AND_BACK, wireframe);
|
||||
shaderProgram->unbind();
|
||||
m_shaderProgram->unbind();
|
||||
}
|
||||
|
||||
void Framebuffer::changeDimensions(uint32_t width, uint32_t height)
|
||||
void FrameBuffer::changeDimensions(uint32_t width, uint32_t height)
|
||||
{
|
||||
// Delete old textures
|
||||
glDeleteTextures(1, &colorBuffer);
|
||||
glDeleteRenderbuffers(1, &depthStencilBuffer);
|
||||
glDeleteTextures(1, &m_colorBuffer);
|
||||
glDeleteRenderbuffers(1, &m_depthStencilBuffer);
|
||||
|
||||
generateTextures(width, height);
|
||||
}
|
||||
|
||||
void Framebuffer::generateTextures(uint32_t width, uint32_t height)
|
||||
void FrameBuffer::generateTextures(uint32_t width, uint32_t height)
|
||||
{
|
||||
bind();
|
||||
|
||||
// Create new textures
|
||||
glGenTextures(1, &colorBuffer);
|
||||
glGenRenderbuffers(1, &depthStencilBuffer);
|
||||
glGenTextures(1, &m_colorBuffer);
|
||||
glGenRenderbuffers(1, &m_depthStencilBuffer);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, colorBuffer);
|
||||
glBindTexture(GL_TEXTURE_2D, m_colorBuffer);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorBuffer, 0);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_colorBuffer, 0);
|
||||
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, depthStencilBuffer);
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, m_depthStencilBuffer);
|
||||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthStencilBuffer);
|
||||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilBuffer);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
||||
std::cout << "[Error] Framebuffer is not complete!" << std::endl;
|
||||
std::cout << "[Error] FrameBuffer is not complete!" << std::endl;
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
unbind();
|
||||
}
|
||||
|
||||
void Framebuffer::setExposureCorrection(bool exposureCorrection)
|
||||
void FrameBuffer::setExposureCorrection(bool exposureCorrection)
|
||||
{
|
||||
shaderProgram->bind();
|
||||
shaderProgram->setUniform("u_exposureCorrection", exposureCorrection);
|
||||
shaderProgram->unbind();
|
||||
m_shaderProgram->bind();
|
||||
m_shaderProgram->setUniform("u_exposureCorrection", exposureCorrection);
|
||||
m_shaderProgram->unbind();
|
||||
}
|
||||
|
||||
GLuint Framebuffer::getTextureId()
|
||||
GLuint FrameBuffer::getTextureId()
|
||||
{
|
||||
return colorBuffer;
|
||||
return m_colorBuffer;
|
||||
}
|
||||
|
||||
DepthMap::DepthMap(int TYPE, int RESOLUTION)
|
||||
DepthMap::DepthMap(DepthMapType type, int RESOLUTION)
|
||||
{
|
||||
glGenFramebuffers(1, &depthMapFBO);
|
||||
glGenFramebuffers(1, &m_depthMapFBO);
|
||||
bind();
|
||||
|
||||
if (TYPE == DEPTHMAP_NORMAL) {
|
||||
if (type == DepthMapType::Normal) {
|
||||
|
||||
glGenTextures(1, &depthMap);
|
||||
glBindTexture(GL_TEXTURE_2D, depthMap);
|
||||
glGenTextures(1, &m_depthMap);
|
||||
glBindTexture(GL_TEXTURE_2D, m_depthMap);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, RESOLUTION, RESOLUTION, 0, GL_DEPTH_COMPONENT, GL_FLOAT,
|
||||
NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
@@ -119,33 +119,33 @@ DepthMap::DepthMap(int TYPE, int RESOLUTION)
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthMap, 0);
|
||||
glDrawBuffer(GL_NONE);
|
||||
glReadBuffer(GL_NONE);
|
||||
|
||||
} else if (TYPE == DEPTHMAP_CUBEMAP) {
|
||||
cubeMap = new CubeMap(RESOLUTION);
|
||||
} else if (type == DepthMapType::CubeMap) {
|
||||
m_cubeMap = new CubeMap(RESOLUTION);
|
||||
|
||||
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, cubeMap->getTextureId(), 0);
|
||||
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_cubeMap->getTextureId(), 0);
|
||||
glDrawBuffer(GL_NONE);
|
||||
glReadBuffer(GL_NONE);
|
||||
}
|
||||
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
||||
std::cout << "[Error] Framebuffer is not complete!" << std::endl;
|
||||
std::cout << "[Error] FrameBuffer is not complete!" << std::endl;
|
||||
|
||||
unbind();
|
||||
}
|
||||
|
||||
DepthMap::~DepthMap()
|
||||
{
|
||||
if (cubeMap)
|
||||
delete cubeMap;
|
||||
if (m_cubeMap)
|
||||
delete m_cubeMap;
|
||||
}
|
||||
|
||||
void DepthMap::bind()
|
||||
{
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_depthMapFBO);
|
||||
}
|
||||
|
||||
void DepthMap::unbind()
|
||||
@@ -155,15 +155,15 @@ void DepthMap::unbind()
|
||||
|
||||
GLuint DepthMap::getFBO()
|
||||
{
|
||||
return depthMapFBO;
|
||||
return m_depthMapFBO;
|
||||
}
|
||||
|
||||
GLuint DepthMap::getDepthMap()
|
||||
{
|
||||
return depthMap;
|
||||
return m_depthMap;
|
||||
}
|
||||
|
||||
GLuint DepthMap::getCubeMapId()
|
||||
{
|
||||
return cubeMap->getTextureId();
|
||||
return m_cubeMap->getTextureId();
|
||||
}
|
||||
@@ -4,11 +4,11 @@
|
||||
#include "Texture.h"
|
||||
#include <glad/glad.h>
|
||||
|
||||
class Framebuffer
|
||||
class FrameBuffer
|
||||
{
|
||||
public:
|
||||
Framebuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram);
|
||||
~Framebuffer();
|
||||
FrameBuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram);
|
||||
~FrameBuffer();
|
||||
|
||||
void bind();
|
||||
void unbind();
|
||||
@@ -23,26 +23,25 @@ public:
|
||||
private:
|
||||
void generateTextures(uint32_t width, uint32_t height);
|
||||
|
||||
private:
|
||||
GLuint FBO;
|
||||
GLuint colorBuffer;
|
||||
GLuint depthStencilBuffer;
|
||||
GLuint m_FBO;
|
||||
GLuint m_colorBuffer;
|
||||
GLuint m_depthStencilBuffer;
|
||||
|
||||
ShaderProgram *shaderProgram;
|
||||
ShaderProgram *m_shaderProgram;
|
||||
};
|
||||
|
||||
enum depthMapType
|
||||
enum class DepthMapType
|
||||
{
|
||||
DEPTHMAP_NORMAL,
|
||||
DEPTHMAP_CUBEMAP
|
||||
Normal,
|
||||
CubeMap
|
||||
};
|
||||
|
||||
// Framebuffer without color buffer. (Shadows)
|
||||
// FrameBuffer without color buffer. (Shadows)
|
||||
class DepthMap
|
||||
{
|
||||
public:
|
||||
// Normal depthMap with texture and point depthMap with cubeMap
|
||||
DepthMap(int TYPE, int RESOLUTION);
|
||||
DepthMap(DepthMapType type, int RESOLUTION);
|
||||
~DepthMap();
|
||||
|
||||
void bind();
|
||||
@@ -53,10 +52,10 @@ public:
|
||||
GLuint getCubeMapId();
|
||||
|
||||
private:
|
||||
GLuint depthMapFBO;
|
||||
GLuint m_depthMapFBO;
|
||||
|
||||
// Either a normal depthMap is used (Directional shadows)
|
||||
// or a cubeMap is used (Point shadows)
|
||||
GLuint depthMap;
|
||||
CubeMap *cubeMap;
|
||||
GLuint m_depthMap;
|
||||
CubeMap *m_cubeMap;
|
||||
};
|
||||
@@ -75,7 +75,7 @@ void Helper::gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum sev
|
||||
break;
|
||||
|
||||
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
|
||||
_type = "UDEFINED BEHAVIOR";
|
||||
_type = "UNDEFINED BEHAVIOR";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_TYPE_PORTABILITY:
|
||||
@@ -131,17 +131,17 @@ void Helper::gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum sev
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
Helper::Timer::Timer(const std::string &name) : name(name)
|
||||
Helper::Timer::Timer(const std::string &name) : m_name(name)
|
||||
{
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
m_start = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
Helper::Timer::~Timer()
|
||||
{
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
m_end = std::chrono::high_resolution_clock::now();
|
||||
|
||||
duration = end - start;
|
||||
float ms = duration.count() * 1000.0f;
|
||||
m_duration = m_end - m_start;
|
||||
float ms = m_duration.count() * 1000.0f;
|
||||
|
||||
std::cout << "Timer " << name << " took " << ms << "ms!" << std::endl;
|
||||
std::cout << "Timer " << m_name << " took " << ms << "ms!" << std::endl;
|
||||
}
|
||||
|
||||
@@ -18,9 +18,11 @@ public:
|
||||
~Timer();
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
std::chrono::high_resolution_clock::time_point start, end;
|
||||
std::chrono::duration<float> duration;
|
||||
std::string m_name;
|
||||
|
||||
std::chrono::high_resolution_clock::time_point m_start;
|
||||
std::chrono::high_resolution_clock::time_point m_end;
|
||||
std::chrono::duration<float> m_duration;
|
||||
};
|
||||
|
||||
} // namespace Helper
|
||||
|
||||
@@ -14,8 +14,8 @@ JsonParser::JsonParser(const std::string &path)
|
||||
}
|
||||
|
||||
std::string errs;
|
||||
rbuilder["collectComments"] = false;
|
||||
bool parsingSuccessful = Json::parseFromStream(rbuilder, file, &root, &errs);
|
||||
m_rbuilder["collectComments"] = false;
|
||||
bool parsingSuccessful = Json::parseFromStream(m_rbuilder, file, &m_root, &errs);
|
||||
file.close();
|
||||
if (!parsingSuccessful) {
|
||||
std::cout << "Failed to parse file\n" << errs << std::endl;
|
||||
@@ -30,7 +30,7 @@ std::vector<Model *> JsonParser::getModels()
|
||||
{
|
||||
std::vector<Model *> temp_models;
|
||||
|
||||
const Json::Value modelsJson = root["models"];
|
||||
const Json::Value modelsJson = m_root["models"];
|
||||
|
||||
struct ModelSkeleton
|
||||
{
|
||||
@@ -72,7 +72,7 @@ std::vector<Entity *> JsonParser::getEntities(std::vector<Model *> &models, std:
|
||||
{
|
||||
std::vector<Entity *> temp_entities;
|
||||
|
||||
const Json::Value entitiesJson = root["entities"];
|
||||
const Json::Value entitiesJson = m_root["entities"];
|
||||
|
||||
for (unsigned int index = 0; index < entitiesJson.size(); index++) {
|
||||
std::string entity_name = entitiesJson[index]["unique_name"].asString();
|
||||
@@ -135,7 +135,7 @@ std::vector<ShaderProgram *> JsonParser::getShaderPrograms()
|
||||
{
|
||||
std::vector<ShaderProgram *> temp_shaderPrograms;
|
||||
|
||||
const Json::Value shaderProgramsJson = root["shaderPrograms"];
|
||||
const Json::Value shaderProgramsJson = m_root["shaderPrograms"];
|
||||
|
||||
for (unsigned int index = 0; index < shaderProgramsJson.size(); index++) {
|
||||
std::string shaderProgram_name = shaderProgramsJson[index]["unique_name"].asString();
|
||||
@@ -166,7 +166,7 @@ std::vector<Light *> JsonParser::getLights(ShaderProgram *shaderProgram)
|
||||
glm::vec3 light_color = {1.0f, 1.0f, 1.0f};
|
||||
float light_intensity = 10.0f;
|
||||
|
||||
const Json::Value directionalLightsJson = root["directionalLight"];
|
||||
const Json::Value directionalLightsJson = m_root["directionalLight"];
|
||||
|
||||
const Json::Value directionJson = directionalLightsJson["direction"];
|
||||
Json::Value colorJson = directionalLightsJson["color"];
|
||||
@@ -192,7 +192,7 @@ std::vector<Light *> JsonParser::getLights(ShaderProgram *shaderProgram)
|
||||
temp_lights.push_back(current_directionalLight);
|
||||
|
||||
// Pointlights
|
||||
const Json::Value pointLightsJson = root["pointLights"];
|
||||
const Json::Value pointLightsJson = m_root["pointLights"];
|
||||
|
||||
int index = 0;
|
||||
for (; index < (int)pointLightsJson.size(); index++) {
|
||||
@@ -235,12 +235,12 @@ std::vector<Light *> JsonParser::getLights(ShaderProgram *shaderProgram)
|
||||
return temp_lights;
|
||||
}
|
||||
|
||||
std::vector<Screen *> JsonParser::getScreens(ShaderProgram *menuProgram, Framebuffer *framebuffer)
|
||||
std::vector<Screen *> JsonParser::getScreens(ShaderProgram *menuProgram, FrameBuffer *framebuffer)
|
||||
{
|
||||
std::vector<Screen *> temp_screens;
|
||||
|
||||
const Json::Value loadingScreenJson = root["loadingScreen"];
|
||||
const Json::Value mainMenuScreenJson = root["mainMenuScreen"];
|
||||
const Json::Value loadingScreenJson = m_root["loadingScreen"];
|
||||
const Json::Value mainMenuScreenJson = m_root["mainMenuScreen"];
|
||||
|
||||
std::string name;
|
||||
Screen *screen;
|
||||
@@ -282,7 +282,7 @@ Skybox *JsonParser::getSkybox(Model *cubeModel, ShaderProgram *skyboxProgram)
|
||||
{
|
||||
Skybox *temp_skybox;
|
||||
|
||||
const Json::Value shaderProgramsJson = root["skybox"];
|
||||
const Json::Value shaderProgramsJson = m_root["skybox"];
|
||||
|
||||
std::string skybox_texturePath = shaderProgramsJson["texturePath"].asString();
|
||||
temp_skybox = new Skybox(cubeModel, skyboxProgram, skybox_texturePath.c_str());
|
||||
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
std::vector<Model *> getModels();
|
||||
std::vector<Entity *> getEntities(std::vector<Model *> &models, std::vector<ShaderProgram *> shaderPrograms);
|
||||
std::vector<Light *> getLights(ShaderProgram *shaderProgram);
|
||||
std::vector<Screen *> getScreens(ShaderProgram *menuProgram, Framebuffer *framebuffer);
|
||||
std::vector<Screen *> getScreens(ShaderProgram *menuProgram, FrameBuffer *framebuffer);
|
||||
Skybox *getSkybox(Model *cubeModel, ShaderProgram *skyboxProgram);
|
||||
|
||||
std::vector<ShaderProgram *> getShaderPrograms();
|
||||
@@ -27,7 +27,6 @@ public:
|
||||
private:
|
||||
std::vector<Widget *> getWidgetsFromScreen(const Json::Value &screenJson);
|
||||
|
||||
private:
|
||||
Json::Value root;
|
||||
Json::CharReaderBuilder rbuilder;
|
||||
Json::Value m_root;
|
||||
Json::CharReaderBuilder m_rbuilder;
|
||||
};
|
||||
|
||||
@@ -2,108 +2,108 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
uint32_t Light::id_counter = 0;
|
||||
uint32_t Light::s_idCounter = 0;
|
||||
|
||||
// Light
|
||||
|
||||
Light::Light(glm::vec3 color, float intensity, ShaderProgram *shaderProgram)
|
||||
: shaderProgram(shaderProgram), intensity(intensity)
|
||||
: m_shaderProgram(shaderProgram), m_intensity(intensity)
|
||||
{
|
||||
id = id_counter++;
|
||||
lightColor = color * intensity;
|
||||
m_id = s_idCounter++;
|
||||
m_lightColor = color * intensity;
|
||||
}
|
||||
|
||||
glm::vec3 Light::getColor()
|
||||
{
|
||||
return lightColor;
|
||||
return m_lightColor;
|
||||
}
|
||||
|
||||
void Light::setShaderProgram(ShaderProgram *shaderProgram)
|
||||
{
|
||||
this->shaderProgram = shaderProgram;
|
||||
this->m_shaderProgram = shaderProgram;
|
||||
update();
|
||||
}
|
||||
|
||||
void Light::setColor(glm::vec3 color)
|
||||
{
|
||||
lightColor = color * intensity;
|
||||
m_lightColor = color * m_intensity;
|
||||
update();
|
||||
}
|
||||
|
||||
void Light::setIntensity(float intensity)
|
||||
{
|
||||
this->intensity = intensity;
|
||||
this->m_intensity = intensity;
|
||||
}
|
||||
|
||||
void Light::setActive(bool active)
|
||||
{
|
||||
isActive = active;
|
||||
m_isActive = active;
|
||||
update();
|
||||
}
|
||||
|
||||
// PointLight
|
||||
|
||||
PointLight::PointLight(glm::vec3 position, glm::vec3 color, float intensity, ShaderProgram *shaderProgram)
|
||||
: Light(color, intensity, shaderProgram), position(position)
|
||||
: Light(color, intensity, shaderProgram), m_position(position)
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
|
||||
void PointLight::update()
|
||||
{
|
||||
shaderProgram->bind();
|
||||
m_shaderProgram->bind();
|
||||
|
||||
shaderProgram->setUniform((getStructMemberName() + "isActive").c_str(), isActive);
|
||||
shaderProgram->setUniform((getStructMemberName() + "position").c_str(), position);
|
||||
shaderProgram->setUniform((getStructMemberName() + "color").c_str(), lightColor);
|
||||
m_shaderProgram->setUniform((getStructMemberName() + "isActive").c_str(), m_isActive);
|
||||
m_shaderProgram->setUniform((getStructMemberName() + "position").c_str(), m_position);
|
||||
m_shaderProgram->setUniform((getStructMemberName() + "color").c_str(), m_lightColor);
|
||||
|
||||
shaderProgram->unbind();
|
||||
m_shaderProgram->unbind();
|
||||
}
|
||||
|
||||
std::string PointLight::getStructMemberName()
|
||||
{
|
||||
// id - 1 because id 0 is always the DirectionalLight!
|
||||
std::string temp = "u_pointLight[" + std::to_string(id - 1) + "].";
|
||||
std::string temp = "u_pointLight[" + std::to_string(m_id - 1) + "].";
|
||||
return temp;
|
||||
}
|
||||
|
||||
glm::vec3 PointLight::getPosition()
|
||||
{
|
||||
return position;
|
||||
return m_position;
|
||||
}
|
||||
|
||||
void PointLight::setPosition(glm::vec3 position)
|
||||
{
|
||||
this->position = position;
|
||||
this->m_position = position;
|
||||
update();
|
||||
}
|
||||
|
||||
// DirectionalLight
|
||||
|
||||
DirectionalLight::DirectionalLight(glm::vec3 direction, glm::vec3 color, float intensity, ShaderProgram *shaderProgram)
|
||||
: Light(color, intensity, shaderProgram), direction(direction)
|
||||
: Light(color, intensity, shaderProgram), m_direction(direction)
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
|
||||
void DirectionalLight::update()
|
||||
{
|
||||
shaderProgram->bind();
|
||||
m_shaderProgram->bind();
|
||||
|
||||
shaderProgram->setUniform("u_directionalLight.isActive", isActive);
|
||||
shaderProgram->setUniform("u_directionalLight.direction", direction);
|
||||
shaderProgram->setUniform("u_directionalLight.color", lightColor);
|
||||
m_shaderProgram->setUniform("u_directionalLight.isActive", m_isActive);
|
||||
m_shaderProgram->setUniform("u_directionalLight.direction", m_direction);
|
||||
m_shaderProgram->setUniform("u_directionalLight.color", m_lightColor);
|
||||
|
||||
shaderProgram->unbind();
|
||||
m_shaderProgram->unbind();
|
||||
}
|
||||
|
||||
void DirectionalLight::setDirection(glm::vec3 direction)
|
||||
{
|
||||
this->direction = direction;
|
||||
this->m_direction = direction;
|
||||
update();
|
||||
}
|
||||
|
||||
glm::vec3 DirectionalLight::getDirection()
|
||||
{
|
||||
return direction;
|
||||
return m_direction;
|
||||
}
|
||||
|
||||
21
src/Light.h
21
src/Light.h
@@ -24,19 +24,18 @@ public:
|
||||
protected:
|
||||
Light(glm::vec3 color, float intensity, ShaderProgram *shaderProgram);
|
||||
|
||||
protected:
|
||||
ShaderProgram *shaderProgram;
|
||||
ShaderProgram *m_shaderProgram;
|
||||
|
||||
uint32_t id;
|
||||
static uint32_t id_counter;
|
||||
uint32_t m_id;
|
||||
static uint32_t s_idCounter;
|
||||
|
||||
bool isActive;
|
||||
bool shouldCastShadow = true;
|
||||
bool m_isActive;
|
||||
bool m_shouldCastShadow = true;
|
||||
|
||||
float intensity;
|
||||
float m_intensity;
|
||||
|
||||
// Color
|
||||
glm::vec3 lightColor;
|
||||
glm::vec3 m_lightColor;
|
||||
};
|
||||
|
||||
class PointLight : public Light
|
||||
@@ -53,8 +52,7 @@ private:
|
||||
void update() override;
|
||||
std::string getStructMemberName();
|
||||
|
||||
private:
|
||||
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 m_position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
};
|
||||
|
||||
class DirectionalLight : public Light
|
||||
@@ -69,6 +67,5 @@ public:
|
||||
private:
|
||||
void update() override;
|
||||
|
||||
private:
|
||||
glm::vec3 direction;
|
||||
glm::vec3 m_direction;
|
||||
};
|
||||
|
||||
28
src/Menu.cpp
28
src/Menu.cpp
@@ -5,29 +5,29 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
Menu::Menu(Framebuffer *p_framebuffer, ShaderProgram *p_shaderProgram)
|
||||
: framebuffer(p_framebuffer), shaderProgram(p_shaderProgram)
|
||||
Menu::Menu(FrameBuffer *p_framebuffer, ShaderProgram *p_shaderProgram)
|
||||
: m_frameBuffer(p_framebuffer), m_shaderProgram(p_shaderProgram)
|
||||
{
|
||||
JsonParser screenParser("data/screens.json");
|
||||
screens = screenParser.getScreens(shaderProgram, framebuffer);
|
||||
m_screens = screenParser.getScreens(m_shaderProgram, m_frameBuffer);
|
||||
}
|
||||
|
||||
Menu::~Menu()
|
||||
{
|
||||
// Iterate over screens delete all items
|
||||
for (auto it = screens.begin(); it != screens.end(); it++) {
|
||||
for (auto it = m_screens.begin(); it != m_screens.end(); it++) {
|
||||
delete (*it);
|
||||
}
|
||||
}
|
||||
|
||||
Screen *Menu::getScreenByName(const std::string &name) const
|
||||
{
|
||||
if (m_screen_name_cache.find(name) != m_screen_name_cache.end())
|
||||
return m_screen_name_cache[name];
|
||||
if (m_screenNameCache.find(name) != m_screenNameCache.end())
|
||||
return m_screenNameCache[name];
|
||||
|
||||
for (auto it : screens) {
|
||||
for (auto it : m_screens) {
|
||||
if (it->getUniqueName() == name) {
|
||||
m_screen_name_cache[name] = it;
|
||||
m_screenNameCache[name] = it;
|
||||
return it;
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ Screen *Menu::getScreenByName(const std::string &name) const
|
||||
|
||||
Screen *Menu::getActiveScreen() const
|
||||
{
|
||||
return activeScreen;
|
||||
return m_activeScreen;
|
||||
}
|
||||
|
||||
void Menu::showScreenByName(const std::string &name)
|
||||
@@ -48,25 +48,25 @@ void Menu::showScreenByName(const std::string &name)
|
||||
return;
|
||||
|
||||
screen->draw();
|
||||
activeScreen = screen;
|
||||
m_activeScreen = screen;
|
||||
}
|
||||
|
||||
void Menu::resetActiveScreen()
|
||||
{
|
||||
activeScreen = nullptr;
|
||||
m_activeScreen = nullptr;
|
||||
}
|
||||
|
||||
void Menu::handleMouseButtonActionMap(const MouseButtonActionMap &mouseButtonActionMap, Window *window)
|
||||
{
|
||||
if (mouseButtonActionMap.at(MouseButtonAction::LeftClicked)) {
|
||||
auto widgets = activeScreen->getWidgets();
|
||||
auto widgets = m_activeScreen->getWidgets();
|
||||
for (auto it = widgets.begin(); it != widgets.end(); it++) {
|
||||
if ((*it)->isHovered(window)) {
|
||||
// std::cout << (*it)->getUniqueName() << " clicked!" << std::endl;
|
||||
if ((*it)->getCallbackId() == 1)
|
||||
resetActiveScreen();
|
||||
if ((*it)->getCallbackId() == 2)
|
||||
shouldExit = true;
|
||||
m_shouldExit = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,7 +74,7 @@ void Menu::handleMouseButtonActionMap(const MouseButtonActionMap &mouseButtonAct
|
||||
|
||||
void Menu::writeWindowActions(WindowActionMap &windowActionMap)
|
||||
{
|
||||
if (shouldExit)
|
||||
if (m_shouldExit)
|
||||
windowActionMap[WindowAction::WindowShouldClose] = true;
|
||||
}
|
||||
|
||||
|
||||
16
src/Menu.h
16
src/Menu.h
@@ -3,7 +3,7 @@
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Framebuffer.h"
|
||||
#include "FrameBuffer.h"
|
||||
#include "JsonParser.h"
|
||||
#include "Screen.h"
|
||||
#include "EventHandler.h"
|
||||
@@ -11,7 +11,7 @@
|
||||
class Menu
|
||||
{
|
||||
public:
|
||||
Menu(Framebuffer *p_framebuffer, ShaderProgram *p_shaderProgram);
|
||||
Menu(FrameBuffer *p_framebuffer, ShaderProgram *p_shaderProgram);
|
||||
~Menu();
|
||||
|
||||
Screen *getScreenByName(const std::string &name) const;
|
||||
@@ -28,17 +28,17 @@ public:
|
||||
void onExitPressed();
|
||||
|
||||
private:
|
||||
Framebuffer *framebuffer;
|
||||
ShaderProgram *shaderProgram;
|
||||
FrameBuffer *m_frameBuffer;
|
||||
ShaderProgram *m_shaderProgram;
|
||||
|
||||
bool shouldExit = false;
|
||||
bool m_shouldExit = false;
|
||||
|
||||
std::vector<Screen *> screens;
|
||||
Screen *activeScreen;
|
||||
std::vector<Screen *> m_screens;
|
||||
Screen *m_activeScreen;
|
||||
/*Screen *loadingScreen;
|
||||
Screen *mainMenuScreen;
|
||||
Screen *optionMenuScreen;
|
||||
Screen *pauseMenuScreen;*/
|
||||
|
||||
mutable std::unordered_map<std::string, Screen *> m_screen_name_cache;
|
||||
mutable std::unordered_map<std::string, Screen *> m_screenNameCache;
|
||||
};
|
||||
|
||||
26
src/Mesh.cpp
26
src/Mesh.cpp
@@ -1,9 +1,9 @@
|
||||
#include "Mesh.h"
|
||||
|
||||
Mesh::Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices, std::vector<Texture *> textures)
|
||||
: numElements(indices.size()), textures(textures),
|
||||
vertexArray(static_cast<void *>(vertices.data()), static_cast<void *>(indices.data()), vertices.size(),
|
||||
indices.size())
|
||||
: m_numElements(indices.size()), m_textures(textures),
|
||||
m_vertexArray(static_cast<void *>(vertices.data()), static_cast<void *>(indices.data()), vertices.size(),
|
||||
indices.size())
|
||||
{}
|
||||
|
||||
void Mesh::draw(ShaderProgram *shaderProgram)
|
||||
@@ -11,8 +11,8 @@ void Mesh::draw(ShaderProgram *shaderProgram)
|
||||
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();
|
||||
for (auto it = m_textures.begin(); it != m_textures.end(); it++) {
|
||||
const int i = it - m_textures.begin();
|
||||
|
||||
TextureType currentTextureType = (*it)->getTextureType();
|
||||
|
||||
@@ -22,24 +22,24 @@ void Mesh::draw(ShaderProgram *shaderProgram)
|
||||
}
|
||||
|
||||
// Draw elements
|
||||
vertexArray.bind();
|
||||
glDrawElements(GL_TRIANGLES, numElements, GL_UNSIGNED_INT, 0);
|
||||
vertexArray.unbind();
|
||||
m_vertexArray.bind();
|
||||
glDrawElements(GL_TRIANGLES, m_numElements, GL_UNSIGNED_INT, 0);
|
||||
m_vertexArray.unbind();
|
||||
|
||||
// Unbind all textures
|
||||
for (auto it = textures.begin(); it != textures.end(); it++) {
|
||||
for (auto it = m_textures.begin(); it != m_textures.end(); it++) {
|
||||
(*it)->unbind();
|
||||
}
|
||||
}
|
||||
|
||||
void Mesh::drawWithoutTextures()
|
||||
{
|
||||
vertexArray.bind();
|
||||
glDrawElements(GL_TRIANGLES, numElements, GL_UNSIGNED_INT, 0);
|
||||
vertexArray.unbind();
|
||||
m_vertexArray.bind();
|
||||
glDrawElements(GL_TRIANGLES, m_numElements, GL_UNSIGNED_INT, 0);
|
||||
m_vertexArray.unbind();
|
||||
}
|
||||
|
||||
VertexArray *Mesh::getVertexArray()
|
||||
{
|
||||
return &vertexArray;
|
||||
return &m_vertexArray;
|
||||
}
|
||||
|
||||
@@ -21,9 +21,8 @@ public:
|
||||
private:
|
||||
void setupMesh();
|
||||
|
||||
private:
|
||||
uint32_t numElements;
|
||||
std::vector<Texture *> textures;
|
||||
uint32_t m_numElements;
|
||||
std::vector<Texture *> m_textures;
|
||||
|
||||
VertexArray vertexArray;
|
||||
VertexArray m_vertexArray;
|
||||
};
|
||||
|
||||
@@ -3,46 +3,46 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
uint32_t Model::id_counter = 0;
|
||||
uint32_t Model::s_idCounter = 0;
|
||||
|
||||
Model::Model(const std::string &modelName, const std::string &modelPath) : unique_name(modelName)
|
||||
Model::Model(const std::string &modelName, const std::string &modelPath) : m_uniqueName(modelName)
|
||||
{
|
||||
directory = modelPath.substr(0, modelPath.find_last_of('/'));
|
||||
m_workingPath = modelPath.substr(0, modelPath.find_last_of('/'));
|
||||
|
||||
loadModel(modelPath);
|
||||
id = id_counter++;
|
||||
m_id = s_idCounter++;
|
||||
}
|
||||
|
||||
Model::~Model()
|
||||
{
|
||||
// Go through all loaded textures and delete them
|
||||
for (auto it = loadedTextures.begin(); it != loadedTextures.end(); it++) {
|
||||
for (auto it = m_loadedTextures.begin(); it != m_loadedTextures.end(); it++) {
|
||||
delete (*it);
|
||||
}
|
||||
}
|
||||
|
||||
void Model::draw(ShaderProgram *shaderProgram)
|
||||
{
|
||||
if (!model_prepared) {
|
||||
if (!m_modelPrepared) {
|
||||
std::cout << "WARNING: Model not prepared! Unable to draw!" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Iterate through every mesh and call the draw function
|
||||
for (auto mesh : meshes) {
|
||||
for (auto mesh : m_meshes) {
|
||||
mesh->draw(shaderProgram);
|
||||
}
|
||||
}
|
||||
|
||||
void Model::drawWithoutTextures()
|
||||
{
|
||||
if (!model_prepared) {
|
||||
if (!m_modelPrepared) {
|
||||
std::cout << "WARNING: Model not prepared! Unable to draw!" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Iterate through every mesh and call the draw function
|
||||
for (auto mesh : meshes) {
|
||||
for (auto mesh : m_meshes) {
|
||||
mesh->drawWithoutTextures();
|
||||
}
|
||||
}
|
||||
@@ -82,12 +82,12 @@ void Model::loadModel(const std::string &pathToModel)
|
||||
|
||||
for (unsigned int i = 0; i < numTextures; i++) {
|
||||
TexturePrototype texture_prototype;
|
||||
std::string texturePath = directory + '/' + textureSources[i].c_str();
|
||||
std::string texturePath = m_workingPath + '/' + textureSources[i].c_str();
|
||||
|
||||
texture_prototype.texturePath = std::move(texturePath);
|
||||
texture_prototype.textureType = textureTypes[i];
|
||||
|
||||
modelTexturePrototypes.push_back(texture_prototype);
|
||||
m_modelTexturePrototypes.push_back(texture_prototype);
|
||||
}
|
||||
|
||||
// When there is no normal map bound, please use fallback texture
|
||||
@@ -103,7 +103,7 @@ void Model::loadModel(const std::string &pathToModel)
|
||||
texture_prototype.texturePath = "data/res/models/tex/fallback_normal.png";
|
||||
texture_prototype.textureType = TextureType::Normal;
|
||||
|
||||
modelTexturePrototypes.push_back(texture_prototype);
|
||||
m_modelTexturePrototypes.push_back(texture_prototype);
|
||||
}
|
||||
|
||||
// Here starts the first mesh
|
||||
@@ -146,7 +146,7 @@ void Model::loadModel(const std::string &pathToModel)
|
||||
mesh_prototype.textureIds.push_back(numTextures);
|
||||
}
|
||||
|
||||
modelMeshPrototypes.push_back(std::move(mesh_prototype));
|
||||
m_modelMeshPrototypes.push_back(std::move(mesh_prototype));
|
||||
}
|
||||
|
||||
input.close();
|
||||
@@ -154,32 +154,32 @@ void Model::loadModel(const std::string &pathToModel)
|
||||
|
||||
void Model::prepareModel()
|
||||
{
|
||||
model_prepared = true;
|
||||
m_modelPrepared = true;
|
||||
|
||||
// Create textures on GPU
|
||||
for (auto &it : modelTexturePrototypes) {
|
||||
for (auto &it : m_modelTexturePrototypes) {
|
||||
Texture *newTex = new Texture(it.texturePath.c_str(), it.textureType);
|
||||
loadedTextures.push_back(newTex);
|
||||
m_loadedTextures.push_back(newTex);
|
||||
}
|
||||
|
||||
// Create meshes on GPU
|
||||
for (const auto &it : modelMeshPrototypes) {
|
||||
for (const auto &it : m_modelMeshPrototypes) {
|
||||
std::vector<Texture *> meshTextures;
|
||||
for (const auto it2 : it.textureIds) {
|
||||
meshTextures.push_back(loadedTextures[it2]);
|
||||
meshTextures.push_back(m_loadedTextures[it2]);
|
||||
}
|
||||
|
||||
Mesh *currentMesh = new Mesh(std::move(it.meshVertices), std::move(it.meshIndices), meshTextures);
|
||||
meshes.push_back(currentMesh);
|
||||
m_meshes.push_back(currentMesh);
|
||||
}
|
||||
}
|
||||
|
||||
Mesh *Model::getMesh(unsigned int index)
|
||||
{
|
||||
return meshes[index];
|
||||
return m_meshes[index];
|
||||
}
|
||||
|
||||
std::string Model::getUniqueName()
|
||||
const std::string &Model::getUniqueName()
|
||||
{
|
||||
return unique_name;
|
||||
return m_uniqueName;
|
||||
}
|
||||
|
||||
21
src/Model.h
21
src/Model.h
@@ -30,23 +30,22 @@ public:
|
||||
void drawWithoutTextures();
|
||||
|
||||
Mesh *getMesh(unsigned int index);
|
||||
std::string getUniqueName();
|
||||
const std::string &getUniqueName();
|
||||
|
||||
private:
|
||||
void loadModel(const std::string &pathToModel);
|
||||
|
||||
private:
|
||||
std::vector<Mesh *> meshes;
|
||||
std::vector<Texture *> loadedTextures;
|
||||
std::vector<Mesh *> m_meshes;
|
||||
std::vector<Texture *> m_loadedTextures;
|
||||
|
||||
std::vector<TexturePrototype> modelTexturePrototypes;
|
||||
std::vector<MeshPrototype> modelMeshPrototypes;
|
||||
std::vector<TexturePrototype> m_modelTexturePrototypes;
|
||||
std::vector<MeshPrototype> m_modelMeshPrototypes;
|
||||
|
||||
std::string directory;
|
||||
std::string m_workingPath;
|
||||
|
||||
bool model_prepared = false;
|
||||
bool m_modelPrepared = false;
|
||||
|
||||
static uint32_t id_counter;
|
||||
uint32_t id;
|
||||
std::string unique_name;
|
||||
static uint32_t s_idCounter;
|
||||
uint32_t m_id;
|
||||
std::string m_uniqueName;
|
||||
};
|
||||
|
||||
@@ -1,44 +1,44 @@
|
||||
#include "Screen.h"
|
||||
#include "Helper.h"
|
||||
|
||||
uint32_t Screen::id_counter = 0;
|
||||
uint32_t Screen::s_idCounter = 0;
|
||||
|
||||
Screen::Screen(const std::string &name, std::vector<Widget *> widgets, Framebuffer *framebuffer,
|
||||
Screen::Screen(const std::string &name, std::vector<Widget *> widgets, FrameBuffer *framebuffer,
|
||||
ShaderProgram *shaderProgram)
|
||||
: unique_name(name), framebuffer(framebuffer), shaderProgram(shaderProgram), widgets(widgets)
|
||||
: m_uniqueName(name), m_frameBuffer(framebuffer), m_shaderProgram(shaderProgram), m_widgets(widgets)
|
||||
{
|
||||
id = id_counter++;
|
||||
m_id = s_idCounter++;
|
||||
}
|
||||
|
||||
Screen::~Screen()
|
||||
{
|
||||
// Iterate over Widgets and Textures to delete all of them
|
||||
for (auto it = widgets.begin(); it != widgets.end(); it++) {
|
||||
for (auto it = m_widgets.begin(); it != m_widgets.end(); it++) {
|
||||
delete *it;
|
||||
}
|
||||
for (auto it = textures.begin(); it != textures.end(); it++) {
|
||||
for (auto it = m_textures.begin(); it != m_textures.end(); it++) {
|
||||
delete *it;
|
||||
}
|
||||
}
|
||||
|
||||
const std::string &Screen::getUniqueName() const
|
||||
{
|
||||
return unique_name;
|
||||
return m_uniqueName;
|
||||
}
|
||||
|
||||
std::vector<Widget *> Screen::getWidgets() const
|
||||
{
|
||||
return widgets;
|
||||
return m_widgets;
|
||||
}
|
||||
|
||||
Widget *Screen::getWidgetByName(const std::string &name) const
|
||||
{
|
||||
if (m_widget_name_cache.find(name) != m_widget_name_cache.end())
|
||||
return m_widget_name_cache[name];
|
||||
if (m_widgetNameCache.find(name) != m_widgetNameCache.end())
|
||||
return m_widgetNameCache[name];
|
||||
|
||||
for (auto it : widgets) {
|
||||
for (auto it : m_widgets) {
|
||||
if (it->getUniqueName() == name) {
|
||||
m_widget_name_cache[name] = it;
|
||||
m_widgetNameCache[name] = it;
|
||||
return it;
|
||||
}
|
||||
}
|
||||
@@ -47,19 +47,19 @@ Widget *Screen::getWidgetByName(const std::string &name) const
|
||||
|
||||
void Screen::addWidget(Widget *widget)
|
||||
{
|
||||
widgets.push_back(widget);
|
||||
m_widgets.push_back(widget);
|
||||
}
|
||||
|
||||
void Screen::draw() const
|
||||
{
|
||||
framebuffer->setExposureCorrection(false);
|
||||
framebuffer->bind();
|
||||
m_frameBuffer->setExposureCorrection(false);
|
||||
m_frameBuffer->bind();
|
||||
|
||||
for (auto it = widgets.begin(); it != widgets.end(); it++) {
|
||||
(*it)->draw(shaderProgram);
|
||||
for (auto it = m_widgets.begin(); it != m_widgets.end(); it++) {
|
||||
(*it)->draw(m_shaderProgram);
|
||||
}
|
||||
|
||||
framebuffer->unbind();
|
||||
framebuffer->render();
|
||||
framebuffer->setExposureCorrection(true);
|
||||
m_frameBuffer->unbind();
|
||||
m_frameBuffer->render();
|
||||
m_frameBuffer->setExposureCorrection(true);
|
||||
}
|
||||
|
||||
20
src/Screen.h
20
src/Screen.h
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Framebuffer.h"
|
||||
#include "FrameBuffer.h"
|
||||
#include "ShaderProgram.h"
|
||||
#include "Widget.h"
|
||||
|
||||
@@ -11,7 +11,7 @@ class Menu;
|
||||
class Screen
|
||||
{
|
||||
public:
|
||||
Screen(const std::string &name, std::vector<Widget *> widgets, Framebuffer *framebuffer,
|
||||
Screen(const std::string &name, std::vector<Widget *> widgets, FrameBuffer *framebuffer,
|
||||
ShaderProgram *shaderProgram);
|
||||
~Screen();
|
||||
|
||||
@@ -23,15 +23,15 @@ public:
|
||||
Widget *getWidgetByName(const std::string &name) const;
|
||||
|
||||
private:
|
||||
uint32_t id;
|
||||
static uint32_t id_counter;
|
||||
std::string unique_name;
|
||||
uint32_t m_id;
|
||||
static uint32_t s_idCounter;
|
||||
std::string m_uniqueName;
|
||||
|
||||
Framebuffer *framebuffer;
|
||||
ShaderProgram *shaderProgram;
|
||||
FrameBuffer *m_frameBuffer;
|
||||
ShaderProgram *m_shaderProgram;
|
||||
|
||||
std::vector<Texture *> textures;
|
||||
std::vector<Widget *> widgets;
|
||||
std::vector<Texture *> m_textures;
|
||||
std::vector<Widget *> m_widgets;
|
||||
|
||||
mutable std::unordered_map<std::string, Widget *> m_widget_name_cache;
|
||||
mutable std::unordered_map<std::string, Widget *> m_widgetNameCache;
|
||||
};
|
||||
|
||||
@@ -8,22 +8,22 @@
|
||||
|
||||
ShaderProgram::ShaderProgram(const std::string &name, const std::string &vertexShaderPath,
|
||||
const std::string &fragmentShaderPath)
|
||||
: unique_name(name)
|
||||
: m_uniqueName(name)
|
||||
{
|
||||
std::string vertexShaderSource = parse(vertexShaderPath.c_str());
|
||||
std::string fragmentShaderSource = parse(fragmentShaderPath.c_str());
|
||||
|
||||
shaderProgramId = glCreateProgram();
|
||||
m_shaderProgramId = glCreateProgram();
|
||||
GLuint vs = compile(vertexShaderSource, GL_VERTEX_SHADER);
|
||||
GLuint fs = compile(fragmentShaderSource, GL_FRAGMENT_SHADER);
|
||||
|
||||
glAttachShader(shaderProgramId, vs);
|
||||
glAttachShader(shaderProgramId, fs);
|
||||
glAttachShader(m_shaderProgramId, vs);
|
||||
glAttachShader(m_shaderProgramId, fs);
|
||||
|
||||
glLinkProgram(shaderProgramId);
|
||||
glLinkProgram(m_shaderProgramId);
|
||||
|
||||
GLint isLinked = 0;
|
||||
glGetProgramiv(shaderProgramId, GL_LINK_STATUS, &isLinked);
|
||||
glGetProgramiv(m_shaderProgramId, GL_LINK_STATUS, &isLinked);
|
||||
if (!isLinked) {
|
||||
std::cout << "Failed to link shaderProgram: " << vertexShaderPath << ", " << fragmentShaderPath << std::endl;
|
||||
}
|
||||
@@ -39,25 +39,25 @@ ShaderProgram::ShaderProgram(const std::string &name, const std::string &vertexS
|
||||
|
||||
ShaderProgram::ShaderProgram(const std::string &name, const std::string &vertexShaderPath,
|
||||
const std::string &geometryShaderPath, const std::string &fragmentShaderPath)
|
||||
: unique_name(name)
|
||||
: m_uniqueName(name)
|
||||
{
|
||||
std::string vertexShaderSource = parse(vertexShaderPath.c_str());
|
||||
std::string geometryShaderSource = parse(geometryShaderPath.c_str());
|
||||
std::string fragmentShaderSource = parse(fragmentShaderPath.c_str());
|
||||
|
||||
shaderProgramId = glCreateProgram();
|
||||
m_shaderProgramId = glCreateProgram();
|
||||
GLuint vs = compile(vertexShaderSource, GL_VERTEX_SHADER);
|
||||
GLuint gs = compile(geometryShaderSource, GL_GEOMETRY_SHADER);
|
||||
GLuint fs = compile(fragmentShaderSource, GL_FRAGMENT_SHADER);
|
||||
|
||||
glAttachShader(shaderProgramId, vs);
|
||||
glAttachShader(shaderProgramId, gs);
|
||||
glAttachShader(shaderProgramId, fs);
|
||||
glAttachShader(m_shaderProgramId, vs);
|
||||
glAttachShader(m_shaderProgramId, gs);
|
||||
glAttachShader(m_shaderProgramId, fs);
|
||||
|
||||
glLinkProgram(shaderProgramId);
|
||||
glLinkProgram(m_shaderProgramId);
|
||||
|
||||
GLint isLinked = 0;
|
||||
glGetProgramiv(shaderProgramId, GL_LINK_STATUS, &isLinked);
|
||||
glGetProgramiv(m_shaderProgramId, GL_LINK_STATUS, &isLinked);
|
||||
if (!isLinked) {
|
||||
std::cout << "Failed to link shaderProgram: " << vertexShaderPath << ", " << geometryShaderPath << ", "
|
||||
<< fragmentShaderPath << std::endl;
|
||||
@@ -76,12 +76,12 @@ ShaderProgram::ShaderProgram(const std::string &name, const std::string &vertexS
|
||||
|
||||
ShaderProgram::~ShaderProgram()
|
||||
{
|
||||
glDeleteProgram(shaderProgramId);
|
||||
glDeleteProgram(m_shaderProgramId);
|
||||
}
|
||||
|
||||
void ShaderProgram::bind()
|
||||
{
|
||||
glUseProgram(shaderProgramId);
|
||||
glUseProgram(m_shaderProgramId);
|
||||
}
|
||||
|
||||
void ShaderProgram::unbind()
|
||||
@@ -127,11 +127,11 @@ GLuint ShaderProgram::compile(const std::string &shaderSource, GLenum type)
|
||||
|
||||
GLint ShaderProgram::retrieveUniformLocation(const std::string &name) const
|
||||
{
|
||||
if (uniformLocationCache.find(name) != uniformLocationCache.end())
|
||||
return uniformLocationCache[name];
|
||||
if (m_uniformLocationCache.find(name) != m_uniformLocationCache.end())
|
||||
return m_uniformLocationCache[name];
|
||||
|
||||
GLint location = glGetUniformLocation(shaderProgramId, name.c_str());
|
||||
uniformLocationCache[name] = location;
|
||||
GLint location = glGetUniformLocation(m_shaderProgramId, name.c_str());
|
||||
m_uniformLocationCache[name] = location;
|
||||
|
||||
return location;
|
||||
}
|
||||
@@ -174,10 +174,10 @@ void ShaderProgram::setUniform(const std::string &name, glm::mat4 matrix) const
|
||||
|
||||
GLuint ShaderProgram::getShaderProgramId()
|
||||
{
|
||||
return shaderProgramId;
|
||||
return m_shaderProgramId;
|
||||
}
|
||||
|
||||
std::string ShaderProgram::getUniqueName()
|
||||
const std::string &ShaderProgram::getUniqueName()
|
||||
{
|
||||
return unique_name;
|
||||
return m_uniqueName;
|
||||
}
|
||||
|
||||
@@ -25,16 +25,14 @@ public:
|
||||
void setUniform(const std::string &name, glm::mat3 matrix) const;
|
||||
void setUniform(const std::string &name, glm::mat4 matrix) const;
|
||||
|
||||
public:
|
||||
GLuint getShaderProgramId();
|
||||
std::string getUniqueName();
|
||||
const std::string &getUniqueName();
|
||||
|
||||
private:
|
||||
std::string parse(const std::string &filename);
|
||||
GLuint compile(const std::string &shaderSource, GLenum type);
|
||||
|
||||
private:
|
||||
GLuint shaderProgramId;
|
||||
std::string unique_name;
|
||||
mutable std::unordered_map<std::string, GLint> uniformLocationCache;
|
||||
GLuint m_shaderProgramId;
|
||||
std::string m_uniqueName;
|
||||
mutable std::unordered_map<std::string, GLint> m_uniformLocationCache;
|
||||
};
|
||||
|
||||
@@ -4,27 +4,27 @@
|
||||
#include <stb/stb_image.h>
|
||||
|
||||
Texture::Texture(const std::string &texturePath, TextureType textureType)
|
||||
: texturePath(texturePath), textureType(textureType)
|
||||
: m_texturePath(texturePath), m_textureType(textureType)
|
||||
{
|
||||
stbi_set_flip_vertically_on_load(1);
|
||||
auto *textureBuffer = stbi_load(texturePath.c_str(), &textureWidth, &textureHeight, &numComponents, 0);
|
||||
auto *textureBuffer = stbi_load(texturePath.c_str(), &m_textureWidth, &m_textureHeight, &m_numComponents, 0);
|
||||
|
||||
GLenum internalFormat;
|
||||
GLenum dataFormat;
|
||||
if (numComponents == 1) {
|
||||
if (m_numComponents == 1) {
|
||||
internalFormat = GL_RED;
|
||||
dataFormat = GL_RED;
|
||||
} else if (numComponents == 3) {
|
||||
} else if (m_numComponents == 3) {
|
||||
internalFormat = (textureType == TextureType::Diffuse) ? GL_SRGB8 : GL_RGB8;
|
||||
dataFormat = GL_RGB;
|
||||
} else if (numComponents == 4) {
|
||||
} else if (m_numComponents == 4) {
|
||||
internalFormat = (textureType == TextureType::Diffuse) ? GL_SRGB8_ALPHA8 : GL_RGBA8;
|
||||
dataFormat = GL_RGBA;
|
||||
}
|
||||
|
||||
// Push texture to grahics card
|
||||
glGenTextures(1, &textureId);
|
||||
glBindTexture(GL_TEXTURE_2D, textureId);
|
||||
glGenTextures(1, &m_textureId);
|
||||
glBindTexture(GL_TEXTURE_2D, m_textureId);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
@@ -38,7 +38,7 @@ Texture::Texture(const std::string &texturePath, TextureType textureType)
|
||||
return;
|
||||
}
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, textureWidth, textureHeight, 0, dataFormat, GL_UNSIGNED_BYTE,
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, m_textureWidth, m_textureHeight, 0, dataFormat, GL_UNSIGNED_BYTE,
|
||||
textureBuffer);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
@@ -48,14 +48,14 @@ Texture::Texture(const std::string &texturePath, TextureType textureType)
|
||||
|
||||
Texture::~Texture()
|
||||
{
|
||||
glDeleteTextures(1, &textureId);
|
||||
glDeleteTextures(1, &m_textureId);
|
||||
}
|
||||
|
||||
void Texture::bind(uint8_t textureUnit, ShaderProgram *shaderProgram, uint8_t textureTypeNum)
|
||||
{
|
||||
std::string uniformName = "texture_";
|
||||
|
||||
switch (textureType) {
|
||||
switch (m_textureType) {
|
||||
|
||||
case TextureType::Diffuse:
|
||||
uniformName += "diffuse" + std::to_string(textureTypeNum);
|
||||
@@ -79,7 +79,7 @@ void Texture::bind(uint8_t textureUnit, ShaderProgram *shaderProgram, uint8_t te
|
||||
|
||||
shaderProgram->setUniform(uniformName.c_str(), textureUnit);
|
||||
glActiveTexture(GL_TEXTURE0 + textureUnit);
|
||||
glBindTexture(GL_TEXTURE_2D, textureId);
|
||||
glBindTexture(GL_TEXTURE_2D, m_textureId);
|
||||
}
|
||||
|
||||
void Texture::unbind()
|
||||
@@ -89,29 +89,29 @@ void Texture::unbind()
|
||||
|
||||
TextureType Texture::getTextureType()
|
||||
{
|
||||
return textureType;
|
||||
return m_textureType;
|
||||
}
|
||||
|
||||
std::string Texture::getPath()
|
||||
{
|
||||
return texturePath;
|
||||
return m_texturePath;
|
||||
}
|
||||
|
||||
GLuint Texture::getTextureId()
|
||||
{
|
||||
return textureId;
|
||||
return m_textureId;
|
||||
}
|
||||
|
||||
CubeMap::CubeMap(const char *texturePseudoPath)
|
||||
{
|
||||
// Reserve space in vector so that elements can be accessed explicitly.
|
||||
texturePaths.resize(CUBEMAP_FACES_NUM_ITEMS);
|
||||
m_texturePaths.resize(CUBEMAP_FACES_NUM_ITEMS);
|
||||
fillTexturePathVector(texturePseudoPath);
|
||||
|
||||
stbi_set_flip_vertically_on_load(0);
|
||||
|
||||
glGenTextures(1, &textureId);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);
|
||||
glGenTextures(1, &m_textureId);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureId);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
@@ -122,7 +122,8 @@ CubeMap::CubeMap(const char *texturePseudoPath)
|
||||
for (unsigned int i = 0; i < CUBEMAP_FACES_NUM_ITEMS; i++) {
|
||||
|
||||
int32_t numComponents;
|
||||
auto *textureBuffer = stbi_load(texturePaths[i].c_str(), &textureWidth, &textureHeight, &numComponents, 0);
|
||||
auto *textureBuffer =
|
||||
stbi_load(m_texturePaths[i].c_str(), &m_textureWidth, &m_textureHeight, &numComponents, 0);
|
||||
|
||||
GLenum internalFormat;
|
||||
GLenum dataFormat;
|
||||
@@ -138,12 +139,12 @@ CubeMap::CubeMap(const char *texturePseudoPath)
|
||||
}
|
||||
|
||||
if (!textureBuffer) {
|
||||
std::cout << "[Warning] CubeMap Texture " << texturePaths[i].c_str() << " not found!" << std::endl;
|
||||
std::cout << "[Warning] CubeMap Texture " << m_texturePaths[i].c_str() << " not found!" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalFormat, textureWidth, textureHeight, 0, dataFormat,
|
||||
GL_UNSIGNED_BYTE, textureBuffer);
|
||||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalFormat, m_textureWidth, m_textureHeight, 0,
|
||||
dataFormat, GL_UNSIGNED_BYTE, textureBuffer);
|
||||
|
||||
stbi_image_free(textureBuffer);
|
||||
}
|
||||
@@ -151,11 +152,11 @@ CubeMap::CubeMap(const char *texturePseudoPath)
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
||||
}
|
||||
|
||||
CubeMap::CubeMap(int RESOLUTION) : textureWidth(RESOLUTION), textureHeight(RESOLUTION)
|
||||
CubeMap::CubeMap(int RESOLUTION) : m_textureWidth(RESOLUTION), m_textureHeight(RESOLUTION)
|
||||
{
|
||||
|
||||
glGenTextures(1, &textureId);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);
|
||||
glGenTextures(1, &m_textureId);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureId);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
@@ -174,7 +175,7 @@ CubeMap::CubeMap(int RESOLUTION) : textureWidth(RESOLUTION), textureHeight(RESOL
|
||||
|
||||
CubeMap::~CubeMap()
|
||||
{
|
||||
glDeleteTextures(1, &textureId);
|
||||
glDeleteTextures(1, &m_textureId);
|
||||
}
|
||||
|
||||
void CubeMap::bind(ShaderProgram *shaderProgram)
|
||||
@@ -183,7 +184,7 @@ void CubeMap::bind(ShaderProgram *shaderProgram)
|
||||
|
||||
shaderProgram->setUniform(uniformName.c_str(), 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureId);
|
||||
}
|
||||
|
||||
void CubeMap::unbind()
|
||||
@@ -194,16 +195,16 @@ void CubeMap::unbind()
|
||||
void CubeMap::fillTexturePathVector(const char *texturePseudoPath)
|
||||
{
|
||||
for (unsigned int i = 0; i < CUBEMAP_FACES_NUM_ITEMS; i++) {
|
||||
texturePaths[cm_front] = std::string(texturePseudoPath) + "front.png";
|
||||
texturePaths[cm_back] = std::string(texturePseudoPath) + "back.png";
|
||||
texturePaths[cm_top] = std::string(texturePseudoPath) + "top.png";
|
||||
texturePaths[cm_bottom] = std::string(texturePseudoPath) + "bottom.png";
|
||||
texturePaths[cm_left] = std::string(texturePseudoPath) + "left.png";
|
||||
texturePaths[cm_right] = std::string(texturePseudoPath) + "right.png";
|
||||
m_texturePaths[cm_front] = std::string(texturePseudoPath) + "front.png";
|
||||
m_texturePaths[cm_back] = std::string(texturePseudoPath) + "back.png";
|
||||
m_texturePaths[cm_top] = std::string(texturePseudoPath) + "top.png";
|
||||
m_texturePaths[cm_bottom] = std::string(texturePseudoPath) + "bottom.png";
|
||||
m_texturePaths[cm_left] = std::string(texturePseudoPath) + "left.png";
|
||||
m_texturePaths[cm_right] = std::string(texturePseudoPath) + "right.png";
|
||||
}
|
||||
}
|
||||
|
||||
GLuint CubeMap::getTextureId()
|
||||
{
|
||||
return textureId;
|
||||
return m_textureId;
|
||||
}
|
||||
|
||||
@@ -35,15 +35,15 @@ public:
|
||||
GLuint getTextureId();
|
||||
|
||||
private:
|
||||
std::string texturePath;
|
||||
std::string m_texturePath;
|
||||
|
||||
int32_t textureWidth;
|
||||
int32_t textureHeight;
|
||||
int32_t numComponents;
|
||||
int32_t m_textureWidth;
|
||||
int32_t m_textureHeight;
|
||||
int32_t m_numComponents;
|
||||
|
||||
GLuint textureId;
|
||||
GLuint m_textureId;
|
||||
|
||||
TextureType textureType;
|
||||
TextureType m_textureType;
|
||||
};
|
||||
|
||||
class CubeMap
|
||||
@@ -62,11 +62,10 @@ public:
|
||||
private:
|
||||
void fillTexturePathVector(const char *texturePseudoPath);
|
||||
|
||||
private:
|
||||
std::vector<std::string> texturePaths;
|
||||
std::vector<std::string> m_texturePaths;
|
||||
|
||||
GLuint textureId;
|
||||
GLuint m_textureId;
|
||||
|
||||
int32_t textureWidth;
|
||||
int32_t textureHeight;
|
||||
int32_t m_textureWidth;
|
||||
int32_t m_textureHeight;
|
||||
};
|
||||
|
||||
@@ -6,15 +6,15 @@
|
||||
|
||||
VertexArray::VertexArray(void *vertexData, void *indexData, uint32_t numVertices, uint32_t numIndices)
|
||||
{
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
glGenVertexArrays(1, &m_VAO);
|
||||
glBindVertexArray(m_VAO);
|
||||
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glGenBuffers(1, &m_VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(Vertex), vertexData, GL_STATIC_DRAW);
|
||||
|
||||
glGenBuffers(1, &EBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glGenBuffers(1, &m_EBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(uint32_t), indexData, GL_STATIC_DRAW);
|
||||
|
||||
// Position
|
||||
@@ -43,12 +43,12 @@ VertexArray::VertexArray(void *vertexData, void *indexData, uint32_t numVertices
|
||||
|
||||
VertexArray::~VertexArray()
|
||||
{
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glDeleteBuffers(1, &m_VBO);
|
||||
}
|
||||
|
||||
void VertexArray::bind()
|
||||
{
|
||||
glBindVertexArray(VAO);
|
||||
glBindVertexArray(m_VAO);
|
||||
}
|
||||
|
||||
void VertexArray::unbind()
|
||||
|
||||
@@ -16,7 +16,7 @@ public:
|
||||
static std::vector<Vertex> createVertices(double *vertices, uint32_t numVertices, float *textureCoordinates);
|
||||
|
||||
private:
|
||||
GLuint VAO;
|
||||
GLuint VBO;
|
||||
GLuint EBO;
|
||||
GLuint m_VAO;
|
||||
GLuint m_VBO;
|
||||
GLuint m_EBO;
|
||||
};
|
||||
|
||||
@@ -3,45 +3,53 @@
|
||||
#include "VertexArray.h"
|
||||
|
||||
Widget::Widget(std::string &name, Texture *texture, float p_x, float p_y, float p_w, float p_h, uint16_t callbackId)
|
||||
: x(p_x), y(p_y), w(p_w), h(p_h), unique_name(name), callbackId(callbackId)
|
||||
: m_posX(p_x), m_posY(p_y), m_width(p_w), m_height(p_h), m_uniqueName(name), m_callbackId(callbackId)
|
||||
{
|
||||
widgetTextures.push_back(texture);
|
||||
m_widgetTextures.push_back(texture);
|
||||
|
||||
double widgetVerticesData[12] = {
|
||||
2 * (x + w) - 1.0f, 2 * (y)-1.0f, 0.0f, // Bottom right
|
||||
2 * (x)-1.0f, 2 * (y + h) - 1.0f, 0.0f, // Top left
|
||||
2 * (x)-1.0f, 2 * (y)-1.0f, 0.0f, // Bottom left
|
||||
2 * (x + w) - 1.0f, 2 * (y + h) - 1.0f, 0.0f // Top right
|
||||
2 * (m_posX + m_width) - 1.0f,
|
||||
2 * (m_posY)-1.0f,
|
||||
0.0f, // Bottom right
|
||||
2 * (m_posX)-1.0f,
|
||||
2 * (m_posY + m_height) - 1.0f,
|
||||
0.0f, // Top left
|
||||
2 * (m_posX)-1.0f,
|
||||
2 * (m_posY)-1.0f,
|
||||
0.0f, // Bottom left
|
||||
2 * (m_posX + m_width) - 1.0f,
|
||||
2 * (m_posY + m_height) - 1.0f,
|
||||
0.0f // Top right
|
||||
};
|
||||
|
||||
unsigned int widgetIndicesData[6] = {0, 1, 2, 0, 3, 1};
|
||||
|
||||
float widgetTextureCoordinates[8] = {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f};
|
||||
|
||||
widgetVertices = VertexArray::createVertices(widgetVerticesData, 12, widgetTextureCoordinates);
|
||||
widgetIndices.assign(widgetIndicesData, widgetIndicesData + 6);
|
||||
widgetMesh = new Mesh(widgetVertices, widgetIndices, widgetTextures);
|
||||
m_widgetVertices = VertexArray::createVertices(widgetVerticesData, 12, widgetTextureCoordinates);
|
||||
m_widgetIndices.assign(widgetIndicesData, widgetIndicesData + 6);
|
||||
m_widgetMesh = new Mesh(m_widgetVertices, m_widgetIndices, m_widgetTextures);
|
||||
}
|
||||
|
||||
Widget::~Widget()
|
||||
{
|
||||
delete widgetMesh;
|
||||
delete m_widgetMesh;
|
||||
}
|
||||
|
||||
std::string Widget::getUniqueName()
|
||||
{
|
||||
return unique_name;
|
||||
return m_uniqueName;
|
||||
}
|
||||
|
||||
uint16_t Widget::getCallbackId()
|
||||
{
|
||||
return callbackId;
|
||||
return m_callbackId;
|
||||
}
|
||||
|
||||
void Widget::draw(ShaderProgram *shaderProgram)
|
||||
{
|
||||
shaderProgram->bind();
|
||||
widgetMesh->draw(shaderProgram);
|
||||
m_widgetMesh->draw(shaderProgram);
|
||||
shaderProgram->unbind();
|
||||
}
|
||||
|
||||
@@ -56,7 +64,7 @@ bool Widget::isHovered(Window *window)
|
||||
double yrel = -ypos / height + 1;
|
||||
|
||||
bool isHovered = false;
|
||||
if (xrel >= x && xrel <= x + w && yrel >= y && yrel <= y + h)
|
||||
if (xrel >= m_posX && xrel <= m_posX + m_width && yrel >= m_posY && yrel <= m_posY + m_height)
|
||||
isHovered = true;
|
||||
|
||||
return isHovered;
|
||||
|
||||
16
src/Widget.h
16
src/Widget.h
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Framebuffer.h"
|
||||
#include "FrameBuffer.h"
|
||||
#include "Mesh.h"
|
||||
#include "Texture.h"
|
||||
#include "Window.h"
|
||||
@@ -22,15 +22,15 @@ public:
|
||||
bool isHovered(Window *window);
|
||||
|
||||
private:
|
||||
double x, y, w, h;
|
||||
double m_posX, m_posY, m_width, m_height;
|
||||
|
||||
std::string unique_name;
|
||||
std::string m_uniqueName;
|
||||
|
||||
uint16_t callbackId;
|
||||
uint16_t m_callbackId;
|
||||
|
||||
std::vector<Vertex> widgetVertices;
|
||||
std::vector<uint32_t> widgetIndices;
|
||||
std::vector<Texture *> widgetTextures;
|
||||
std::vector<Vertex> m_widgetVertices;
|
||||
std::vector<uint32_t> m_widgetIndices;
|
||||
std::vector<Texture *> m_widgetTextures;
|
||||
|
||||
Mesh *widgetMesh;
|
||||
Mesh *m_widgetMesh;
|
||||
};
|
||||
|
||||
@@ -13,8 +13,8 @@ Window::Window()
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
width = INIT_WINDOW_WIDTH;
|
||||
height = INIT_WINDOW_HEIGHT;
|
||||
m_width = INIT_WINDOW_WIDTH;
|
||||
m_height = INIT_WINDOW_HEIGHT;
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
@@ -28,7 +28,7 @@ Window::Window()
|
||||
glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
|
||||
#endif
|
||||
|
||||
window = glfwCreateWindow(width, height, "OpenGL", NULL, NULL);
|
||||
window = glfwCreateWindow(m_width, m_height, "OpenGL", NULL, NULL);
|
||||
if (!window) {
|
||||
std::cout << "Failed to create window" << std::endl;
|
||||
}
|
||||
@@ -36,8 +36,8 @@ Window::Window()
|
||||
// Wait for window to maximize (in case)
|
||||
Helper::sleep(1000);
|
||||
|
||||
glfwGetWindowPos(window, &posX, &posY);
|
||||
glfwGetWindowSize(window, &width, &height);
|
||||
glfwGetWindowPos(window, &m_posX, &m_posY);
|
||||
glfwGetWindowSize(window, &m_width, &m_height);
|
||||
|
||||
// Create OpenGL context
|
||||
glfwMakeContextCurrent(window);
|
||||
@@ -54,7 +54,7 @@ Window::Window()
|
||||
glDebugMessageCallback(Helper::gl_debug_callback, NULL);
|
||||
|
||||
// Disable mouse cursor
|
||||
mouseCatched = false;
|
||||
m_mouseCatched = false;
|
||||
#endif
|
||||
|
||||
// Enable z buffer
|
||||
@@ -71,9 +71,9 @@ Window::Window()
|
||||
// Disable VSync since my sleep function handles this
|
||||
glfwSwapInterval(0);
|
||||
|
||||
setCatchedCursor(mouseCatched);
|
||||
setCatchedCursor(m_mouseCatched);
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
glViewport(0, 0, m_width, m_height);
|
||||
|
||||
// Tell GLFW which function to call when window is resized
|
||||
// glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
@@ -91,15 +91,15 @@ bool Window::isWindowResized()
|
||||
glfwGetFramebufferSize(window, &new_width, &new_height);
|
||||
glfwGetWindowPos(window, &new_posx, &new_posy);
|
||||
|
||||
return !(new_width == width && new_height == height && new_posx == posX && new_posy == posY);
|
||||
return !(new_width == m_width && new_height == m_height && new_posx == m_posX && new_posy == m_posY);
|
||||
}
|
||||
|
||||
void Window::updateWindowDimensions()
|
||||
{
|
||||
glfwGetFramebufferSize(window, &width, &height);
|
||||
glfwGetWindowPos(window, &posX, &posY);
|
||||
glfwGetFramebufferSize(window, &m_width, &m_height);
|
||||
glfwGetWindowPos(window, &m_posX, &m_posY);
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
glViewport(0, 0, m_width, m_height);
|
||||
}
|
||||
|
||||
void Window::setCatchedCursor(bool value)
|
||||
@@ -115,8 +115,8 @@ void Window::handleWindowActionMap(WindowActionMap &windowActionMap)
|
||||
{
|
||||
if (windowActionMap.at(WindowAction::WireFrameToggle)) {
|
||||
windowActionMap[WindowAction::WireFrameToggle] = false;
|
||||
wireFrameMode = !wireFrameMode;
|
||||
if (wireFrameMode) {
|
||||
m_wireFrameMode = !m_wireFrameMode;
|
||||
if (m_wireFrameMode) {
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
} else {
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
@@ -125,8 +125,8 @@ void Window::handleWindowActionMap(WindowActionMap &windowActionMap)
|
||||
|
||||
if (windowActionMap.at(WindowAction::MouseCatchToggle)) {
|
||||
windowActionMap[WindowAction::MouseCatchToggle] = false;
|
||||
mouseCatched = !mouseCatched;
|
||||
setCatchedCursor(mouseCatched);
|
||||
m_mouseCatched = !m_mouseCatched;
|
||||
setCatchedCursor(m_mouseCatched);
|
||||
}
|
||||
|
||||
if (windowActionMap.at(WindowAction::WindowShouldClose)) {
|
||||
@@ -154,20 +154,20 @@ GLFWwindow *Window::getGLFWwindow()
|
||||
|
||||
int Window::getWindowWidth()
|
||||
{
|
||||
return width;
|
||||
return m_width;
|
||||
}
|
||||
|
||||
int Window::getWindowHeight()
|
||||
{
|
||||
return height;
|
||||
return m_height;
|
||||
}
|
||||
|
||||
float Window::getWindowAspectRatio()
|
||||
{
|
||||
return (float)width / (float)height;
|
||||
return (float)m_width / (float)m_height;
|
||||
}
|
||||
|
||||
bool Window::getMouseIsCatched()
|
||||
{
|
||||
return mouseCatched;
|
||||
return m_mouseCatched;
|
||||
}
|
||||
|
||||
@@ -27,12 +27,11 @@ private:
|
||||
static void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||
void setCatchedCursor(bool value);
|
||||
|
||||
private:
|
||||
GLFWwindow *window;
|
||||
|
||||
int posX, posY;
|
||||
int width, height;
|
||||
int m_posX, m_posY;
|
||||
int m_width, m_height;
|
||||
|
||||
bool mouseCatched = true;
|
||||
bool wireFrameMode = false;
|
||||
bool m_mouseCatched = true;
|
||||
bool m_wireFrameMode = false;
|
||||
};
|
||||
|
||||
126
src/World.cpp
126
src/World.cpp
@@ -5,62 +5,62 @@
|
||||
#include <iostream>
|
||||
|
||||
World::World(std::vector<ShaderProgram *> shaderPrograms)
|
||||
: shaderProgram(Controller::getShaderProgramByName(shaderPrograms, "defaultProgram")),
|
||||
depthMapDirectionalFBO(DEPTHMAP_NORMAL, SHADOW_RES)
|
||||
: m_shaderProgram(Controller::getShaderProgramByName(shaderPrograms, "defaultProgram")),
|
||||
m_depthMapDirectionalFBO(DepthMapType::Normal, SHADOW_RES)
|
||||
{
|
||||
// Create 4 depthMaps
|
||||
for (int i = 0; i < 4; i++) {
|
||||
DepthMap *temp_depthMap = new DepthMap(DEPTHMAP_CUBEMAP, SHADOW_RES);
|
||||
depthMapPointFBO.push_back(temp_depthMap);
|
||||
DepthMap *temp_depthMap = new DepthMap(DepthMapType::CubeMap, SHADOW_RES);
|
||||
m_depthMapPointFBO.push_back(temp_depthMap);
|
||||
}
|
||||
|
||||
// This will be removed in future when gloss maps are implemented
|
||||
shaderProgram->bind();
|
||||
shaderProgram->setUniform("u_material.shininess", 100.0f);
|
||||
shaderProgram->unbind();
|
||||
m_shaderProgram->bind();
|
||||
m_shaderProgram->setUniform("u_material.shininess", 100.0f);
|
||||
m_shaderProgram->unbind();
|
||||
|
||||
JsonParser modelParser("data/models.json");
|
||||
models = modelParser.getModels();
|
||||
m_models = modelParser.getModels();
|
||||
|
||||
for (const auto &it : models) {
|
||||
for (const auto &it : m_models) {
|
||||
it->prepareModel();
|
||||
}
|
||||
|
||||
entities = modelParser.getEntities(models, shaderPrograms);
|
||||
skybox = modelParser.getSkybox(getModelByName("cube"),
|
||||
Controller::getShaderProgramByName(shaderPrograms, "skyboxProgram"));
|
||||
m_entities = modelParser.getEntities(m_models, shaderPrograms);
|
||||
m_skybox = modelParser.getSkybox(getModelByName("cube"),
|
||||
Controller::getShaderProgramByName(shaderPrograms, "skyboxProgram"));
|
||||
|
||||
JsonParser lightParser("data/lights.json");
|
||||
lights = lightParser.getLights(shaderProgram);
|
||||
m_lights = lightParser.getLights(m_shaderProgram);
|
||||
}
|
||||
|
||||
World::~World()
|
||||
{
|
||||
// Iterate over depthMapPointFBO vector and delete all items
|
||||
for (auto it = depthMapPointFBO.begin(); it != depthMapPointFBO.end(); it++) {
|
||||
for (auto it = m_depthMapPointFBO.begin(); it != m_depthMapPointFBO.end(); it++) {
|
||||
delete (*it);
|
||||
}
|
||||
// Iterate over models and entities and delete all items
|
||||
for (auto it = models.begin(); it != models.end(); it++) {
|
||||
for (auto it = m_models.begin(); it != m_models.end(); it++) {
|
||||
delete (*it);
|
||||
}
|
||||
for (auto it = entities.begin(); it != entities.end(); it++) {
|
||||
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
|
||||
delete (*it);
|
||||
}
|
||||
|
||||
delete skybox;
|
||||
delete m_skybox;
|
||||
}
|
||||
|
||||
void World::addEntity(Entity *entity)
|
||||
{
|
||||
entities.push_back(entity);
|
||||
m_entities.push_back(entity);
|
||||
}
|
||||
|
||||
void World::removeEntityByName(std::string name)
|
||||
{
|
||||
for (auto it = entities.begin(); it != entities.end(); it++) {
|
||||
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
|
||||
if ((*it)->getUniqueName() == name) {
|
||||
entities.erase(it);
|
||||
m_entities.erase(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -70,8 +70,8 @@ void World::removeEntityByName(std::string name)
|
||||
|
||||
void World::clearEntities()
|
||||
{
|
||||
for (auto it = entities.begin(); it != entities.end(); it++) {
|
||||
entities.erase(it);
|
||||
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
|
||||
m_entities.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ void World::updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 c
|
||||
void World::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition)
|
||||
{
|
||||
// Draw all entities
|
||||
for (auto it = entities.begin(); it != entities.end(); it++) {
|
||||
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
|
||||
(*it)->draw(viewProjMatrix, viewPosition);
|
||||
}
|
||||
|
||||
@@ -113,60 +113,66 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg
|
||||
// Switch face culling (Peter panning)
|
||||
glCullFace(GL_BACK);
|
||||
|
||||
depthMapDirectionalFBO.bind();
|
||||
m_depthMapDirectionalFBO.bind();
|
||||
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// --- Directional shadows ---
|
||||
glm::mat4 directionalLightView =
|
||||
glm::lookAt(-5.0f * glm::vec3(-0.2f, -1.0f, -0.3f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
glm::mat4 directionalLightViewProjectionMatrix = directionalLightProjection * directionalLightView;
|
||||
glm::mat4 directionalLightViewProjectionMatrix = m_directionalLightProjection * directionalLightView;
|
||||
|
||||
// Draw scene from light perspective
|
||||
// Draw all entities
|
||||
for (auto it = entities.begin(); it != entities.end(); it++) {
|
||||
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
|
||||
(*it)->drawDirectionalShadows(directionalLightViewProjectionMatrix, directionalShaderProgram);
|
||||
}
|
||||
|
||||
depthMapDirectionalFBO.unbind();
|
||||
m_depthMapDirectionalFBO.unbind();
|
||||
|
||||
shaderProgram->bind();
|
||||
m_shaderProgram->bind();
|
||||
|
||||
// Send lightViewProjMatrix to basic shader
|
||||
shaderProgram->setUniform("u_directionalLightViewProjMatrix", directionalLightViewProjectionMatrix);
|
||||
m_shaderProgram->setUniform("u_directionalLightViewProjMatrix", directionalLightViewProjectionMatrix);
|
||||
|
||||
// Send shadowMap to basic shader
|
||||
int textureUnit = static_cast<int>(TextureType::TEXTURE_TYPE_NUM_ITEMS) * 2;
|
||||
shaderProgram->setUniform("u_texture_directionalShadowMap", (int)textureUnit);
|
||||
m_shaderProgram->setUniform("u_texture_directionalShadowMap", (int)textureUnit);
|
||||
glActiveTexture(GL_TEXTURE0 + textureUnit);
|
||||
glBindTexture(GL_TEXTURE_2D, depthMapDirectionalFBO.getDepthMap());
|
||||
glBindTexture(GL_TEXTURE_2D, m_depthMapDirectionalFBO.getDepthMap());
|
||||
|
||||
shaderProgram->unbind();
|
||||
m_shaderProgram->unbind();
|
||||
|
||||
// --- Point shadows ---
|
||||
std::vector<PointLight *> pointLights = getPointLights();
|
||||
|
||||
// 4 depthMaps for 4 point lights
|
||||
for (int i = 0; i < 1; i++) {
|
||||
depthMapPointFBO[i]->bind();
|
||||
m_depthMapPointFBO[i]->bind();
|
||||
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// Create 6 view matrices for every face of the cubeMap
|
||||
std::vector<glm::mat4> viewProjMatrices;
|
||||
glm::vec3 lightPos = pointLights[i]->getPosition();
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(1.0f, 0.0f, 0.0f),
|
||||
glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(-1.0f, 0.0f, 0.0f),
|
||||
glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
glm::vec3(0.0f, 0.0f, 1.0f)));
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, -1.0f, 0.0f),
|
||||
glm::vec3(0.0f, 0.0f, -1.0f)));
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, 1.0f),
|
||||
glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, -1.0f),
|
||||
glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos,
|
||||
lightPos + glm::vec3(1.0f, 0.0f, 0.0f),
|
||||
glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos,
|
||||
lightPos + glm::vec3(-1.0f, 0.0f, 0.0f),
|
||||
glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos,
|
||||
lightPos + glm::vec3(0.0f, 1.0f, 0.0f),
|
||||
glm::vec3(0.0f, 0.0f, 1.0f)));
|
||||
viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos,
|
||||
lightPos + glm::vec3(0.0f, -1.0f, 0.0f),
|
||||
glm::vec3(0.0f, 0.0f, -1.0f)));
|
||||
viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos,
|
||||
lightPos + glm::vec3(0.0f, 0.0f, 1.0f),
|
||||
glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos,
|
||||
lightPos + glm::vec3(0.0f, 0.0f, -1.0f),
|
||||
glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
|
||||
pointShaderProgram->bind();
|
||||
|
||||
@@ -175,27 +181,27 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg
|
||||
viewProjMatrices[i]);
|
||||
}
|
||||
|
||||
pointShaderProgram->setUniform("pointShadowDepthMapFarPlane", far_plane_point);
|
||||
pointShaderProgram->setUniform("pointShadowDepthMapFarPlane", m_farPlanePoint);
|
||||
pointShaderProgram->setUniform("v_lightPos", lightPos);
|
||||
|
||||
// Draw scene from light perspective
|
||||
// Draw all entities
|
||||
for (auto it = entities.begin(); it != entities.end(); it++) {
|
||||
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
|
||||
(*it)->drawPointShadows(pointShaderProgram);
|
||||
}
|
||||
|
||||
depthMapPointFBO[i]->unbind();
|
||||
m_depthMapPointFBO[i]->unbind();
|
||||
|
||||
shaderProgram->bind();
|
||||
m_shaderProgram->bind();
|
||||
|
||||
shaderProgram->setUniform("pointShadowDepthMapFarPlane", far_plane_point);
|
||||
m_shaderProgram->setUniform("pointShadowDepthMapFarPlane", m_farPlanePoint);
|
||||
|
||||
textureUnit = static_cast<int>(TextureType::TEXTURE_TYPE_NUM_ITEMS) * 2 + i + 1;
|
||||
shaderProgram->setUniform("u_texture_pointShadowMap0", (int)textureUnit);
|
||||
m_shaderProgram->setUniform("u_texture_pointShadowMap0", (int)textureUnit);
|
||||
glActiveTexture(GL_TEXTURE0 + textureUnit);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, depthMapPointFBO[i]->getCubeMapId());
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, m_depthMapPointFBO[i]->getCubeMapId());
|
||||
|
||||
shaderProgram->unbind();
|
||||
m_shaderProgram->unbind();
|
||||
}
|
||||
|
||||
// Reset viewport size
|
||||
@@ -205,7 +211,7 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg
|
||||
|
||||
Model *World::getModelByName(std::string name)
|
||||
{
|
||||
for (auto it = models.begin(); it != models.end(); it++) {
|
||||
for (auto it = m_models.begin(); it != m_models.end(); it++) {
|
||||
if ((*it)->getUniqueName() == name) {
|
||||
return *it;
|
||||
}
|
||||
@@ -216,7 +222,7 @@ Model *World::getModelByName(std::string name)
|
||||
|
||||
Entity *World::getEntityByName(std::string name)
|
||||
{
|
||||
for (auto it = entities.begin(); it != entities.end(); it++) {
|
||||
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
|
||||
if ((*it)->getUniqueName() == name) {
|
||||
return *it;
|
||||
}
|
||||
@@ -227,7 +233,7 @@ Entity *World::getEntityByName(std::string name)
|
||||
|
||||
Entity *World::getEntityById(uint32_t id)
|
||||
{
|
||||
for (auto it = entities.begin(); it != entities.end(); it++) {
|
||||
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
|
||||
if ((*it)->getId() == id) {
|
||||
return *it;
|
||||
}
|
||||
@@ -240,7 +246,7 @@ std::vector<PointLight *> World::getPointLights()
|
||||
{
|
||||
std::vector<PointLight *> temp_pointLights;
|
||||
|
||||
for (auto it = lights.begin(); it != lights.end(); it++) {
|
||||
for (auto it = m_lights.begin(); it != m_lights.end(); it++) {
|
||||
PointLight *temp_pointLight = dynamic_cast<PointLight *>(*it);
|
||||
if (temp_pointLight) {
|
||||
temp_pointLights.push_back(temp_pointLight);
|
||||
@@ -254,7 +260,7 @@ DirectionalLight *World::getDirectionalLight()
|
||||
{
|
||||
DirectionalLight *temp_directionalLight = nullptr;
|
||||
|
||||
for (auto it = lights.begin(); it != lights.end(); it++) {
|
||||
for (auto it = m_lights.begin(); it != m_lights.end(); it++) {
|
||||
temp_directionalLight = dynamic_cast<DirectionalLight *>(*it);
|
||||
if (temp_directionalLight)
|
||||
break;
|
||||
@@ -265,10 +271,10 @@ DirectionalLight *World::getDirectionalLight()
|
||||
|
||||
std::vector<Entity *> World::getEntities()
|
||||
{
|
||||
return entities;
|
||||
return m_entities;
|
||||
}
|
||||
|
||||
Skybox *World::getSkybox()
|
||||
{
|
||||
return skybox;
|
||||
return m_skybox;
|
||||
}
|
||||
|
||||
34
src/World.h
34
src/World.h
@@ -4,7 +4,7 @@
|
||||
|
||||
#include "Camera.h"
|
||||
#include "Entity.h"
|
||||
#include "Framebuffer.h"
|
||||
#include "FrameBuffer.h"
|
||||
#include "Light.h"
|
||||
#include "ShaderProgram.h"
|
||||
|
||||
@@ -33,27 +33,27 @@ public:
|
||||
void calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram);
|
||||
|
||||
private:
|
||||
ShaderProgram *shaderProgram;
|
||||
ShaderProgram *m_shaderProgram;
|
||||
|
||||
std::vector<Model *> models;
|
||||
std::vector<Entity *> entities;
|
||||
Skybox *skybox;
|
||||
std::vector<Model *> m_models;
|
||||
std::vector<Entity *> m_entities;
|
||||
Skybox *m_skybox;
|
||||
|
||||
// Lights
|
||||
std::vector<Light *> lights;
|
||||
std::vector<Light *> m_lights;
|
||||
|
||||
// Shadows
|
||||
const int SHADOW_RES = 4096 / 4;
|
||||
DepthMap depthMapDirectionalFBO;
|
||||
std::vector<DepthMap *> depthMapPointFBO;
|
||||
DepthMap m_depthMapDirectionalFBO;
|
||||
std::vector<DepthMap *> m_depthMapPointFBO;
|
||||
// Shadow projection matrices
|
||||
const float near_plane_directional = 1.0f;
|
||||
const float far_plane_directional = 15.0f;
|
||||
glm::mat4 directionalLightProjection =
|
||||
glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane_directional, far_plane_directional);
|
||||
const float aspect_ratio_point = 1.0f;
|
||||
const float near_plane_point = 1.0f;
|
||||
const float far_plane_point = 25.0f;
|
||||
glm::mat4 pointLightProjection =
|
||||
glm::perspective(glm::radians(90.0f), aspect_ratio_point, near_plane_point, far_plane_point);
|
||||
const float m_nearPlaneDirectional = 1.0f;
|
||||
const float m_farPlaneDirectional = 15.0f;
|
||||
glm::mat4 m_directionalLightProjection =
|
||||
glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, m_nearPlaneDirectional, m_farPlaneDirectional);
|
||||
const float m_aspectRatioPoint = 1.0f;
|
||||
const float m_nearPlanePoint = 1.0f;
|
||||
const float m_farPlanePoint = 25.0f;
|
||||
glm::mat4 m_pointLightProjection =
|
||||
glm::perspective(glm::radians(90.0f), m_aspectRatioPoint, m_nearPlanePoint, m_farPlanePoint);
|
||||
};
|
||||
|
||||
@@ -60,9 +60,9 @@ int main(int argc, char** argv) {
|
||||
Model currentModel;
|
||||
if(((*it).find('/')) < (*it).length()) {
|
||||
// source includes a /
|
||||
currentModel.directory = (*it).substr(0, (*it).find_last_of('/'));
|
||||
currentModel.m_workingPath = (*it).substr(0, (*it).find_last_of('/'));
|
||||
} else {
|
||||
currentModel.directory = ".";
|
||||
currentModel.m_workingPath = ".";
|
||||
}
|
||||
|
||||
processNode(scene->mRootNode, scene, ¤tModel);
|
||||
@@ -96,9 +96,9 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
|
||||
// Write meshes
|
||||
uint32_t numMeshes = currentModel.meshes.size();
|
||||
uint32_t numMeshes = currentModel.m_meshes.size();
|
||||
output.write((char*) &numMeshes, sizeof(uint32_t));
|
||||
for(auto it1 = currentModel.meshes.begin(); it1 != currentModel.meshes.end(); it1++) {
|
||||
for (auto it1 = currentModel.m_meshes.begin(); it1 != currentModel.m_meshes.end(); it1++) {
|
||||
uint32_t numVertices = (*it1).vertices.size();
|
||||
uint32_t numIndices = (*it1).indices.size();
|
||||
uint32_t numTextureIds = (*it1).textureIds.size();
|
||||
@@ -128,7 +128,7 @@ void processNode(aiNode *node, const aiScene *scene, Model* model) {
|
||||
// Push the node's meshes into the mesh vector
|
||||
for(uint32_t i = 0; i < node->mNumMeshes; i++) {
|
||||
aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
|
||||
model->meshes.push_back(processMesh(mesh, scene, model));
|
||||
model->m_meshes.push_back(processMesh(mesh, scene, model));
|
||||
}
|
||||
|
||||
// Process child nodes too
|
||||
@@ -228,7 +228,7 @@ std::vector<Texture> loadMaterialTextures(aiMaterial *mat, aiTextureType type, u
|
||||
aiString filename;
|
||||
mat->GetTexture(type, i, &filename);
|
||||
|
||||
std::string currentPath = model->directory + '/' + filename.C_Str();
|
||||
std::string currentPath = model->m_workingPath + '/' + filename.C_Str();
|
||||
|
||||
bool skip = 0;
|
||||
for(uint32_t j = 0; j < model->textures.size(); j++) {
|
||||
@@ -244,12 +244,12 @@ std::vector<Texture> loadMaterialTextures(aiMaterial *mat, aiTextureType type, u
|
||||
texture.pathToTexture = currentPath;
|
||||
texture.textureType = textureType;
|
||||
// textureIds start at 0, but vector elements start at 1.
|
||||
texture.textureId = model->textures.size();
|
||||
texture.m_textureId = model->textures.size();
|
||||
|
||||
model->textures.push_back(texture);
|
||||
|
||||
// Add newest texture id to mesh
|
||||
mesh->textureIds.push_back(texture.textureId);
|
||||
mesh->textureIds.push_back(texture.m_textureId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,7 @@ struct Texture {
|
||||
|
||||
std::string pathToTexture;
|
||||
uint32_t textureType;
|
||||
uint32_t textureId;
|
||||
|
||||
uint32_t m_textureId;
|
||||
};
|
||||
|
||||
struct Mesh {
|
||||
@@ -22,7 +21,6 @@ struct Mesh {
|
||||
struct Model {
|
||||
|
||||
std::vector<Texture> textures;
|
||||
std::vector<Mesh> meshes;
|
||||
std::string directory;
|
||||
|
||||
std::vector<Mesh> m_meshes;
|
||||
std::string m_workingPath;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user