This commit is contained in:
Daniel Ledda
2025-01-12 23:40:39 +01:00
parent 25384c743d
commit c0deceec65
9 changed files with 247 additions and 98 deletions

View File

@@ -1,40 +1,39 @@
#include <iostream>
#include <vector>
#include "Mesh.h"
#define TINYOBJLOADER_IMPLEMENTATION
#include "../lib/loaders/tinyobj.h"
#include "../lib/djstdlib/core.h"
Mesh createMesh(const char* obj_file) {
Scratch temp = scratchStart(0, 0);
Mesh result = {0};
tinyobj::ObjReader reader = tinyobj::ObjReader();
bool success = reader.ParseFromFile(obj_file);
std::cout << reader.Error() << std::endl;
log("%s\n", reader.Error().c_str());
const tinyobj::attrib_t attrib = reader.GetAttrib();
std::vector<tinyobj::index_t> indices_t = reader.GetShapes().at(0).mesh.indices;
std::vector<uint32> indices = std::vector<uint32>(indices_t.size());
list<uint32> indices = PushFullList(temp.arena, uint32, indices_t.size());
std::vector<real32> vertices = std::vector<real32>(3*indices_t.size());
std::vector<real32> normals = std::vector<real32>(3*indices_t.size());
std::vector<real32> texcoords = std::vector<real32>(2*indices_t.size());
list<real32> vertices = PushFullList(temp.arena, real32, 3*indices_t.size());
list<real32> normals = PushFullList(temp.arena, real32, 3*indices_t.size());
list<real32> texcoords = PushFullList(temp.arena, real32, 2*indices_t.size());
for (int i = 0; i < indices_t.size(); i++) {
tinyobj::index_t vertex_data = indices_t[i];
vertices[3*i] = attrib.vertices[3*vertex_data.vertex_index];
vertices[3*i+1] = attrib.vertices[3*vertex_data.vertex_index + 1];
vertices[3*i+2] = attrib.vertices[3*vertex_data.vertex_index + 2];
vertices.data[3*i] = attrib.vertices[3*vertex_data.vertex_index];
vertices.data[3*i+1] = attrib.vertices[3*vertex_data.vertex_index + 1];
vertices.data[3*i+2] = attrib.vertices[3*vertex_data.vertex_index + 2];
normals[3*i] = attrib.normals[3*vertex_data.normal_index];
normals[3*i+1] = attrib.normals[3*vertex_data.normal_index + 1];
normals[3*i+2] = attrib.normals[3*vertex_data.normal_index + 2];
normals.data[3*i] = attrib.normals[3*vertex_data.normal_index];
normals.data[3*i+1] = attrib.normals[3*vertex_data.normal_index + 1];
normals.data[3*i+2] = attrib.normals[3*vertex_data.normal_index + 2];
texcoords[2*i] = attrib.texcoords[2*vertex_data.texcoord_index];
texcoords[2*i+1] = attrib.texcoords[2*vertex_data.texcoord_index + 1];
texcoords.data[2*i] = attrib.texcoords[2*vertex_data.texcoord_index];
texcoords.data[2*i+1] = attrib.texcoords[2*vertex_data.texcoord_index + 1];
indices[i] = i;
indices.data[i] = i;
}
result.num_indices = indices_t.size();
@@ -47,22 +46,24 @@ Mesh createMesh(const char* obj_file) {
glBindVertexArray(result.vao);
glBindBuffer(GL_ARRAY_BUFFER, result.vbo_xyz);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertices.length * sizeof(float), vertices.data, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, result.vbo_uv);
glBufferData(GL_ARRAY_BUFFER, texcoords.size() * sizeof(float), texcoords.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, texcoords.length * sizeof(float), texcoords.data, GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, result.vbo_norm);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(float), normals.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, normals.length * sizeof(float), normals.data, GL_STATIC_DRAW);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(2);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
scratchEnd(temp);
return result;
}

View File

@@ -1,3 +1,4 @@
#include <glm/gtc/type_ptr.hpp>
#include "Shader.h"
#include "../lib/djstdlib/core.h"
#include "../lib/glad/glad.h"
@@ -58,3 +59,32 @@ Shader createShader(string vertex_path, string fragment_path) {
return result;
}
void setUniformMat4fv(Shader *s, const char *uniformName, glm::mat4 *matrix) {
glUniformMatrix4fv(
glGetUniformLocation(s->prog_id, uniformName),
1, GL_FALSE, glm::value_ptr(*matrix));
}
void setUniformMat4fv(int uniformLocation, glm::mat4 *matrix) {
glUniformMatrix4fv(uniformLocation, 1, GL_FALSE, glm::value_ptr(*matrix));
}
void setUniform4fv(Shader *s, const char *uniformName, glm::vec4 *vector) {
glUniform4fv(glGetUniformLocation(s->prog_id, uniformName), 1, glm::value_ptr(*vector));
}
void setUniform4fv(int uniformLocation, glm::vec4 *vector) {
glUniform4fv(uniformLocation, 1, glm::value_ptr(*vector));
}
void setUniform3fv(Shader *s, const char *uniformName, glm::vec3 *vector) {
glUniform3fv(glGetUniformLocation(s->prog_id, uniformName), 1, glm::value_ptr(*vector));
}
void setUniform3fv(int uniformLocation, glm::vec3 *vector) {
glUniform3fv(uniformLocation, 1, glm::value_ptr(*vector));
}
int getUniformLocation(Shader *s, const char *uniformName) {
return glGetUniformLocation(s->prog_id, uniformName);
}

View File

@@ -2,6 +2,7 @@
#define LEDDA_SHADER_H
#include "../lib/djstdlib/core.h"
#include "glm/glm.hpp"
struct Shader {
uint32 prog_id;
@@ -9,4 +10,15 @@ struct Shader {
Shader createShader(string vertex_path, string fragment_path);
void setUniformMat4fv(Shader *s, const char *uniformName, glm::mat4 *matrix);
void setUniformMat4fv(int uniformLocation, glm::mat4 *matrix);
void setUniform4fv(Shader *s, const char *uniformName, glm::vec4 *vector);
void setUniform4fv(int uniformLocation, glm::vec4 *vector);
void setUniform3fv(Shader *s, const char *uniformName, glm::vec3 *vector);
void setUniform3fv(int uniformLocation, glm::vec3 *vector);
int getUniformLocation(Shader *s, const char *uniformName);
#endif

View File

@@ -1,7 +1,7 @@
#include "Texture.h"
#include <iostream>
#include "../lib/loaders/stb_image.h"
#include "../lib/glad/glad.h"
#include "../lib/djstdlib/core.h"
Texture createTexture(const char* source_path) {
Texture result = {0};
@@ -18,7 +18,7 @@ Texture createTexture(const char* source_path) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, result.width, result.height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
std::cout << "Failed to load texture." << std::endl;
log("Failed to load texture.");
}
stbi_image_free(data);