Broken stuff

pull/1/head
E. Almqvist 2 years ago
parent e38192b594
commit 902f3484cf
  1. 12
      headers/renderer.hpp
  2. 18
      src/main.cpp
  3. 52
      src/renderer.cpp

@ -21,11 +21,16 @@
namespace Renderer {
class RenderObject {
public:
RenderObject();
RenderObject(std::vector<float> verts, std::vector<unsigned int> indices);
void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform);
void transform(glm::mat4 T);
private:
std::vector<float> verts;
std::vector<unsigned int> indices;
Shaders::Shader shader;
unsigned int EBO;
unsigned int VBO;
unsigned int VAO;
std::vector<float> vertsVec;
std::vector<unsigned int> indicesVec;
};
class Renderer3D {
@ -35,6 +40,7 @@ namespace Renderer {
void setCamera(glm::vec3 pos);
void setFOV(float deg);
void spawnObject(RenderObject ro);
void render();
private:
std::vector<RenderObject> renderObjects = std::vector<RenderObject>();

@ -49,6 +49,8 @@ int main() {
return 1;
}
Renderer::Renderer3D renderer(win);
glfwMakeContextCurrent(win);
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
@ -56,18 +58,18 @@ int main() {
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, 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
};
});
// Vert struc: x y z r g b tx ty
unsigned int indices[] = {
std::vector<unsigned int> indices({
0, 1, 3,
1, 2, 3
};
});
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
@ -75,8 +77,9 @@ int main() {
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
Renderer::Obj2D ro(indices, sizeof(indices), verts, sizeof(verts));
ro.setTexture(RUSTY_METAL_TEXTURE);
Renderer::RenderObject ro(verts, indices);
renderer.spawnObject(ro);
// ro.setTexture(RUSTY_METAL_TEXTURE);
// Window width & height
while (!glfwWindowShouldClose(win)) {
@ -97,7 +100,8 @@ int main() {
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));
ro.transform(T);
ro.render(win);
renderer.render();
// glfw
glfwSwapBuffers(win);

@ -19,6 +19,10 @@ namespace Renderer {
renderObjects = ROs;
}
void Renderer3D::spawnObject(RenderObject ro) {
renderObjects.push_back(ro);
}
void Renderer3D::setFOV(float fov) {
int width, height;
glfwGetWindowSize(window, &width, &height);
@ -35,11 +39,55 @@ namespace Renderer {
}
// RenderObject
RenderObject::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;
float* vertsArray = &vertsVec[0];
unsigned int* indicesArray = &indicesVec[0];
// Vertex Array Object
glGenVertexArrays(1, &VAO); // gen the VAO
glBindVertexArray(VAO); // bind it
// Copy the verts into the buffer
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertsVec.size(), vertsArray, GL_DYNAMIC_DRAW); // for moving stuff
// Copy the indices for the verts into another buffer
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesVec.size(), indicesArray, GL_STATIC_DRAW);
// Shader stuff
// Pos
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)0);
glEnableVertexAttribArray(0);
// Color
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
// Texture
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(6*sizeof(float)));
glEnableVertexAttribArray(2);
}
void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) {}
void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) {
shader.setMat4("view", cameraTransform);
shader.setMat4("projection", projectionTransform);
shader.use();
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);
}
void Obj2D::transform(glm::mat4 T) {
shader.setMat4("model", T);

Loading…
Cancel
Save