Add RenderDevice and model/view/perspective matrix calculation

master
rubenwardy 2019-08-22 18:22:22 +01:00
parent 188b49e962
commit eb5c69a1b1
11 changed files with 84 additions and 13 deletions

View File

@ -43,7 +43,8 @@ set(SOURCE_LIST
src/tests/tests.cpp
src/libs/stb_image.c
src/engine/Image.cpp
src/engine/Texture.cpp)
src/engine/Texture.cpp
src/engine/RenderDevice.cpp)
file(MAKE_DIRECTORY "bin")
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin")

View File

@ -2,13 +2,14 @@
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
uniform mat4 model;
uniform mat4 viewprojection;
out vec3 ourColor;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
void main() {
gl_Position = viewprojection * model * vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = aTexCoord;
}

View File

@ -0,0 +1,21 @@
#include "RenderDevice.hpp"
#include "Window.hpp"
#include <glm/gtc/matrix_transform.hpp>
RenderDevice::RenderDevice():
window(), viewProjection() {
updateViewProjectionMatrix();
}
void RenderDevice::updateViewProjectionMatrix() {
// model ((view perspective))
// model --------> world -------> camera --------------> screen
//
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
auto size = window.getSize();
float ratio = (float)size.x / (float)size.y;
viewProjection = glm::perspective(glm::radians(45.0f), ratio, 0.1f, 100.0f) * view;
}

View File

@ -0,0 +1,22 @@
#pragma once
#include <memory>
#include "Window.hpp"
class RenderDevice {
Window window;
glm::mat4 viewProjection;
public:
RenderDevice();
RenderDevice(const RenderDevice &other) = delete;
inline Window &getWindow() { return window; }
inline const glm::mat4 &getViewProjectionMatrix() const { return viewProjection; }
private:
void updateViewProjectionMatrix();
};

View File

@ -4,6 +4,8 @@
#include "Shader.hpp"
#include "gl.hpp"
#include <glm/gtc/type_ptr.hpp>
bool loadShader(int &handle, int type, const std::string &path) {
std::ifstream t(path);
std::string str((std::istreambuf_iterator<char>(t)),
@ -50,3 +52,8 @@ void Shader::use() const {
glUseProgram(handle);
}
void Shader::setUniform(const std::string& name, glm::mat4 mat4) const {
int modelLoc = glGetUniformLocation(handle, name.c_str());
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(mat4));
}

View File

@ -1,9 +1,13 @@
#pragma once
#include <glm/glm.hpp>
class Shader {
unsigned int handle;
public:
Shader(const std::string &vpath, const std::string &fpath);
void use() const;
void setUniform(const std::string& name, glm::mat4 mat4) const;
};

View File

@ -44,9 +44,12 @@ void Window::swapBuffers() {
glfwSwapBuffers(window);
}
void Window::clear(const glm::vec4 &color) {
glClearColor(color.r, color.g, color.b, color.a);
glClear(GL_COLOR_BUFFER_BIT);
}
glm::ivec2 Window::getSize() const {
return { 800, 600 };
}

View File

@ -12,4 +12,6 @@ public:
bool run();
void swapBuffers();
void clear(const glm::vec4 &color);
glm::ivec2 getSize() const;
};

View File

@ -4,10 +4,12 @@
#include "../engine/Image.hpp"
#include <iostream>
#include <glm/gtc/matrix_transform.hpp>
const std::string MeshDrawComponent::className = "MeshDrawComponent";
MeshDrawComponent::MeshDrawComponent(Node::Ptr node, const Shader &shader):
Component(std::move(node)), shader(shader) {
MeshDrawComponent::MeshDrawComponent(Node::Ptr node, RenderDevice *device, const Shader &shader):
Component(std::move(node)), device(device), shader(shader) {
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
glGenVertexArrays(1, &vao);
@ -65,9 +67,15 @@ void MeshDrawComponent::update(float delta) {
}
void MeshDrawComponent::draw() const {
glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
shader.use();
texture.bind();
shader.setUniform("model", model);
shader.setUniform("viewprojection", device->getViewProjectionMatrix());
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

View File

@ -3,14 +3,16 @@
#include "../graph/Component.hpp"
#include "../engine/Shader.hpp"
#include "../engine/Texture.hpp"
#include "../engine/RenderDevice.hpp"
class MeshDrawComponent : public Component {
unsigned int vbo, ebo, vao;
Texture texture;
const Shader &shader;
RenderDevice *device;
public:
MeshDrawComponent(Node::Ptr node, const Shader &shader);
MeshDrawComponent(Node::Ptr node, RenderDevice *device, const Shader &shader);
void update(float delta) override;
void draw() const;

View File

@ -13,18 +13,18 @@ int main() {
return 0;
}
Window window;
RenderDevice device;
Shader shaderProgram("assets/shaders/base.vert.glsl", "assets/shaders/base.frag.glsl");
auto root = std::make_shared<Node>();
{
auto square = std::make_shared<Node>();
square->addComponent<MeshDrawComponent>(square, shaderProgram);
square->addComponent<MeshDrawComponent>(square, &device, shaderProgram);
root->addChild(square);
}
while (window.run()) {
window.clear({ 0.2f, 0.3f, 0.3f, 1.0f });
while (device.getWindow().run()) {
device.getWindow().clear({ 0.2f, 0.3f, 0.3f, 1.0f });
root->recurseComponents<MeshDrawComponent>([](auto x) {
// TODO: visibility
@ -32,6 +32,6 @@ int main() {
return true;
});
window.swapBuffers();
device.getWindow().swapBuffers();
}
}