Skip to content

Commit

Permalink
wip: textures loading
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJPinto committed May 22, 2024
1 parent df8c639 commit 7ca18d4
Show file tree
Hide file tree
Showing 13 changed files with 425 additions and 373 deletions.
4 changes: 1 addition & 3 deletions common/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ struct Point2D {
return x == other.x && y == other.y;
}

auto cross(const Point2D& other) {
return x * other.y - y * other.x;
}
auto cross(const Point2D& other) { return x * other.y - y * other.x; }
};

struct PointHash {
Expand Down
14 changes: 6 additions & 8 deletions engine/include/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ extern "C" {
#endif
}

#include <stb_image/stb_image.h>

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
#include <stb_image/stb_image.h>

#include "light.hpp"
#include "utils.hpp"
Expand All @@ -27,7 +28,7 @@ std::vector<unsigned int> generateIBO(const std::vector<Vertex>& points,

class Model {
public:
std::string filename;
std::string filename, texture_filepath;
std::vector<Vertex> vbo;
std::vector<unsigned int> ibo;
int id;
Expand All @@ -39,19 +40,16 @@ class Model {

void initModel();
void drawModel();

bool loadTexture();
void setupModel();

std::vector<Vertex> getPoints();

private:
GLuint _vbo, _ibo, _normals, _textures;
GLuint _vbo, _ibo, _normals, _textures, _texture_id;
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
4 changes: 3 additions & 1 deletion engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void drawAxis(void) {

glColor3f(1.0f, 1.0f, 1.0f);
glEnd();
if(c.lights.size() != 0) {
if (c.lights.size() != 0) {
glEnable(GL_LIGHTING);
}
}
Expand Down Expand Up @@ -244,6 +244,7 @@ int main(int argc, char** argv) {
glewInit();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

// put callback registry here
glutIdleFunc(renderScene);
Expand All @@ -258,6 +259,7 @@ int main(int argc, char** argv) {
// some OpenGL settings
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
setupLights(c.lights);
setupModels(c.group);

Expand Down
64 changes: 53 additions & 11 deletions engine/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,32 +104,67 @@ 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;

if (this->texture_filepath != "") {
bool tex = loadTexture();
if (!tex) {
std::cerr << "Error loading texture\n";
}
}
}
}

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;
unsigned char* image_data = stbi_load(this->texture_filepath.data(), &width,
&height, &num_channels, 0);

if (!image_data) {
std::cerr << "Failed to load texture: " << this->texture_filepath
<< std::endl;
return false;
}

// 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);
glGenTextures(1, &this->_texture_id);
glBindTexture(GL_TEXTURE_2D, this->_texture_id);

// Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// Determine the correct format
GLenum format;
if (num_channels == 1)
format = GL_RED;
else if (num_channels == 3)
format = GL_RGB;
else if (num_channels == 4)
format = GL_RGBA;
else {
std::cerr << "Unsupported number of channels: " << num_channels
<< std::endl;
stbi_image_free(image_data);
return false;
}

// Upload data to GPU
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
GL_UNSIGNED_BYTE, image_data);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);

// Unbind the texture
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 @@ -162,15 +197,22 @@ void Model::setupModel() {
void Model::drawModel() {
initModel();

glBindTexture(GL_TEXTURE_2D, this->_texture_id);

glBindBuffer(GL_ARRAY_BUFFER, this->_vbo);
glVertexPointer(3, GL_FLOAT, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, this->_normals);
glNormalPointer(GL_FLOAT, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, this->_textures);
glTexCoordPointer(2, GL_FLOAT, 0, 0);

glColor3f(1.0, 1.0, 1.0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->_ibo);
glDrawElements(GL_TRIANGLES, this->ibo.size(), GL_UNSIGNED_INT, 0);

glBindTexture(GL_TEXTURE_2D, 0);
}

std::vector<Vertex> Model::getPoints() { return this->_points; }
17 changes: 8 additions & 9 deletions engine/src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,15 @@ void parseModels(rapidxml::xml_node<>* modelsNode, Group& group) {
std::cerr << "Error reading model file: " << file << std::endl;
return;
}
rapidxml::xml_node<>* texture = modelNode->first_node("texture");

if (texture) {
model.texture_filepath = texture->first_attribute("file")->value();
} else {
model.texture_filepath = "";
}

rapidxml::xml_node<>* color = modelNode->first_node("color");
/*
<color>
<diffuse R="200" G="200" B="200" />
<ambient R="50" G="50" B="50" />
<specular R="0" G="0" B="0" />
<emissive R="0" G="0" B="0" />
<shininess value="0" />
</color>
*/
// Set the vector for diffuse, ambient, specular and emissive based on the
// above example
glm::vec4 diffuse_vec = glm::vec4(0.8f, 0.8f, 0.8f, 1.0f);
Expand Down
Loading

0 comments on commit 7ca18d4

Please sign in to comment.