X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=material.cpp;h=32bf9e0fef2d9b609265955a1c455cc64ef99fef;hp=470f1a2caa370bd2292a474f2ccc34f39e9662bf;hb=d0c631f46c6db417e013b1bc0edec24cb9c2824a;hpb=37cba564a96018a5500e942498d4e48c0ebe73ed diff --git a/material.cpp b/material.cpp index 470f1a2..32bf9e0 100644 --- a/material.cpp +++ b/material.cpp @@ -1,42 +1,75 @@ #include "material.hpp" -#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); + if (name == aiString("default material")) { + abort(); + } -Material::Material(const aiMaterial &ai, GLuint progId): progId(progId) { - if (ai.GetTextureCount(aiTextureType_DIFFUSE) > 0) { aiString path; - ai.GetTexture(aiTextureType_DIFFUSE, 0, &path); - texture = new Texture(std::string(path.C_Str())); + ai.GetTexture(aiTextureType_DIFFUSE, 1, &path); + albedo = new Texture(path, scene); + path = ""; + + ai.GetTexture(aiTextureType_NORMALS, 0, &path); + normal = new Texture(path, scene); + path = ""; + + ai.GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &path); + metallicRoughness = new Texture(path, scene); + path = ""; + + ai.GetTexture(aiTextureType_LIGHTMAP, 0, &path); + if (path == aiString("")) { + fprintf(stderr, "Material %s does not have an AO map", name.C_Str()); + abort(); } - 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); + ambientOcclusion = new Texture(path, scene); } -Material::Texture::Texture(const std::string &path) { - auto provider = CGDataProviderCreateWithFilename(path.c_str()); - auto ref = CGImageCreateWithJPEGDataProvider(provider, nullptr, false, kCGRenderingIntentDefault); - auto dataRef = CGDataProviderCopyData(CGImageGetDataProvider(ref)); - auto img = (unsigned char*) CFDataGetBytePtr(dataRef); - +Material::Texture::Texture(const aiString fileName, const aiScene &scene) { 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); + 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); } 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"), texture != nullptr); - - if (texture) - glBindTexture(GL_TEXTURE_2D, texture->texId); + glUniform1i(glGetUniformLocation(progId, "albedoMap"), 0); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, albedo->texId); + + glUniform1i(glGetUniformLocation(progId, "normalMap"), 1); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, normal->texId); + + glUniform1i(glGetUniformLocation(progId, "metallicRoughnessMap"), 2); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, metallicRoughness->texId); + + glUniform1i(glGetUniformLocation(progId, "aoMap"), 3); + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, ambientOcclusion->texId); }