Abstracted away the creation of buffers

Added a transform
Updated the TODO.md
Updated default shaders to include the apropriate three 4x4 matrices to
render in 3D
This commit is contained in:
Nigel Barink 2022-05-04 15:27:42 +02:00
parent af4a114fad
commit eb0e7f7a51
8 changed files with 131 additions and 50 deletions

View File

@ -0,0 +1,47 @@
#include <MyGraphicsEngine/Buffer.h>
int Buffer::getBufferID() {
return id;
}
void Buffer::createBuffer() {
glGenBuffers(1, (GLuint*) &id);
}
void Buffer::setBufferData(void* data, size_t dataSize, bool elementBuffer = false ) {
if (elementBuffer) {
glBufferData(GL_ELEMENT_ARRAY_BUFFER, dataSize, data, GL_STATIC_DRAW);
}
else {
glBufferData(GL_ARRAY_BUFFER, dataSize, data, GL_STATIC_DRAW);
}
}
void Buffer::Bind(bool elementBuffer = false ) {
if (elementBuffer) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
}
else {
glBindBuffer(GL_ARRAY_BUFFER, id);
}
}
void Buffer::Unbind(bool elementBuffer = false) {
if (elementBuffer) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
else {
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}
void Buffer::Delete() {
glDeleteBuffers(1, (GLuint*) &id);
}

View File

@ -0,0 +1,19 @@
#pragma once
#include <glad/glad.h>
class Buffer {
private:
unsigned int id;
public:
int getBufferID();
void createBuffer();
void setBufferData(void* data, size_t dataSize, bool elementBuffer );
void Bind(bool elementBuffer);
void Unbind(bool elementBuffer);
void Delete();
};

View File

@ -0,0 +1,8 @@
#pragma once
#include <glm/glm.hpp>
struct Transform {
glm::vec3 Position;
glm::vec3 Rotation;
glm::vec3 Scale;
};

View File

@ -1,7 +1,7 @@
#version 330 core #version 440 core
out vec4 FragColor; out vec4 FragColor;
void main{ void main(){
FragColor = vec4(0.0f, 1.0f, 0.0f , 1.0f); FragColor = vec4(0.0f, 1.0f, 0.0f , 1.0f);
} }

View File

@ -1,6 +1,10 @@
#version 330 core #version 440 core
layout in vec3 aPos; in layout(location=0) vec3 aPos;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
void main() { void main() {
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); gl_Position = P * V * M * vec4(aPos.x, aPos.y, aPos.z, 1.0);
} }

View File

