Add a basic entity class
This commit is contained in:
@@ -9,6 +9,7 @@ add_executable(Fall-Fever
|
||||
Camera.cpp
|
||||
Mesh.cpp
|
||||
Model.cpp
|
||||
Entity.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
|
||||
class Camera {
|
||||
|
||||
|
||||
@@ -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
26
src/Entity.cpp
Normal 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
36
src/Entity.h
Normal 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;
|
||||
|
||||
};
|
||||
@@ -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
|
||||
|
||||
@@ -18,8 +18,7 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<uint32_t> indices;
|
||||
uint32_t numElements;
|
||||
std::vector<Texture*> textures;
|
||||
|
||||
VertexBuffer vertexBuffer;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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; }
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user