Revert "Reverted back to old array thing"

This reverts commit 2ad685b874.
pull/1/head
E. Almqvist 2 years ago
parent 2ad685b874
commit bcf5682f42
  1. 5
      headers/renderer.hpp
  2. 24
      src/main.cpp
  3. 18
      src/renderer.cpp

@ -21,15 +21,16 @@
namespace Renderer { namespace Renderer {
class RenderObject { class RenderObject {
public: public:
RenderObject(float verts[], unsigned int indices[]); RenderObject(std::vector<float> verts, std::vector<unsigned int> indices);
void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform); void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform);
void transform(glm::mat4 T); void transform(glm::mat4 T);
private: private:
Shaders::Shader shader; Shaders::Shader shader;
unsigned int vertCount;
unsigned int EBO; unsigned int EBO;
unsigned int VBO; unsigned int VBO;
unsigned int VAO; unsigned int VAO;
std::vector<float> vertsVec;
std::vector<unsigned int> indicesVec;
}; };
class Renderer3D { class Renderer3D {

@ -49,6 +49,8 @@ int main() {
return 1; return 1;
} }
Renderer::Renderer3D renderer(win);
glfwMakeContextCurrent(win); glfwMakeContextCurrent(win);
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) { if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
@ -56,18 +58,18 @@ int main() {
return 1; return 1;
} }
float verts[] = { std::vector<float> verts({
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f
}; });
// Vert struc: x y z r g b tx ty // Vert struc: x y z r g b tx ty
unsigned int indices[] = { std::vector<unsigned int> indices({
0, 1, 3, 0, 1, 3,
1, 2, 3 1, 2, 3
}; });
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback); glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
@ -75,13 +77,8 @@ int main() {
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
printf("%u %u\n", sizeof(indices), sizeof(verts)); Renderer::RenderObject ro(verts, indices);
Renderer::Obj2D ro2(indices, sizeof(indices), verts, sizeof(verts)); renderer.spawnObject(ro);
ro2.setTexture(RUSTY_METAL_TEXTURE);
// Renderer::RenderObject ro(verts, indices);
// Renderer::Renderer3D renderer(win);
// renderer.spawnObject(ro);
// ro.setTexture(RUSTY_METAL_TEXTURE); // ro.setTexture(RUSTY_METAL_TEXTURE);
// Window width & height // Window width & height
@ -102,10 +99,9 @@ int main() {
glm::mat4 T = glm::mat4(1.0f); glm::mat4 T = glm::mat4(1.0f);
T = glm::rotate(T, rotang, glm::vec3(1.0, 0.0, 1.0)); T = glm::rotate(T, rotang, glm::vec3(1.0, 0.0, 1.0));
T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5)); T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5));
ro2.transform(T); ro.transform(T);
ro2.render(win);
//renderer.render(); renderer.render();
// glfw // glfw
glfwSwapBuffers(win); glfwSwapBuffers(win);

@ -1,4 +1,3 @@
#include <cstdio>
#include <glad/glad.h> #include <glad/glad.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
@ -40,26 +39,27 @@ namespace Renderer {
} }
// RenderObject // RenderObject
RenderObject::RenderObject(float verts[], unsigned int indices[]) RenderObject::RenderObject(std::vector<float> verts, std::vector<unsigned int> indices)
: shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) { : shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) {
vertsVec = verts;
indicesVec = indices;
float* vertsArray = &vertsVec[0];
unsigned int* indicesArray = &indicesVec[0];
// Vertex Array Object // Vertex Array Object
glGenVertexArrays(1, &VAO); // gen the VAO glGenVertexArrays(1, &VAO); // gen the VAO
glBindVertexArray(VAO); // bind it glBindVertexArray(VAO); // bind it
// Copy the verts into the buffer // Copy the verts into the buffer
// unsigned int vertsSize = sizeof(verts);
unsigned int vertsSize = 4;
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertsSize, verts, GL_DYNAMIC_DRAW); // for moving stuff glBufferData(GL_ARRAY_BUFFER, vertsVec.size(), vertsArray, GL_DYNAMIC_DRAW); // for moving stuff
// Copy the indices for the verts into another buffer // Copy the indices for the verts into another buffer
// vertCount = sizeof(indices);
vertCount = 6;
glGenBuffers(1, &EBO); glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertCount, indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesVec.size(), indicesArray, GL_STATIC_DRAW);
// Shader stuff // Shader stuff
// Pos // Pos
@ -82,7 +82,7 @@ namespace Renderer {
shader.use(); shader.use();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, vertCount, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, indicesVec.size(), GL_UNSIGNED_INT, 0);
} }
void RenderObject::transform(glm::mat4 T) { void RenderObject::transform(glm::mat4 T) {

Loading…
Cancel
Save