#include "material.hpp"
#include "image.hpp"
+#include <assimp/pbrmaterial.h>
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, 0, &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.length != 0)
ambientOcclusion = new Texture(path, scene);
}
glBindTexture(GL_TEXTURE_2D, texId);
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);
+ 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");
glTexImage2D(GL_TEXTURE_2D, 0, img.internalFormat(), img.width(), img.height(), 0, img.format(), img.type(), img.data());
}
+ // 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);
+
+ float aniso = 0;
+ glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
+
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+
glGenerateMipmap(GL_TEXTURE_2D);
}
void Material::bind() const {
- glUniform1i(glGetUniformLocation(progId, "albedoMap"), 0);
+ if (albedo != nullptr) {
+ glUniform1i(glGetUniformLocation(progId, "mat.albedoMap"), 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, albedo->texId);
+ glUniform1i(glGetUniformLocation(progId, "mat.hasAlbedo"), 1);
+ } else {
+ glUniform1i(glGetUniformLocation(progId, "mat.hasAlbedo"), 0);
+ }
- glUniform1i(glGetUniformLocation(progId, "normalMap"), 1);
+ if (normal != nullptr) {
+ glUniform1i(glGetUniformLocation(progId, "mat.normalMap"), 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, normal->texId);
+ glUniform1i(glGetUniformLocation(progId, "mat.hasNormal"), 1);
+ } else {
+ glUniform1i(glGetUniformLocation(progId, "mat.hasNormal"), 0);
+ }
- glUniform1i(glGetUniformLocation(progId, "metallicRoughnessMap"), 2);
+ if (metallicRoughness != nullptr) {
+ glUniform1i(glGetUniformLocation(progId, "mat.metallicRoughnessMap"), 2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, metallicRoughness->texId);
+ glUniform1i(glGetUniformLocation(progId, "mat.hasMetallicRoughness"), 1);
+ } else {
+ glUniform1i(glGetUniformLocation(progId, "mat.hasMetallicRoughness"), 0);
+ }
- glUniform1i(glGetUniformLocation(progId, "aoMap"), 3);
+ 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);
+ }
}