Add RenderDevice and model/view/perspective matrix calculation
parent
188b49e962
commit
eb5c69a1b1
|
@ -43,7 +43,8 @@ set(SOURCE_LIST
|
||||||
src/tests/tests.cpp
|
src/tests/tests.cpp
|
||||||
src/libs/stb_image.c
|
src/libs/stb_image.c
|
||||||
src/engine/Image.cpp
|
src/engine/Image.cpp
|
||||||
src/engine/Texture.cpp)
|
src/engine/Texture.cpp
|
||||||
|
src/engine/RenderDevice.cpp)
|
||||||
|
|
||||||
file(MAKE_DIRECTORY "bin")
|
file(MAKE_DIRECTORY "bin")
|
||||||
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin")
|
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin")
|
||||||
|
|
|
@ -2,13 +2,14 @@
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
layout (location = 1) in vec3 aColor;
|
layout (location = 1) in vec3 aColor;
|
||||||
layout (location = 2) in vec2 aTexCoord;
|
layout (location = 2) in vec2 aTexCoord;
|
||||||
|
uniform mat4 model;
|
||||||
|
uniform mat4 viewprojection;
|
||||||
|
|
||||||
out vec3 ourColor;
|
out vec3 ourColor;
|
||||||
out vec2 TexCoord;
|
out vec2 TexCoord;
|
||||||
|
|
||||||
void main()
|
void main() {
|
||||||
{
|
gl_Position = viewprojection * model * vec4(aPos, 1.0);
|
||||||
gl_Position = vec4(aPos, 1.0);
|
|
||||||
ourColor = aColor;
|
ourColor = aColor;
|
||||||
TexCoord = aTexCoord;
|
TexCoord = aTexCoord;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -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();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
#include "Shader.hpp"
|
#include "Shader.hpp"
|
||||||
#include "gl.hpp"
|
#include "gl.hpp"
|
||||||
|
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
bool loadShader(int &handle, int type, const std::string &path) {
|
bool loadShader(int &handle, int type, const std::string &path) {
|
||||||
std::ifstream t(path);
|
std::ifstream t(path);
|
||||||
std::string str((std::istreambuf_iterator<char>(t)),
|
std::string str((std::istreambuf_iterator<char>(t)),
|
||||||
|
@ -50,3 +52,8 @@ void Shader::use() const {
|
||||||
glUseProgram(handle);
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
class Shader {
|
class Shader {
|
||||||
unsigned int handle;
|
unsigned int handle;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Shader(const std::string &vpath, const std::string &fpath);
|
Shader(const std::string &vpath, const std::string &fpath);
|
||||||
void use() const;
|
void use() const;
|
||||||
|
|
||||||
|
void setUniform(const std::string& name, glm::mat4 mat4) const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -44,9 +44,12 @@ void Window::swapBuffers() {
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Window::clear(const glm::vec4 &color) {
|
void Window::clear(const glm::vec4 &color) {
|
||||||
glClearColor(color.r, color.g, color.b, color.a);
|
glClearColor(color.r, color.g, color.b, color.a);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glm::ivec2 Window::getSize() const {
|
||||||
|
return { 800, 600 };
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,4 +12,6 @@ public:
|
||||||
bool run();
|
bool run();
|
||||||
void swapBuffers();
|
void swapBuffers();
|
||||||
void clear(const glm::vec4 &color);
|
void clear(const glm::vec4 &color);
|
||||||
|
|
||||||
|
glm::ivec2 getSize() const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,10 +4,12 @@
|
||||||
#include "../engine/Image.hpp"
|
#include "../engine/Image.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
const std::string MeshDrawComponent::className = "MeshDrawComponent";
|
const std::string MeshDrawComponent::className = "MeshDrawComponent";
|
||||||
|
|
||||||
MeshDrawComponent::MeshDrawComponent(Node::Ptr node, const Shader &shader):
|
MeshDrawComponent::MeshDrawComponent(Node::Ptr node, RenderDevice *device, const Shader &shader):
|
||||||
Component(std::move(node)), shader(shader) {
|
Component(std::move(node)), device(device), shader(shader) {
|
||||||
glGenBuffers(1, &vbo);
|
glGenBuffers(1, &vbo);
|
||||||
glGenBuffers(1, &ebo);
|
glGenBuffers(1, &ebo);
|
||||||
glGenVertexArrays(1, &vao);
|
glGenVertexArrays(1, &vao);
|
||||||
|
@ -65,9 +67,15 @@ void MeshDrawComponent::update(float delta) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void MeshDrawComponent::draw() const {
|
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();
|
shader.use();
|
||||||
texture.bind();
|
texture.bind();
|
||||||
|
|
||||||
|
shader.setUniform("model", model);
|
||||||
|
shader.setUniform("viewprojection", device->getViewProjectionMatrix());
|
||||||
|
|
||||||
glBindVertexArray(vao);
|
glBindVertexArray(vao);
|
||||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
|
|
@ -3,14 +3,16 @@
|
||||||
#include "../graph/Component.hpp"
|
#include "../graph/Component.hpp"
|
||||||
#include "../engine/Shader.hpp"
|
#include "../engine/Shader.hpp"
|
||||||
#include "../engine/Texture.hpp"
|
#include "../engine/Texture.hpp"
|
||||||
|
#include "../engine/RenderDevice.hpp"
|
||||||
|
|
||||||
class MeshDrawComponent : public Component {
|
class MeshDrawComponent : public Component {
|
||||||
unsigned int vbo, ebo, vao;
|
unsigned int vbo, ebo, vao;
|
||||||
Texture texture;
|
Texture texture;
|
||||||
const Shader &shader;
|
const Shader &shader;
|
||||||
|
RenderDevice *device;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MeshDrawComponent(Node::Ptr node, const Shader &shader);
|
MeshDrawComponent(Node::Ptr node, RenderDevice *device, const Shader &shader);
|
||||||
|
|
||||||
void update(float delta) override;
|
void update(float delta) override;
|
||||||
void draw() const;
|
void draw() const;
|
||||||
|
|
10
src/main.cpp
10
src/main.cpp
|
@ -13,18 +13,18 @@ int main() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Window window;
|
RenderDevice device;
|
||||||
Shader shaderProgram("assets/shaders/base.vert.glsl", "assets/shaders/base.frag.glsl");
|
Shader shaderProgram("assets/shaders/base.vert.glsl", "assets/shaders/base.frag.glsl");
|
||||||
|
|
||||||
auto root = std::make_shared<Node>();
|
auto root = std::make_shared<Node>();
|
||||||
{
|
{
|
||||||
auto square = std::make_shared<Node>();
|
auto square = std::make_shared<Node>();
|
||||||
square->addComponent<MeshDrawComponent>(square, shaderProgram);
|
square->addComponent<MeshDrawComponent>(square, &device, shaderProgram);
|
||||||
root->addChild(square);
|
root->addChild(square);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (window.run()) {
|
while (device.getWindow().run()) {
|
||||||
window.clear({ 0.2f, 0.3f, 0.3f, 1.0f });
|
device.getWindow().clear({ 0.2f, 0.3f, 0.3f, 1.0f });
|
||||||
|
|
||||||
root->recurseComponents<MeshDrawComponent>([](auto x) {
|
root->recurseComponents<MeshDrawComponent>([](auto x) {
|
||||||
// TODO: visibility
|
// TODO: visibility
|
||||||
|
@ -32,6 +32,6 @@ int main() {
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
window.swapBuffers();
|
device.getWindow().swapBuffers();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue