X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=material.cpp;h=32bf9e0fef2d9b609265955a1c455cc64ef99fef;hp=3da687c3a87d0c40d20577a06848be7f6a520d2e;hb=d0c631f46c6db417e013b1bc0edec24cb9c2824a;hpb=0291188124d08368ce28b308482604218bd75393 diff --git a/material.cpp b/material.cpp index 3da687c..32bf9e0 100644 --- a/material.cpp +++ b/material.cpp @@ -1,29 +1,58 @@ #include "material.hpp" #include "image.hpp" +#include + +Material::Material(const aiMaterial &ai, const aiScene &scene, GLuint progId): progId(progId) { + aiString name; + ai.Get(AI_MATKEY_NAME, name); + if (name == aiString("default material")) { + abort(); + } -Material::Material(const aiMaterial &ai, GLuint progId): progId(progId) { aiString path; ai.GetTexture(aiTextureType_DIFFUSE, 1, &path); - albedo = new Texture(std::string(path.C_Str())); + albedo = new Texture(path, scene); path = ""; ai.GetTexture(aiTextureType_NORMALS, 0, &path); - normal = new Texture(std::string(path.C_Str())); + normal = new Texture(path, scene); path = ""; - ai.GetTexture(aiTextureType_UNKNOWN, 0, &path); - metallicRoughness = new Texture(std::string(path.C_Str())); + ai.GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &path); + metallicRoughness = new Texture(path, scene); path = ""; ai.GetTexture(aiTextureType_LIGHTMAP, 0, &path); - ambientOcclusion = new Texture(std::string(path.C_Str())); + if (path == aiString("")) { + fprintf(stderr, "Material %s does not have an AO map", name.C_Str()); + abort(); + } + ambientOcclusion = new Texture(path, scene); } -Material::Texture::Texture(const std::string &fileName) { +Material::Texture::Texture(const aiString fileName, const aiScene &scene) { glGenTextures(1, &texId); glBindTexture(GL_TEXTURE_2D, texId); - Image img("models/" + fileName); + + std::string path; + unsigned char *data; + if (fileName.data[0] == '*') { + // embedded + int embIdx = atoi(&fileName.data[1]); + aiTexture *texture = scene.mTextures[embIdx]; + if (texture->mHeight == 0) { + Image img((unsigned char*)texture->pcData, texture->mWidth, texture->achFormatHint); glTexImage2D(GL_TEXTURE_2D, 0, img.internalFormat(), img.width(), img.height(), 0, img.format(), img.type(), img.data()); + } else { + fprintf(stderr, "TODO: handle uncompressed embedded textures"); + abort(); + } + } else { + // not embedded + Image img("models/" + std::string(fileName.C_Str())); + glTexImage2D(GL_TEXTURE_2D, 0, img.internalFormat(), img.width(), img.height(), 0, img.format(), img.type(), img.data()); + } + glGenerateMipmap(GL_TEXTURE_2D); }