The engine can now import models with textures but bugs.
More complex models have a bug where the same textures are potentially being loaded too many times.
This commit is contained in:
parent
a33ff9d029
commit
18a785613d
3 changed files with 88 additions and 73 deletions
|
|
@ -55,6 +55,8 @@ void Mesh::draw(ShaderLoader &shader) {
|
|||
|
||||
shader.setFloat(("material." + name + number).c_str(), i);
|
||||
glBindTexture(GL_TEXTURE_2D, textures[i].id);
|
||||
error.log(std::to_string(textures[i].id));
|
||||
error.log(std::to_string(GL_TEXTURE0 + i));
|
||||
}
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
|
|
|
|||
|
|
@ -130,8 +130,19 @@ unsigned int Model::loadTextureFromFile(std::string file,
|
|||
if (image == nullptr) {
|
||||
error.crash("SDL2_image was unable to load a texture", IMG_GetError());
|
||||
}
|
||||
|
||||
// Generate the texture and put its reference id in the texture variable
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
// set some textue defaults
|
||||
float borderColor[] = {1.0f, 1.0f, 0.0f, 1.0f};
|
||||
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
|
||||
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_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
||||
GL_LINEAR_MIPMAP_LINEAR);
|
||||
|
||||
// Handle different SDL Surface data types
|
||||
int mode = GL_RGB;
|
||||
|
|
@ -148,5 +159,6 @@ unsigned int Model::loadTextureFromFile(std::string file,
|
|||
SDL_FreeSurface(image);
|
||||
image = nullptr;
|
||||
|
||||
error.log("loaded texture: " + std::to_string(texture));
|
||||
return texture;
|
||||
}
|
||||
|
|
|
|||
147
src/main.cpp
147
src/main.cpp
|
|
@ -77,30 +77,30 @@ int main(int argc, char **argv) {
|
|||
ROOT_DIR "data/shaders/fragment.glsl");
|
||||
|
||||
// Load texture image
|
||||
SDL_Surface *image = IMG_Load(ROOT_DIR "data/container.jpg");
|
||||
if (image == nullptr) {
|
||||
error.crash("SDL2_image was unable to load a texture", IMG_GetError());
|
||||
}
|
||||
// SDL_Surface *image = IMG_Load(ROOT_DIR "data/container.jpg");
|
||||
// if (image == nullptr) {
|
||||
// error.crash("SDL2_image was unable to load a texture", IMG_GetError());
|
||||
//}
|
||||
|
||||
// create and bind texture object
|
||||
unsigned int texture;
|
||||
glGenTextures(1, &texture);
|
||||
// glBindTexture(GL_TEXTURE_2D, texture);
|
||||
// unsigned int texture;
|
||||
// glGenTextures(1, &texture);
|
||||
//// glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
// Handle different SDL Surface data types
|
||||
int mode = GL_RGB;
|
||||
if (image->format->BytesPerPixel == 4) {
|
||||
mode = GL_RGBA;
|
||||
}
|
||||
//// Handle different SDL Surface data types
|
||||
// int mode = GL_RGB;
|
||||
// if (image->format->BytesPerPixel == 4) {
|
||||
// mode = GL_RGBA;
|
||||
//}
|
||||
|
||||
// Generate texture and mipmap from image
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, mode, image->w, image->h, 0, mode,
|
||||
GL_UNSIGNED_BYTE, image->pixels);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
//// Generate texture and mipmap from image
|
||||
// glTexImage2D(GL_TEXTURE_2D, 0, mode, image->w, image->h, 0, mode,
|
||||
// GL_UNSIGNED_BYTE, image->pixels);
|
||||
// glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
// remove image surface now it's no longer needed to create texture
|
||||
SDL_FreeSurface(image);
|
||||
image = nullptr;
|
||||
// SDL_FreeSurface(image);
|
||||
// image = nullptr;
|
||||
|
||||
// Generate Mesh
|
||||
// Define some useful structures to be used in the mesh object
|
||||
|
|
@ -121,70 +121,70 @@ int main(int argc, char **argv) {
|
|||
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left
|
||||
};
|
||||
|
||||
unsigned int indices[] = {
|
||||
0, 1, 3, // First triangle
|
||||
1, 2, 3 // Second triangle
|
||||
};
|
||||
// unsigned int indices[] = {
|
||||
// 0, 1, 3, // First triangle
|
||||
// 1, 2, 3 // Second triangle
|
||||
//};
|
||||
|
||||
std::vector<Vertex> mvertices;
|
||||
Vertex vertex;
|
||||
vertex.Position = glm::vec3(0.5f, 0.5f, 0.0f);
|
||||
vertex.Normal = glm::vec3();
|
||||
vertex.TexCoords = glm::vec2(1.0f, 1.0f);
|
||||
mvertices.push_back(vertex);
|
||||
vertex.Position = glm::vec3(0.5f, -0.5f, 0.0f);
|
||||
vertex.Normal = glm::vec3();
|
||||
vertex.TexCoords = glm::vec2(1.0f, 0.0f);
|
||||
mvertices.push_back(vertex);
|
||||
vertex.Position = glm::vec3(-0.5f, -0.5f, 0.0f);
|
||||
vertex.Normal = glm::vec3();
|
||||
vertex.TexCoords = glm::vec2(0.0f, 0.0f);
|
||||
mvertices.push_back(vertex);
|
||||
vertex.Position = glm::vec3(-0.5f, 0.5f, 0.0f);
|
||||
vertex.Normal = glm::vec3();
|
||||
vertex.TexCoords = glm::vec2(0.0f, 1.0f);
|
||||
mvertices.push_back(vertex);
|
||||
// std::vector<Vertex> mvertices;
|
||||
// Vertex vertex;
|
||||
// vertex.Position = glm::vec3(0.5f, 0.5f, 0.0f);
|
||||
// vertex.Normal = glm::vec3();
|
||||
// vertex.TexCoords = glm::vec2(1.0f, 1.0f);
|
||||
// mvertices.push_back(vertex);
|
||||
// vertex.Position = glm::vec3(0.5f, -0.5f, 0.0f);
|
||||
// vertex.Normal = glm::vec3();
|
||||
// vertex.TexCoords = glm::vec2(1.0f, 0.0f);
|
||||
// mvertices.push_back(vertex);
|
||||
// vertex.Position = glm::vec3(-0.5f, -0.5f, 0.0f);
|
||||
// vertex.Normal = glm::vec3();
|
||||
// vertex.TexCoords = glm::vec2(0.0f, 0.0f);
|
||||
// mvertices.push_back(vertex);
|
||||
// vertex.Position = glm::vec3(-0.5f, 0.5f, 0.0f);
|
||||
// vertex.Normal = glm::vec3();
|
||||
// vertex.TexCoords = glm::vec2(0.0f, 1.0f);
|
||||
// mvertices.push_back(vertex);
|
||||
|
||||
std::vector<unsigned int> mindices{
|
||||
0, 3, 1, // First triangle
|
||||
1, 3, 2 // Second triangle
|
||||
};
|
||||
// std::vector<unsigned int> mindices{
|
||||
// 0, 3, 1, // First triangle
|
||||
// 1, 3, 2 // Second triangle
|
||||
//};
|
||||
|
||||
std::vector<Texture> mtextures;
|
||||
Texture mtexture;
|
||||
mtexture.id = texture;
|
||||
mtexture.type = "texture_diffuse";
|
||||
// std::vector<Texture> mtextures;
|
||||
// Texture mtexture;
|
||||
// mtexture.id = texture;
|
||||
// mtexture.type = "texture_diffuse";
|
||||
|
||||
Mesh mesh(mvertices, mindices, mtextures);
|
||||
// Mesh mesh(mvertices, mindices, mtextures);
|
||||
|
||||
std::vector<Vertex> mvertices2;
|
||||
vertex.Position = glm::vec3(0.5f, 0.5f, 1.0f);
|
||||
vertex.Normal = glm::vec3();
|
||||
vertex.TexCoords = glm::vec2(1.0f, 1.0f);
|
||||
mvertices2.push_back(vertex);
|
||||
vertex.Position = glm::vec3(0.5f, -0.5f, 1.0f);
|
||||
vertex.Normal = glm::vec3();
|
||||
vertex.TexCoords = glm::vec2(1.0f, 0.0f);
|
||||
mvertices2.push_back(vertex);
|
||||
vertex.Position = glm::vec3(-0.5f, -0.5f, 1.0f);
|
||||
vertex.Normal = glm::vec3();
|
||||
vertex.TexCoords = glm::vec2(0.0f, 0.0f);
|
||||
mvertices2.push_back(vertex);
|
||||
vertex.Position = glm::vec3(-0.5f, 0.5f, 1.0f);
|
||||
vertex.Normal = glm::vec3();
|
||||
vertex.TexCoords = glm::vec2(0.0f, 1.0f);
|
||||
mvertices2.push_back(vertex);
|
||||
// std::vector<Vertex> mvertices2;
|
||||
// vertex.Position = glm::vec3(0.5f, 0.5f, 1.0f);
|
||||
// vertex.Normal = glm::vec3();
|
||||
// vertex.TexCoords = glm::vec2(1.0f, 1.0f);
|
||||
// mvertices2.push_back(vertex);
|
||||
// vertex.Position = glm::vec3(0.5f, -0.5f, 1.0f);
|
||||
// vertex.Normal = glm::vec3();
|
||||
// vertex.TexCoords = glm::vec2(1.0f, 0.0f);
|
||||
// mvertices2.push_back(vertex);
|
||||
// vertex.Position = glm::vec3(-0.5f, -0.5f, 1.0f);
|
||||
// vertex.Normal = glm::vec3();
|
||||
// vertex.TexCoords = glm::vec2(0.0f, 0.0f);
|
||||
// mvertices2.push_back(vertex);
|
||||
// vertex.Position = glm::vec3(-0.5f, 0.5f, 1.0f);
|
||||
// vertex.Normal = glm::vec3();
|
||||
// vertex.TexCoords = glm::vec2(0.0f, 1.0f);
|
||||
// mvertices2.push_back(vertex);
|
||||
|
||||
Mesh mesh2(mvertices2, mindices, mtextures);
|
||||
// Mesh mesh2(mvertices2, mindices, mtextures);
|
||||
|
||||
std::vector<Mesh> modelMeshes;
|
||||
modelMeshes.push_back(mesh);
|
||||
modelMeshes.push_back(mesh2);
|
||||
Model model(modelMeshes);
|
||||
// std::vector<Mesh> modelMeshes;
|
||||
// modelMeshes.push_back(mesh);
|
||||
// modelMeshes.push_back(mesh2);
|
||||
// Model model(modelMeshes);
|
||||
|
||||
Model model2(mesh);
|
||||
// Model model2(mesh);
|
||||
|
||||
model.translate(glm::vec3(1.0f, 0.0f, 0.0f));
|
||||
// model.translate(glm::vec3(1.0f, 0.0f, 0.0f));
|
||||
|
||||
// Model backpack(ROOT_DIR "data/models/backpack/backpack.obj");
|
||||
Model cube(ROOT_DIR "data/models/cube/cube.obj");
|
||||
|
|
@ -231,6 +231,7 @@ int main(int argc, char **argv) {
|
|||
// model2.draw(shader);
|
||||
// model.draw(shader);
|
||||
cube.draw(shader);
|
||||
// backpack.draw(shader);
|
||||
|
||||
// Finally render everything
|
||||
shader.use();
|
||||
|
|
|
|||
Loading…
Reference in a new issue