Add a basic entity class

This commit is contained in:
4VRDriver
2020-09-06 13:02:40 +02:00
parent c0cbf8c257
commit e4139105ee
20 changed files with 150 additions and 77 deletions

2
.vscode/launch.json vendored
View File

@@ -14,7 +14,7 @@
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"externalConsole": true,
"MIMode": "gdb",
"preLaunchTask": "Build",
"setupCommands": [

View File

@@ -72,6 +72,7 @@
"cfenv": "cpp",
"typeindex": "cpp",
"valarray": "cpp",
"variant": "cpp"
"variant": "cpp",
"forward_list": "cpp"
}
}

View File

@@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.10)
project(Fall-Fever)
#if(WIN32)
if(WIN32)
set(GLFW3_ROOT "${PROJECT_SOURCE_DIR}/glfw")
#endif(WIN32)
endif(WIN32)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
@@ -21,3 +21,5 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")
add_subdirectory(${PROJECT_SOURCE_DIR}/lib)
add_subdirectory(${PROJECT_SOURCE_DIR}/src)
add_subdirectory(${PROJECT_SOURCE_DIR}/tools)

View File

@@ -1,5 +1,7 @@
#version 330 core
layout(location = 0) out vec4 f_color;
struct Material {
sampler2D u_texture_diffuse0;
sampler2D u_texture_diffuse1;
@@ -7,18 +9,19 @@ struct Material {
sampler2D u_texture_specular1;
};
layout(location = 0) out vec4 f_color;
in vec4 v_color;
in vec2 v_texCoord;
uniform vec3 v_objectColor;
uniform vec3 v_lightColor;
uniform Material material;
uniform float mix_val;
void main() {
//f_color = v_color;
vec4 texColor1 = texture(material.u_texture_diffuse0, v_texCoord);
vec4 texColor2 = texture(material.u_texture_diffuse1, v_texCoord);
f_color = mix(texColor1, texColor2, mix_val);
//vec4 texColor2 = texture(material.u_texture_diffuse1, v_texCoord);
//f_color = vec4(v_objectColor * v_lightColor, 1.0f);
f_color = texColor1;
}

7
res/shaders/light.fs Normal file
View File

@@ -0,0 +1,7 @@
#version 330 core
layout(location = 0) out vec4 f_color;
void main() {
f_color = vec4(1.0f);
}

16
res/shaders/light.vs Normal file
View File

@@ -0,0 +1,16 @@
#version 330 core
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec2 a_texCoord;
layout(location = 2) in vec4 a_color;
out vec4 v_color;
out vec2 v_texCoord;
uniform mat4 u_modelViewProj;
void main() {
gl_Position = u_modelViewProj * vec4(a_position, 1.0f);
v_texCoord = a_texCoord;
v_color = a_color;
}

View File

@@ -9,6 +9,7 @@ add_executable(Fall-Fever
Camera.cpp
Mesh.cpp
Model.cpp
Entity.cpp
)
target_link_libraries(

View File

@@ -1,6 +1,9 @@
#include "Camera.h"
#include "eventActions.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/ext/matrix_transform.hpp>
Camera::Camera(float fov, int width, int height) {
this->fov = fov;
viewMatrix = glm::mat4(1.0f);

View File

@@ -1,8 +1,6 @@
#pragma once
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/ext/matrix_transform.hpp>
class Camera {

View File

@@ -15,6 +15,7 @@
#include "Controller.h"
#include "Texture.h"
#include "Model.h"
#include "Entity.h"
Controller::Controller() {
if(!glfwInit()) exit(-1);
@@ -24,9 +25,8 @@ Controller::Controller() {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef _DEBUG
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
#endif
glfwSetErrorCallback(error_callback);
#endif
gameWindow = new Window();
gameEventHandler = new EventHandler(gameWindow->getGLFWwindow());
@@ -36,6 +36,7 @@ Controller::Controller() {
Controller::~Controller() {
delete gameWindow;
delete gameEventHandler;
delete camera;
glfwTerminate();
}
@@ -43,56 +44,22 @@ void Controller::run() {
glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
ShaderProgram shaderProgram("res/shaders/basic.vs", "res/shaders/basic.fs");
shaderProgram.bind();
std::vector<Vertex> vertices {
Vertex{
glm::vec3(-0.5f, -0.5f, 0.0f),
glm::vec2(0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec4(1.0f, 0.0f, 0.0f, 1.0f)},
Vertex{
glm::vec3(0.5f, -0.5f, 0.0f),
glm::vec2(1.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec4(0.0f, 1.0f, 0.0f, 1.0f)},
Vertex{
glm::vec3(-0.5f, 0.5f, 0.0f),
glm::vec2(0.0f, 1.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)},
Vertex{
glm::vec3(0.5f, 0.5f, 0.0f),
glm::vec2(1.0f, 1.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)}
};
ShaderProgram lightProgram("res/shaders/light.vs", "res/shaders/light.fs");
std::vector<uint32_t> indices {
0, 1, 2,
1, 2, 3
};
std::vector<Entity> scene;
std::vector<Texture*> textures {
new Texture("res/textures/tex2.png", texture_diffuse),
new Texture("res/textures/tex1.png", texture_diffuse)
};
//Model model_backpack("res/models/backpack.obj");
Model model_cube("res/models/cube.obj");
Model model1("res/models/cube.obj");
//Entity backpack1(&model_backpack, &shaderProgram);
Entity cube(&model_cube, &shaderProgram);
Entity lightSource(&model_cube, &lightProgram);
//Mesh mesh1(vertices, indices, textures);
glm::mat4 model = glm::mat4(1.0f);
//scene.push_back(backpack1);
scene.push_back(cube);
camera->translate(glm::vec3(0.0f, 0.0f, 2.5f));
camera->translate(glm::vec3(0.0f, 0.0f, 7.5f));
// This is the game loop
while(!glfwWindowShouldClose(gameWindow->getGLFWwindow())) {
@@ -109,23 +76,15 @@ void Controller::run() {
// Update game
// ...
shaderProgram.setUniform("mix_val", (float) (sin(glfwGetTime()*0.25)*sin(glfwGetTime()*0.25)));
camera->lookForward();
camera->updateVPM();
glm::mat4 modelViewProj = camera->getViewProj() * model;
shaderProgram.setUniform("u_modelViewProj", modelViewProj);
// Render and buffer swap
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera->lookForward();
camera->updateVPM();
for(int i=0;i<20;i++) {
//mesh1.draw(&shaderProgram);
model1.draw(&shaderProgram);
camera->updateVPM();
glm::mat4 modelViewProj = camera->getViewProj() * model;
shaderProgram.setUniform("u_modelViewProj", modelViewProj);
}
//backpack1.draw(camera->getViewProj());
cube.draw(camera->getViewProj());
glfwSwapBuffers(gameWindow->getGLFWwindow());

26
src/Entity.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "Entity.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/ext/matrix_transform.hpp>
Entity::Entity(Model *model, ShaderProgram *shaderProgram)
: model(model), shaderProgram(shaderProgram) {
// Empty...
}
void Entity::draw(glm::mat4 viewProjMatrix) {
glm::mat4 modelViewProj = viewProjMatrix * modelMatrix;
shaderProgram->setUniform("u_modelViewProj", modelViewProj);
model->draw(shaderProgram);
}
void Entity::translate(glm::vec3 vector) {
position += vector;
modelMatrix = glm::translate(modelMatrix, vector);
}
void Entity::rotate(glm::vec3 axis, float radians) {
modelMatrix = glm::rotate(modelMatrix, radians, axis);
}

36
src/Entity.h Normal file
View File

@@ -0,0 +1,36 @@
#pragma once
#include "Model.h"
#include "ShaderProgram.h"
#include <glm/glm.hpp>
class Entity {
public:
Entity(Model *model, ShaderProgram *shaderProgram);
~Entity() = default;
void draw(glm::mat4 viewProjMatrix);
void translate(glm::vec3 vector);
void rotate(glm::vec3 axis, float radians);
glm::mat4 getModelMatrix() { return modelMatrix; }
private:
// May be used later...
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::vec3 velocity = glm::vec3(0.0f, 0.0f, 0.0f);
Model *model;
glm::mat4 modelMatrix = glm::mat4(1.0f);
ShaderProgram* shaderProgram;
};

View File

@@ -1,18 +1,18 @@
#include "Mesh.h"
Mesh::Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices, std::vector<Texture*> textures)
: vertices(vertices),
indices(indices),
: numElements(indices.size()),
textures(textures),
vertexBuffer(static_cast<void*>(vertices.data()), static_cast<void*>(indices.data()), vertices.size(), indices.size()) {
// Empty...
}
void Mesh::draw(ShaderProgram *shaderProgram) {
uint8_t typeNumberCount[TEXTURE_TYPE_NUM_ITEMS] {0};
glBindTexture(GL_TEXTURE_2D, 0);
// Bind all textures in order to its texture unit
for(auto it = textures.begin(); it != textures.end(); it++) {
int i = it - textures.begin();
@@ -26,7 +26,7 @@ void Mesh::draw(ShaderProgram *shaderProgram) {
// Draw elements
vertexBuffer.bind();
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, numElements, GL_UNSIGNED_INT, 0);
vertexBuffer.unbind();
// Unbind all textures

View File

@@ -18,8 +18,7 @@ public:
private:
std::vector<Vertex> vertices;
std::vector<uint32_t> indices;
uint32_t numElements;
std::vector<Texture*> textures;
VertexBuffer vertexBuffer;

View File

@@ -3,6 +3,8 @@
#include <iostream>
Model::Model(const char* pathToModel) {
// Todo: check if model isn't already loaded --> will boost startup time drastically
// actually all models should be loaded at startup and only a handle should be given to the entites...
loadModel(pathToModel);
}
@@ -128,7 +130,7 @@ std::vector<Texture*> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType
Texture *texture = new Texture(currentPath.c_str(), textureType);
loadedTextures.push_back(texture);
// Add newest texture pointer to the mesh's texture vector
// Add newest texture pointer to the mesh's texture-pointer vector
Texture *new_tex = loadedTextures.back();
textures.push_back(new_tex);
}

View File

@@ -106,6 +106,11 @@ void ShaderProgram::setUniform(const char *name, float value) const {
glUniform1f(location, value);
}
void ShaderProgram::setUniform(const char *name, glm::vec3 vector) const {
GLint location = glGetUniformLocation(shaderProgramId, name);
glUniform3f(location, vector.x, vector.y, vector.z);
}
void ShaderProgram::setUniform(const char *name, glm::mat4 matrix) const {
GLint location = glGetUniformLocation(shaderProgramId, name);
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix));

View File

@@ -18,6 +18,7 @@ public:
void setUniform(const char *name, bool value) const;
void setUniform(const char *name, int value) const;
void setUniform(const char *name, float value) const;
void setUniform(const char *name, glm::vec3 vector) const;
void setUniform(const char *name, glm::mat4 matrix) const;
GLuint getShaderProgramId() { return shaderProgramId; }

View File

@@ -28,7 +28,9 @@ Window::Window() {
glEnable(GL_DEPTH_TEST);
// Disable mouse cursor
#ifndef _DEBUG
setGrabbedCursor(1);
#endif
#ifdef _DEBUG
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;

5
tools/CMakeLists.txt Normal file
View File

@@ -0,0 +1,5 @@
project(obj-converter)
add_executable(obj-converter main.cpp)
target_link_libraries(obj-converter assimp)

7
tools/main.cpp Normal file
View File

@@ -0,0 +1,7 @@
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
int main(int argc, char** argv) {
return 1;
}