X-Git-Url: http://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=material.cpp;h=a1d44beb6d07929865ee5309b6916f4ab109d951;hp=06bf6fdeef06c3494229c03f7bd03a7003992351;hb=45e43c5d0b6a1415185211128d82190fd19c3fe9;hpb=ba9c738e8660304aa0341eb44118e63502a4a009 diff --git a/material.cpp b/material.cpp index 06bf6fd..a1d44be 100644 --- a/material.cpp +++ b/material.cpp @@ -1,80 +1,100 @@ #include "material.hpp" -#include -#include -#include +#include "image.hpp" +#include + +Material::Material(const aiMaterial &ai, const aiScene &scene, GLuint progId): progId(progId) { + aiString name; + ai.Get(AI_MATKEY_NAME, name); -Material::Material(const aiMaterial &ai, GLuint progId): progId(progId) { - if (ai.GetTextureCount(aiTextureType_DIFFUSE) > 0) { - aiString path; - ai.GetTexture(aiTextureType_DIFFUSE, 0, &path); - diffuseMap = new Texture(std::string(path.C_Str())); - } - if (ai.GetTextureCount(aiTextureType_SPECULAR) > 0) { - aiString path; - ai.GetTexture(aiTextureType_SPECULAR, 0, &path); - specularMap = new Texture(std::string(path.C_Str())); - } - if (ai.GetTextureCount(aiTextureType_NORMALS) > 0) { aiString path; + ai.GetTexture(aiTextureType_DIFFUSE, 1, &path); + if (path.length != 0) + albedo = new Texture(path, scene); + path = ""; + ai.GetTexture(aiTextureType_NORMALS, 0, &path); - normalMap = new Texture(std::string(path.C_Str())); - } - ai.Get(AI_MATKEY_SHININESS, shininess); - ai.Get(AI_MATKEY_COLOR_AMBIENT, ambient); - ai.Get(AI_MATKEY_COLOR_DIFFUSE, diffuse); - ai.Get(AI_MATKEY_COLOR_SPECULAR, specular); -} + if (path.length != 0) + normal = new Texture(path, scene); + path = ""; -Material::Texture::Texture(const std::string &path) { + ai.GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &path); + if (path.length != 0) + metallicRoughness = new Texture(path, scene); + path = ""; - auto provider = CGDataProviderCreateWithFilename(path.c_str()); - std::ifstream file(path); - long magic; - file.read((char*)&magic, 8); - file.close(); + ai.GetTexture(aiTextureType_LIGHTMAP, 0, &path); + if (path.length != 0) + ambientOcclusion = new Texture(path, scene); +} + +Material::Texture::Texture(const aiString fileName, const aiScene &scene) { + glGenTextures(1, &texId); + glBindTexture(GL_TEXTURE_2D, texId); - CGImageRef ref; + std::string path; + 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()); + } - if (magic == 0x0a1a0a0d474e5089) // png magic number - ref = CGImageCreateWithPNGDataProvider(provider, nullptr, false, kCGRenderingIntentDefault); - else - ref = CGImageCreateWithJPEGDataProvider(provider, nullptr, false, kCGRenderingIntentDefault); + // magnifying - no bigger mipmap available, so use linear filtering + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + // minifying - use a linear blend of two mipmaps + // which are themselves filtered linearly + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - auto dataRef = CGDataProviderCopyData(CGImageGetDataProvider(ref)); - auto img = (unsigned char*) CFDataGetBytePtr(dataRef); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glGenTextures(1, &texId); - glBindTexture(GL_TEXTURE_2D, texId); - auto width = CGImageGetWidth(ref), height = CGImageGetHeight(ref); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img); glGenerateMipmap(GL_TEXTURE_2D); - - CGImageRelease(ref); - CFRelease(dataRef); } void Material::bind() const { - glUniform3f(glGetUniformLocation(progId, "material.ambient"), ambient.r, ambient.g, ambient.b); - glUniform3f(glGetUniformLocation(progId, "material.diffuse"), diffuse.r, diffuse.g, diffuse.b); - glUniform3f(glGetUniformLocation(progId, "material.specular"), specular.r, specular.g, specular.b); - glUniform1f(glGetUniformLocation(progId, "material.shininess"), shininess); - glUniform1i(glGetUniformLocation(progId, "material.hasTexture"), diffuseMap != nullptr); - glUniform1i(glGetUniformLocation(progId, "material.hasSpecularMap"), specularMap != nullptr); - glUniform1i(glGetUniformLocation(progId, "material.hasNormalMap"), normalMap != nullptr); - - if (diffuseMap) { - glUniform1i(glGetUniformLocation(progId, "material.diffuseMap"), 0); + if (ambientOcclusion != nullptr) { + glUniform1i(glGetUniformLocation(progId, "mat.albedoMap"), 0); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, diffuseMap->texId); + glBindTexture(GL_TEXTURE_2D, albedo->texId); + glUniform1i(glGetUniformLocation(progId, "mat.hasAlbedo"), 1); + } else { + glUniform1i(glGetUniformLocation(progId, "mat.hasAlbedo"), 0); } - if (specularMap) { - glUniform1i(glGetUniformLocation(progId, "material.specularMap"), 1); + + if (normal != nullptr) { + glUniform1i(glGetUniformLocation(progId, "mat.normalMap"), 1); glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, specularMap->texId); + glBindTexture(GL_TEXTURE_2D, normal->texId); + glUniform1i(glGetUniformLocation(progId, "mat.hasNormal"), 1); + } else { + glUniform1i(glGetUniformLocation(progId, "mat.hasNormal"), 0); } - if (normalMap) { - glUniform1i(glGetUniformLocation(progId, "material.normalMap"), 2); + + if (metallicRoughness != nullptr) { + glUniform1i(glGetUniformLocation(progId, "mat.metallicRoughnessMap"), 2); glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, normalMap->texId); + glBindTexture(GL_TEXTURE_2D, metallicRoughness->texId); + glUniform1i(glGetUniformLocation(progId, "mat.hasMetallicRoughness"), 1); + } else { + glUniform1i(glGetUniformLocation(progId, "mat.hasMetallicRoughness"), 0); + } + + if (ambientOcclusion != nullptr) { + glUniform1i(glGetUniformLocation(progId, "mat.aoMap"), 3); + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, ambientOcclusion->texId); + glUniform1i(glGetUniformLocation(progId, "mat.hasAo"), 1); + } else { + glUniform1i(glGetUniformLocation(progId, "mat.hasAo"), 0); } }