Add specular and normal mapping
[opengl.git] / material.cpp
index 470f1a2caa370bd2292a474f2ccc34f39e9662bf..06bf6fdeef06c3494229c03f7bd03a7003992351 100644 (file)
@@ -1,12 +1,23 @@
 #include "material.hpp"
 #include <iostream>
+#include <fstream>
 #include <CoreGraphics/CoreGraphics.h>
 
 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()));
+               diffuseMap = new Texture(std::string(path.C_Str()));
+       }
+       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_COLOR_AMBIENT, ambient);
@@ -15,8 +26,20 @@ Material::Material(const aiMaterial &ai, GLuint progId): progId(progId) {
 }
 
 Material::Texture::Texture(const std::string &path) {
+
        auto provider = CGDataProviderCreateWithFilename(path.c_str());
-       auto ref = CGImageCreateWithJPEGDataProvider(provider, nullptr, false, kCGRenderingIntentDefault);
+       std::ifstream file(path);
+       long magic;
+       file.read((char*)&magic, 8);
+       file.close();
+
+       CGImageRef ref;
+
+       if (magic == 0x0a1a0a0d474e5089) // png magic number
+               ref = CGImageCreateWithPNGDataProvider(provider, nullptr, false, kCGRenderingIntentDefault);
+       else
+               ref = CGImageCreateWithJPEGDataProvider(provider, nullptr, false, kCGRenderingIntentDefault);
+       
        auto dataRef = CGDataProviderCopyData(CGImageGetDataProvider(ref));
        auto img = (unsigned char*) CFDataGetBytePtr(dataRef);
 
@@ -35,8 +58,23 @@ void Material::bind() const {
        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);
+       glUniform1i(glGetUniformLocation(progId, "material.hasTexture"), diffuseMap != nullptr);
+       glUniform1i(glGetUniformLocation(progId, "material.hasSpecularMap"), specularMap != nullptr);
+       glUniform1i(glGetUniformLocation(progId, "material.hasNormalMap"), normalMap != nullptr);
 
-       if (texture) 
-               glBindTexture(GL_TEXTURE_2D, texture->texId);
+       if (diffuseMap) {
+               glUniform1i(glGetUniformLocation(progId, "material.diffuseMap"), 0);
+               glActiveTexture(GL_TEXTURE0);
+               glBindTexture(GL_TEXTURE_2D, diffuseMap->texId);
+       }
+       if (specularMap) {
+               glUniform1i(glGetUniformLocation(progId, "material.specularMap"), 1);
+               glActiveTexture(GL_TEXTURE1);
+               glBindTexture(GL_TEXTURE_2D, specularMap->texId);
+       }
+       if (normalMap) {
+               glUniform1i(glGetUniformLocation(progId, "material.normalMap"), 2);
+               glActiveTexture(GL_TEXTURE2);
+               glBindTexture(GL_TEXTURE_2D, normalMap->texId);
+       }
 }