Basic concept working

This commit is contained in:
4VRDriver
2020-09-12 21:21:36 +02:00
parent 712c2655cf
commit 917c0db0d0
9 changed files with 84 additions and 20 deletions

View File

@@ -4,10 +4,10 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/ext/matrix_transform.hpp>
Camera::Camera(float fov, int width, int height) {
Camera::Camera(float fov, float aspectRatio) {
this->fov = fov;
viewMatrix = glm::mat4(1.0f);
updateAspectRatio(width, height);
updateAspectRatio(aspectRatio);
updateVPM();
}
@@ -15,9 +15,9 @@ void Camera::updateVPM() {
viewProjectionMatrix = projectionMatrix * viewMatrix;
}
void Camera::updateAspectRatio(int width, int height) {
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, (float)width / (float)height, 0.1f, 1000.0f);
projectionMatrix = glm::perspective(fov/2.0f, aspectRatio, 0.1f, 1000.0f);
updateVPM();
}

View File

@@ -6,11 +6,11 @@ class Camera {
public:
Camera(float fov, int width, int height);
Camera(float fov, float aspectRatio);
~Camera() = default;
void updateVPM();
void updateAspectRatio(int width, int height);
void updateAspectRatio(float aspectRatio);
void updatePositionFromKeyboardInput(bool *actionCameraRegister, float deltaTime);
void updateDirectionFromMouseInput(double *cameraMouseActionRegister);

View File

@@ -38,7 +38,8 @@ Controller::Controller() {
gameWindow = new Window();
gameEventHandler = new EventHandler(gameWindow->getGLFWwindow());
camera = new Camera(90.0f, gameWindow->getWindowWidth(), gameWindow->getWindowHeight());
camera = new Camera(90.0f, gameWindow->getWindowAspectRatio());
#ifdef _DEBUG
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
@@ -77,8 +78,6 @@ void Controller::run() {
ShaderProgram shaderProgram("res/shaders/basic.vert", "res/shaders/basic.frag");
ShaderProgram lightProgram("res/shaders/light.vert", "res/shaders/light.frag");
std::vector<Entity> scene;
//Model model_backpack("res/models/backpack.ffo");
//Model model_plant("res/models/plant.ffo");
Model model_container("res/models/container.ffo");
@@ -113,9 +112,8 @@ void Controller::run() {
shaderProgram.unbind();
Scene scene(&shaderProgram);
scene.push_back(dragon);
scene.push_back(lightSource);
scene.addEntity(dragon);
scene.addEntity(lightSource);
camera->translate(glm::vec3(0.0f, 0.0f, 7.5f));
@@ -133,12 +131,10 @@ void Controller::run() {
camera->lookForward();
camera->updateVPM();
for(auto it = scene.begin(); it != scene.end(); it++) {
it->draw(camera->getViewProj(), camera->getPosition());
}
scene.drawScene(camera->getViewProj(), camera->getPosition());
#ifdef _DEBUG
renderImGui(&scene[0]);
renderImGui(scene.getEntities().data());
#endif
glfwSwapBuffers(gameWindow->getGLFWwindow());
@@ -187,7 +183,7 @@ void Controller::error_callback(int error, const char* description) {
}
void Controller::updateWindowSize() {
camera->updateAspectRatio(gameWindow->getWindowWidth(), gameWindow->getWindowHeight());
camera->updateAspectRatio(gameWindow->getWindowAspectRatio());
gameEventHandler->setFirstMouseInput(1);
}

View File

@@ -20,6 +20,9 @@ public:
void setOrientiation(glm::vec3 orientation);
void setScale(float scaleFactor);
void setId(uint32_t id) { this->id = id; }
uint32_t getId() { return id; }
glm::vec3 getPosition() { return position; }
glm::mat4 getModelMatrix() { return modelMatrix; }
@@ -27,7 +30,6 @@ private:
void updateModelMatrix();
// May be used later...
uint32_t id;
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);

View File

@@ -5,6 +5,7 @@
PointLight::PointLight(ShaderProgram *shaderProgram)
: Light(shaderProgram) {
isActive = false;
}

View File

@@ -13,9 +13,14 @@ public:
virtual void update() = 0;
void setActive(bool active) { isActive = active; }
void setShaderProgram(ShaderProgram *shaderProgram) {
this->shaderProgram = shaderProgram;
update();
}
protected:
Light() = default;
Light(ShaderProgram *shaderProgram) : shaderProgram(shaderProgram) {}
ShaderProgram *shaderProgram;
@@ -32,15 +37,23 @@ protected:
class PointLight : public Light {
public:
PointLight() = default;
PointLight(ShaderProgram *shaderProgram);
void setPosition(glm::vec3 position) {
this->position = position;
update();
}
void update();
private:
unsigned int lightId;
glm::vec3 position;
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
float K_c = 1.0f;
float K_l = 0.09f;
float K_q = 0.032f;

View File

@@ -1,6 +1,43 @@
#include "Scene.h"
#include <iostream>
// At this moment, I don't have any idea how to implement the initializer list
// with an array of objects that don't have an default contructor. So the default
// constructors for Light and PointLight have only this reason to exist.
Scene::Scene(ShaderProgram *shaderProgram)
: shaderProgram(shaderProgram), pointLights(shaderProgram) {
: shaderProgram(shaderProgram) {
for(unsigned int i = 0; i < NUM_POINT_LIGHTS; i++) {
pointLights[i].setShaderProgram(shaderProgram);
}
pointLights[0].setActive(true);
}
void Scene::addEntity(Entity entity) {
uint32_t new_id = entities.size();
entity.setId(new_id);
entities.push_back(entity);
}
void Scene::removeEntity(uint32_t id) {
for(auto it = entities.begin(); it != entities.end(); it++) {
if(it->getId() == id) {
entities.erase(it);
}
}
std::cout << "[Warning] No Entity found with ID " << id << std::endl;
}
void Scene::drawScene(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) {
for(auto it = entities.begin(); it != entities.end(); it++) {
it->draw(viewProjMatrix, viewPosition);
}
}

View File

@@ -1,17 +1,31 @@
#pragma once
#include <vector>
#include "Light.h"
#include "Camera.h"
#include "Entity.h"
class Scene {
public:
Scene(ShaderProgram *shaderProgram);
~Scene() = default;
void addEntity(Entity entity);
void removeEntity(uint32_t id);
std::vector<Entity> getEntities() { return entities; }
void drawScene(glm::mat4 viewProjMatrix, glm::vec3 viewPosition);
private:
ShaderProgram *shaderProgram;
std::vector<Entity> entities;
//DirectionalLight directionalLight;
PointLight pointLights[NUM_POINT_LIGHTS];
//SpotLight spotLight;

View File

@@ -13,6 +13,7 @@ public:
int getWindowWidth() { return width; }
int getWindowHeight() { return height; }
float getWindowAspectRatio() { return (float) width / (float) height; }
bool getMouseIsCatched() { return mouseCatched; }