Skip to content

Commit

Permalink
Merge pull request #10 from JimmyDaSilva/add_textures
Browse files Browse the repository at this point in the history
Add textures to renderer
  • Loading branch information
vrabaud authored Sep 25, 2016
2 parents 479f90d + 1d57951 commit e26dedf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ Model::recursiveTextureLoad(const struct aiScene *sc, const aiNode* nd)

// See if another mesh is already using this texture, if so, just copy GLuint instead of remaking entire texture
bool newTextureToBeLoaded = true;
for (int x = 0; x < texturesAndPaths.size(); x++)
for(std::map<std::pair<const aiNode*, int>, TextureAndPath>::iterator it = texturesAndPaths.begin(); it != texturesAndPaths.end(); ++it)
{
if (texturesAndPaths[x].pathName == *str)
if(it->second.pathName == *str)
{
TextureAndPath reusedTexture;
reusedTexture.hTexture = texturesAndPaths[x].hTexture;
reusedTexture.hTexture = it->second.hTexture;
reusedTexture.pathName = *str;
texturesAndPaths.push_back(reusedTexture);
texturesAndPaths[std::make_pair(nd, n)] = reusedTexture;
newTextureToBeLoaded = false;

std::cout << "Texture reused." << std::endl;
Expand Down Expand Up @@ -138,7 +138,7 @@ Model::recursiveTextureLoad(const struct aiScene *sc, const aiNode* nd)

std::cout << "texture loaded." << std::endl;

texturesAndPaths.push_back(newTexture);
texturesAndPaths[std::make_pair(nd, n)] = newTexture;
}
}
}
Expand Down Expand Up @@ -207,10 +207,11 @@ Model::recursive_render(const struct aiScene *sc, const aiNode* nd) const
// draw all meshes assigned to this node
for (; n < nd->mNumMeshes; ++n)
{
glEnable(GL_TEXTURE_2D);
const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]];

if (n < texturesAndPaths.size())
glBindTexture(GL_TEXTURE_2D, texturesAndPaths[n].hTexture);
glBindTexture(GL_TEXTURE_2D, texturesAndPaths.at(std::make_pair(nd, n)).hTexture);

apply_material(sc->mMaterials[mesh->mMaterialIndex]);

Expand Down
3 changes: 2 additions & 1 deletion src/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

#include <FreeImage.h>
#include <vector>
#include <map>

#include <iostream>

Expand All @@ -60,7 +61,7 @@ struct TextureAndPath
class Model
{
private:
std::vector<TextureAndPath> texturesAndPaths;
std::map<std::pair<const aiNode*, int>, TextureAndPath> texturesAndPaths;
const struct aiScene* scene;

void
Expand Down

0 comments on commit e26dedf

Please sign in to comment.