Get stuff building again
authorLuke Lau <luke_lau@icloud.com>
Sun, 9 Feb 2020 21:59:01 +0000 (21:59 +0000)
committerLuke Lau <luke_lau@icloud.com>
Sun, 9 Feb 2020 21:59:01 +0000 (21:59 +0000)
Makefile
image.cpp
image.hpp
material.cpp
models/newtonsCradle.blend

index 05d7b83b50de1502dd68b1599b187a9ffb611d72..42ebf39a4decb5882ac181c406f8193a24f2628b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,10 +1,13 @@
 all: bin/main
 
+CXX_FLAGS := -g --std=c++17
+
 bin/main: model.o material.o image.o skybox.o program.o main.cpp
-       clang++ -g --std=c++17 $^ -I../assimp/include -L../assimp/lib -lassimp \
+       clang++ $(CXX_FLAGS) $^ \
                -I/usr/local/include -L/usr/local/lib \
+               -lassimp \
                -framework OpenGL -framework glut -framework CoreGraphics -framework CoreFoundation -framework ImageIO -lglew -o $@
        ctags *.cpp
 
 %.o: %.cpp
-       clang++ -g --std=c++17 -I/usr/local/include -c $< -o $@
+       clang++ $(CXX_FLAGS) -I/usr/local/include -c $< -o $@
index 6c09fa31caaca7335e18f302fb687d6393a85dc5..02fd60b878ee1122bd9a027e38df10304d681ef8 100644 (file)
--- a/image.cpp
+++ b/image.cpp
@@ -10,9 +10,15 @@ Image::Image(const std::string &path) {
        CGImageRelease(ref);
 }
 
-Image::Image(const unsigned char *data, size_t length) {
+Image::Image(const unsigned char *data, size_t length, const char *formatHint) {
        CGDataProviderRef dpRef = CGDataProviderCreateWithData(NULL, data, length, NULL);
-       CGImageRef ref = CGImageCreateWithJPEGDataProvider(dpRef, NULL, false, kCGRenderingIntentDefault);
+       CGImageRef ref;
+       if (strcmp("png", formatHint) == 0)
+               ref = CGImageCreateWithPNGDataProvider(dpRef, NULL, false, kCGRenderingIntentDefault);
+       else if (strcmp("jpg", formatHint) == 0)
+               ref = CGImageCreateWithJPEGDataProvider(dpRef, NULL, false, kCGRenderingIntentDefault);
+       else
+               abort();
        initWithImageRef(ref);
        CGImageRelease(ref);
 }
index 6864553c316b702efb124e375f0efa586e5a956e..5a75b0daa6add203c96c2f155ca44638c1ba0600 100644 (file)
--- a/image.hpp
+++ b/image.hpp
@@ -11,7 +11,7 @@
 class Image {
        public:
                Image(const std::string &path);
-               Image(const unsigned char *data, size_t length);
+               Image(const unsigned char *data, size_t length, const char *formatHint);
                ~Image();
                unsigned char *data() const;
                GLfloat width() const;
index c059226009dfeef2e328238bb50a3286d0bcc47f..32bf9e0fef2d9b609265955a1c455cc64ef99fef 100644 (file)
@@ -1,5 +1,6 @@
 #include "material.hpp"
 #include "image.hpp"
+#include <assimp/pbrmaterial.h>
 
 Material::Material(const aiMaterial &ai, const aiScene &scene, GLuint progId): progId(progId) {
        aiString name;
@@ -17,11 +18,15 @@ Material::Material(const aiMaterial &ai, const aiScene &scene, GLuint progId): p
        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);
        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();
+       }
        ambientOcclusion = new Texture(path, scene);
 }
 
@@ -36,7 +41,7 @@ Material::Texture::Texture(const aiString fileName, const aiScene &scene) {
                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");
index 28b9c28ceec409b16800652e2823975e217d635e..f26a49894fba5f457f1de4e6d158ae63ba5b4927 100644 (file)
Binary files a/models/newtonsCradle.blend and b/models/newtonsCradle.blend differ