Fix stupid typo
[opengl.git] / material.cpp
1 #include "material.hpp"
2 #include "image.hpp"
3 #include <assimp/pbrmaterial.h>
4
5 Material::Material(const aiMaterial &ai, const aiScene &scene, GLuint progId): progId(progId) {
6         aiString name;
7         ai.Get(AI_MATKEY_NAME, name);   
8
9         aiString path;
10         ai.GetTexture(aiTextureType_DIFFUSE, 1, &path);
11         if (path.length != 0)
12                 albedo = new Texture(path, scene);
13         path = "";
14
15         ai.GetTexture(aiTextureType_NORMALS, 0, &path);
16         if (path.length != 0)
17                 normal = new Texture(path, scene);
18         path = "";
19
20         ai.GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &path);
21         if (path.length != 0)
22                 metallicRoughness = new Texture(path, scene);
23         path = "";
24         
25         ai.GetTexture(aiTextureType_LIGHTMAP, 0, &path);
26         if (path.length != 0)
27                 ambientOcclusion = new Texture(path, scene);
28 }
29
30 Material::Texture::Texture(const aiString fileName, const aiScene &scene) {
31         glGenTextures(1, &texId);
32         glBindTexture(GL_TEXTURE_2D, texId);
33         
34         std::string path;
35         if (fileName.data[0] == '*') {
36                 // embedded
37                 int embIdx = atoi(&fileName.data[1]);
38                 aiTexture *texture = scene.mTextures[embIdx];
39                 if (texture->mHeight == 0) {
40                         Image img((unsigned char*)texture->pcData, texture->mWidth, texture->achFormatHint);
41                         glTexImage2D(GL_TEXTURE_2D, 0, img.internalFormat(), img.width(), img.height(), 0, img.format(), img.type(), img.data());
42                 } else {
43                         fprintf(stderr, "TODO: handle uncompressed embedded textures");
44                         abort();
45                 }
46         } else {
47                 // not embedded
48                 Image img("models/" + std::string(fileName.C_Str()));
49                 glTexImage2D(GL_TEXTURE_2D, 0, img.internalFormat(), img.width(), img.height(), 0, img.format(), img.type(), img.data());
50         }
51
52         // magnifying - no bigger mipmap available, so use linear filtering
53         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
54         // minifying - use a linear blend of two mipmaps
55         // which are themselves filtered linearly
56         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
57     
58     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
59     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
60     
61         glGenerateMipmap(GL_TEXTURE_2D);
62 }
63
64 void Material::bind() const {
65         if (albedo != nullptr) {
66                 glUniform1i(glGetUniformLocation(progId, "mat.albedoMap"), 0);
67                 glActiveTexture(GL_TEXTURE0);
68                 glBindTexture(GL_TEXTURE_2D, albedo->texId);
69                 glUniform1i(glGetUniformLocation(progId, "mat.hasAlbedo"), 1);
70         } else {
71                 glUniform1i(glGetUniformLocation(progId, "mat.hasAlbedo"), 0);
72         }
73
74         if (normal != nullptr) {
75                 glUniform1i(glGetUniformLocation(progId, "mat.normalMap"), 1);
76                 glActiveTexture(GL_TEXTURE1);
77                 glBindTexture(GL_TEXTURE_2D, normal->texId);
78                 glUniform1i(glGetUniformLocation(progId, "mat.hasNormal"), 1);
79         } else {
80                 glUniform1i(glGetUniformLocation(progId, "mat.hasNormal"), 0);
81         }
82
83         if (metallicRoughness != nullptr) {
84                 glUniform1i(glGetUniformLocation(progId, "mat.metallicRoughnessMap"), 2);
85                 glActiveTexture(GL_TEXTURE2);
86                 glBindTexture(GL_TEXTURE_2D, metallicRoughness->texId);
87                 glUniform1i(glGetUniformLocation(progId, "mat.hasMetallicRoughness"), 1);
88         } else {
89                 glUniform1i(glGetUniformLocation(progId, "mat.hasMetallicRoughness"), 0);
90         }
91
92         if (ambientOcclusion != nullptr) {
93                 glUniform1i(glGetUniformLocation(progId, "mat.aoMap"), 3);
94                 glActiveTexture(GL_TEXTURE3);
95                 glBindTexture(GL_TEXTURE_2D, ambientOcclusion->texId);
96                 glUniform1i(glGetUniformLocation(progId, "mat.hasAo"), 1);
97         } else {
98                 glUniform1i(glGetUniformLocation(progId, "mat.hasAo"), 0);
99         }
100 }