From 3c7968407b3d526cab39f22ba84fc9c78eb78059 Mon Sep 17 00:00:00 2001 From: 4VRDriver <44267643+4VRDriver@users.noreply.github.com> Date: Thu, 17 Sep 2020 21:23:38 +0200 Subject: [PATCH] Implement Quaternions Undefined behaviour of the light source --- imgui.ini | 4 ++-- res/shaders/basic.frag | 2 +- res/shaders/light.vert | 2 -- src/Controller.cpp | 17 ++++++++--------- src/Entity.cpp | 25 ++++++++++++++----------- src/Entity.h | 8 ++++++-- src/Light.cpp | 10 ---------- src/Light.h | 5 ++++- src/Window.cpp | 10 ++++++++++ src/World.cpp | 2 +- src/defines.h | 4 ++-- 11 files changed, 48 insertions(+), 41 deletions(-) diff --git a/imgui.ini b/imgui.ini index 6bd5573..16404b5 100644 --- a/imgui.ini +++ b/imgui.ini @@ -4,7 +4,7 @@ Size=400,400 Collapsed=0 [Window][Object Modifier] -Pos=59,22 -Size=925,127 +Pos=23,18 +Size=899,152 Collapsed=0 diff --git a/res/shaders/basic.frag b/res/shaders/basic.frag index b83ff29..faa564f 100644 --- a/res/shaders/basic.frag +++ b/res/shaders/basic.frag @@ -109,7 +109,7 @@ vec3 directionalLightContribution(DirectionalLight light, vec3 normal, vec3 view vec3 ambient, diffuse, specular; computeShading(light.ambient, light.diffuse, light.specular, lightDir, viewDir, normal, ambient, diffuse, specular); - return (ambient + diffuse + specular); + return (ambient + diffuse + specular) * 0.25f; } vec3 pointLightContribution(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) { diff --git a/res/shaders/light.vert b/res/shaders/light.vert index 08995b5..7864c96 100644 --- a/res/shaders/light.vert +++ b/res/shaders/light.vert @@ -1,8 +1,6 @@ #version 330 core layout(location = 0) in vec3 a_position; -layout(location = 1) in vec2 a_texCoord; -layout(location = 2) in vec4 a_color; uniform mat4 u_modelViewProjMatrix; diff --git a/src/Controller.cpp b/src/Controller.cpp index dbb7a7a..c5a2c3c 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -80,9 +80,10 @@ void Controller::run() { //Model model_backpack("res/models/backpack.ffo"); //Model model_plant("res/models/plant.ffo"); - Model model_container("res/models/container.ffo"); + //Model model_container("res/models/container.ffo"); Model model_cube("res/models/cube.ffo"); Model model_dragon("res/models/dragon.ffo"); + //Model model_moon("res/models/moon.ffo"); //Model model_hut("res/models/hut.ffo"); //Model model_sphere("res/models/sphere.ffo"); @@ -91,19 +92,18 @@ void Controller::run() { //Entity container(&model_container, &shaderProgram); //Entity hut(&model_hut, &shaderProgram); Entity dragon(&model_dragon, &shaderProgram); + //Entity moon(&model_moon, &shaderProgram); //Entity plant(&model_plant, &shaderProgram); Entity lightSource(&model_cube, &lightProgram); - lightSource.translate(glm::vec3(-5.0f, 1.0f, 0.0f)); - lightSource.setScale(0.2f); - //plant.setScale(5.0f); - //dragon.setScale(0.2f); + dragon.setScale(0.2f); + lightSource.setScale(0.1f); World world(&shaderProgram); world.addEntity(dragon); world.addEntity(lightSource); - camera->translate(glm::vec3(0.0f, 0.0f, 7.5f)); + camera->translate(glm::vec3(0.0f, 1.5f, 5.0f)); // This is the game loop while(!glfwWindowShouldClose(gameWindow->getGLFWwindow())) { @@ -112,9 +112,8 @@ void Controller::run() { // Update game // ... - //world.getEntities()->operator[](0).rotate(glm::vec3(0.f,1.0f,0.f), deltaTime*0.2); float radius = 4.0; - glm::vec3 newPos = glm::vec3(cos(glfwGetTime()*0.2), 0.f, sin(glfwGetTime()*0.2)) * radius; + glm::vec3 newPos = glm::vec3(-cos(glfwGetTime()*0.2), 0.5f, sin(glfwGetTime()*0.2)) * radius; world.getEntities()->operator[](1).setPosition(newPos); static glm::vec3 lightColor = glm::vec3(1.f); world.updatePointLight(0, true, world.getEntities()->operator[](1).getPosition(), lightColor); @@ -203,7 +202,7 @@ void Controller::renderImGui(std::vector *entites, glm::vec3 *lightColor ImGui::SliderFloat("Scale", &scale, 0.02, 2.0); entites->operator[](0).setPosition(glm::vec3(translation[0], 0.0f, translation[1])); - entites->operator[](0).setRotation(glm::vec3(0.f,1.0f,0.f) * rotation); + entites->operator[](0).setRotation(glm::vec3(0.f,1.0f,0.f), rotation); entites->operator[](0).setScale(scale); // color picker diff --git a/src/Entity.cpp b/src/Entity.cpp index fe0bb8f..0b9dec9 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -3,6 +3,7 @@ #include #include + Entity::Entity(Model *model, ShaderProgram *shaderProgram) : model(model), shaderProgram(shaderProgram) { @@ -37,7 +38,8 @@ void Entity::translate(glm::vec3 vector) { } void Entity::rotate(glm::vec3 axis, float radians) { - orientation += axis * radians; + glm::quat rotation = glm::angleAxis(radians, axis); + quaternion = rotation * quaternion; updateModelMatrix(); } @@ -46,8 +48,13 @@ void Entity::setPosition(glm::vec3 position) { updateModelMatrix(); } -void Entity::setRotation(glm::vec3 orientation) { - this->orientation = orientation; +void Entity::setRotation(glm::vec3 eulerAngles) { + quaternion = glm::quat(eulerAngles); + updateModelMatrix(); +} + +void Entity::setRotation(glm::vec3 axis, float radians) { + quaternion = glm::angleAxis(radians, axis); updateModelMatrix(); } @@ -58,22 +65,18 @@ void Entity::setScale(float scaleFactor) { void Entity::updateModelMatrix() { - glm::mat4 newModelMatrix(1.0f); - // Translate * Rotate * Scale * vertex_vec; // First scaling, then rotation, then translation // Translate - newModelMatrix = glm::translate(newModelMatrix, position); + glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), position); // Rotate - newModelMatrix = glm::rotate(newModelMatrix, orientation.x, glm::vec3(1.f, 0.f, 0.f)); - newModelMatrix = glm::rotate(newModelMatrix, orientation.y, glm::vec3(0.f, 1.f, 0.f)); - newModelMatrix = glm::rotate(newModelMatrix, orientation.z, glm::vec3(0.f, 0.f, 1.f)); + glm::mat4 rotationMatrix = glm::toMat4(quaternion); // Scale - newModelMatrix = glm::scale(newModelMatrix, glm::vec3(modelScale, modelScale, modelScale)); + glm::mat4 scaleMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(modelScale, modelScale, modelScale)); - modelMatrix = newModelMatrix; + modelMatrix = translationMatrix * rotationMatrix * scaleMatrix; } \ No newline at end of file diff --git a/src/Entity.h b/src/Entity.h index e568dc9..4ff85c9 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -2,7 +2,9 @@ #include "Model.h" #include "ShaderProgram.h" + #include +#include class Entity { @@ -17,7 +19,8 @@ public: void rotate(glm::vec3 axis, float radians); void setPosition(glm::vec3 position); - void setRotation(glm::vec3 orientation); + void setRotation(glm::vec3 eulerAngles); + void setRotation(glm::vec3 axis, float radians); void setScale(float scaleFactor); void setId(uint32_t id) { this->id = id; } @@ -33,9 +36,10 @@ private: uint32_t id; glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f); - glm::vec3 orientation = glm::vec3(0.0f, 1.0f, 0.0f); + glm::quat quaternion; glm::vec3 velocity = glm::vec3(0.0f, 0.0f, 0.0f); + float modelScale = 1.0f; Model *model; diff --git a/src/Light.cpp b/src/Light.cpp index 3b5339c..8c745ef 100644 --- a/src/Light.cpp +++ b/src/Light.cpp @@ -11,11 +11,6 @@ PointLight::PointLight(ShaderProgram *shaderProgram) void PointLight::update() { - // Only temp - glm::vec3 diffuseColor = lightColor * glm::vec3(1.0f); - glm::vec3 ambientColor = diffuseColor * glm::vec3(0.1f); - glm::vec3 specularColor = lightColor * glm::vec3(1.0f); - shaderProgram->bind(); shaderProgram->setUniform((_getStructMemberName() + "isActive").c_str(), isActive); @@ -46,11 +41,6 @@ DirectionalLight::DirectionalLight(ShaderProgram *shaderProgram) void DirectionalLight::update() { - // Only temp - glm::vec3 diffuseColor = lightColor * glm::vec3(1.0f); - glm::vec3 ambientColor = diffuseColor * glm::vec3(0.1f); - glm::vec3 specularColor = lightColor * glm::vec3(1.0f); - shaderProgram->bind(); shaderProgram->setUniform("u_directionalLight.isActive", isActive); diff --git a/src/Light.h b/src/Light.h index 31558b2..a7770a1 100644 --- a/src/Light.h +++ b/src/Light.h @@ -17,7 +17,10 @@ public: update(); } void setColor(glm::vec3 color) { - lightColor = color; + lightColor = color; + diffuseColor = lightColor * glm::vec3(1.0f); + ambientColor = diffuseColor * glm::vec3(0.1f); + specularColor = lightColor * glm::vec3(1.0f); update(); } diff --git a/src/Window.cpp b/src/Window.cpp index e5a95d9..e95ff84 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -28,12 +28,22 @@ Window::Window() { // Enable z buffer glEnable(GL_DEPTH_TEST); + // Enable face culling + glEnable(GL_CULL_FACE); + glFrontFace(GL_CW); + glCullFace(GL_FRONT); + // Disable mouse cursor #ifdef _DEBUG mouseCatched = false; #endif setCatchedCursor(mouseCatched); + // Maximize in release build + #ifndef _DEBUG + glfwMaximizeWindow(window); + #endif + #ifdef _DEBUG std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl; glEnable(GL_DEBUG_OUTPUT); diff --git a/src/World.cpp b/src/World.cpp index 0b7becd..5bac0af 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -50,8 +50,8 @@ void World::updatePointLight(unsigned int lightId, bool active, glm::vec3 positi void World::updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 color) { directionalLight.setActive(active); + directionalLight.setDirection(direction); directionalLight.setDirection(glm::vec3(-0.2f, -1.0f, -0.3f)); - //directionalLight.setDirection(direction); directionalLight.setColor(color); } diff --git a/src/defines.h b/src/defines.h index e5fbdbf..e6007e5 100644 --- a/src/defines.h +++ b/src/defines.h @@ -1,8 +1,8 @@ - #pragma once +#pragma once #include -#define INIT_WINDOW_WIDTH 960 +#define INIT_WINDOW_WIDTH 1280 #define INIT_WINDOW_HEIGHT 720 enum textureType{