@ -2,6 +2,9 @@
#include <MyGraphicsEngine/Window.h> #include <MyGraphicsEngine/Window.h>
#include <MyGraphicsEngine/Camera.h> #include <MyGraphicsEngine/Camera.h>
#include <MyGraphicsEngine/Mesh.h> #include <MyGraphicsEngine/Mesh.h>
#include <MyGraphicsEngine/Transform.h>
#include <MyGraphicsEngine/Buffer.h>
#include <string> #include <string>
#include "imgui.h" #include "imgui.h"
@ -29,6 +32,10 @@ int main(int argc, char* argv[]) {
//ModelImporter::Test(); //ModelImporter::Test();
Transform t;
t.Scale.x = 1.0f;
t.Scale.y = 1.0f;
t.Rotation.y = 90.0f;
Mesh mesh; Mesh mesh;
@ -44,6 +51,8 @@ int main(int argc, char* argv[]) {
0,1,3, 0,1,3,
1,2,3 1,2,3
}; };
Camera cam(glm::vec3(2.0f, 0.0f, 0.0f), glm::vec3(0.0f,0.0f,0.0f), 90.0f); Camera cam(glm::vec3(2.0f, 0.0f, 0.0f), glm::vec3(0.0f,0.0f,0.0f), 90.0f);
@ -73,43 +82,30 @@ int main(int argc, char* argv[]) {
spdlog::info("Vertices: {}, {} bytes", mesh.vertices.size(), sizeof(glm::vec3)); spdlog::info("Vertices: {}, {} bytes", mesh.vertices.size(), sizeof(glm::vec3));
spdlog::info("Elements: {}, {} bytes", mesh.elements.size(), sizeof(GLushort)); spdlog::info("Elements: {}, {} bytes", mesh.elements.size(), sizeof(GLushort));
unsigned int VBO, VAO, EBO;
Buffer VertexBuffer;
Buffer ElementBuffer;
unsigned int VAO;
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); VertexBuffer.createBuffer();
glBufferData(GL_ARRAY_BUFFER, mesh.vertices.size() * sizeof(glm::vec3), &mesh.vertices[0], GL_STATIC_DRAW); VertexBuffer.Bind(false);
VertexBuffer.setBufferData(&mesh.vertices[0], mesh.vertices.size() * sizeof(glm::vec3),false);
ElementBuffer.createBuffer();
ElementBuffer.Bind(true);
ElementBuffer.setBufferData(&mesh.elements[0], mesh.elements.size() * sizeof(GLushort), true);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.elements.size() * sizeof(GLushort), &mesh.elements[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0); VertexBuffer.Unbind(false);
glBindVertexArray(0); glBindVertexArray(0);
glm::vec3 rotation = glm::vec3(0.0f , 90.0f, 0.0f);
glm::mat4 tran = glm::translate(glm::mat4(),glm::vec3(0.0f, 0.0f, 0.0f));
glm::mat4 scale = glm::scale(glm::mat4(), glm::vec3(1.0f, 1.0f, 1.0f));
glm::mat4 rot =
glm::rotate(glm::mat4(), glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) *
glm::rotate(glm::mat4(), glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::rotate(glm::mat4(), glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 model = tran * rot * scale;
glm::mat4 projection = glm::perspective(cam.Zoom, (800.0f / 600.0f), 0.001f, 100.0f);
/* /*
* gau_Manager* mgr; * gau_Manager* mgr;
ga_Mixer* mixer; ga_Mixer* mixer;
@ -131,6 +127,17 @@ int main(int argc, char* argv[]) {
while (!GameWindow.WindowShouldClose()) { while (!GameWindow.WindowShouldClose()) {
glm::mat4 tran = glm::translate(glm::mat4(), t.Position);
glm::mat4 scale = glm::scale(glm::mat4(), t.Scale);
glm::mat4 rot =
glm::rotate(glm::mat4(), glm::radians(t.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) *
glm::rotate(glm::mat4(), glm::radians(t.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::rotate(glm::mat4(), glm::radians(t.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 model = tran * rot * scale;
glm::mat4 projection = glm::perspective(cam.Zoom, (800.0f / 600.0f), 0.001f, 100.0f);
GameWindow.Poll(); GameWindow.Poll();
@ -142,7 +149,7 @@ int main(int argc, char* argv[]) {
shader.setUniformMat4("M", model); shader.setUniformMat4("M", model);
shader.setUniformMat4("V", cam.GetViewMatrix()); shader.setUniformMat4("V", cam.GetViewMatrix());
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); ElementBuffer.Bind(true);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0); glBindVertexArray(0);
@ -151,13 +158,13 @@ int main(int argc, char* argv[]) {
ImGui_ImplGlfw_NewFrame(); ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
ImGui::Begin("Test"); ImGui::Begin("Transform Test Tool");
ImGui::SliderFloat("Scale Y:", (float*)&t.Scale.y, 1, 4);
ImGui::SliderFloat("Scale X:", (float*)&t.Scale.x, 1, 4);
ImGui::SliderFloat("Position X:", (float*)&t.Position.z, -5, 5);
ImGui::Text("Hello world!");
bool isChecked = false;
ImGui::Checkbox("Yellow!", &isChecked);
float v = 5;
ImGui::SliderFloat("Something to slide on..", &v, 1, 10 );
ImGui::End(); ImGui::End();
@ -176,7 +183,7 @@ int main(int argc, char* argv[]) {
ImGui::DestroyContext(); ImGui::DestroyContext();
glDeleteVertexArrays(1, &VAO); glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &EBO); ElementBuffer.Delete();
} }

View File

@ -6,10 +6,11 @@
<input type="checkbox" checked></input> Setup build system \ <input type="checkbox" checked></input> Setup build system \
<input type="checkbox" checked></input> Link with GLFW \ <input type="checkbox" checked></input> Link with GLFW \
<input type="checkbox" checked></input> Basic Window \ <input type="checkbox" checked></input> Basic Window \
<input type="checkbox"></input> Basic Triangle rendering \ <input type="checkbox" checked></input> Basic Triangle rendering \
<input type="checkbox" checked></input> Basic IMGui \
<input type="checkbox"></input> Basic Textures \ <input type="checkbox"></input> Basic Textures \
<input type="checkbox"></input> Link GLEW \ <input type="checkbox"></input> Link GLEW \
<input type="checkbox"></input> Work on basic logging \ <input type="checkbox" checked></input> Work on basic logging \
<input type="checkbox"></input> Input handling \ <input type="checkbox"></input> Input handling \
<input type="checkbox"></input> More shader work \ <input type="checkbox"></input> More shader work \
<input type="checkbox"></input> Load FBX model files \ <input type="checkbox"></input> Load FBX model files \

View File

@ -3,13 +3,8 @@ Pos=60,60
Size=400,400 Size=400,400
Collapsed=0 Collapsed=0
[Window][Test] [Window][Transform Test Tool]
Pos=268,47 Pos=443,154
Size=201,94 Size=396,490
Collapsed=0
[Window][Dear ImGui Demo]
Pos=650,20
Size=550,680
Collapsed=0 Collapsed=0