|
|
|
@ -2,70 +2,71 @@ |
|
|
|
|
#include <glm/glm.hpp> |
|
|
|
|
#include <glm/gtc/matrix_transform.hpp> |
|
|
|
|
|
|
|
|
|
#include "GLFW/glfw3.h" |
|
|
|
|
#include "renderer.hpp" |
|
|
|
|
#include "shaders.hpp" |
|
|
|
|
|
|
|
|
|
namespace Renderer { |
|
|
|
|
|
|
|
|
|
Renderer3D::Renderer3D() { |
|
|
|
|
// TODO: Make more OOP
|
|
|
|
|
} |
|
|
|
|
// Renderer3D
|
|
|
|
|
Renderer3D::Renderer3D(GLFWwindow* win) { |
|
|
|
|
window = win;
|
|
|
|
|
|
|
|
|
|
Renderer3D::Renderer3D(std::vector<RenderObject> ROs) : Renderer3D() { |
|
|
|
|
RenderObjects = ROs; |
|
|
|
|
setFOV(DEFAULT_FOV); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Obj2D::transform(glm::mat4 T) { |
|
|
|
|
shader.setMat4("model", T); |
|
|
|
|
Renderer3D::Renderer3D(GLFWwindow* win, std::vector<RenderObject> ROs) : Renderer3D(win) { |
|
|
|
|
renderObjects = ROs; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Obj2D::setTexture(const char* t_src) { |
|
|
|
|
texture.texture_src = t_src; |
|
|
|
|
texture.load(); |
|
|
|
|
void Renderer3D::spawnObject(RenderObject ro) { |
|
|
|
|
renderObjects.push_back(ro); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Obj2D::render(GLFWwindow* win) { |
|
|
|
|
void Renderer3D::setFOV(float fov) { |
|
|
|
|
int width, height; |
|
|
|
|
glfwGetWindowSize(window, &width, &height); |
|
|
|
|
projectionTransform = glm::perspective(glm::radians(fov), (float)width / (float)height, NEAR_PLANE, FAR_PLANE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Camera
|
|
|
|
|
glm::mat4 view = glm::mat4(1.0f); |
|
|
|
|
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -8.0f)); // Camera position
|
|
|
|
|
shader.setMat4("view", view); |
|
|
|
|
|
|
|
|
|
glfwGetWindowSize(win, &width, &height); |
|
|
|
|
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f); |
|
|
|
|
shader.setMat4("projection", projection); |
|
|
|
|
|
|
|
|
|
// Use the shader etc
|
|
|
|
|
shader.use(); |
|
|
|
|
|
|
|
|
|
if (texture.loaded) |
|
|
|
|
texture.bind(); |
|
|
|
|
void Renderer3D::setCamera(glm::vec3 pos) { |
|
|
|
|
cameraTransform = glm::translate(cameraTransform, pos);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); |
|
|
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); |
|
|
|
|
void Renderer3D::render() { |
|
|
|
|
for ( RenderObject ro: renderObjects )
|
|
|
|
|
ro.render(window, cameraTransform, projectionTransform); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Obj2D::Obj2D(unsigned int indices[], unsigned int icount, float verts[], unsigned int vcount)
|
|
|
|
|
// RenderObject
|
|
|
|
|
RenderObject::RenderObject(std::vector<float> verts, std::vector<unsigned int> indices)
|
|
|
|
|
: shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) { |
|
|
|
|
vertsVec = verts; |
|
|
|
|
indicesVec = indices; |
|
|
|
|
|
|
|
|
|
// Vertex buffer object
|
|
|
|
|
glGenBuffers(1, &VBO); |
|
|
|
|
float vertsArray[vertsVec.size()]; |
|
|
|
|
std::copy(vertsVec.begin(), vertsVec.end(), vertsArray); |
|
|
|
|
|
|
|
|
|
unsigned int indicesArray[indicesVec.size()]; |
|
|
|
|
std::copy(indicesVec.begin(), indicesVec.end(), indicesArray); |
|
|
|
|
|
|
|
|
|
// Vertex Array Object
|
|
|
|
|
glGenVertexArrays(1, &VAO); // gen the VAO
|
|
|
|
|
glBindVertexArray(VAO); // bind it
|
|
|
|
|
|
|
|
|
|
// Copy verts into buffer
|
|
|
|
|
// Copy the verts into the buffer
|
|
|
|
|
glGenBuffers(1, &VBO); |
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO); |
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, vcount, verts, GL_DYNAMIC_DRAW); // for moving stuff
|
|
|
|
|
//glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, vertsVec.size() * sizeof(vertsVec[0]), vertsArray, GL_DYNAMIC_DRAW); // for moving stuff
|
|
|
|
|
// printf("%u\n", sizeof(vertsVec[0]) * vertsVec.size());
|
|
|
|
|
|
|
|
|
|
// Copy the indices for the verts into another buffer
|
|
|
|
|
glGenBuffers(1, &EBO); |
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); |
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, icount, indices, GL_STATIC_DRAW); |
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesVec.size() * sizeof(indicesVec[0]), indicesArray, GL_STATIC_DRAW); |
|
|
|
|
// printf("%u\n",indicesVec.size() * sizeof(indicesVec[0]));
|
|
|
|
|
|
|
|
|
|
// Shader stuff
|
|
|
|
|
// Pos
|
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)0); |
|
|
|
|
glEnableVertexAttribArray(0); |
|
|
|
@ -79,6 +80,36 @@ namespace Renderer { |
|
|
|
|
glEnableVertexAttribArray(2); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void RenderObject::preRenderHook() {} |
|
|
|
|
|
|
|
|
|
void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) { |
|
|
|
|
shader.setMat4("view", cameraTransform); |
|
|
|
|
shader.setMat4("projection", projectionTransform); |
|
|
|
|
|
|
|
|
|
shader.use(); |
|
|
|
|
|
|
|
|
|
preRenderHook(); |
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); |
|
|
|
|
glDrawElements(GL_TRIANGLES, indicesVec.size(), GL_UNSIGNED_INT, 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void RenderObject::transform(glm::mat4 T) { |
|
|
|
|
shader.setMat4("model", T); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Obj2D
|
|
|
|
|
void Obj2D::setTexture(const char* t_src) { |
|
|
|
|
texture.texture_src = t_src; |
|
|
|
|
texture.load(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Obj2D::preRenderHook() { |
|
|
|
|
printf("OBJ2D was here!\n"); |
|
|
|
|
if (texture.loaded) |
|
|
|
|
texture.bind(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Private stuff
|
|
|
|
|
void Obj2D::bind_texture(Textures::Texture2D new_texture) { |
|
|
|
|
texture = new_texture; |
|
|
|
|