Skip to content

Commit

Permalink
add stb_image
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJPinto committed May 21, 2024
1 parent 6f7b601 commit df8c639
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
8 changes: 7 additions & 1 deletion engine/include/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern "C" {
#include <iostream>
#include <set>
#include <vector>
#include <stb_image/stb_image.h>

#include "light.hpp"
#include "utils.hpp"
Expand All @@ -36,16 +37,21 @@ class Model {
Model();
Model(std::string filename, std::vector<Vertex> points);

void setupModel();
void initModel();
void drawModel();


std::vector<Vertex> getPoints();

private:
GLuint _vbo, _ibo, _normals, _textures;
std::vector<Vertex> _points;
std::string _texture_filepath;
Model(std::string filename, std::vector<Vertex> vbo,
std::vector<unsigned int> ibo, int id, std::vector<Vertex> points);
bool loadTexture();
void setupModel();

};

#endif // MODEL_HPP
10 changes: 10 additions & 0 deletions engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ void setupConfig(char* arg) {
}
}

void setupModels(Group& group) {
for (Model& model : group.models) {
model.initModel();
}
for (Group& g : group.subgroups) {
setupModels(g);
}
}

int main(int argc, char** argv) {
if (argc == 1) {
std::cout << "Invalid Arguments\n";
Expand Down Expand Up @@ -250,6 +259,7 @@ int main(int argc, char** argv) {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
setupLights(c.lights);
setupModels(c.group);

// enter GLUT�s main cycle
glutMainLoop();
Expand Down
36 changes: 32 additions & 4 deletions engine/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern "C" {
#endif
}

#define STB_IMAGE_IMPLEMENTATION
#define _USE_MATH_DEFINES
#include <math.h>

Expand Down Expand Up @@ -99,6 +100,36 @@ Model::Model(std::string filename, std::vector<Vertex> points) {
counter++;
}

void Model::initModel() {
if (!this->initialized) {
this->initialized = true;
setupModel();
bool tex = loadTexture();
if (!tex) {
std::cout << "Didn't manage to read filepath: " << this->_texture_filepath << std::endl;
}
}
}

bool Model::loadTexture() {
// Load image data
int width, height, num_channels;
unsigned char* image_data = stbi_load(this->_texture_filepath.data(), &width, &height, &num_channels, 0);
unsigned int texture_id;

// Create texture buffer and upload data to gpu
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);

// Free image data after uploading it
stbi_image_free(image_data);
return true;
}


void Model::setupModel() {
std::vector<float> points = positionsFloats(this->vbo);
std::vector<float> normals = normalFloats(this->vbo);
Expand Down Expand Up @@ -129,10 +160,7 @@ void Model::setupModel() {
}

void Model::drawModel() {
if (!this->initialized) {
this->initialized = true;
setupModel();
}
initModel();

glBindBuffer(GL_ARRAY_BUFFER, this->_vbo);
glVertexPointer(3, GL_FLOAT, 0, 0);
Expand Down
1 change: 1 addition & 0 deletions lib/stb_image/stb_image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "stb_image.h"

0 comments on commit df8c639

Please sign in to comment.