Add glTF PBR model loading
[opengl.git] / material.cpp
index df8ef8b1867b36ba9e8a80b8eab6e17a9009d393..3da687c3a87d0c40d20577a06848be7f6a520d2e 100644 (file)
@@ -2,72 +2,45 @@
 #include "image.hpp"
 
 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()));
-       }
+       ai.GetTexture(aiTextureType_DIFFUSE, 1, &path);
+       albedo = new Texture(std::string(path.C_Str()));
+       path = "";
 
-       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_NORMALS, 0, &path);
-               normalMap = new Texture(std::string(path.C_Str()));
-       }
-       ai.Get(AI_MATKEY_SHININESS, shininess);
-       ai.Get(AI_MATKEY_REFLECTIVITY, reflectivity);
-       ai.Get(AI_MATKEY_REFRACTI, refractiveIndex);
-       if (ai.Get(AI_MATKEY_OPACITY, opacity) == aiReturn_FAILURE)
-               opacity = 1.f;
+       normal = new Texture(std::string(path.C_Str()));
+       path = "";
+
+       ai.GetTexture(aiTextureType_UNKNOWN, 0, &path);
+       metallicRoughness = new Texture(std::string(path.C_Str()));
+       path = "";
        
-       ai.Get(AI_MATKEY_COLOR_AMBIENT, ambient);
-       ai.Get(AI_MATKEY_COLOR_DIFFUSE, diffuse);
-       ai.Get(AI_MATKEY_COLOR_SPECULAR, specular);
+       ai.GetTexture(aiTextureType_LIGHTMAP, 0, &path);
+       ambientOcclusion = new Texture(std::string(path.C_Str()));
 }
 
 Material::Texture::Texture(const std::string &fileName) {
        glGenTextures(1, &texId);
        glBindTexture(GL_TEXTURE_2D, texId);
        Image img("models/" + fileName);
-       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width(), img.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.data());
+       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, "albedo"), 0.5f, 0.f, 0.f); */
-       /* glUniform1f(glGetUniformLocation(progId, "metallic"), 0.2f); */
-       /* glUniform1f(glGetUniformLocation(progId, "roughness"), 0.3f); */
-       /* glUniform1f(glGetUniformLocation(progId, "ao"), 1.f); */
-
-       /* glUniform4f(glGetUniformLocation(progId, "material.ambient"), ambient.r, ambient.g, ambient.b, ambient.a); */
-       /* glUniform4f(glGetUniformLocation(progId, "material.diffuse"), diffuse.r, diffuse.g, diffuse.b, diffuse.a); */
-       /* glUniform4f(glGetUniformLocation(progId, "material.specular"), specular.r, specular.g, specular.b, specular.a); */
-       /* glUniform1f(glGetUniformLocation(progId, "material.shininess"), shininess); */
-       /* glUniform1f(glGetUniformLocation(progId, "material.reflectivity"), reflectivity); */
-       /* glUniform1f(glGetUniformLocation(progId, "material.refractiveIndex"), refractiveIndex); */
-       /* glUniform1f(glGetUniformLocation(progId, "material.opacity"), opacity); */
-
-       /* glUniform1i(glGetUniformLocation(progId, "material.hasTexture"), diffuseMap != nullptr); */
-       /* glUniform1i(glGetUniformLocation(progId, "material.hasSpecularMap"), specularMap != nullptr); */
-       /* glUniform1i(glGetUniformLocation(progId, "material.hasNormalMap"), normalMap != nullptr); */
-       /* glUniform1i(glGetUniformLocation(progId, "material.hasTexture"), diffuseMap != nullptr); */
-       /* glUniform1i(glGetUniformLocation(progId, "material.hasSpecularMap"), specularMap != nullptr); */
-       /* glUniform1i(glGetUniformLocation(progId, "material.hasNormalMap"), normalMap != nullptr); */
+       glUniform1i(glGetUniformLocation(progId, "albedoMap"), 0);
+       glActiveTexture(GL_TEXTURE0);
+       glBindTexture(GL_TEXTURE_2D, albedo->texId);
 
-       /* glUniform1i(glGetUniformLocation(progId, "material.diffuseMap"), 2); */
-       /* glActiveTexture(GL_TEXTURE2); */
-       /* if (diffuseMap) glBindTexture(GL_TEXTURE_2D, diffuseMap->texId); */
+       glUniform1i(glGetUniformLocation(progId, "normalMap"), 1);
+       glActiveTexture(GL_TEXTURE1);
+       glBindTexture(GL_TEXTURE_2D, normal->texId);
 
-       /* glUniform1i(glGetUniformLocation(progId, "material.specularMap"), 3); */
-       /* glActiveTexture(GL_TEXTURE3); */
-       /* if (specularMap) glBindTexture(GL_TEXTURE_2D, specularMap->texId); */
+       glUniform1i(glGetUniformLocation(progId, "metallicRoughnessMap"), 2);
+       glActiveTexture(GL_TEXTURE2);
+       glBindTexture(GL_TEXTURE_2D, metallicRoughness->texId);
 
-       /* glUniform1i(glGetUniformLocation(progId, "material.normalMap"), 4); */
-       /* glActiveTexture(GL_TEXTURE4); */
-       /* if (normalMap) glBindTexture(GL_TEXTURE_2D, normalMap->texId); */
+       glUniform1i(glGetUniformLocation(progId, "aoMap"), 3);
+       glActiveTexture(GL_TEXTURE3);
+       glBindTexture(GL_TEXTURE_2D, ambientOcclusion->texId);
 }