loading obj files working.
All checks were successful
Verify build / verify_build (push) Successful in 1m0s

This commit is contained in:
k 2025-05-28 23:56:20 -04:00
parent a75360a8ef
commit 692fbf8196
7 changed files with 124 additions and 25 deletions

View File

@ -11,6 +11,7 @@ public:
void addFPos(glm::vec3 pos); void addFPos(glm::vec3 pos);
void setRotate(float yaw, float pitch); void setRotate(float yaw, float pitch);
void addRotate(float yaw, float pitch); void addRotate(float yaw, float pitch);
glm::vec3 getPos();
glm::mat4 getView(); glm::mat4 getView();
glm::mat4 getProjection(); glm::mat4 getProjection();
float aspect; float aspect;

View File

@ -8,6 +8,7 @@
class Mesh { class Mesh {
public: public:
Mesh(Shader *s, unsigned int VAO, int count); Mesh(Shader *s, unsigned int VAO, int count);
Mesh(Shader *s, std::string path);
~Mesh(); ~Mesh();
void draw(Camera *c); void draw(Camera *c);
void setPosition(glm::vec3 p); void setPosition(glm::vec3 p);

4
headers/obj.hpp Normal file
View File

@ -0,0 +1,4 @@
#include <glm/glm.hpp>
#include <vector>
unsigned int loadOBJ(const char *path, std::vector<float> *out_vertices);

View File

@ -52,6 +52,8 @@ glm::mat4 Camera::getView() {
return view; return view;
} }
glm::vec3 Camera::getPos() { return pos; }
glm::mat4 Camera::getProjection() { glm::mat4 Camera::getProjection() {
return glm::perspective(glm::radians(fov), aspect, 0.1f, 100.0f); return glm::perspective(glm::radians(fov), aspect, 0.1f, 100.0f);
} }

View File

@ -1,11 +1,10 @@
#include "camera.hpp" #include "camera.hpp"
#include <glad/glad.h> /*Must be before GLFW*/ #include "glad/glad.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <cassert> #include <cassert>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <mesh.hpp> #include <mesh.hpp>
#include <shader.hpp> #include <shader.hpp>
#include <shapes.h>
#include <texture.hpp> #include <texture.hpp>
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
@ -37,21 +36,11 @@ GLFWwindow *init() {
return window; return window;
} }
unsigned int meshInit() { void showFPS(GLFWwindow *window, double delta) {
unsigned int VBO, VAO; char title[22];
glGenVertexArrays(1, &VAO); title[21] = '\0';
glBindVertexArray(VAO); snprintf(title, 20, "OpenGl FPS: [%3.2f]", (1 / delta));
glfwSetWindowTitle(window, title);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(uvcube), uvcube, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
(void *)(3 * sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
return VAO;
} }
int main(void) { int main(void) {
@ -67,9 +56,8 @@ int main(void) {
Shader *lightShader = new Shader("./shaders/lighting/Vert.glsl", Shader *lightShader = new Shader("./shaders/lighting/Vert.glsl",
"./shaders/lighting/LightFrag.glsl"); "./shaders/lighting/LightFrag.glsl");
uint VAO = meshInit(); Mesh *cube = new Mesh(cubeShader, "./cube.obj");
Mesh *cube = new Mesh(cubeShader, VAO, 36); Mesh *light = new Mesh(lightShader, "./cube.obj");
Mesh *light = new Mesh(lightShader, VAO, 36);
cube->setOwnedShader(true); cube->setOwnedShader(true);
light->setOwnedShader(true); light->setOwnedShader(true);
@ -79,9 +67,10 @@ int main(void) {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
float lastFrame = 0; float lastFrame = 0;
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
float currentFrame = static_cast<float>(glfwGetTime()); float currentFrame = (float)glfwGetTime();
deltaTime = currentFrame - lastFrame; deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame; lastFrame = currentFrame;
showFPS(window, deltaTime);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -91,7 +80,7 @@ int main(void) {
s->setVec3("lightColor", lightColor); s->setVec3("lightColor", lightColor);
s->setVec3("ourColor", glm::vec3(0.24725, 0.1995, 0.0745)); s->setVec3("ourColor", glm::vec3(0.24725, 0.1995, 0.0745));
s->setVec3("lightPos", lightPos); s->setVec3("lightPos", lightPos);
s->setVec3("viewPos", glm::vec3(0.0, 0.0, 5.0)); s->setVec3("viewPos", cam->getPos());
s->setVec3("light.ambient", glm::vec3(0.2)); s->setVec3("light.ambient", glm::vec3(0.2));
s->setVec3("light.diffuse", glm::vec3(0.5)); s->setVec3("light.diffuse", glm::vec3(0.5));

View File

@ -1,16 +1,28 @@
#include "camera.hpp" #include "camera.hpp"
#include "obj.hpp"
#include "shader.hpp" #include "shader.hpp"
#include <mesh.hpp> #include <mesh.hpp>
#include <vector>
unsigned int meshInit(std::string path, std::vector<float> &data);
Mesh::Mesh(Shader *s, unsigned int VAO, int count) Mesh::Mesh(Shader *s, unsigned int VAO, int count)
: buffersOwned(false), shaderOwned(false), count(count), VAO(VAO), : buffersOwned(false), shaderOwned(false), count(count), VAO(VAO),
shader(s), position(glm::vec3(0)), scale(glm::vec3(1)), shader(s), position(glm::vec3(0)), scale(glm::vec3(1)),
rotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f)) {} rotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f)) {}
Mesh::Mesh(Shader *s, std::string path)
: buffersOwned(true), shaderOwned(false), shader(s), position(glm::vec3(0)),
scale(glm::vec3(1)), rotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f)) {
std::vector<float> data;
VAO = meshInit(path, data);
count = data.size() / 8;
}
Mesh::~Mesh() { Mesh::~Mesh() {
if (shaderOwned) if (shaderOwned)
delete shader; delete shader;
if (buffersOwned){ if (buffersOwned) {
glDeleteVertexArrays(1, &VAO); glDeleteVertexArrays(1, &VAO);
// TODO vbo, ibo // TODO vbo, ibo
} }
@ -18,7 +30,7 @@ Mesh::~Mesh() {
void Mesh::draw(Camera *c) { void Mesh::draw(Camera *c) {
glm::mat4 model = glm::mat4(1); glm::mat4 model = glm::mat4(1);
glBindVertexArray(VAO);
shader->use(); shader->use();
shader->setMat4("view", c->getView()); shader->setMat4("view", c->getView());
shader->setMat4("projection", c->getProjection()); shader->setMat4("projection", c->getProjection());
@ -26,7 +38,7 @@ void Mesh::draw(Camera *c) {
model = glm::scale(model, scale); model = glm::scale(model, scale);
model = model * glm::mat4_cast(rotation); model = model * glm::mat4_cast(rotation);
shader->setMat4("model", model); shader->setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, count);
} }
void Mesh::setPosition(glm::vec3 p) { position = p; } void Mesh::setPosition(glm::vec3 p) { position = p; }
@ -40,3 +52,27 @@ void Mesh::setOwnedBuffers(bool o) { buffersOwned = o; }
void Mesh::setOwnedShader(bool o) { shaderOwned = o; } void Mesh::setOwnedShader(bool o) { shaderOwned = o; }
Shader *Mesh::getShader() { return shader; } Shader *Mesh::getShader() { return shader; }
unsigned int meshInit(std::string path, std::vector<float> &data) {
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
loadOBJ(path.c_str(), &data);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), data.data(),
GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
(void *)(3 * sizeof(float)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
(void *)(6 * sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2); // not in use
glBindVertexArray(0);
return VAO;
}

66
src/obj.cpp Normal file
View File

@ -0,0 +1,66 @@
#include "obj.hpp"
#include <cassert>
#include <cstddef>
#include <cstdio>
#include <cstring>
/*this is ugly but works*/
unsigned int loadOBJ(const char *path, std::vector<float> *out_vertices) {
std::vector<glm::vec3> verts, norms;
std::vector<glm::vec2> uvs;
FILE *file = fopen(path, "r");
assert(file && "file not found");
int tmp = 0;
while (1) {
char lineHeader[128];
int res = fscanf(file, "%s", lineHeader);
if (res == EOF)
break;
if (strcmp(lineHeader, "v") == 0) {
glm::vec3 vert;
tmp = fscanf(file, "%f %f %f\n", &vert.x, &vert.y, &vert.z);
assert(tmp == 3 && "bad vert");
verts.push_back(vert);
} else if (strcmp(lineHeader, "vt") == 0) {
glm::vec2 uv;
tmp = fscanf(file, "%f %f\n", &uv.x, &uv.y);
assert(tmp == 2 && "bad uv");
uvs.push_back(uv);
} else if (strcmp(lineHeader, "vn") == 0) {
glm::vec3 norm;
tmp = fscanf(file, "%f %f %f\n", &norm.x, &norm.y, &norm.z);
assert(tmp == 3 && "bad norm");
norms.push_back(norm);
} else if (strcmp(lineHeader, "f") == 0) {
unsigned int vIndex[3], uvIndex[3], nIndex[3];
tmp = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vIndex[0],
&uvIndex[0], &nIndex[0], &vIndex[1], &uvIndex[1], &nIndex[1],
&vIndex[2], &uvIndex[2], &nIndex[2]);
assert(tmp == 9 && "bad face");
for (size_t i = 0; i < 3; ++i) {
size_t vi, ni, ui;
vi = (vIndex[i] - 1);
ni = (nIndex[i] - 1);
ui = (uvIndex[i] - 1);
assert(verts.size() > vi);
assert(norms.size() > ni);
assert(uvs.size() > ui);
auto vert = verts[vi];
auto norm = norms[ni];
auto uv = uvs[ui];
out_vertices->push_back(vert.x);
out_vertices->push_back(vert.y);
out_vertices->push_back(vert.z);
out_vertices->push_back(norm.x);
out_vertices->push_back(norm.y);
out_vertices->push_back(norm.z);
out_vertices->push_back(uv.x);
out_vertices->push_back(uv.y);
}
}
}
return verts.size();
}