Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add textures to renderer #10

Merged
merged 1 commit into from
Sep 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,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 @@ -130,7 +130,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 @@ -199,10 +199,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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice addition.

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 @@ -39,6 +39,7 @@

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

#include <iostream>

Expand All @@ -54,7 +55,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