X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=material.cpp;h=e1fb5c3a391605fe4c118db74b594cb39358b0d3;hp=9ed7e9029401a859dc1428f39e091deecfeefa9b;hb=83eb84c159c8ec75cda9fbeb05e3d5746b54ad53;hpb=fbe4b82329e3fea129cb50141e382e77df04f46b diff --git a/material.cpp b/material.cpp index 9ed7e90..e1fb5c3 100644 --- a/material.cpp +++ b/material.cpp @@ -5,28 +5,25 @@ 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(); - } aiString path; ai.GetTexture(aiTextureType_DIFFUSE, 1, &path); + if (path.length != 0) albedo = new Texture(path, scene); path = ""; ai.GetTexture(aiTextureType_NORMALS, 0, &path); + if (path.length != 0) normal = new Texture(path, scene); path = ""; ai.GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &path); + if (path.length != 0) 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(); - } + if (path.length != 0) ambientOcclusion = new Texture(path, scene); } @@ -56,19 +53,39 @@ Material::Texture::Texture(const aiString fileName, const aiScene &scene) { } void Material::bind() const { - glUniform1i(glGetUniformLocation(progId, "albedoMap"), 0); + if (ambientOcclusion != nullptr) { + glUniform1i(glGetUniformLocation(progId, "material.albedoMap"), 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, albedo->texId); + glUniform1i(glGetUniformLocation(progId, "material.hasAlbedo"), 1); + } else { + glUniform1i(glGetUniformLocation(progId, "material.hasAlbedo"), 0); + } - glUniform1i(glGetUniformLocation(progId, "normalMap"), 1); + if (normal != nullptr) { + glUniform1i(glGetUniformLocation(progId, "material.normalMap"), 1); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, normal->texId); + glUniform1i(glGetUniformLocation(progId, "material.hasNormal"), 1); + } else { + glUniform1i(glGetUniformLocation(progId, "material.hasNormal"), 0); + } + if (metallicRoughness != nullptr) { glUniform1i(glGetUniformLocation(progId, "metallicRoughnessMap"), 2); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, metallicRoughness->texId); + glUniform1i(glGetUniformLocation(progId, "material.hasMetallicRoughness"), 1); + } else { + glUniform1i(glGetUniformLocation(progId, "material.hasMetallicRoughness"), 0); + } + if (ambientOcclusion != nullptr) { glUniform1i(glGetUniformLocation(progId, "aoMap"), 3); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, ambientOcclusion->texId); + glUniform1i(glGetUniformLocation(progId, "material.hasAo"), 1); + } else { + glUniform1i(glGetUniformLocation(progId, "material.hasAo"), 0); + } }