From cece6088814b76b50d97959edf9719915aebefc8 Mon Sep 17 00:00:00 2001 From: kearwood Date: Tue, 25 Oct 2011 05:03:10 +0000 Subject: [PATCH] Initial import of KREngine --HG-- extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%404 --- KREngine/KREngine.xcodeproj/project.pbxproj | 380 +++++ .../contents.xcworkspacedata | 7 + .../KREngine/Classes/KRBoundingVolume.cpp | 264 +++ KREngine/KREngine/Classes/KRBoundingVolume.h | 38 + KREngine/KREngine/Classes/KRCamera.cpp | 65 + KREngine/KREngine/Classes/KRCamera.h | 55 + KREngine/KREngine/Classes/KREngine.h | 107 ++ KREngine/KREngine/Classes/KREngine.mm | 1099 +++++++++++++ KREngine/KREngine/Classes/KRInstance.cpp | 48 + KREngine/KREngine/Classes/KRInstance.h | 42 + KREngine/KREngine/Classes/KRMat4.cpp | 264 +++ KREngine/KREngine/Classes/KRMat4.h | 75 + KREngine/KREngine/Classes/KRMaterial.cpp | 144 ++ KREngine/KREngine/Classes/KRMaterial.h | 59 + .../KREngine/Classes/KRMaterialManager.cpp | 184 +++ KREngine/KREngine/Classes/KRMaterialManager.h | 37 + KREngine/KREngine/Classes/KRModel.cpp | 617 +++++++ KREngine/KREngine/Classes/KRModel.h | 98 ++ KREngine/KREngine/Classes/KRModelManager.cpp | 35 + KREngine/KREngine/Classes/KRModelManager.h | 32 + KREngine/KREngine/Classes/KRScene.cpp | 95 ++ KREngine/KREngine/Classes/KRScene.h | 41 + KREngine/KREngine/Classes/KRShader.cpp | 181 +++ KREngine/KREngine/Classes/KRShader.h | 67 + KREngine/KREngine/Classes/KRShaderManager.cpp | 68 + KREngine/KREngine/Classes/KRShaderManager.h | 43 + KREngine/KREngine/Classes/KRTexture.cpp | 209 +++ KREngine/KREngine/Classes/KRTexture.h | 49 + .../KREngine/Classes/KRTextureManager.cpp | 66 + KREngine/KREngine/Classes/KRTextureManager.h | 32 + KREngine/KREngine/Classes/KRVector3.cpp | 97 ++ KREngine/KREngine/Classes/KRVector3.h | 54 + KREngine/KREngine/KREngine-Prefix.pch | 7 + KREngine/KREngine/Shaders/ObjectShader.fsh | 154 ++ KREngine/KREngine/Shaders/ObjectShader.vsh | 101 ++ KREngine/KREngine/Shaders/PostShader.fsh | 153 ++ KREngine/KREngine/Shaders/PostShader.vsh | 10 + KREngine/KREngine/Shaders/ShadowShader.fsh | 8 + KREngine/KREngine/Shaders/ShadowShader.vsh | 26 + krengine.xcworkspace/contents.xcworkspacedata | 13 + objpack/objpack.xcodeproj/project.pbxproj | 215 +++ .../contents.xcworkspacedata | 7 + objpack/objpack/KROBJPacker.cpp | 491 ++++++ objpack/objpack/KROBJPacker.h | 55 + objpack/objpack/KRVector3.cpp | 76 + objpack/objpack/KRVector3.h | 49 + objpack/objpack/main.cpp | 29 + objpack/objpack/objpack.1 | 79 + objview/Classes/KRObjViewAppDelegate.h | 21 + objview/Classes/KRObjViewAppDelegate.mm | 34 + objview/Classes/KRObjViewGLView.h | 45 + objview/Classes/KRObjViewGLView.mm | 217 +++ objview/Classes/KRObjViewViewController.h | 54 + objview/Classes/KRObjViewViewController.mm | 257 +++ objview/Classes/krengine/KREngine.h | 67 + objview/Classes/krengine/KREngine.mm | 566 +++++++ objview/Classes/krengine/KRMat4.cpp | 133 ++ objview/Classes/krengine/KRMat4.h | 69 + objview/Classes/krengine/KRMaterial.cpp | 148 ++ objview/Classes/krengine/KRMaterial.h | 46 + .../Classes/krengine/KRMaterialManager.cpp | 185 +++ objview/Classes/krengine/KRMaterialManager.h | 32 + objview/Classes/krengine/KRModel.cpp | 615 +++++++ objview/Classes/krengine/KRModel.h | 97 ++ objview/Classes/krengine/KRModelManager.cpp | 9 + objview/Classes/krengine/KRModelManager.h | 7 + objview/Classes/krengine/KRTexture.cpp | 206 +++ objview/Classes/krengine/KRTexture.h | 47 + objview/Classes/krengine/KRTextureManager.cpp | 66 + objview/Classes/krengine/KRTextureManager.h | 30 + objview/Classes/krengine/KRVector3.cpp | 76 + objview/Classes/krengine/KRVector3.h | 49 + .../Classes/krengine/Shaders/ObjectShader.fsh | 168 ++ .../Classes/krengine/Shaders/ObjectShader.vsh | 171 ++ .../Classes/krengine/Shaders/PostShader.fsh | 150 ++ .../Classes/krengine/Shaders/PostShader.vsh | 10 + .../Classes/krengine/Shaders/ShadowShader.fsh | 35 + .../Classes/krengine/Shaders/ShadowShader.vsh | 58 + objview/KRObjView-Info.plist | 38 + objview/KRObjView.xcodeproj/applemac.mode1v3 | 1442 +++++++++++++++++ objview/KRObjView.xcodeproj/project.pbxproj | 349 ++++ .../contents.xcworkspacedata | 7 + objview/KRObjView_Prefix.pch | 8 + objview/MainWindow.xib | 262 +++ objview/iPad/MainWindow-iPad.xib | 274 ++++ objview/main.m | 17 + 86 files changed, 12190 insertions(+) create mode 100644 KREngine/KREngine.xcodeproj/project.pbxproj create mode 100644 KREngine/KREngine.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 KREngine/KREngine/Classes/KRBoundingVolume.cpp create mode 100644 KREngine/KREngine/Classes/KRBoundingVolume.h create mode 100644 KREngine/KREngine/Classes/KRCamera.cpp create mode 100644 KREngine/KREngine/Classes/KRCamera.h create mode 100644 KREngine/KREngine/Classes/KREngine.h create mode 100644 KREngine/KREngine/Classes/KREngine.mm create mode 100644 KREngine/KREngine/Classes/KRInstance.cpp create mode 100644 KREngine/KREngine/Classes/KRInstance.h create mode 100644 KREngine/KREngine/Classes/KRMat4.cpp create mode 100644 KREngine/KREngine/Classes/KRMat4.h create mode 100644 KREngine/KREngine/Classes/KRMaterial.cpp create mode 100644 KREngine/KREngine/Classes/KRMaterial.h create mode 100644 KREngine/KREngine/Classes/KRMaterialManager.cpp create mode 100644 KREngine/KREngine/Classes/KRMaterialManager.h create mode 100644 KREngine/KREngine/Classes/KRModel.cpp create mode 100644 KREngine/KREngine/Classes/KRModel.h create mode 100644 KREngine/KREngine/Classes/KRModelManager.cpp create mode 100644 KREngine/KREngine/Classes/KRModelManager.h create mode 100644 KREngine/KREngine/Classes/KRScene.cpp create mode 100644 KREngine/KREngine/Classes/KRScene.h create mode 100644 KREngine/KREngine/Classes/KRShader.cpp create mode 100644 KREngine/KREngine/Classes/KRShader.h create mode 100644 KREngine/KREngine/Classes/KRShaderManager.cpp create mode 100644 KREngine/KREngine/Classes/KRShaderManager.h create mode 100644 KREngine/KREngine/Classes/KRTexture.cpp create mode 100644 KREngine/KREngine/Classes/KRTexture.h create mode 100644 KREngine/KREngine/Classes/KRTextureManager.cpp create mode 100644 KREngine/KREngine/Classes/KRTextureManager.h create mode 100644 KREngine/KREngine/Classes/KRVector3.cpp create mode 100644 KREngine/KREngine/Classes/KRVector3.h create mode 100644 KREngine/KREngine/KREngine-Prefix.pch create mode 100644 KREngine/KREngine/Shaders/ObjectShader.fsh create mode 100644 KREngine/KREngine/Shaders/ObjectShader.vsh create mode 100644 KREngine/KREngine/Shaders/PostShader.fsh create mode 100644 KREngine/KREngine/Shaders/PostShader.vsh create mode 100644 KREngine/KREngine/Shaders/ShadowShader.fsh create mode 100644 KREngine/KREngine/Shaders/ShadowShader.vsh create mode 100644 krengine.xcworkspace/contents.xcworkspacedata create mode 100644 objpack/objpack.xcodeproj/project.pbxproj create mode 100644 objpack/objpack.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 objpack/objpack/KROBJPacker.cpp create mode 100644 objpack/objpack/KROBJPacker.h create mode 100644 objpack/objpack/KRVector3.cpp create mode 100644 objpack/objpack/KRVector3.h create mode 100644 objpack/objpack/main.cpp create mode 100644 objpack/objpack/objpack.1 create mode 100644 objview/Classes/KRObjViewAppDelegate.h create mode 100644 objview/Classes/KRObjViewAppDelegate.mm create mode 100644 objview/Classes/KRObjViewGLView.h create mode 100644 objview/Classes/KRObjViewGLView.mm create mode 100644 objview/Classes/KRObjViewViewController.h create mode 100644 objview/Classes/KRObjViewViewController.mm create mode 100644 objview/Classes/krengine/KREngine.h create mode 100644 objview/Classes/krengine/KREngine.mm create mode 100644 objview/Classes/krengine/KRMat4.cpp create mode 100644 objview/Classes/krengine/KRMat4.h create mode 100644 objview/Classes/krengine/KRMaterial.cpp create mode 100644 objview/Classes/krengine/KRMaterial.h create mode 100644 objview/Classes/krengine/KRMaterialManager.cpp create mode 100644 objview/Classes/krengine/KRMaterialManager.h create mode 100644 objview/Classes/krengine/KRModel.cpp create mode 100644 objview/Classes/krengine/KRModel.h create mode 100644 objview/Classes/krengine/KRModelManager.cpp create mode 100644 objview/Classes/krengine/KRModelManager.h create mode 100644 objview/Classes/krengine/KRTexture.cpp create mode 100644 objview/Classes/krengine/KRTexture.h create mode 100644 objview/Classes/krengine/KRTextureManager.cpp create mode 100644 objview/Classes/krengine/KRTextureManager.h create mode 100644 objview/Classes/krengine/KRVector3.cpp create mode 100644 objview/Classes/krengine/KRVector3.h create mode 100644 objview/Classes/krengine/Shaders/ObjectShader.fsh create mode 100644 objview/Classes/krengine/Shaders/ObjectShader.vsh create mode 100644 objview/Classes/krengine/Shaders/PostShader.fsh create mode 100644 objview/Classes/krengine/Shaders/PostShader.vsh create mode 100644 objview/Classes/krengine/Shaders/ShadowShader.fsh create mode 100644 objview/Classes/krengine/Shaders/ShadowShader.vsh create mode 100644 objview/KRObjView-Info.plist create mode 100644 objview/KRObjView.xcodeproj/applemac.mode1v3 create mode 100644 objview/KRObjView.xcodeproj/project.pbxproj create mode 100644 objview/KRObjView.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 objview/KRObjView_Prefix.pch create mode 100644 objview/MainWindow.xib create mode 100644 objview/iPad/MainWindow-iPad.xib create mode 100644 objview/main.m diff --git a/KREngine/KREngine.xcodeproj/project.pbxproj b/KREngine/KREngine.xcodeproj/project.pbxproj new file mode 100644 index 0000000..ab28fdf --- /dev/null +++ b/KREngine/KREngine.xcodeproj/project.pbxproj @@ -0,0 +1,380 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + E414BAE21435557300A668C4 /* KRInstance.h in Headers */ = {isa = PBXBuildFile; fileRef = E414BAE11435557300A668C4 /* KRInstance.h */; }; + E414BAE51435558900A668C4 /* KRInstance.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E414BAE41435558800A668C4 /* KRInstance.cpp */; }; + E414BAE7143557D200A668C4 /* KRScene.h in Headers */ = {isa = PBXBuildFile; fileRef = E414BAE6143557D200A668C4 /* KRScene.h */; }; + E414BAE91435585A00A668C4 /* KRScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E414BAE81435585A00A668C4 /* KRScene.cpp */; }; + E414BAEB14355E5500A668C4 /* KRBoundingVolume.h in Headers */ = {isa = PBXBuildFile; fileRef = E414BAEA14355E5500A668C4 /* KRBoundingVolume.h */; }; + E414BAED14355EFF00A668C4 /* KRBoundingVolume.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E414BAEC14355EFF00A668C4 /* KRBoundingVolume.cpp */; }; + E47C25A213F4F65A00FF4370 /* KRShaderManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E47C25A113F4F65A00FF4370 /* KRShaderManager.h */; }; + E47C25A513F4F66F00FF4370 /* KRShader.h in Headers */ = {isa = PBXBuildFile; fileRef = E47C25A413F4F66F00FF4370 /* KRShader.h */; }; + E47C25A713F4F6AB00FF4370 /* KRShaderManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E47C25A613F4F6AB00FF4370 /* KRShaderManager.cpp */; }; + E47C25A913F4F6DD00FF4370 /* KRShader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E47C25A813F4F6DD00FF4370 /* KRShader.cpp */; }; + E48B3CBD14393DF5000C50E2 /* KRCamera.h in Headers */ = {isa = PBXBuildFile; fileRef = E48B3CBC14393DF5000C50E2 /* KRCamera.h */; }; + E48B3CC014393E30000C50E2 /* KRCamera.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E48B3CBF14393E2F000C50E2 /* KRCamera.cpp */; }; + E491016513C99B9E0098455B /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E491016413C99B9E0098455B /* Foundation.framework */; }; + E491018713C99BDC0098455B /* KREngine.mm in Sources */ = {isa = PBXBuildFile; fileRef = E491016F13C99BDC0098455B /* KREngine.mm */; }; + E491018A13C99BDC0098455B /* KREngine.h in Headers */ = {isa = PBXBuildFile; fileRef = E491017213C99BDC0098455B /* KREngine.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E491018E13C99BDC0098455B /* KRMat4.h in Headers */ = {isa = PBXBuildFile; fileRef = E491017613C99BDC0098455B /* KRMat4.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E491018F13C99BDC0098455B /* KRMat4.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017713C99BDC0098455B /* KRMat4.cpp */; }; + E491019113C99BDC0098455B /* KRModel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017913C99BDC0098455B /* KRModel.cpp */; }; + E491019213C99BDC0098455B /* KRModel.h in Headers */ = {isa = PBXBuildFile; fileRef = E491017A13C99BDC0098455B /* KRModel.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E491019313C99BDC0098455B /* KRMaterialManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017B13C99BDC0098455B /* KRMaterialManager.cpp */; }; + E491019413C99BDC0098455B /* KRMaterial.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017C13C99BDC0098455B /* KRMaterial.cpp */; }; + E491019513C99BDC0098455B /* KRMaterial.h in Headers */ = {isa = PBXBuildFile; fileRef = E491017D13C99BDC0098455B /* KRMaterial.h */; settings = {ATTRIBUTES = (Private, ); }; }; + E491019613C99BDC0098455B /* KRVector3.h in Headers */ = {isa = PBXBuildFile; fileRef = E491017E13C99BDC0098455B /* KRVector3.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E491019713C99BDC0098455B /* KRVector3.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491017F13C99BDC0098455B /* KRVector3.cpp */; }; + E491019813C99BDC0098455B /* KRTextureManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491018013C99BDC0098455B /* KRTextureManager.cpp */; }; + E491019913C99BDC0098455B /* KRTexture.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491018113C99BDC0098455B /* KRTexture.cpp */; }; + E491019A13C99BDC0098455B /* KRModelManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E491018213C99BDC0098455B /* KRModelManager.cpp */; }; + E491019B13C99BDC0098455B /* KRModelManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E491018313C99BDC0098455B /* KRModelManager.h */; settings = {ATTRIBUTES = (Public, ); }; }; + E491019C13C99BDC0098455B /* KRMaterialManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E491018413C99BDC0098455B /* KRMaterialManager.h */; settings = {ATTRIBUTES = (Private, ); }; }; + E491019D13C99BDC0098455B /* KRTextureManager.h in Headers */ = {isa = PBXBuildFile; fileRef = E491018513C99BDC0098455B /* KRTextureManager.h */; settings = {ATTRIBUTES = (Private, ); }; }; + E491019E13C99BDC0098455B /* KRTexture.h in Headers */ = {isa = PBXBuildFile; fileRef = E491018613C99BDC0098455B /* KRTexture.h */; settings = {ATTRIBUTES = (Private, ); }; }; + E49101A013C99BF50098455B /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E491019F13C99BF50098455B /* OpenGLES.framework */; }; +/* End PBXBuildFile section */ + +/* Begin PBXBuildRule section */ + E4AE8AB913CA2D77007E86AF /* PBXBuildRule */ = { + isa = PBXBuildRule; + compilerSpec = com.apple.compilers.proxy.script; + fileType = pattern.proxy; + isEditable = 1; + outputFiles = ( + ); + script = ""; + }; +/* End PBXBuildRule section */ + +/* Begin PBXFileReference section */ + E414BAE11435557300A668C4 /* KRInstance.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRInstance.h; path = Classes/KRInstance.h; sourceTree = ""; }; + E414BAE41435558800A668C4 /* KRInstance.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRInstance.cpp; path = Classes/KRInstance.cpp; sourceTree = ""; }; + E414BAE6143557D200A668C4 /* KRScene.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRScene.h; path = Classes/KRScene.h; sourceTree = ""; }; + E414BAE81435585A00A668C4 /* KRScene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRScene.cpp; path = Classes/KRScene.cpp; sourceTree = ""; }; + E414BAEA14355E5500A668C4 /* KRBoundingVolume.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRBoundingVolume.h; path = Classes/KRBoundingVolume.h; sourceTree = ""; }; + E414BAEC14355EFF00A668C4 /* KRBoundingVolume.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRBoundingVolume.cpp; path = Classes/KRBoundingVolume.cpp; sourceTree = ""; }; + E45772F113C9A13C0037BEEA /* ShadowShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ShadowShader.vsh; path = Shaders/ShadowShader.vsh; sourceTree = ""; }; + E45772F213C9A13C0037BEEA /* ShadowShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ShadowShader.fsh; path = Shaders/ShadowShader.fsh; sourceTree = ""; }; + E45772F313C9A13C0037BEEA /* PostShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = PostShader.fsh; path = Shaders/PostShader.fsh; sourceTree = ""; }; + E45772F413C9A13C0037BEEA /* ObjectShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ObjectShader.fsh; path = Shaders/ObjectShader.fsh; sourceTree = ""; }; + E45772F513C9A13C0037BEEA /* ObjectShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ObjectShader.vsh; path = Shaders/ObjectShader.vsh; sourceTree = ""; }; + E45772F613C9A13C0037BEEA /* PostShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = PostShader.vsh; path = Shaders/PostShader.vsh; sourceTree = ""; }; + E47C25A113F4F65A00FF4370 /* KRShaderManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRShaderManager.h; path = Classes/KRShaderManager.h; sourceTree = ""; }; + E47C25A413F4F66F00FF4370 /* KRShader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRShader.h; path = Classes/KRShader.h; sourceTree = ""; }; + E47C25A613F4F6AB00FF4370 /* KRShaderManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRShaderManager.cpp; path = Classes/KRShaderManager.cpp; sourceTree = ""; }; + E47C25A813F4F6DD00FF4370 /* KRShader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRShader.cpp; path = Classes/KRShader.cpp; sourceTree = ""; }; + E48B3CBC14393DF5000C50E2 /* KRCamera.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRCamera.h; path = Classes/KRCamera.h; sourceTree = ""; }; + E48B3CBF14393E2F000C50E2 /* KRCamera.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRCamera.cpp; path = Classes/KRCamera.cpp; sourceTree = ""; }; + E491016113C99B9E0098455B /* libKREngine.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libKREngine.a; sourceTree = BUILT_PRODUCTS_DIR; }; + E491016413C99B9E0098455B /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; + E491016813C99B9E0098455B /* KREngine-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "KREngine-Prefix.pch"; sourceTree = ""; }; + E491016F13C99BDC0098455B /* KREngine.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = KREngine.mm; path = Classes/KREngine.mm; sourceTree = ""; }; + E491017213C99BDC0098455B /* KREngine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KREngine.h; path = Classes/KREngine.h; sourceTree = ""; }; + E491017613C99BDC0098455B /* KRMat4.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRMat4.h; path = Classes/KRMat4.h; sourceTree = ""; }; + E491017713C99BDC0098455B /* KRMat4.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRMat4.cpp; path = Classes/KRMat4.cpp; sourceTree = ""; }; + E491017913C99BDC0098455B /* KRModel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRModel.cpp; path = Classes/KRModel.cpp; sourceTree = ""; }; + E491017A13C99BDC0098455B /* KRModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRModel.h; path = Classes/KRModel.h; sourceTree = ""; }; + E491017B13C99BDC0098455B /* KRMaterialManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRMaterialManager.cpp; path = Classes/KRMaterialManager.cpp; sourceTree = ""; }; + E491017C13C99BDC0098455B /* KRMaterial.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRMaterial.cpp; path = Classes/KRMaterial.cpp; sourceTree = ""; }; + E491017D13C99BDC0098455B /* KRMaterial.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRMaterial.h; path = Classes/KRMaterial.h; sourceTree = ""; }; + E491017E13C99BDC0098455B /* KRVector3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRVector3.h; path = Classes/KRVector3.h; sourceTree = ""; }; + E491017F13C99BDC0098455B /* KRVector3.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRVector3.cpp; path = Classes/KRVector3.cpp; sourceTree = ""; }; + E491018013C99BDC0098455B /* KRTextureManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRTextureManager.cpp; path = Classes/KRTextureManager.cpp; sourceTree = ""; }; + E491018113C99BDC0098455B /* KRTexture.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRTexture.cpp; path = Classes/KRTexture.cpp; sourceTree = ""; }; + E491018213C99BDC0098455B /* KRModelManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRModelManager.cpp; path = Classes/KRModelManager.cpp; sourceTree = ""; }; + E491018313C99BDC0098455B /* KRModelManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRModelManager.h; path = Classes/KRModelManager.h; sourceTree = ""; }; + E491018413C99BDC0098455B /* KRMaterialManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRMaterialManager.h; path = Classes/KRMaterialManager.h; sourceTree = ""; }; + E491018513C99BDC0098455B /* KRTextureManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRTextureManager.h; path = Classes/KRTextureManager.h; sourceTree = ""; }; + E491018613C99BDC0098455B /* KRTexture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRTexture.h; path = Classes/KRTexture.h; sourceTree = ""; }; + E491019F13C99BF50098455B /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + E491015E13C99B9E0098455B /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + E49101A013C99BF50098455B /* OpenGLES.framework in Frameworks */, + E491016513C99B9E0098455B /* Foundation.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + E45772E313C99F160037BEEA /* Shaders */ = { + isa = PBXGroup; + children = ( + E45772F113C9A13C0037BEEA /* ShadowShader.vsh */, + E45772F213C9A13C0037BEEA /* ShadowShader.fsh */, + E45772F513C9A13C0037BEEA /* ObjectShader.vsh */, + E45772F313C9A13C0037BEEA /* PostShader.fsh */, + E45772F413C9A13C0037BEEA /* ObjectShader.fsh */, + E45772F613C9A13C0037BEEA /* PostShader.vsh */, + ); + name = Shaders; + sourceTree = ""; + }; + E491015613C99B9D0098455B = { + isa = PBXGroup; + children = ( + E491016613C99B9E0098455B /* KREngine */, + E491016313C99B9E0098455B /* Frameworks */, + E491016213C99B9E0098455B /* Products */, + ); + sourceTree = ""; + }; + E491016213C99B9E0098455B /* Products */ = { + isa = PBXGroup; + children = ( + E491016113C99B9E0098455B /* libKREngine.a */, + ); + name = Products; + sourceTree = ""; + }; + E491016313C99B9E0098455B /* Frameworks */ = { + isa = PBXGroup; + children = ( + E491019F13C99BF50098455B /* OpenGLES.framework */, + E491016413C99B9E0098455B /* Foundation.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; + E491016613C99B9E0098455B /* KREngine */ = { + isa = PBXGroup; + children = ( + E45772E313C99F160037BEEA /* Shaders */, + E491016E13C99BAE0098455B /* Classes */, + E491016713C99B9E0098455B /* Supporting Files */, + ); + path = KREngine; + sourceTree = ""; + }; + E491016713C99B9E0098455B /* Supporting Files */ = { + isa = PBXGroup; + children = ( + E491016813C99B9E0098455B /* KREngine-Prefix.pch */, + ); + name = "Supporting Files"; + sourceTree = ""; + }; + E491016E13C99BAE0098455B /* Classes */ = { + isa = PBXGroup; + children = ( + E491016F13C99BDC0098455B /* KREngine.mm */, + E491017213C99BDC0098455B /* KREngine.h */, + E491017613C99BDC0098455B /* KRMat4.h */, + E491017713C99BDC0098455B /* KRMat4.cpp */, + E491017913C99BDC0098455B /* KRModel.cpp */, + E491017A13C99BDC0098455B /* KRModel.h */, + E491017B13C99BDC0098455B /* KRMaterialManager.cpp */, + E491017C13C99BDC0098455B /* KRMaterial.cpp */, + E491017D13C99BDC0098455B /* KRMaterial.h */, + E491017E13C99BDC0098455B /* KRVector3.h */, + E491017F13C99BDC0098455B /* KRVector3.cpp */, + E491018013C99BDC0098455B /* KRTextureManager.cpp */, + E491018113C99BDC0098455B /* KRTexture.cpp */, + E491018213C99BDC0098455B /* KRModelManager.cpp */, + E491018313C99BDC0098455B /* KRModelManager.h */, + E491018413C99BDC0098455B /* KRMaterialManager.h */, + E491018513C99BDC0098455B /* KRTextureManager.h */, + E491018613C99BDC0098455B /* KRTexture.h */, + E47C25A113F4F65A00FF4370 /* KRShaderManager.h */, + E47C25A413F4F66F00FF4370 /* KRShader.h */, + E47C25A613F4F6AB00FF4370 /* KRShaderManager.cpp */, + E47C25A813F4F6DD00FF4370 /* KRShader.cpp */, + E414BAE11435557300A668C4 /* KRInstance.h */, + E414BAE41435558800A668C4 /* KRInstance.cpp */, + E414BAE6143557D200A668C4 /* KRScene.h */, + E414BAE81435585A00A668C4 /* KRScene.cpp */, + E414BAEA14355E5500A668C4 /* KRBoundingVolume.h */, + E414BAEC14355EFF00A668C4 /* KRBoundingVolume.cpp */, + E48B3CBC14393DF5000C50E2 /* KRCamera.h */, + E48B3CBF14393E2F000C50E2 /* KRCamera.cpp */, + ); + name = Classes; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + E491015F13C99B9E0098455B /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + E491018A13C99BDC0098455B /* KREngine.h in Headers */, + E491018E13C99BDC0098455B /* KRMat4.h in Headers */, + E491019B13C99BDC0098455B /* KRModelManager.h in Headers */, + E491019213C99BDC0098455B /* KRModel.h in Headers */, + E491019613C99BDC0098455B /* KRVector3.h in Headers */, + E491019513C99BDC0098455B /* KRMaterial.h in Headers */, + E491019C13C99BDC0098455B /* KRMaterialManager.h in Headers */, + E491019D13C99BDC0098455B /* KRTextureManager.h in Headers */, + E491019E13C99BDC0098455B /* KRTexture.h in Headers */, + E47C25A213F4F65A00FF4370 /* KRShaderManager.h in Headers */, + E47C25A513F4F66F00FF4370 /* KRShader.h in Headers */, + E414BAE21435557300A668C4 /* KRInstance.h in Headers */, + E414BAE7143557D200A668C4 /* KRScene.h in Headers */, + E414BAEB14355E5500A668C4 /* KRBoundingVolume.h in Headers */, + E48B3CBD14393DF5000C50E2 /* KRCamera.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + E491016013C99B9E0098455B /* KREngine */ = { + isa = PBXNativeTarget; + buildConfigurationList = E491016B13C99B9E0098455B /* Build configuration list for PBXNativeTarget "KREngine" */; + buildPhases = ( + E491015D13C99B9E0098455B /* Sources */, + E491015E13C99B9E0098455B /* Frameworks */, + E491015F13C99B9E0098455B /* Headers */, + ); + buildRules = ( + E4AE8AB913CA2D77007E86AF /* PBXBuildRule */, + ); + dependencies = ( + ); + name = KREngine; + productName = KREngine; + productReference = E491016113C99B9E0098455B /* libKREngine.a */; + productType = "com.apple.product-type.library.static"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + E491015813C99B9D0098455B /* Project object */ = { + isa = PBXProject; + attributes = { + ORGANIZATIONNAME = "Kearwood Software"; + }; + buildConfigurationList = E491015B13C99B9D0098455B /* Build configuration list for PBXProject "KREngine" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = E491015613C99B9D0098455B; + productRefGroup = E491016213C99B9E0098455B /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + E491016013C99B9E0098455B /* KREngine */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + E491015D13C99B9E0098455B /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + E491018713C99BDC0098455B /* KREngine.mm in Sources */, + E491018F13C99BDC0098455B /* KRMat4.cpp in Sources */, + E491019113C99BDC0098455B /* KRModel.cpp in Sources */, + E491019313C99BDC0098455B /* KRMaterialManager.cpp in Sources */, + E491019413C99BDC0098455B /* KRMaterial.cpp in Sources */, + E491019713C99BDC0098455B /* KRVector3.cpp in Sources */, + E491019813C99BDC0098455B /* KRTextureManager.cpp in Sources */, + E491019913C99BDC0098455B /* KRTexture.cpp in Sources */, + E491019A13C99BDC0098455B /* KRModelManager.cpp in Sources */, + E47C25A713F4F6AB00FF4370 /* KRShaderManager.cpp in Sources */, + E47C25A913F4F6DD00FF4370 /* KRShader.cpp in Sources */, + E414BAE51435558900A668C4 /* KRInstance.cpp in Sources */, + E414BAE91435585A00A668C4 /* KRScene.cpp in Sources */, + E414BAED14355EFF00A668C4 /* KRBoundingVolume.cpp in Sources */, + E48B3CC014393E30000C50E2 /* KRCamera.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + E491016913C99B9E0098455B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ARCHS = "$(ARCHS_STANDARD_32_BIT)"; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = DEBUG; + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_VERSION = com.apple.compilers.llvmgcc42; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 4.3; + SDKROOT = iphoneos; + }; + name = Debug; + }; + E491016A13C99B9E0098455B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ARCHS = "$(ARCHS_STANDARD_32_BIT)"; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_VERSION = com.apple.compilers.llvmgcc42; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 4.3; + SDKROOT = iphoneos; + }; + name = Release; + }; + E491016C13C99B9E0098455B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + DSTROOT = /tmp/KREngine.dst; + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = "KREngine/KREngine-Prefix.pch"; + OTHER_LDFLAGS = "-ObjC"; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + E491016D13C99B9E0098455B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + DSTROOT = /tmp/KREngine.dst; + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = "KREngine/KREngine-Prefix.pch"; + OTHER_LDFLAGS = "-ObjC"; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + E491015B13C99B9D0098455B /* Build configuration list for PBXProject "KREngine" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + E491016913C99B9E0098455B /* Debug */, + E491016A13C99B9E0098455B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + E491016B13C99B9E0098455B /* Build configuration list for PBXNativeTarget "KREngine" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + E491016C13C99B9E0098455B /* Debug */, + E491016D13C99B9E0098455B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = E491015813C99B9D0098455B /* Project object */; +} diff --git a/KREngine/KREngine.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/KREngine/KREngine.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..3e3b0dc --- /dev/null +++ b/KREngine/KREngine.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/KREngine/KREngine/Classes/KRBoundingVolume.cpp b/KREngine/KREngine/Classes/KRBoundingVolume.cpp new file mode 100644 index 0000000..0e8d68e --- /dev/null +++ b/KREngine/KREngine/Classes/KRBoundingVolume.cpp @@ -0,0 +1,264 @@ +// +// KRBoundingVolume.cpp +// KREngine +// +// Created by Kearwood Gilbert on 11-09-29. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#include + +#import "KRBoundingVolume.h" + + +KRBoundingVolume::KRBoundingVolume(const Vector3 *pVertices) { + for(int iVertex=0; iVertex < 8; iVertex++) { + m_vertices[iVertex] = pVertices[iVertex]; + } +} + +KRBoundingVolume::KRBoundingVolume(const Vector3 &corner1, const Vector3 &corner2, const KRMat4 modelMatrix) { + m_vertices[0] = Vector3(corner1.x, corner1.y, corner1.z); + m_vertices[1] = Vector3(corner2.x, corner1.y, corner1.z); + m_vertices[2] = Vector3(corner2.x, corner2.y, corner1.z); + m_vertices[3] = Vector3(corner1.x, corner2.y, corner1.z); + m_vertices[4] = Vector3(corner1.x, corner1.y, corner2.z); + m_vertices[5] = Vector3(corner2.x, corner1.y, corner2.z); + m_vertices[6] = Vector3(corner2.x, corner2.y, corner2.z); + m_vertices[7] = Vector3(corner1.x, corner2.y, corner2.z); + + for(int iVertex=0; iVertex < 8; iVertex++) { + m_vertices[iVertex] = modelMatrix.dot(m_vertices[iVertex]); + } +} + +KRBoundingVolume::KRBoundingVolume(const KRMat4 &matView, GLfloat fov, GLfloat aspect, GLfloat nearz, GLfloat farz) { + // Construct a bounding volume representing the volume of the view frustrum + + KRMat4 invView = matView; + invView.invert(); + + GLfloat r = tan(fov / 2.0); + + m_vertices[0] = Vector3(-1.0 * r * nearz * aspect, -1.0 * r * nearz, -nearz); + m_vertices[1] = Vector3(1.0 * r * nearz * aspect, -1.0 * r * nearz, -nearz); + m_vertices[2] = Vector3(1.0 * r * nearz * aspect, 1.0 * r * nearz, -nearz); + m_vertices[3] = Vector3(-1.0 * r * nearz * aspect, 1.0 * r * nearz, -nearz); + m_vertices[4] = Vector3(-1.0 * r * farz * aspect, -1.0 * r * farz, -farz); + m_vertices[5] = Vector3(1.0 * r * farz * aspect, -1.0 * r * farz, -farz); + m_vertices[6] = Vector3(1.0 * r * farz * aspect, 1.0 * r * farz, -farz); + m_vertices[7] = Vector3(-1.0 * r * farz * aspect, 1.0 * r * farz, -farz); + + for(int iVertex=0; iVertex < 8; iVertex++) { + m_vertices[iVertex] = invView.dot(m_vertices[iVertex]); + } +} + +KRBoundingVolume::~KRBoundingVolume() { + +} + +KRBoundingVolume::KRBoundingVolume(const KRBoundingVolume& p) { + for(int iVertex=0; iVertex < 8; iVertex++) { + m_vertices[iVertex] = p.m_vertices[iVertex]; + } +} + +KRBoundingVolume& KRBoundingVolume::operator = ( const KRBoundingVolume& p ) { + for(int iVertex=0; iVertex < 8; iVertex++) { + m_vertices[iVertex] = p.m_vertices[iVertex]; + } + return *this; +} + +KRBoundingVolume KRBoundingVolume::get_union(const KRBoundingVolume &p) const { + // Simple, non-aligned bounding box calculated that contains both volumes. + + Vector3 minPoint = m_vertices[0], maxPoint = m_vertices[0]; + for(int iVertex=1; iVertex < 8; iVertex++) { + if(m_vertices[iVertex].x < minPoint.x) { + minPoint.x = m_vertices[iVertex].x; + } + if(m_vertices[iVertex].y < minPoint.y) { + minPoint.y = m_vertices[iVertex].y; + } + if(m_vertices[iVertex].z < minPoint.z) { + minPoint.z = m_vertices[iVertex].z; + } + if(m_vertices[iVertex].x > maxPoint.x) { + maxPoint.x = m_vertices[iVertex].x; + } + if(m_vertices[iVertex].y > maxPoint.y) { + maxPoint.y = m_vertices[iVertex].y; + } + if(m_vertices[iVertex].z > maxPoint.z) { + maxPoint.z = m_vertices[iVertex].z; + } + } + for(int iVertex=0; iVertex < 8; iVertex++) { + if(p.m_vertices[iVertex].x < minPoint.x) { + minPoint.x = p.m_vertices[iVertex].x; + } + if(p.m_vertices[iVertex].y < minPoint.y) { + minPoint.y =p.m_vertices[iVertex].y; + } + if(p.m_vertices[iVertex].z < minPoint.z) { + minPoint.z = p.m_vertices[iVertex].z; + } + if(p.m_vertices[iVertex].x > maxPoint.x) { + maxPoint.x = p.m_vertices[iVertex].x; + } + if(p.m_vertices[iVertex].y > maxPoint.y) { + maxPoint.y = p.m_vertices[iVertex].y; + } + if(p.m_vertices[iVertex].z > maxPoint.z) { + maxPoint.z = p.m_vertices[iVertex].z; + } + } + return KRBoundingVolume(minPoint, maxPoint, KRMat4()); +} + +bool KRBoundingVolume::test_intersect(const KRBoundingVolume &p) const { + // Simple, non-aligned bounding box intersection test + + Vector3 minPoint = m_vertices[0], maxPoint = m_vertices[0], minPoint2 = p.m_vertices[0], maxPoint2 = p.m_vertices[0]; + for(int iVertex=1; iVertex < 8; iVertex++) { + if(m_vertices[iVertex].x < minPoint.x) { + minPoint.x = m_vertices[iVertex].x; + } + if(m_vertices[iVertex].y < minPoint.y) { + minPoint.y = m_vertices[iVertex].y; + } + if(m_vertices[iVertex].z < minPoint.z) { + minPoint.z = m_vertices[iVertex].z; + } + if(m_vertices[iVertex].x > maxPoint.x) { + maxPoint.x = m_vertices[iVertex].x; + } + if(m_vertices[iVertex].y > maxPoint.y) { + maxPoint.y = m_vertices[iVertex].y; + } + if(m_vertices[iVertex].z > maxPoint.z) { + maxPoint.z = m_vertices[iVertex].z; + } + } + for(int iVertex=1; iVertex < 8; iVertex++) { + if(p.m_vertices[iVertex].x < minPoint2.x) { + minPoint2.x = p.m_vertices[iVertex].x; + } + if(p.m_vertices[iVertex].y < minPoint2.y) { + minPoint2.y =p.m_vertices[iVertex].y; + } + if(p.m_vertices[iVertex].z < minPoint2.z) { + minPoint2.z = p.m_vertices[iVertex].z; + } + if(p.m_vertices[iVertex].x > maxPoint2.x) { + maxPoint2.x = p.m_vertices[iVertex].x; + } + if(p.m_vertices[iVertex].y > maxPoint2.y) { + maxPoint2.y = p.m_vertices[iVertex].y; + } + if(p.m_vertices[iVertex].z > maxPoint2.z) { + maxPoint2.z = p.m_vertices[iVertex].z; + } + } + + bool bIntersect = maxPoint.x >= minPoint2.x && maxPoint.y >= minPoint2.y && maxPoint.z >= minPoint2.z + && minPoint.x <= maxPoint2.x && minPoint.y <= maxPoint2.y && minPoint.z <= maxPoint2.z; + + return bIntersect; +} + + +KRMat4 KRBoundingVolume::calcShadowProj(KRScene *pScene, GLfloat sun_yaw, GLfloat sun_pitch) const { + KRBoundingVolume sceneVolume = pScene->getExtents(); + + KRMat4 shadowvp; + shadowvp.rotate(sun_pitch, X_AXIS); + shadowvp.rotate(sun_yaw, Y_AXIS); + shadowvp.invert(); + shadowvp.scale(1.0, 1.0, -1.0); + + Vector3 minPointFrustrum = shadowvp.dot(m_vertices[0]), maxPointFrustrum = minPointFrustrum; + for(int iVertex=1; iVertex < 8; iVertex++) { + Vector3 v = shadowvp.dot(m_vertices[iVertex]); + if(v.x < minPointFrustrum.x) { + minPointFrustrum.x = v.x; + } + if(v.y < minPointFrustrum.y) { + minPointFrustrum.y = v.y; + } + if(v.z < minPointFrustrum.z) { + minPointFrustrum.z = v.z; + } + if(v.x > maxPointFrustrum.x) { + maxPointFrustrum.x = v.x; + } + if(v.y > maxPointFrustrum.y) { + maxPointFrustrum.y = v.y; + } + if(v.z > maxPointFrustrum.z) { + maxPointFrustrum.z = v.z; + } + } + + + Vector3 minPointScene = shadowvp.dot(sceneVolume.m_vertices[0]), maxPointScene = minPointScene; + for(int iVertex=1; iVertex < 8; iVertex++) { + Vector3 v = shadowvp.dot(sceneVolume.m_vertices[iVertex]); + if(v.x < minPointScene.x) { + minPointScene.x = v.x; + } + if(v.y < minPointScene.y) { + minPointScene.y = v.y; + } + if(v.z < minPointScene.z) { + minPointScene.z = v.z; + } + if(v.x > maxPointScene.x) { + maxPointScene.x = v.x; + } + if(v.y > maxPointScene.y) { + maxPointScene.y = v.y; + } + if(v.z > maxPointScene.z) { + maxPointScene.z = v.z; + } + } + + // Include potential shadow casters outside of view frustrum + minPointFrustrum.z = minPointScene.z; + + if(maxPointScene.z < maxPointFrustrum.z) { + maxPointFrustrum.z = maxPointScene.z; + } + + /* + // Include potential shadow casters outside of view frustrum + GLfloat maxFrustrumDepth = maxPointFrustrum.z; + + for(int i=0; i<8; i++) { + Vector3 v = shadowvp.dot(sceneVolume.m_vertices[i]); + if(i == 0) { + minPointFrustrum.z = v.z; + maxPointFrustrum.z = v.z; + } else { + if(v.z < minPointFrustrum.z) { + minPointFrustrum.z = v.z; + } + if(v.z > maxPointFrustrum.z) { + maxPointFrustrum.z = v.z; + } + } + } + if(maxPointFrustrum.z > maxFrustrumDepth) { + maxPointFrustrum.z = maxFrustrumDepth; + } + */ + + shadowvp.translate(-minPointFrustrum.x, -minPointFrustrum.y, -minPointFrustrum.z); + shadowvp.scale(2.0/(maxPointFrustrum.x - minPointFrustrum.x), 2.0/(maxPointFrustrum.y - minPointFrustrum.y), 1.0/(maxPointFrustrum.z - minPointFrustrum.z)); + shadowvp.translate(-1.0, -1.0, 0.0); + return shadowvp; + +} diff --git a/KREngine/KREngine/Classes/KRBoundingVolume.h b/KREngine/KREngine/Classes/KRBoundingVolume.h new file mode 100644 index 0000000..cfec0ab --- /dev/null +++ b/KREngine/KREngine/Classes/KRBoundingVolume.h @@ -0,0 +1,38 @@ +// +// KRBoundingVolume.h +// KREngine +// +// Created by Kearwood Gilbert on 11-09-29. +// Copyright 2011 Kearwood Software. All rights reserved. +// + + +#ifndef KRBOUNDINGVOLUME_H +#define KRBOUNDINGVOLUME_H + + +#import "KRVector3.h" +#import "KRMat4.h" +#import "KRScene.h" + +class KRScene; + +class KRBoundingVolume { +public: + KRBoundingVolume(const Vector3 *pVertices); + KRBoundingVolume(const Vector3 &corner1, const Vector3 &corner2, const KRMat4 modelMatrix); + KRBoundingVolume(const KRMat4 &matView, GLfloat fov, GLfloat aspect, GLfloat nearz, GLfloat farz); + ~KRBoundingVolume(); + + KRBoundingVolume(const KRBoundingVolume& p); + KRBoundingVolume& operator = ( const KRBoundingVolume& p ); + + KRBoundingVolume get_union(const KRBoundingVolume &p) const; + bool test_intersect(const KRBoundingVolume &p) const; + + KRMat4 calcShadowProj(KRScene *pScene, GLfloat sun_yaw, GLfloat sun_pitch) const; +private: + Vector3 m_vertices[8]; +}; + +#endif diff --git a/KREngine/KREngine/Classes/KRCamera.cpp b/KREngine/KREngine/Classes/KRCamera.cpp new file mode 100644 index 0000000..fa3e83b --- /dev/null +++ b/KREngine/KREngine/Classes/KRCamera.cpp @@ -0,0 +1,65 @@ +// +// KRSettings.cpp +// KREngine +// +// Created by Kearwood Gilbert on 11-10-02. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#include + +#import "KRCamera.h" + +KRCamera::KRCamera() { + double const PI = 3.141592653589793f; + + bShowShadowBuffer = false; + bEnablePerPixel = true; + bEnableDiffuseMap = true; + bEnableNormalMap = true; + bEnableSpecMap = true; + bDebugPSSM = false; + bEnableAmbient = true; + bEnableDiffuse = true; + bEnableSpecular = true; + bDebugSuperShiny = false; + + dAmbientR = 0.25f; + dAmbientG = 0.25f; + dAmbientB = 0.35f; + + dSunR = 1.0f; + dSunG = 1.0f; + dSunB = 0.70f; + + perspective_fov = PI / 8.0; + perspective_aspect = 1.3333; + perspective_nearz = 0.25f; + perspective_farz = 100.0f; + + dof_quality = 0; + dof_depth = 0.05f; + dof_falloff = 0.05f; + + bEnableFlash = false; + flash_intensity = 1.0f; + flash_depth = 0.7f; + flash_falloff = 0.5f; + + + bEnableVignette = false; + vignette_radius = 0.4f; + vignette_falloff = 1.0f; + +} + +KRCamera::~KRCamera() { + +} + +KRMat4 KRCamera::getProjectionMatrix() { + KRMat4 projectionMatrix; + projectionMatrix.perspective(perspective_fov, perspective_aspect, perspective_nearz, perspective_farz); + projectionMatrix.rotate(-90 * 0.0174532925199, Z_AXIS); + return projectionMatrix; +} \ No newline at end of file diff --git a/KREngine/KREngine/Classes/KRCamera.h b/KREngine/KREngine/Classes/KRCamera.h new file mode 100644 index 0000000..317d030 --- /dev/null +++ b/KREngine/KREngine/Classes/KRCamera.h @@ -0,0 +1,55 @@ +// +// KRSettings.h +// KREngine +// +// Created by Kearwood Gilbert on 11-10-02. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#ifndef KRCAMERA_H +#define KRCAMERA_H + +#import "KRMat4.h" + +class KRCamera { +public: + KRCamera(); + ~KRCamera(); + + KRMat4 getProjectionMatrix(); + + bool bEnablePerPixel; + bool bEnableDiffuseMap; + bool bEnableNormalMap; + bool bEnableSpecMap; + bool bDebugPSSM; + bool bDebugSuperShiny; + bool bShowShadowBuffer; + bool bEnableAmbient; + bool bEnableDiffuse; + bool bEnableSpecular; + double dSunR; + double dSunG; + double dSunB; + double dAmbientR; + double dAmbientG; + double dAmbientB; + double perspective_fov; + double perspective_nearz; + double perspective_farz; + double perspective_aspect; + + int dof_quality; + double dof_depth; + double dof_falloff; + bool bEnableFlash; + double flash_intensity; + double flash_depth; + double flash_falloff; + + bool bEnableVignette; + double vignette_radius; + double vignette_falloff; +}; + +#endif diff --git a/KREngine/KREngine/Classes/KREngine.h b/KREngine/KREngine/Classes/KREngine.h new file mode 100644 index 0000000..44ab770 --- /dev/null +++ b/KREngine/KREngine/Classes/KREngine.h @@ -0,0 +1,107 @@ +// +// KREngine.h +// gldemo +// +// Created by Kearwood Gilbert on 10-09-16. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import +#import + +// #import "KRTextureManager.h" +#import +#import "KRMat4.h" +#import "KRVector3.h" +#import "KRModel.h" +#import "KRScene.h" +#import "KRTextureManager.h" +#import "KRMaterialManager.h" +#import "KRShaderManager.h" +#import "KRModelManager.h" +#import "KRCamera.h" + +typedef enum KREngineParameterType {KRENGINE_PARAMETER_INT, KRENGINE_PARAMETER_FLOAT, KRENGINE_PARAMETER_BOOL} KREngineParameterType; + +#define KRENGINE_MAX_SHADOW_BUFFERS 3 +#define KRENGINE_SHADOW_MAP_WIDTH 2048 +#define KRENGINE_SHADOW_MAP_HEIGHT 2048 + +@interface KREngine : NSObject +{ +@private + + GLint backingWidth, backingHeight; + + GLuint compositeFramebuffer, compositeDepthTexture, compositeColorTexture; + + int m_cShadowBuffers; + GLuint shadowFramebuffer[KRENGINE_MAX_SHADOW_BUFFERS], shadowDepthTexture[KRENGINE_MAX_SHADOW_BUFFERS]; + bool shadowValid[KRENGINE_MAX_SHADOW_BUFFERS]; + KRMat4 shadowmvpmatrix[KRENGINE_MAX_SHADOW_BUFFERS]; /* MVP Matrix for view from light source */ + + // uniform index + enum { + KRENGINE_UNIFORM_MATERIAL_AMBIENT, + KRENGINE_UNIFORM_MATERIAL_DIFFUSE, + KRENGINE_UNIFORM_MATERIAL_SPECULAR, + KRENGINE_UNIFORM_MVP, + KRENGINE_UNIFORM_SHADOWMVP1, + KRENGINE_UNIFORM_SHADOWMVP2, + KRENGINE_UNIFORM_SHADOWMVP3, + KRENGINE_UNIFORM_LIGHTDIRECTION, + KRENGINE_UNIFORM_CAMERAPOS, + KRENGINE_NUM_UNIFORMS + }; + GLint m_shadowUniforms[KRENGINE_NUM_UNIFORMS]; + + GLuint m_postShaderProgram; + GLuint m_shadowShaderProgram; + + KRTextureManager *m_pTextureManager; + KRMaterialManager *m_pMaterialManager; + KRShaderManager *m_pShaderManager; + KRModelManager *m_pModelManager; + + int m_iFrame; + + double sun_pitch, sun_yaw; + + KRCamera m_camera; + NSString *debug_text; + +} +- (id)initForWidth: (GLuint)width Height: (GLuint)height; + +- (BOOL)loadVertexShader:(NSString *)vertexShaderName fragmentShader:(NSString *)fragmentShaderName forProgram:(GLuint *)programPointer withOptions:(NSString *)options; +- (BOOL)loadResource:(NSString *)path; + +- (void)renderShadowBufferNumber: (int)iShadow ForScene: (KRScene *)pScene; +- (void)renderScene: (KRScene *)pScene WithViewMatrix: (KRMat4)viewMatrix LightDirection: (Vector3)lightDirection CameraPosition: (Vector3)cameraPosition; +- (KRModelManager *)getModelManager; +- (void)invalidateShadowBuffers; +- (void)allocateShadowBuffers; + +- (void)invalidatePostShader; +- (void)bindPostShader; + +// Parameter enumeration interface +-(int)getParameterCount; +-(NSString *)getParameterNameWithIndex: (int)i; +-(NSString *)getParameterLabelWithIndex: (int)i; +-(KREngineParameterType)getParameterTypeWithIndex: (int)i; +-(double)getParameterMinWithIndex: (int)i; +-(double)getParameterMaxWithIndex: (int)i; +-(double)getParameterValueWithIndex: (int)i; +-(void)setParameterValueWithIndex: (int)i Value: (double)v; +-(void)setParameterValueWithName: (NSString *)name Value: (double)v; + +- (void)renderScene: (KRScene *)pScene WithViewMatrix: (KRMat4)viewMatrix; +- (void)renderScene: (KRScene *)pScene WithPosition: (Vector3)position Yaw: (GLfloat)yaw Pitch: (GLfloat)pitch Roll: (GLfloat)roll; +- (void)setNearZ: (double)dNearZ; +- (void)setFarZ: (double)dFarZ; +- (void)setAspect: (double)dAspect; +- (void)setDebugText: (NSString *)text; + +@end + diff --git a/KREngine/KREngine/Classes/KREngine.mm b/KREngine/KREngine/Classes/KREngine.mm new file mode 100644 index 0000000..92b04cd --- /dev/null +++ b/KREngine/KREngine/Classes/KREngine.mm @@ -0,0 +1,1099 @@ +// +// KREngine.mm +// gldemo +// +// Created by Kearwood Gilbert on 10-09-16. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import "KREngine.h" +#import "KRVector3.h" +#import "KRScene.h" + +#import +#import + +using namespace std; + + + +@interface KREngine (PrivateMethods) +//- (BOOL)loadObjects; +- (BOOL)loadShaders; +- (BOOL)createBuffers; +- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file; +- (BOOL)linkProgram:(GLuint)prog; +- (BOOL)validateProgram:(GLuint)prog; +- (void)renderPost; +- (BOOL)loadResource:(NSString *)path; +@end + +@implementation KREngine +double const PI = 3.141592653589793f; + +- (id)initForWidth: (GLuint)width Height: (GLuint)height +{ + debug_text = @""; + sun_yaw = 4.333; + sun_pitch = 0.55; + m_iFrame = 0; + m_cShadowBuffers = 0; + backingWidth = width; + backingHeight = height; + memset(shadowFramebuffer, sizeof(GLuint) * 3, 0); + memset(shadowDepthTexture, sizeof(GLuint) * 3, 0); + + + m_postShaderProgram = 0; + + + + + if ((self = [super init])) { + NSString *vertShaderPathname = [[NSBundle mainBundle] pathForResource:@"ObjectShader" ofType:@"vsh"]; + NSString *fragShaderPathname = [[NSBundle mainBundle] pathForResource:@"ObjectShader" ofType:@"fsh"]; + GLchar * szVertShaderSource = (GLchar *)[[NSString stringWithContentsOfFile:vertShaderPathname encoding:NSUTF8StringEncoding error:nil] UTF8String]; + GLchar * szFragShaderSource = (GLchar *)[[NSString stringWithContentsOfFile:fragShaderPathname encoding:NSUTF8StringEncoding error:nil] UTF8String]; + + m_pShaderManager = new KRShaderManager(szVertShaderSource, szFragShaderSource); + m_pTextureManager = new KRTextureManager(); + m_pMaterialManager = new KRMaterialManager(m_pTextureManager, m_pShaderManager); + m_pModelManager = new KRModelManager(m_pMaterialManager); + + if (![self createBuffers] || ![self loadShaders] /*|| ![self loadObjects]*/ ) + { + [self release]; + return nil; + } + } + + return self; +} + +- (void)allocateShadowBuffers +{ + // First deallocate buffers no longer needed + for(int iShadow = m_cShadowBuffers; iShadow < KRENGINE_MAX_SHADOW_BUFFERS; iShadow++) { + if (shadowDepthTexture[iShadow]) { + glDeleteTextures(1, shadowDepthTexture + iShadow); + shadowDepthTexture[iShadow] = 0; + } + + if (shadowFramebuffer[iShadow]) { + glDeleteFramebuffers(1, shadowFramebuffer + iShadow); + shadowFramebuffer[iShadow] = 0; + } + } + + // Allocate newly required buffers + for(int iShadow = 0; iShadow < m_cShadowBuffers; iShadow++) { + if(!shadowDepthTexture[iShadow]) { + shadowValid[iShadow] = false; + + glGenFramebuffers(1, shadowFramebuffer + iShadow); + glGenTextures(1, shadowDepthTexture + iShadow); + // ===== Create offscreen shadow framebuffer object ===== + + glBindFramebuffer(GL_FRAMEBUFFER, shadowFramebuffer[iShadow]); + + // ----- Create Depth Texture for shadowFramebuffer ----- + glBindTexture(GL_TEXTURE_2D, shadowDepthTexture[iShadow]); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, KRENGINE_SHADOW_MAP_WIDTH, KRENGINE_SHADOW_MAP_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); + + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowDepthTexture[iShadow], 0); + } + } +} + +- (BOOL)createBuffers +{ + // ===== Create offscreen compositing framebuffer object ===== + glGenFramebuffers(1, &compositeFramebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, compositeFramebuffer); + + // ----- Create texture color buffer for compositeFramebuffer ----- + glGenTextures(1, &compositeColorTexture); + glBindTexture(GL_TEXTURE_2D, compositeColorTexture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // This is necessary for non-power-of-two textures + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // This is necessary for non-power-of-two textures + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, backingWidth, backingHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, compositeColorTexture, 0); + + // ----- Create Depth Texture for compositeFramebuffer ----- + glGenTextures(1, &compositeDepthTexture); + glBindTexture(GL_TEXTURE_2D, compositeDepthTexture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // This is necessary for non-power-of-two textures + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // This is necessary for non-power-of-two textures + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, backingWidth, backingHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, compositeDepthTexture, 0); + + [self allocateShadowBuffers]; + return TRUE; +} + +- (void)destroyBuffers +{ + m_cShadowBuffers = 0; + [self allocateShadowBuffers]; + + if (compositeDepthTexture) { + glDeleteTextures(1, &compositeDepthTexture); + compositeDepthTexture = 0; + } + + if (compositeColorTexture) { + glDeleteTextures(1, &compositeColorTexture); + compositeColorTexture = 0; + } + + if (compositeFramebuffer) { + glDeleteFramebuffers(1, &compositeFramebuffer); + compositeFramebuffer = 0; + } +} + +- (void)renderScene: (KRScene *)pScene WithPosition: (Vector3)position Yaw: (GLfloat)yaw Pitch: (GLfloat)pitch Roll: (GLfloat)roll +{ + KRMat4 viewMatrix; + viewMatrix.translate(-position.x, -position.y, -position.z); + viewMatrix.rotate(yaw, Y_AXIS); + viewMatrix.rotate(pitch, X_AXIS); + viewMatrix.rotate(roll, Z_AXIS); + [self renderScene: pScene WithViewMatrix: viewMatrix]; +} + +- (void)renderScene: (KRScene *)pScene WithViewMatrix: (KRMat4)viewMatrix +{ + KRMat4 invViewMatrix = viewMatrix; + invViewMatrix.invert(); + + Vector3 cameraPosition = invViewMatrix.dot(Vector3(0.0,0.0,0.0)); + + Vector3 lightDirection(0.0, 0.0, 1.0); + + // ----- Render Model ----- + KRMat4 shadowvp; + shadowvp.rotate(sun_pitch, X_AXIS); + shadowvp.rotate(sun_yaw, Y_AXIS); + lightDirection = shadowvp.dot(lightDirection); + shadowvp.invert(); + + + lightDirection.normalize(); + + [self allocateShadowBuffers]; + for(int iShadow=0; iShadow < m_cShadowBuffers; iShadow++) { + if(!shadowValid[iShadow] || true) { + shadowValid[iShadow] = true; + + GLfloat shadowMinDepths[3][3] = {{0.0, 0.0, 0.0},{0.0, 0.0, 0.0},{0.0, 0.05, 0.3}}; + GLfloat shadowMaxDepths[3][3] = {{0.0, 0.0, 1.0},{0.1, 0.0, 0.0},{0.1, 0.3, 1.0}}; + + + if(shadowMaxDepths[m_cShadowBuffers - 1][iShadow] == 0.0) { + shadowmvpmatrix[iShadow] = pScene->getExtents().calcShadowProj(pScene, sun_yaw, sun_pitch); + } else { + KRBoundingVolume frustrumSliceVolume = KRBoundingVolume(viewMatrix, m_camera.perspective_fov, m_camera.perspective_aspect, m_camera.perspective_nearz + (m_camera.perspective_farz - m_camera.perspective_nearz) * shadowMinDepths[m_cShadowBuffers - 1][iShadow], m_camera.perspective_nearz + (m_camera.perspective_farz - m_camera.perspective_nearz) * shadowMaxDepths[m_cShadowBuffers - 1][iShadow]); + shadowmvpmatrix[iShadow] = frustrumSliceVolume.calcShadowProj(pScene, sun_yaw, sun_pitch); + } + + [self renderShadowBufferNumber: iShadow ForScene: pScene]; + } + } + + /* + NSLog(@"LightDirection: (%f, %f, %f)", lightDirection.x, lightDirection.y, lightDirection.z); + NSLog(@"CameraPos: (%f, %f, %f)", cameraPos.x, cameraPos.y, cameraPos.z); + NSLog(@"LightPosObject: (%f, %f, %f)", lightPosObject.x, lightPosObject.y, lightPosObject.z); + NSLog(@"CameraPosObject: (%f, %f, %f)", cameraPosObject.x, cameraPosObject.y, cameraPosObject.z); + */ + + [self renderScene: pScene WithViewMatrix: viewMatrix LightDirection: lightDirection CameraPosition: cameraPosition]; + + [self renderPost]; + + m_iFrame++; +} + +- (void)renderShadowBufferNumber: (int)iShadow ForScene: (KRScene *)pScene; +{ + glViewport(0, 0, KRENGINE_SHADOW_MAP_WIDTH-1, KRENGINE_SHADOW_MAP_HEIGHT-1); + + glBindFramebuffer(GL_FRAMEBUFFER, shadowFramebuffer[iShadow]); + glClearDepthf(1.0f); + glClear(GL_DEPTH_BUFFER_BIT); + + //glViewport(1, 1, 2046, 2046); + + glDisable(GL_DITHER); + + glCullFace(GL_BACK); // Enable frontface culling, which eliminates some self-cast shadow artifacts + glEnable(GL_CULL_FACE); + + // Enable z-buffer test + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + glDepthRangef(0.0, 1.0); + + // Disable alpha blending as we are using alpha channel for packed depth info + glDisable(GL_BLEND); + + // Use shader program + glUseProgram(m_shadowShaderProgram); + + // Sets the diffuseTexture variable to the first texture unit + /* + glUniform1i(glGetUniformLocation(m_shadowShaderProgram, "diffuseTexture"), 0); + */ + + // Validate program before drawing. This is a good check, but only really necessary in a debug build. + // DEBUG macro must be defined in your debug configurations if that's not already the case. +#if defined(DEBUG) + if (![self validateProgram:m_shadowShaderProgram]) + { + NSLog(@"Failed to validate program: %d", m_shadowShaderProgram); + return; + } +#endif + + + + // Bind our modelmatrix variable to be a uniform called mvpmatrix in our shaderprogram + glUniformMatrix4fv(m_shadowUniforms[KRENGINE_UNIFORM_SHADOWMVP1], 1, GL_FALSE, shadowmvpmatrix[iShadow].getPointer()); + + + // Calculate the bounding volume of the shadow map + KRMat4 matInvShadow = shadowmvpmatrix[iShadow]; + matInvShadow.invert(); + + Vector3 vertices[8]; + vertices[0] = Vector3(-1.0, -1.0, 0.0); + vertices[1] = Vector3(1.0, -1.0, 0.0); + vertices[2] = Vector3(1.0, 1.0, 0.0); + vertices[3] = Vector3(-1.0, 1.0, 0.0); + vertices[4] = Vector3(-1.0, -1.0, 1.0); + vertices[5] = Vector3(1.0, -1.0, 1.0); + vertices[6] = Vector3(1.0, 1.0, 1.0); + vertices[7] = Vector3(-1.0, 1.0, 1.0); + + for(int iVertex=0; iVertex < 8; iVertex++) { + vertices[iVertex] = matInvShadow.dot(vertices[iVertex]); + } + + Vector3 cameraPosition; + Vector3 lightDirection; + KRBoundingVolume shadowVolume = KRBoundingVolume(vertices); + pScene->render(&m_camera, shadowVolume, m_pMaterialManager, true, shadowmvpmatrix[iShadow], cameraPosition, lightDirection, shadowmvpmatrix, NULL, 0); + + glViewport(0, 0, 768, 1024); +} + +- (void)renderScene: (KRScene *)pScene WithViewMatrix: (KRMat4)viewMatrix LightDirection: (Vector3)lightDirection CameraPosition: (Vector3)cameraPosition +{ + + glBindFramebuffer(GL_FRAMEBUFFER, compositeFramebuffer); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // Enable backface culling + glCullFace(GL_BACK); + glEnable(GL_CULL_FACE); + + // Enable z-buffer test + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glDepthRangef(0.0, 1.0); + + // Enable alpha blending + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + KRBoundingVolume frustrumVolume = KRBoundingVolume(viewMatrix, m_camera.perspective_fov, m_camera.perspective_aspect, m_camera.perspective_nearz, m_camera.perspective_farz); + pScene -> render(&m_camera, frustrumVolume, m_pMaterialManager, false, viewMatrix, cameraPosition, lightDirection, shadowmvpmatrix, shadowDepthTexture, m_cShadowBuffers); +} + +- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file withOptions: (NSString *)options +{ + GLint status; + const GLchar *source[2]; + + source[0] = (GLchar *)[[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil] UTF8String]; + if (!source[0]) + { + NSLog(@"Failed to load vertex shader"); + return FALSE; + } + if(options) { + source[1] = source[0]; + source[0] = [options UTF8String]; + } + + + *shader = glCreateShader(type); + glShaderSource(*shader, options ? 2 : 1, source, NULL); + glCompileShader(*shader); + +#if defined(DEBUG) + GLint logLength; + glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &logLength); + if (logLength > 0) + { + GLchar *log = (GLchar *)malloc(logLength); + glGetShaderInfoLog(*shader, logLength, &logLength, log); + NSLog(@"Shader compile log:\n%s", log); + free(log); + } +#endif + + glGetShaderiv(*shader, GL_COMPILE_STATUS, &status); + if (status == 0) { + glDeleteShader(*shader); + return FALSE; + } + + return TRUE; +} + +- (BOOL)linkProgram:(GLuint)prog +{ + GLint status; + + glLinkProgram(prog); + +#if defined(DEBUG) + GLint logLength; + glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength); + if (logLength > 0) + { + GLchar *log = (GLchar *)malloc(logLength); + glGetProgramInfoLog(prog, logLength, &logLength, log); + NSLog(@"Program link log:\n%s", log); + free(log); + } +#endif + + glGetProgramiv(prog, GL_LINK_STATUS, &status); + if (status == 0) + return FALSE; + + return TRUE; +} + +- (BOOL)validateProgram:(GLuint)prog +{ + GLint logLength, status; + + glValidateProgram(prog); + glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength); + if (logLength > 0) + { + GLchar *log = (GLchar *)malloc(logLength); + glGetProgramInfoLog(prog, logLength, &logLength, log); + NSLog(@"Program validate log:\n%s", log); + free(log); + } + + glGetProgramiv(prog, GL_VALIDATE_STATUS, &status); + if (status == 0) + return FALSE; + + return TRUE; +} + + +- (BOOL)loadVertexShader:(NSString *)vertexShaderName fragmentShader:(NSString *)fragmentShaderName forProgram:(GLuint *)programPointer withOptions:(NSString *)options; +{ + GLuint vertexShader, fragShader; + + NSString *vertShaderPathname, *fragShaderPathname; + + // Create shader program. + *programPointer = glCreateProgram(); + + // Create and compile vertex shader. + + vertShaderPathname = [[NSBundle mainBundle] pathForResource:vertexShaderName ofType:@"vsh"]; + if (![self compileShader:&vertexShader type:GL_VERTEX_SHADER file:vertShaderPathname withOptions: options]) + { + NSLog(@"Failed to compile vertex shader"); + return FALSE; + } + + // Create and compile fragment shader. + fragShaderPathname = [[NSBundle mainBundle] pathForResource:fragmentShaderName ofType:@"fsh"]; + if (![self compileShader:&fragShader type:GL_FRAGMENT_SHADER file:fragShaderPathname withOptions: options]) + { + NSLog(@"Failed to compile fragment shader"); + return FALSE; + } + + // Attach vertex shader to program. + glAttachShader(*programPointer, vertexShader); + + // Attach fragment shader to program. + glAttachShader(*programPointer, fragShader); + + // Bind attribute locations. + // This needs to be done prior to linking. + glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_VERTEX, "position"); + glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_TEXUV, "inputTextureCoordinate"); + glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_VERTEX, "myVertex"); + glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_NORMAL, "myNormal"); + glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_TANGENT, "myTangent"); + glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_TEXUV, "myUV"); + + // Link program. + if (![self linkProgram:*programPointer]) + { + NSLog(@"Failed to link program: %d", *programPointer); + + if (vertexShader) + { + glDeleteShader(vertexShader); + vertexShader = 0; + } + if (fragShader) + { + glDeleteShader(fragShader); + fragShader = 0; + } + if (*programPointer) + { + glDeleteProgram(*programPointer); + *programPointer = 0; + } + + return FALSE; + } + + // Release vertex and fragment shaders. + if (vertexShader) + { + glDeleteShader(vertexShader); + } + if (fragShader) + { + glDeleteShader(fragShader); + } + + return TRUE; +} + +- (BOOL)loadShaders +{ + [self loadVertexShader:@"ShadowShader" fragmentShader:@"ShadowShader" forProgram:&m_shadowShaderProgram withOptions: NULL]; + + m_shadowUniforms[KRENGINE_UNIFORM_SHADOWMVP1] = glGetUniformLocation(m_shadowShaderProgram, "myShadowMVPMatrix1"); + + + return TRUE; +} + +- (BOOL)loadResource:(NSString *)path +{ + NSString *name = [[path lastPathComponent] stringByDeletingPathExtension]; + if([path hasSuffix: @".krobject"]) { + NSLog(@"object: %@", path); + m_pModelManager->loadModel([name UTF8String], [path UTF8String]); + } else if([path hasSuffix: @".pvr"]) { + NSLog(@"texture: %@", path); + m_pTextureManager->loadTexture([name UTF8String], [path UTF8String]); + } else if([path hasSuffix: @".mtl"]) { + NSLog(@"material: %@", path); + m_pMaterialManager->loadFile([path UTF8String]); + } + + return TRUE; +} + +- (void)dealloc +{ + if(m_pModelManager) { + delete m_pModelManager; + m_pModelManager = NULL; + } + + if(m_pTextureManager) { + delete m_pTextureManager; + m_pTextureManager = NULL; + } + + if(m_pMaterialManager) { + delete m_pMaterialManager; + m_pMaterialManager = NULL; + } + + if(m_pShaderManager) { + delete m_pShaderManager; + m_pShaderManager = NULL; + } + + + + [self invalidatePostShader]; + [self destroyBuffers]; + [super dealloc]; +} + +- (void)invalidatePostShader +{ + if(m_postShaderProgram) { + glDeleteProgram(m_postShaderProgram); + m_postShaderProgram = 0; + } +} + +- (void)bindPostShader +{ + if(!m_postShaderProgram) { + stringstream stream; + stream.precision(std::numeric_limits::digits10); + + stream << "#define DOF_QUALITY " << m_camera.dof_quality; + stream << "\n#define ENABLE_FLASH " << (m_camera.bEnableFlash ? "1" : "0"); + stream << "\n#define ENABLE_VIGNETTE " << (m_camera.bEnableVignette ? "1" : "0"); + stream.setf(ios::fixed,ios::floatfield); + stream << "\n#define DOF_DEPTH " << m_camera.dof_depth; + stream << "\n#define DOF_FALLOFF " << m_camera.dof_falloff; + stream << "\n#define FLASH_DEPTH " << m_camera.flash_depth; + stream << "\n#define FLASH_FALLOFF " << m_camera.flash_falloff; + stream << "\n#define FLASH_INTENSITY " << m_camera.flash_intensity; + stream << "\n#define VIGNETTE_RADIUS " << m_camera.vignette_radius; + stream << "\n#define VIGNETTE_FALLOFF " << m_camera.vignette_falloff; + + stream << "\n"; + NSString *options = [NSString stringWithUTF8String: stream.str().c_str()]; + + [self loadVertexShader:@"PostShader" fragmentShader:@"PostShader" forProgram:&m_postShaderProgram withOptions: options]; + } + glUseProgram(m_postShaderProgram); +} + +- (void)renderPost +{ + + glBindFramebuffer(GL_FRAMEBUFFER, 1); // renderFramebuffer + + // Replace the implementation of this method to do your own custom drawing. + static const GLfloat squareVertices[] = { + -1.0f, -1.0f, + 1.0f, -1.0f, + -1.0f, 1.0f, + 1.0f, 1.0f, + }; + + static const GLfloat squareVerticesShadow[3][8] = {{ + -1.0f, -1.0f, + -0.60f, -1.0f, + -1.0f, -0.60f, + -0.60f, -0.60f, + },{ + -0.50f, -1.0f, + -0.10f, -1.0f, + -0.50f, -0.60f, + -0.10f, -0.60f, + },{ + 0.00f, -1.0f, + 0.40f, -1.0f, + 0.00f, -0.60f, + 0.40f, -0.60f, + }}; + + static const GLfloat textureVertices[] = { + 0.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 1.0f, + 1.0f, 1.0f, + }; + + glDisable(GL_DEPTH_TEST); + [self bindPostShader]; + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, compositeDepthTexture); + glUniform1i(glGetUniformLocation(m_postShaderProgram, "depthFrame"), 0); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, compositeColorTexture); + + glUniform1i(glGetUniformLocation(m_postShaderProgram, "renderFrame"), 1); + + // Update attribute values. + glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices); + glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_VERTEX); + glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TEXUV, 2, GL_FLOAT, 0, 0, textureVertices); + glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_TEXUV); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, 0); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, 0); + + + if(m_camera.bShowShadowBuffer) { + glDisable(GL_DEPTH_TEST); + glUseProgram(m_postShaderProgram); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, compositeDepthTexture); + glUniform1i(glGetUniformLocation(m_postShaderProgram, "depthFrame"), 0); + + + glUniform1i(glGetUniformLocation(m_postShaderProgram, "renderFrame"), 1); + + // Update attribute values. + + glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TEXUV, 2, GL_FLOAT, 0, 0, textureVertices); + glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_TEXUV); + + for(int iShadow=0; iShadow < m_cShadowBuffers; iShadow++) { + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, shadowDepthTexture[iShadow]); + glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVerticesShadow[iShadow]); + glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_VERTEX); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + } + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, 0); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, 0); + } + + + + const char *szText = [debug_text UTF8String]; + if(*szText) { + KRTexture *pFontTexture = m_pTextureManager->getTexture("font"); + + glDisable(GL_DEPTH_TEST); + glUseProgram(m_postShaderProgram); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, compositeDepthTexture); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, pFontTexture->getName()); + + glUniform1i(glGetUniformLocation(m_postShaderProgram, "depthFrame"), 0); + glUniform1i(glGetUniformLocation(m_postShaderProgram, "renderFrame"), 1); + + const char *pChar = szText; + int iPos=0; + double dScale = 1.0 / 24.0; + double dTexScale = 1.0 / 16.0; + while(*pChar) { + int iChar = *pChar++ - '\0'; + int iCol = iChar % 16; + int iRow = 15 - (iChar - iCol) / 16; + + GLfloat charVertices[] = { + -1.0f, dScale * iPos - 1.0, + -1.0 + dScale, dScale * iPos - 1.0, + -1.0f, dScale * iPos + dScale - 1.0, + -1.0 + dScale, dScale * iPos + dScale - 1.0, + }; + + /* + GLfloat charTexCoords[] = { + dTexScale * iCol, dTexScale * iRow, + dTexScale * iCol + dTexScale, dTexScale * iRow, + dTexScale * iCol, dTexScale * iRow + dTexScale, + dTexScale * iCol + dTexScale, dTexScale * iRow + dTexScale + }; + */ + + GLfloat charTexCoords[] = { + dTexScale * iCol, dTexScale * iRow + dTexScale, + dTexScale * iCol, dTexScale * iRow, + dTexScale * iCol + dTexScale, dTexScale * iRow + dTexScale, + dTexScale * iCol + dTexScale, dTexScale * iRow + }; + + glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TEXUV, 2, GL_FLOAT, 0, 0, charTexCoords); + glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_TEXUV); + + glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, charVertices); + glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_VERTEX); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + iPos++; + } + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, 0); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, 0); + } + +} + +-(int)getParameterCount +{ + return 30; +} + +-(NSString *)getParameterNameWithIndex: (int)i +{ + NSString *parameter_names[30] = { + @"camera_fov", + @"sun_direction", + @"sun_attitude", + @"shadow_quality", + @"enable_per_pixel", + @"enable_diffuse_map", + @"enable_normal_map", + @"enable_spec_map", + @"ambient_r", + @"ambient_g", + @"ambient_b", + @"sun_r", + @"sun_g", + @"sun_b", + @"dof_quality", + @"dof_depth", + @"dof_falloff", + @"flash_enable", + @"flash_intensity", + @"flash_depth", + @"flash_falloff", + @"vignette_enable", + @"vignette_radius", + @"vignette_falloff", + @"debug_shadowmap", + @"debug_pssm", + @"debug_enable_ambient", + @"debug_enable_diffuse", + @"debug_enable_specular", + @"debug_super_shiny" + }; + return parameter_names[i]; +} +-(NSString *)getParameterLabelWithIndex: (int)i +{ + NSString *parameter_labels[30] = { + @"Camera FOV", + @"Sun Direction", + @"Sun Attitude", + @"Shadow Quality (0 - 2)", + @"Enable per-pixel lighting", + @"Enable diffuse map", + @"Enable normal map", + @"Enable specular map", + @"Ambient light red intensity", + @"Ambient light green intensity", + @"Ambient light blue intensity", + @"Sun red intensity", + @"Sun green intensity", + @"Sun blue intensity", + @"DOF Quality", + @"DOF Depth", + @"DOF Falloff", + @"Enable Night/Flash Effect", + @"Flash Intensity", + @"Flash Depth", + @"Flash Falloff", + @"Enable Vignette", + @"Vignette Radius", + @"Vignette Falloff", + @"Debug - View Shadow Map", + @"Debug - PSSM", + @"Debug - Enable Ambient", + @"Debug - Enable Diffuse", + @"Debug - Enable Specular", + @"Debug - Super Shiny" + }; + return parameter_labels[i]; +} +-(KREngineParameterType)getParameterTypeWithIndex: (int)i +{ + KREngineParameterType types[30] = { + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_INT, + KRENGINE_PARAMETER_BOOL, + KRENGINE_PARAMETER_BOOL, + KRENGINE_PARAMETER_BOOL, + KRENGINE_PARAMETER_BOOL, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_INT, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_BOOL, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_BOOL, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_FLOAT, + KRENGINE_PARAMETER_BOOL, + KRENGINE_PARAMETER_BOOL, + KRENGINE_PARAMETER_BOOL, + KRENGINE_PARAMETER_BOOL, + KRENGINE_PARAMETER_BOOL, + KRENGINE_PARAMETER_BOOL + }; + return types[i]; +} +-(double)getParameterValueWithIndex: (int)i +{ + double values[30] = { + m_camera.perspective_fov, + sun_yaw, + sun_pitch, + (double)m_cShadowBuffers, + m_camera.bEnablePerPixel ? 1.0f : 0.0f, + m_camera.bEnableDiffuseMap ? 1.0f : 0.0f, + m_camera.bEnableNormalMap ? 1.0f : 0.0f, + m_camera.bEnableSpecMap ? 1.0f : 0.0f, + m_camera.dAmbientR, + m_camera.dAmbientG, + m_camera.dAmbientB, + m_camera.dSunR, + m_camera.dSunG, + m_camera.dSunB, + m_camera.dof_quality, + m_camera.dof_depth, + m_camera.dof_falloff, + m_camera.bEnableFlash ? 1.0f : 0.0f, + m_camera.flash_intensity, + m_camera.flash_depth, + m_camera.flash_falloff, + m_camera.bEnableVignette ? 1.0f : 0.0f, + m_camera.vignette_radius, + m_camera.vignette_falloff, + m_camera.bShowShadowBuffer ? 1.0f : 0.0f, + m_camera.bDebugPSSM ? 1.0f : 0.0f, + m_camera.bEnableAmbient ? 1.0f : 0.0f, + m_camera.bEnableDiffuse ? 1.0f : 0.0f, + m_camera.bEnableSpecular ? 1.0f : 0.0f, + m_camera.bDebugSuperShiny ? 1.0f : 0.0f + }; + return values[i]; +} +-(void)setParameterValueWithIndex: (int)i Value: (double)v +{ + bool bNewBoolVal = v > 0.5; + NSLog(@"Set Parameter: (%s, %f)", [[self getParameterNameWithIndex: i] UTF8String], v); + switch(i) { + case 0: // FOV + m_camera.perspective_fov = v; + break; + case 1: // sun_yaw + if(sun_yaw != v) { + sun_yaw = v; + [self invalidateShadowBuffers]; + } + break; + case 2: // sun_pitch + if(sun_pitch != v) { + sun_pitch = v; + [self invalidateShadowBuffers]; + } + break; + case 3: // Shadow Quality + m_cShadowBuffers = (int)v; + break; + case 4: + m_camera.bEnablePerPixel = bNewBoolVal; + break; + case 5: + m_camera.bEnableDiffuseMap = bNewBoolVal; + break; + case 6: + m_camera.bEnableNormalMap = bNewBoolVal; + break; + case 7: + m_camera.bEnableSpecMap = bNewBoolVal; + break; + case 8: + m_camera.dAmbientR = v; + break; + case 9: + m_camera.dAmbientG = v; + break; + case 10: + m_camera.dAmbientB = v; + break; + case 11: + m_camera.dSunR = v; + break; + case 12: + m_camera.dSunG = v; + break; + case 13: + m_camera.dSunB = v; + break; + case 14: + if(m_camera.dof_quality != (int)v) { + m_camera.dof_quality = (int)v; + [self invalidatePostShader]; + } + break; + case 15: + if(m_camera.dof_depth != v) { + m_camera.dof_depth = v; + [self invalidatePostShader]; + } + break; + case 16: + if(m_camera.dof_falloff != v) { + m_camera.dof_falloff = v; + [self invalidatePostShader]; + } + break; + case 17: + if(m_camera.bEnableFlash != bNewBoolVal) { + m_camera.bEnableFlash = bNewBoolVal; + [self invalidatePostShader]; + } + break; + case 18: + if(m_camera.flash_intensity != v) { + m_camera.flash_intensity = v; + [self invalidatePostShader]; + } + break; + case 19: + if(m_camera.flash_depth != v) { + m_camera.flash_depth = v; + [self invalidatePostShader]; + } + break; + case 20: + if(m_camera.flash_falloff != v) { + m_camera.flash_falloff = v; + [self invalidatePostShader]; + } + break; + case 21: + if(m_camera.bEnableVignette != bNewBoolVal) { + m_camera.bEnableVignette = bNewBoolVal; + [self invalidatePostShader]; + } + break; + case 22: + if(m_camera.vignette_radius != v) { + m_camera.vignette_radius = v; + [self invalidatePostShader]; + } + break; + case 23: + if(m_camera.vignette_falloff != v) { + m_camera.vignette_falloff = v; + [self invalidatePostShader]; + } + break; + case 24: + if(m_camera.bShowShadowBuffer != bNewBoolVal) { + m_camera.bShowShadowBuffer = bNewBoolVal; + } + break; + case 25: + if(m_camera.bDebugPSSM != bNewBoolVal) { + m_camera.bDebugPSSM = bNewBoolVal; + } + break; + case 26: + if(m_camera.bEnableAmbient != bNewBoolVal) { + m_camera.bEnableAmbient = bNewBoolVal; + } + break; + case 27: + if(m_camera.bEnableDiffuse != bNewBoolVal) { + m_camera.bEnableDiffuse = bNewBoolVal; + } + break; + case 28: + if(m_camera.bEnableSpecular != bNewBoolVal) { + m_camera.bEnableSpecular = bNewBoolVal; + } + break; + case 29: + if(m_camera.bDebugSuperShiny != bNewBoolVal) { + m_camera.bDebugSuperShiny = bNewBoolVal; + } + break; + } +} + +-(double)getParameterMinWithIndex: (int)i +{ + double minValues[30] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}; + return minValues[i]; +} + +-(double)getParameterMaxWithIndex: (int)i +{ + double maxValues[30] = {PI, 2.0f * PI, PI, 3.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 2.0f, 1.0f, 1.0f, 1.0f, 5.0f, 1.0f, 0.5f, 1.0f, 2.0f, 2.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f}; + return maxValues[i]; +} + +-(void)setParameterValueWithName: (NSString *)name Value: (double)v +{ + int cParameters = [self getParameterCount]; + for(int i=0; i < cParameters; i++) { + if([[self getParameterNameWithIndex: i] caseInsensitiveCompare:name] == NSOrderedSame) { + [self setParameterValueWithIndex:i Value:v]; + } + } +} + +- (KRModelManager *)getModelManager { + return m_pModelManager; +} + +- (void)invalidateShadowBuffers { + for(int i=0; i < m_cShadowBuffers; i++) { + shadowValid[i] = false; + } +} + +- (void)setNearZ: (double)dNearZ +{ + if(m_camera.perspective_nearz != dNearZ) { + m_camera.perspective_nearz = dNearZ; + [self invalidateShadowBuffers]; + } +} +- (void)setFarZ: (double)dFarZ +{ + if(m_camera.perspective_farz != dFarZ) { + m_camera.perspective_farz = dFarZ; + [self invalidateShadowBuffers]; + } +} +- (void)setAspect: (double)dAspect +{ + if(m_camera.perspective_aspect != dAspect) { + m_camera.perspective_aspect = dAspect; + [self invalidateShadowBuffers]; + } +} + +- (void)setDebugText: (NSString *)text +{ + debug_text = text; +} + + +@end diff --git a/KREngine/KREngine/Classes/KRInstance.cpp b/KREngine/KREngine/Classes/KRInstance.cpp new file mode 100644 index 0000000..a7cf874 --- /dev/null +++ b/KREngine/KREngine/Classes/KRInstance.cpp @@ -0,0 +1,48 @@ +// +// KRInstance.cpp +// KREngine +// +// Created by Kearwood Gilbert on 11-09-29. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#include +#import "KRInstance.h" + +KRInstance::KRInstance(KRModel *pModel, const KRMat4 modelMatrix) { + m_pModel = pModel; + m_modelMatrix = modelMatrix; +} + +KRInstance::~KRInstance() { + +} + +KRMat4 &KRInstance::getModelMatrix() { + return m_modelMatrix; +} +KRModel *KRInstance::getModel() { + return m_pModel; +} + +void KRInstance::render(KRCamera *pCamera, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &viewMatrix, Vector3 &cameraPosition, Vector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers) { + + KRMat4 projectionMatrix; + if(!bRenderShadowMap) { + projectionMatrix = pCamera->getProjectionMatrix(); + } + KRMat4 mvpmatrix = m_modelMatrix * viewMatrix * projectionMatrix; + + // Transform location of camera to object space for calculation of specular halfVec + KRMat4 inverseModelMatrix = m_modelMatrix; + inverseModelMatrix.invert(); + Vector3 cameraPosObject = inverseModelMatrix.dot(cameraPosition); + Vector3 lightDirObject = inverseModelMatrix.dot(lightDirection); + + m_pModel->render(pCamera, pMaterialManager, bRenderShadowMap, mvpmatrix, cameraPosObject, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers); + +} + +KRBoundingVolume KRInstance::getExtents() { + return KRBoundingVolume(Vector3(m_pModel->getMinX(), m_pModel->getMinY(), m_pModel->getMinZ()), Vector3(m_pModel->getMaxX(), m_pModel->getMaxY(), m_pModel->getMaxZ()), m_modelMatrix); +} \ No newline at end of file diff --git a/KREngine/KREngine/Classes/KRInstance.h b/KREngine/KREngine/Classes/KRInstance.h new file mode 100644 index 0000000..375f9f2 --- /dev/null +++ b/KREngine/KREngine/Classes/KRInstance.h @@ -0,0 +1,42 @@ +// +// KRInstance.h +// KREngine +// +// Created by Kearwood Gilbert on 11-09-29. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#import "KRModel.h" +#import "KRMat4.h" +#import "KRVector3.h" +#import "KRBoundingVolume.h" +#import "KRInstance.h" +#import "KRCamera.h" + +#import +#import + +#ifndef KRINSTANCE_H +#define KRINSTANCE_H + +class KRBoundingVolume; + +class KRInstance { + +public: + KRInstance(KRModel *pModel, const KRMat4 modelMatrix); + ~KRInstance(); + void render(KRCamera *pCamera, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &viewMatrix, Vector3 &cameraPosition, Vector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers); + + KRBoundingVolume getExtents(); + + KRMat4 &getModelMatrix(); + KRModel *getModel(); + +private: + KRModel *m_pModel; + KRMat4 m_modelMatrix; +}; + + +#endif diff --git a/KREngine/KREngine/Classes/KRMat4.cpp b/KREngine/KREngine/Classes/KRMat4.cpp new file mode 100644 index 0000000..16e59b0 --- /dev/null +++ b/KREngine/KREngine/Classes/KRMat4.cpp @@ -0,0 +1,264 @@ +// +// KRMat4.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-09-21. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#include +#include +#include + +#include "KRMat4.h" + +KRMat4::KRMat4() { + // Default constructor - Initialize with an identity matrix + static const GLfloat IDENTITY_MATRIX[] = { + 1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0 + }; + memcpy(m_mat, IDENTITY_MATRIX, sizeof(GLfloat) * 16); + +} + +KRMat4::KRMat4(GLfloat *pMat) { + memcpy(m_mat, pMat, sizeof(GLfloat) * 16); +} + +KRMat4::~KRMat4() { + +} + +GLfloat *KRMat4::getPointer() { + return m_mat; +} + +// Copy constructor +KRMat4::KRMat4(const KRMat4 &m) { + + memcpy(m_mat, m.m_mat, sizeof(GLfloat) * 16); +} + +KRMat4& KRMat4::operator=(const KRMat4 &m) { + if(this != &m) { // Prevent self-assignment. + memcpy(m_mat, m.m_mat, sizeof(GLfloat) * 16); + } + return *this; +} + +// Overload compound multiply operator +KRMat4& KRMat4::operator*=(const KRMat4 &m) { + GLfloat temp[16]; + + int x,y; + + for (x=0; x < 4; x++) + { + for(y=0; y < 4; y++) + { + temp[y + (x*4)] = (m_mat[x*4] * m.m_mat[y]) + + (m_mat[(x*4)+1] * m.m_mat[y+4]) + + (m_mat[(x*4)+2] * m.m_mat[y+8]) + + (m_mat[(x*4)+3] * m.m_mat[y+12]); + } + } + + memcpy(m_mat, temp, sizeof(GLfloat) << 4); + return *this; +} + +/* +// Overload multiply operator +KRMat4& KRMat4::operator*(const KRMat4 &m, const KRMat4 &m) { + KRMat4 result = *this; + result *= m; + return result; +} + */ + +KRMat4 KRMat4::operator*(const KRMat4 &m) { + KRMat4 ret = *this; + ret *= m; + return ret; +} + + +/* Generate a perspective view matrix using a field of view angle fov, + * window aspect ratio, near and far clipping planes */ +void KRMat4::perspective(GLfloat fov, GLfloat aspect, GLfloat nearz, GLfloat farz) { + GLfloat range; + + range = tan(fov / 2.0f) * nearz; + memset(m_mat, 0, sizeof(GLfloat) * 16); + m_mat[0] = (2 * nearz) / ((range * aspect) - (-range * aspect)); + m_mat[5] = (2 * nearz) / (2 * range); + m_mat[10] = -(farz + nearz) / (farz - nearz); + m_mat[11] = -1; + m_mat[14] = -(2 * farz * nearz) / (farz - nearz); + +} + +/* Perform translation operations on a matrix */ +void KRMat4::translate(GLfloat x, GLfloat y, GLfloat z) { + KRMat4 newMatrix; // Create new identity matrix + + newMatrix.m_mat[12] = x; + newMatrix.m_mat[13] = y; + newMatrix.m_mat[14] = z; + + *this *= newMatrix; +} + +/* Rotate a matrix by an angle on a X, Y, or Z axis */ +void KRMat4::rotate(GLfloat angle, AXIS axis) { + // const GLfloat d2r = 0.0174532925199; /* PI / 180 */ + const int cos1[3] = { 5, 0, 0 }; + const int cos2[3] = { 10, 10, 5 }; + const int sin1[3] = { 6, 2, 1 }; + const int sin2[3] = { 9, 8, 4 }; + + KRMat4 newMatrix; // Create new identity matrix + + newMatrix.m_mat[cos1[axis]] = cos(angle); + newMatrix.m_mat[sin1[axis]] = -sin(angle); + newMatrix.m_mat[sin2[axis]] = -newMatrix.m_mat[sin1[axis]]; + newMatrix.m_mat[cos2[axis]] = newMatrix.m_mat[cos1[axis]]; + + *this *= newMatrix; +} + +void KRMat4::scale(GLfloat x, GLfloat y, GLfloat z) { + KRMat4 newMatrix; // Create new identity matrix + + newMatrix.m_mat[0] = x; + newMatrix.m_mat[5] = y; + newMatrix.m_mat[10] = z; + + *this *= newMatrix; +} + +void KRMat4::scale(GLfloat s) { + scale(s,s,s); +} + +void KRMat4::bias() { + // Initialize with a bias matrix + static const GLfloat BIAS_MATRIX[] = { + 0.5, 0.0, 0.0, 0.0, + 0.0, 0.5, 0.0, 0.0, + 0.0, 0.0, 0.5, 0.0, + 0.5, 0.5, 0.5, 1.0 + }; + memcpy(m_mat, BIAS_MATRIX, sizeof(GLfloat) * 16); +} + + +/* Generate an orthographic view matrix */ +void KRMat4::ortho(GLfloat left, GLfloat right, GLfloat top, GLfloat bottom, GLfloat nearz, GLfloat farz) { + memset(m_mat, 0, sizeof(GLfloat) * 16); + /* + m_mat[0] = 2.0f / (right - left); + m_mat[3] = -(right + left) / (right - left); + m_mat[5] = 2.0f / (top - bottom); + m_mat[7] = -(top + bottom) / (top - bottom); + m_mat[10] = -2.0f / (farz - nearz); + m_mat[11] = -(farz + nearz) / (farz - nearz); + m_mat[15] = 1.0f; + */ + m_mat[0] = 2.0f / (right - left); + m_mat[5] = 2.0f / (bottom - top); + m_mat[10] = -1.0f / (farz - nearz); + m_mat[11] = -nearz / (farz - nearz); + m_mat[15] = 1.0f; +} + +bool KRMat4::invert() { + /* + GLfloat inverseTranslation[16]; + GLfloat inverseRotation[16]; + + + + inverseTranslation[0] = 1 ; inverseTranslation[4] = 0 ; inverseTranslation[8] = 0 ; inverseTranslation[12] = -m_mat[12] ; + inverseTranslation[1] = 0 ; inverseTranslation[5] = 1 ; inverseTranslation[9] = 0 ; inverseTranslation[13] = -m_mat[13] ; + inverseTranslation[2] = 0 ; inverseTranslation[6] = 0 ; inverseTranslation[10] = 1 ; inverseTranslation[14] = -m_mat[14] ; + inverseTranslation[3] = 0 ; inverseTranslation[7] = 0 ; inverseTranslation[11] = 0 ; inverseTranslation[15] = 1 ; + + inverseRotation[0] = m_mat[0] ; inverseRotation[4] = m_mat[1] ; inverseRotation[8] = m_mat[2] ; inverseRotation[12] = 0 ; + inverseRotation[1] = m_mat[4] ; inverseRotation[5] = m_mat[5] ; inverseRotation[9] = m_mat[6] ; inverseRotation[13] = 0 ; + inverseRotation[2] = m_mat[8] ; inverseRotation[6] = m_mat[9] ; inverseRotation[10] = m_mat[10] ; inverseRotation[14] = 0 ; + inverseRotation[3] = 0 ; inverseRotation[7] = 0 ; inverseRotation[11] = 0 ; inverseRotation[15] = 1 ; + + KRMat4 inverseRotMat(inverseRotation); + KRMat4 inverseTransMat(inverseTranslation); + + KRMat4 m = inverseRotMat * inverseTransMat; + memcpy(m_mat, m.m_mat, sizeof(GLfloat) * 16); + */ + + // Based on gluInvertMatrix implementation + + double inv[16], det; + int i; + + inv[0] = m_mat[5]*m_mat[10]*m_mat[15] - m_mat[5]*m_mat[11]*m_mat[14] - m_mat[9]*m_mat[6]*m_mat[15] + + m_mat[9]*m_mat[7]*m_mat[14] + m_mat[13]*m_mat[6]*m_mat[11] - m_mat[13]*m_mat[7]*m_mat[10]; + inv[4] = -m_mat[4]*m_mat[10]*m_mat[15] + m_mat[4]*m_mat[11]*m_mat[14] + m_mat[8]*m_mat[6]*m_mat[15] + - m_mat[8]*m_mat[7]*m_mat[14] - m_mat[12]*m_mat[6]*m_mat[11] + m_mat[12]*m_mat[7]*m_mat[10]; + inv[8] = m_mat[4]*m_mat[9]*m_mat[15] - m_mat[4]*m_mat[11]*m_mat[13] - m_mat[8]*m_mat[5]*m_mat[15] + + m_mat[8]*m_mat[7]*m_mat[13] + m_mat[12]*m_mat[5]*m_mat[11] - m_mat[12]*m_mat[7]*m_mat[9]; + inv[12] = -m_mat[4]*m_mat[9]*m_mat[14] + m_mat[4]*m_mat[10]*m_mat[13] + m_mat[8]*m_mat[5]*m_mat[14] + - m_mat[8]*m_mat[6]*m_mat[13] - m_mat[12]*m_mat[5]*m_mat[10] + m_mat[12]*m_mat[6]*m_mat[9]; + inv[1] = -m_mat[1]*m_mat[10]*m_mat[15] + m_mat[1]*m_mat[11]*m_mat[14] + m_mat[9]*m_mat[2]*m_mat[15] + - m_mat[9]*m_mat[3]*m_mat[14] - m_mat[13]*m_mat[2]*m_mat[11] + m_mat[13]*m_mat[3]*m_mat[10]; + inv[5] = m_mat[0]*m_mat[10]*m_mat[15] - m_mat[0]*m_mat[11]*m_mat[14] - m_mat[8]*m_mat[2]*m_mat[15] + + m_mat[8]*m_mat[3]*m_mat[14] + m_mat[12]*m_mat[2]*m_mat[11] - m_mat[12]*m_mat[3]*m_mat[10]; + inv[9] = -m_mat[0]*m_mat[9]*m_mat[15] + m_mat[0]*m_mat[11]*m_mat[13] + m_mat[8]*m_mat[1]*m_mat[15] + - m_mat[8]*m_mat[3]*m_mat[13] - m_mat[12]*m_mat[1]*m_mat[11] + m_mat[12]*m_mat[3]*m_mat[9]; + inv[13] = m_mat[0]*m_mat[9]*m_mat[14] - m_mat[0]*m_mat[10]*m_mat[13] - m_mat[8]*m_mat[1]*m_mat[14] + + m_mat[8]*m_mat[2]*m_mat[13] + m_mat[12]*m_mat[1]*m_mat[10] - m_mat[12]*m_mat[2]*m_mat[9]; + inv[2] = m_mat[1]*m_mat[6]*m_mat[15] - m_mat[1]*m_mat[7]*m_mat[14] - m_mat[5]*m_mat[2]*m_mat[15] + + m_mat[5]*m_mat[3]*m_mat[14] + m_mat[13]*m_mat[2]*m_mat[7] - m_mat[13]*m_mat[3]*m_mat[6]; + inv[6] = -m_mat[0]*m_mat[6]*m_mat[15] + m_mat[0]*m_mat[7]*m_mat[14] + m_mat[4]*m_mat[2]*m_mat[15] + - m_mat[4]*m_mat[3]*m_mat[14] - m_mat[12]*m_mat[2]*m_mat[7] + m_mat[12]*m_mat[3]*m_mat[6]; + inv[10] = m_mat[0]*m_mat[5]*m_mat[15] - m_mat[0]*m_mat[7]*m_mat[13] - m_mat[4]*m_mat[1]*m_mat[15] + + m_mat[4]*m_mat[3]*m_mat[13] + m_mat[12]*m_mat[1]*m_mat[7] - m_mat[12]*m_mat[3]*m_mat[5]; + inv[14] = -m_mat[0]*m_mat[5]*m_mat[14] + m_mat[0]*m_mat[6]*m_mat[13] + m_mat[4]*m_mat[1]*m_mat[14] + - m_mat[4]*m_mat[2]*m_mat[13] - m_mat[12]*m_mat[1]*m_mat[6] + m_mat[12]*m_mat[2]*m_mat[5]; + inv[3] = -m_mat[1]*m_mat[6]*m_mat[11] + m_mat[1]*m_mat[7]*m_mat[10] + m_mat[5]*m_mat[2]*m_mat[11] + - m_mat[5]*m_mat[3]*m_mat[10] - m_mat[9]*m_mat[2]*m_mat[7] + m_mat[9]*m_mat[3]*m_mat[6]; + inv[7] = m_mat[0]*m_mat[6]*m_mat[11] - m_mat[0]*m_mat[7]*m_mat[10] - m_mat[4]*m_mat[2]*m_mat[11] + + m_mat[4]*m_mat[3]*m_mat[10] + m_mat[8]*m_mat[2]*m_mat[7] - m_mat[8]*m_mat[3]*m_mat[6]; + inv[11] = -m_mat[0]*m_mat[5]*m_mat[11] + m_mat[0]*m_mat[7]*m_mat[9] + m_mat[4]*m_mat[1]*m_mat[11] + - m_mat[4]*m_mat[3]*m_mat[9] - m_mat[8]*m_mat[1]*m_mat[7] + m_mat[8]*m_mat[3]*m_mat[5]; + inv[15] = m_mat[0]*m_mat[5]*m_mat[10] - m_mat[0]*m_mat[6]*m_mat[9] - m_mat[4]*m_mat[1]*m_mat[10] + + m_mat[4]*m_mat[2]*m_mat[9] + m_mat[8]*m_mat[1]*m_mat[6] - m_mat[8]*m_mat[2]*m_mat[5]; + + det = m_mat[0]*inv[0] + m_mat[1]*inv[4] + m_mat[2]*inv[8] + m_mat[3]*inv[12]; + + if (det == 0) { + return false; + } + + det = 1.0 / det; + + for (i = 0; i < 16; i++) { + m_mat[i] = inv[i] * det; + } + + + + return true; +} + + +Vector3 KRMat4::dot(const Vector3 &v) const { + return Vector3( + v.x * (float)m_mat[0*4 + 0] + v.y * (float)m_mat[1*4 + 0] + v.z * (float)m_mat[2*4 + 0] + (float)m_mat[3*4 + 0], + v.x * (float)m_mat[0*4 + 1] + v.y * (float)m_mat[1*4 + 1] + v.z * (float)m_mat[2*4 + 1] + (float)m_mat[3*4 + 1], + v.x * (float)m_mat[0*4 + 2] + v.y * (float)m_mat[1*4 + 2] + v.z * (float)m_mat[2*4 + 2] + (float)m_mat[3*4 + 2] + ); +} diff --git a/KREngine/KREngine/Classes/KRMat4.h b/KREngine/KREngine/Classes/KRMat4.h new file mode 100644 index 0000000..d36f4bc --- /dev/null +++ b/KREngine/KREngine/Classes/KRMat4.h @@ -0,0 +1,75 @@ +// +// KRMat4.h +// gldemo +// +// Created by Kearwood Gilbert on 10-09-21. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import +#import + +#import "KRVector3.h" + +#ifndef KRMAT4_I +#define KRMAT4_I + + +#define EMPTY_MATRIX4 { 0.0, 0.0, 0.0, 0.0,\ +0.0, 0.0, 0.0, 0.0,\ +0.0, 0.0, 0.0, 0.0,\ +0.0, 0.0, 0.0, 0.0 } + +#define IDENTITY_MATRIX4 { 1.0, 0.0, 0.0, 0.0,\ +0.0, 1.0, 0.0, 0.0,\ +0.0, 0.0, 1.0, 0.0,\ +0.0, 0.0, 0.0, 1.0 } + +typedef enum { + X_AXIS, + Y_AXIS, + Z_AXIS +} AXIS; + + +class KRMat4 { + + GLfloat m_mat[16]; + +public: + + // Default constructor - Creates an identity matrix + KRMat4(); + + KRMat4(GLfloat *pMat); + + // Destructor + ~KRMat4(); + + // Copy constructor + KRMat4(const KRMat4 &m); + + // Overload assignment operator + KRMat4& operator=(const KRMat4 &m); + + // Overload compound multiply operator + KRMat4& operator*=(const KRMat4 &m); + + // Overload multiply operator + //KRMat4& operator*(const KRMat4 &m); + KRMat4 operator*(const KRMat4 &m); + + GLfloat *getPointer(); + + void perspective(GLfloat fov, GLfloat aspect, GLfloat nearz, GLfloat farz); + void ortho(GLfloat left, GLfloat right, GLfloat top, GLfloat bottom, GLfloat nearz, GLfloat farz); + void translate(GLfloat x, GLfloat y, GLfloat z); + void scale(GLfloat x, GLfloat y, GLfloat z); + void scale(GLfloat s); + void rotate(GLfloat angle, AXIS axis); + void bias(); + bool invert(); + Vector3 dot(const Vector3 &v) const; +}; + +#endif // KRMAT4_I \ No newline at end of file diff --git a/KREngine/KREngine/Classes/KRMaterial.cpp b/KREngine/KREngine/Classes/KRMaterial.cpp new file mode 100644 index 0000000..32e8390 --- /dev/null +++ b/KREngine/KREngine/Classes/KRMaterial.cpp @@ -0,0 +1,144 @@ +// +// KRMaterial.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-10-24. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#include "KRMaterial.h" + +KRMaterial::KRMaterial(KRShaderManager *pShaderManager) { + m_pAmbientMap = NULL; + m_pDiffuseMap = NULL; + m_pSpecularMap = NULL; + m_pNormalMap = NULL; + m_ka_r = (GLfloat)0.0f; + m_ka_g = (GLfloat)0.0f; + m_ka_b = (GLfloat)0.0f; + m_kd_r = (GLfloat)1.0f; + m_kd_g = (GLfloat)1.0f; + m_kd_b = (GLfloat)1.0f; + m_ks_r = (GLfloat)1.0f; + m_ks_g = (GLfloat)1.0f; + m_ks_b = (GLfloat)1.0f; + m_tr = (GLfloat)0.0f; + m_ns = (GLfloat)0.0f; + + m_pShaderManager = pShaderManager; +} + +KRMaterial::~KRMaterial() { + +} + +void KRMaterial::setAmbientMap(KRTexture *pTexture) { + m_pAmbientMap = pTexture; +} + +void KRMaterial::setDiffuseMap(KRTexture *pTexture) { + m_pDiffuseMap = pTexture; +} + +void KRMaterial::setSpecularMap(KRTexture *pTexture) { + m_pSpecularMap = pTexture; +} + +void KRMaterial::setNormalMap(KRTexture *pTexture) { + m_pNormalMap = pTexture; +} + +void KRMaterial::setAmbient(GLfloat r, GLfloat g, GLfloat b) { + m_ka_r = r; + m_ka_g = g; + m_ka_b = b; +} + +void KRMaterial::setDiffuse(GLfloat r, GLfloat g, GLfloat b) { + m_kd_r = r; + m_kd_g = g; + m_kd_b = b; +} + +void KRMaterial::setSpecular(GLfloat r, GLfloat g, GLfloat b) { + m_ks_r = r; + m_ks_g = g; + m_ks_b = b; +} + +void KRMaterial::setTransparency(GLfloat a) { + m_tr = a; +} + +void KRMaterial::setShininess(GLfloat s) { + m_ns = s; +} + +bool KRMaterial::isTransparent() { + return m_tr != 0.0; +} + +void KRMaterial::bind(KRCamera *pCamera, KRMat4 &mvpMatrix, Vector3 &cameraPosition, Vector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers) { + + bool bDiffuseMap = m_pDiffuseMap != NULL && pCamera->bEnableDiffuseMap; + bool bNormalMap = m_pNormalMap != NULL && pCamera->bEnableNormalMap; + bool bSpecMap = m_pSpecularMap != NULL && pCamera->bEnableSpecMap; + + KRShader *pShader = m_pShaderManager->getShader(pCamera, bDiffuseMap, bNormalMap, bSpecMap, cShadowBuffers); + pShader->bind(pCamera, mvpMatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers); + + glUniform3f( + pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_MATERIAL_AMBIENT], + m_ka_r + pCamera->dAmbientR, + m_ka_g + pCamera->dAmbientG, + m_ka_b + pCamera->dAmbientB + ); + + glUniform3f( + pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_MATERIAL_DIFFUSE], + m_kd_r * pCamera->dSunR, + m_kd_g * pCamera->dSunG, + m_kd_b * pCamera->dSunB + ); + + glUniform3f( + pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_MATERIAL_SPECULAR], + m_ks_r * pCamera->dSunR, + m_ks_g * pCamera->dSunG, + m_ks_b * pCamera->dSunB + ); + + glUniform1f(pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_MATERIAL_ALPHA], 1.0f - m_tr); + glUniform1f(pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_MATERIAL_SHININESS], pCamera->bDebugSuperShiny ? 20.0 : m_ns); + + int iTextureName = 0; + if(bDiffuseMap) { + iTextureName = m_pDiffuseMap->getName(); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, iTextureName); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + } + + iTextureName = 0; + if(bSpecMap) { + iTextureName = m_pSpecularMap->getName(); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, iTextureName); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + } + + + iTextureName = 0; + if(bNormalMap) { + iTextureName = m_pNormalMap->getName(); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, iTextureName); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + } +} \ No newline at end of file diff --git a/KREngine/KREngine/Classes/KRMaterial.h b/KREngine/KREngine/Classes/KRMaterial.h new file mode 100644 index 0000000..b8863c5 --- /dev/null +++ b/KREngine/KREngine/Classes/KRMaterial.h @@ -0,0 +1,59 @@ +// +// KRMaterial.h +// gldemo +// +// Created by Kearwood Gilbert on 10-10-24. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import +#import +#import +#import +#import + +using std::list; + +#ifndef KRMATERIAL_H +#define KRMATRIAL_H + +#import "KRTexture.h" +#import "KRShaderManager.h" +#import "KRShader.h" +#import "KRCamera.h" + +class KRMaterial { +public: + KRMaterial(KRShaderManager *pShaderManager); + ~KRMaterial(); + + void setAmbientMap(KRTexture *pTexture); + void setDiffuseMap(KRTexture *pTexture); + void setSpecularMap(KRTexture *pTexture); + void setNormalMap(KRTexture *pTexture); + void setAmbient(GLfloat r, GLfloat g, GLfloat b); + void setDiffuse(GLfloat r, GLfloat g, GLfloat b); + void setSpecular(GLfloat r, GLfloat g, GLfloat b); + void setTransparency(GLfloat a); + void setShininess(GLfloat s); + + void bind(KRCamera *pCamera, KRMat4 &mvpMatrix, Vector3 &cameraPosition, Vector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers); + bool isTransparent(); + +private: + KRTexture *m_pAmbientMap; // mtl map_Ka value + KRTexture *m_pDiffuseMap; // mtl map_Kd value + KRTexture *m_pSpecularMap; // mtl map_Ks value + KRTexture *m_pNormalMap; // mtl map_Normal value + + GLfloat m_ka_r, m_ka_g, m_ka_b; // Ambient rgb + GLfloat m_kd_r, m_kd_g, m_kd_b; // Diffuse rgb + GLfloat m_ks_r, m_ks_g, m_ks_b; // Specular rgb + + GLfloat m_tr; // Transparency + GLfloat m_ns; // Shininess + + KRShaderManager *m_pShaderManager; +}; + +#endif diff --git a/KREngine/KREngine/Classes/KRMaterialManager.cpp b/KREngine/KREngine/Classes/KRMaterialManager.cpp new file mode 100644 index 0000000..d965c05 --- /dev/null +++ b/KREngine/KREngine/Classes/KRMaterialManager.cpp @@ -0,0 +1,184 @@ +// +// KRMaterialManager.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-10-24. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#include "KRMaterialManager.h" + +#import +#include +#include +#include +#include +#include + + +KRMaterialManager::KRMaterialManager(KRTextureManager *pTextureManager, KRShaderManager *pShaderManager) { + m_pTextureManager = pTextureManager; + m_pShaderManager = pShaderManager; +} + +KRMaterialManager::~KRMaterialManager() { + +} + + +KRMaterial *KRMaterialManager::getMaterial(const char *szName) { + map::iterator itr = m_materials.find(szName); + if(itr == m_materials.end()) { + // Not found + return NULL; + } else { + return (*itr).second; + } +} + +bool KRMaterialManager::loadFile(const char *szPath) { + bool bSuccess = false; + + int fdFile = 0; + int fileSize = 0; + void *pFile = NULL; + KRMaterial *pMaterial = NULL; + char szSymbol[16][64]; + + struct stat statbuf; + fdFile = open(szPath, O_RDONLY); + if(fdFile >= 0) { + if(fstat(fdFile, &statbuf) >= 0) { + if ((pFile = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fdFile, 0)) == (caddr_t) -1) { + } else { + fileSize = statbuf.st_size; + + char *pScan = (char *)pFile; + char *pEnd = (char *)pFile + fileSize; + while(pScan < pEnd) { + + // Scan through whitespace + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t' || *pScan == '\r' || *pScan == '\n')) { + pScan++; + } + + if(*pScan == '#') { + // Line is a comment line + + // Scan to the end of the line + while(pScan < pEnd && *pScan != '\r' && *pScan != '\n') { + pScan++; + } + } else { + int cSymbols = 0; + while(pScan < pEnd && *pScan != '\n' && *pScan != '\r') { + + char *pDest = szSymbol[cSymbols++]; + while(pScan < pEnd && *pScan != ' ' && *pScan != '\n' && *pScan != '\r') { + *pDest++ = *pScan++; + } + *pDest = '\0'; + + // Scan through whitespace, but don't advance to next line + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t')) { + pScan++; + } + } + + if(strcmp(szSymbol[0], "newmtl") == 0 && cSymbols >= 2) { + + pMaterial = new KRMaterial(m_pShaderManager); + m_materials[szSymbol[1]] = pMaterial; + } if(pMaterial != NULL) { + if(strcmp(szSymbol[0], "Ka") == 0) { + char *pScan2 = szSymbol[1]; + double r = strtof(pScan2, &pScan2); + if(cSymbols == 2) { + pMaterial->setAmbient(r, r, r); + } else if(cSymbols == 4) { + pScan2 = szSymbol[2]; + double g = strtof(pScan2, &pScan2); + pScan2 = szSymbol[3]; + double b = strtof(pScan2, &pScan2); + pMaterial->setAmbient(r, g, b); + } + } else if(strcmp(szSymbol[0], "Kd") == 0) { + char *pScan2 = szSymbol[1]; + double r = strtof(pScan2, &pScan2); + if(cSymbols == 2) { + pMaterial->setDiffuse(r, r, r); + } else if(cSymbols == 4) { + pScan2 = szSymbol[2]; + double g = strtof(pScan2, &pScan2); + pScan2 = szSymbol[3]; + double b = strtof(pScan2, &pScan2); + pMaterial->setDiffuse(r, g, b); + } + } else if(strcmp(szSymbol[0], "Ks") == 0) { + char *pScan2 = szSymbol[1]; + double r = strtof(pScan2, &pScan2); + if(cSymbols == 2) { + pMaterial->setSpecular(r, r, r); + } else if(cSymbols == 4) { + pScan2 = szSymbol[2]; + double g = strtof(pScan2, &pScan2); + pScan2 = szSymbol[3]; + double b = strtof(pScan2, &pScan2); + pMaterial->setSpecular(r, g, b); + } + } else if(strcmp(szSymbol[0], "Tr") == 0) { + char *pScan2 = szSymbol[1]; + double a = strtof(pScan2, &pScan2); + pMaterial->setTransparency(a); + } else if(strcmp(szSymbol[0], "Ns") == 0) { + char *pScan2 = szSymbol[1]; + double a = strtof(pScan2, &pScan2); + pMaterial->setShininess(a); + } else if(strncmp(szSymbol[0], "map", 3) == 0) { + // Truncate file extension + char *pScan2 = szSymbol[1]; + char *pLastPeriod = NULL; + while(*pScan2 != '\0') { + if(*pScan2 == '.') { + pLastPeriod = pScan2; + } + pScan2++; + } + if(pLastPeriod) { + *pLastPeriod = '\0'; + } + + KRTexture *pTexture = m_pTextureManager->getTexture(szSymbol[1]); + if(pTexture) { + if(strcmp(szSymbol[0], "map_Ka") == 0) { + pMaterial->setAmbientMap(pTexture); + } else if(strcmp(szSymbol[0], "map_Kd") == 0) { + pMaterial->setDiffuseMap(pTexture); + } else if(strcmp(szSymbol[0], "map_Ks") == 0) { + pMaterial->setSpecularMap(pTexture); + } else if(strcmp(szSymbol[0], "map_Normal") == 0) { + pMaterial->setNormalMap(pTexture); + } + } + } + } + + } + + } + + bSuccess = true; + } + } + } + + if(pFile != NULL) { + munmap(pFile, fileSize); + } + + if(fdFile != 0) { + close(fdFile); + } + + return bSuccess; +} diff --git a/KREngine/KREngine/Classes/KRMaterialManager.h b/KREngine/KREngine/Classes/KRMaterialManager.h new file mode 100644 index 0000000..c7611d0 --- /dev/null +++ b/KREngine/KREngine/Classes/KRMaterialManager.h @@ -0,0 +1,37 @@ +// +// KRMaterialManager.h +// gldemo +// +// Created by Kearwood Gilbert on 10-10-24. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#ifndef KRMATERIALMANAGER_H +#define KRMATERIALMANAGER_H + +#include "KRMaterial.h" +#include "KRTextureManager.h" + +#include "KRMaterialManager.h" + +#include +#import + +using std::map; + +class KRMaterialManager { +public: + KRMaterialManager(KRTextureManager *pTextureManager, KRShaderManager *pShaderManager); + ~KRMaterialManager(); + + bool loadFile(const char *szPath); + KRMaterial *getMaterial(const char *szName); + +private: + map m_materials; + KRTextureManager *m_pTextureManager; + KRShaderManager *m_pShaderManager; +}; + +#endif + diff --git a/KREngine/KREngine/Classes/KRModel.cpp b/KREngine/KREngine/Classes/KRModel.cpp new file mode 100644 index 0000000..29efaa5 --- /dev/null +++ b/KREngine/KREngine/Classes/KRModel.cpp @@ -0,0 +1,617 @@ +// +// KRModel.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-09-22. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import +#include +#include +#include +#include +#include +#include +#include + + +#include "KRModel.h" + +#include "KRVector3.h" +#import "KRShader.h" + +#define MAX_VBO_SIZE 65535 +// MAX_VBO_SIZE must be divisible by 3 so triangles aren't split across VBO objects... + +#define BUFFER_OFFSET(i) ((char *)NULL + (i)) + +KRModel::KRModel(std::string path, KRMaterialManager *pMaterialManager) { + + m_fdPackFile = 0; + m_pPackFile = NULL; + m_iPackFileSize = 0; + m_cBuffers = 0; + m_pBuffers = NULL; + + // loadWavefront(path, pMaterialManager); + loadPack(path, pMaterialManager); +} + +void KRModel::loadPack(std::string path, KRMaterialManager *pMaterialManager) { + struct stat statbuf; + m_fdPackFile = open(path.c_str(), O_RDONLY); + if(m_fdPackFile >= 0) { + if(fstat(m_fdPackFile, &statbuf) >= 0) { + if ((m_pPackFile = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, m_fdPackFile, 0)) == (caddr_t) -1) { + } else { + m_iPackFileSize = statbuf.st_size; + + + pack_header *pHeader = (pack_header *)m_pPackFile; + + m_minx = pHeader->minx; + m_miny = pHeader->miny; + m_minz = pHeader->minz; + m_maxx = pHeader->maxx; + m_maxy = pHeader->maxy; + m_maxz = pHeader->maxz; + + pack_material *pPackMaterials = (pack_material *)(pHeader+1); + + for(int iMaterial=0; iMaterial < pHeader->material_count; iMaterial++) { + pack_material *pPackMaterial = pPackMaterials + iMaterial; + + Material *pMaterial = new Material(); + pMaterial->start_vertex = pPackMaterial->start_vertex; + pMaterial->vertex_count = pPackMaterial->vertex_count; + pMaterial->pMaterial = pMaterialManager->getMaterial(pPackMaterial->szName); + m_materials.push_back(pMaterial); + } + + m_pVertexData = (VertexData *)(pPackMaterials + pHeader->material_count); + + + m_cBuffers = (pHeader->vertex_count + MAX_VBO_SIZE - 1) / MAX_VBO_SIZE; + m_pBuffers = new GLuint[m_cBuffers]; + glGenBuffers(m_cBuffers, m_pBuffers); + for(GLsizei iBuffer=0; iBuffer < m_cBuffers; iBuffer++) { +// if(iBuffer < 30) { + GLsizei cVertexes = iBuffer < m_cBuffers - 1 ? MAX_VBO_SIZE : pHeader->vertex_count % MAX_VBO_SIZE; + glBindBuffer(GL_ARRAY_BUFFER, m_pBuffers[iBuffer]); + glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData) * cVertexes, m_pVertexData + iBuffer * MAX_VBO_SIZE, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); +// } + + } + } + } + } +} + +void KRModel::loadWavefront(std::string path, KRMaterialManager *pMaterialManager) { + const char *szPath = path.c_str(); + + int fdFile = 0; + int fileSize = 0; + void *pFile = NULL; + char szSymbol[16][64]; + + std::vector materials; + + + Vertex3D *pVertices = NULL; + Vector3D *pNormals = NULL; + TexCoord *pTexCoords = NULL; + int *pFaces = NULL; + + struct stat statbuf; + fdFile = open(szPath, O_RDONLY); + if(fdFile >= 0) { + if(fstat(fdFile, &statbuf) >= 0) { + if ((pFile = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fdFile, 0)) == (caddr_t) -1) { + } else { + fileSize = statbuf.st_size; + + + + // Pass 1 - Get counts + + int cVertices = 0; + int cNormals = 0; + int cTexCoords = 0; + int cVertexData = 0; + + + cVertices = 0; + int cFaces = 1; + GLint cMaterialFaceStart = 1; + + + + // --------- + + + char *pScan = (char *)pFile; + char *pEnd = (char *)pFile + fileSize; + while(pScan < pEnd) { + + // Scan through whitespace + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t' || *pScan == '\r' || *pScan == '\n')) { + pScan++; + } + + if(*pScan == '#') { + // Line is a comment line + + // Scan to the end of the line + while(pScan < pEnd && *pScan != '\r' && *pScan != '\n') { + pScan++; + } + } else { + int cSymbols = 0; + while(pScan < pEnd && *pScan != '\n' && *pScan != '\r') { + + char *pDest = szSymbol[cSymbols++]; + while(pScan < pEnd && *pScan != ' ' && *pScan != '\n' && *pScan != '\r') { + *pDest++ = *pScan++; + } + *pDest = '\0'; + + // Scan through whitespace, but don't advance to next line + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t')) { + pScan++; + } + } + + if(strcmp(szSymbol[0], "v") == 0) { + // Vertex (v) + cVertices++; + } else if(strcmp(szSymbol[0], "vt") == 0) { + // Vertex Texture UV Coordinate (vt) + cTexCoords++; + } else if(strcmp(szSymbol[0], "vn") == 0) { + // Vertex Normal (vn) + cNormals++; + } else if(strcmp(szSymbol[0], "f") == 0) { + // Face (f) + int cFaceVertexes = (cSymbols - 3) * 3; // 3 vertexes per triangle. Triangles have 4 symbols. Quads have 5 symbols and generate two triangles. + cVertexData += cFaceVertexes; + cFaces += cFaceVertexes * 3 + 1; // Allocate space for count of vertices, Vertex Index, Texture Coordinate Index, and Normal Index + + } else if(strcmp(szSymbol[0], "usemtl") == 0) { + // Use Material (usemtl) + if(cMaterialFaceStart - cFaces > 0) { + cFaces++; + + } + materials.push_back(pMaterialManager->getMaterial(szSymbol[1])); + } + + } + } + + + // Pass 2 - Populate vertexes and faces + Vertex3D *pVertices = (Vertex3D *)malloc(sizeof(Vertex3D) * cVertices); + Vector3D *pNormals = (Vector3D *)malloc(sizeof(Vector3D) *cNormals); + TexCoord *pTexCoords = (TexCoord *)malloc(sizeof(TexCoord) * cTexCoords); + int *pFaces = (int *)malloc(sizeof(int *) * (cFaces + 1)); + + + Vertex3D *pVertice = pVertices; + Vector3D *pNormal = pNormals; + TexCoord *pTexCoord = pTexCoords; + int *pFace = pFaces; + int *pMaterialFaces = pFace++; + *pMaterialFaces = 0; + + + std::vector::iterator material_itr = materials.begin(); + + // -------- + + pScan = (char *)pFile; + while(pScan < pEnd) { + + // Scan through whitespace + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t' || *pScan == '\r' || *pScan == '\n')) { + pScan++; + } + + if(*pScan == '#') { + // Line is a comment line + + // Scan to the end of the line + while(pScan < pEnd && *pScan != '\r' && *pScan != '\n') { + pScan++; + } + } else { + int cSymbols = 0; + while(pScan < pEnd && *pScan != '\n' && *pScan != '\r') { + + char *pDest = szSymbol[cSymbols++]; + while(pScan < pEnd && *pScan != ' ' && *pScan != '\n' && *pScan != '\r') { + *pDest++ = *pScan++; + } + *pDest = '\0'; + + // Scan through whitespace, but don't advance to next line + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t')) { + pScan++; + } + } + + if(strcmp(szSymbol[0], "v") == 0) { + // Vertex (v) + char *pChar = szSymbol[1]; + pVertice -> x = strtof(pChar, &pChar); + pChar = szSymbol[2]; + pVertice -> y = strtof(pChar, &pChar); + pChar = szSymbol[3]; + pVertice -> z = strtof(pChar, &pChar); + pVertice++; + } else if(strcmp(szSymbol[0], "vt") == 0) { + // Vertex Texture UV Coordinate (vt) + char *pChar = szSymbol[1]; + pTexCoord -> u = strtof(pChar, &pChar); + pChar = szSymbol[2]; + pTexCoord -> v = strtof(pChar, &pChar); + pTexCoord++; + } else if(strcmp(szSymbol[0], "vn") == 0) { + // Vertex Normal (vn) + char *pChar = szSymbol[1]; + pNormal -> x = strtof(pChar, &pChar); + pChar = szSymbol[2]; + pNormal -> y = strtof(pChar, &pChar); + pChar = szSymbol[3]; + pNormal -> z = strtof(pChar, &pChar); + pNormal++; + } else if(strcmp(szSymbol[0], "f") == 0) { + // Face (f) + GLint cFaceVertices = cSymbols - 1; + + *pFace++ = cFaceVertices; + for(int iSymbol=1; iSymbol < cSymbols; iSymbol++) { + char *pChar = szSymbol[iSymbol]; + if(*pChar == '.' || (*pChar >= '0' && *pChar <= '9')) { + *pFace++ = strtol(pChar, &pChar, 10) - 1; // Vertex Index + + if(*pChar == '/') { + pChar++; + if(*pChar == '/') { + *pFace++ = -1; + } else { + *pFace++ = strtol(pChar, &pChar, 10) - 1; // Texture Coordinate Index + } + } else { + *pFace++ = -1; + } + + if(*pChar == '/') { + pChar++; + if(*pChar == '/') { + *pFace++ = -1; + } else { + *pFace++ = strtol(pChar, &pChar, 10) - 1; // Normal Index + } + } else { + *pFace++ = -1; + } + while(*pChar == '/') { + pChar++; + strtol(pChar, &pChar, 10); + } + } + } + + + } else if(strcmp(szSymbol[0], "usemtl") == 0) { + // Use Material (usemtl) + if(pFace - pMaterialFaces > 1) { + *pMaterialFaces = pFace - pMaterialFaces - 1; + pMaterialFaces = pFace++; + } + } + } + } + + + *pMaterialFaces = pFace - pMaterialFaces - 1; + *pFace++ = 0; + + + m_pVertexData = (VertexData *)malloc(sizeof(VertexData) * cVertexData); + + VertexData *pData = m_pVertexData; + + int iVertex = 0; + Material *pMaterial = new Material(); + pMaterial->start_vertex = iVertex; + pMaterial->vertex_count = 0; + if(material_itr < materials.end()) { + pMaterial->pMaterial = *material_itr++; + } else { + pMaterial->pMaterial = NULL; + } + m_materials.push_back(pMaterial); + + + pFace = pFaces; + while(*pFace != 0 && iVertex < cVertexData) { + pMaterial->start_vertex = iVertex; + + int *pMaterialEndFace = pFace + *pFace++; + while(pFace < pMaterialEndFace) { + int cFaceVertexes = *pFace; + VertexData *pFirstFaceVertex = NULL; + VertexData *pPrevFaceVertex = NULL; + for(int iFaceVertex=0; iFaceVertex < cFaceVertexes; iFaceVertex++) { + if(iFaceVertex > 2) { + // There have already been 3 vertices. Now we need to split the quad into a second triangle composed of the 1st, 3rd, and 4th vertices + memcpy(pData++, pFirstFaceVertex, sizeof(VertexData)); + memcpy(pData++, pPrevFaceVertex, sizeof(VertexData)); + iVertex+=2; + } + Vertex3D *pVertex = pVertices + pFace[iFaceVertex*3+1]; + if(iFaceVertex==0) { + pFirstFaceVertex = pData; + } + pPrevFaceVertex = pData; + pData->vertex.x = pVertex -> x; + pData->vertex.y = pVertex -> y; + pData->vertex.z = pVertex -> z; + + if(pFace[iFaceVertex*3+2] >= 0) { + TexCoord *pTexCoord = pTexCoords + pFace[iFaceVertex*3+2]; + pData->texcoord.u = pTexCoord -> u; + pData->texcoord.v = pTexCoord -> v; + } else { + pData->texcoord.u = 0; + pData->texcoord.v = 0; + } + + if(pFace[iFaceVertex*3+3] >= 0){ + Vector3D *pNormal = pNormals + pFace[iFaceVertex*3+3]; + pData->normal.x = pNormal -> x; + pData->normal.y = pNormal -> y; + pData->normal.z = pNormal -> z; + } else { + pData->normal.x = 0; + pData->normal.y = 0; + pData->normal.z = 0; + } + + pData++; + iVertex++; + } + pFace += cFaceVertexes * 3 + 1; + } + pMaterial->vertex_count = iVertex - pMaterial->start_vertex; + if(*pFace != 0) { + pMaterial = new Material(); + pMaterial->start_vertex = iVertex; + pMaterial->vertex_count = 0; + if(material_itr < materials.end()) { + pMaterial->pMaterial = *material_itr++; + } else { + pMaterial->pMaterial = NULL; + } + m_materials.push_back(pMaterial); + } + } + } + } + } + + + m_minx = 0.0; + m_miny = 0.0; + m_minz = 0.0; + m_maxx = 0.0; + m_maxy = 0.0; + m_maxz = 0.0; + + + + // Calculate surface normals and tangents + // http://www.terathon.com/code/tangent.html + // http://www.fabiensanglard.net/bumpMapping/index.php + + for(std::vector::iterator itr = m_materials.begin(); itr != m_materials.end(); itr++) { + VertexData *pStart = m_pVertexData + (*itr)->start_vertex; + VertexData *pEnd = pStart + (*itr)->vertex_count; + for(VertexData *pVertex = pStart; pVertex < pEnd; pVertex+=3) { + if(pVertex->vertex.x < m_minx) m_minx = pVertex->vertex.x; + if(pVertex->vertex.x > m_maxx) m_maxx = pVertex->vertex.x; + if(pVertex->vertex.y < m_miny) m_miny = pVertex->vertex.y; + if(pVertex->vertex.y > m_maxy) m_maxy = pVertex->vertex.y; + if(pVertex->vertex.z < m_minz) m_minz = pVertex->vertex.z; + if(pVertex->vertex.z > m_maxz) m_maxz = pVertex->vertex.z; + } + + + for(VertexData *pVertex = pStart; pVertex < pEnd; pVertex+=3) { + Vector3 p1(pVertex[0].vertex.x, pVertex[0].vertex.y, pVertex[0].vertex.z); + Vector3 p2(pVertex[1].vertex.x, pVertex[1].vertex.y, pVertex[1].vertex.z); + Vector3 p3(pVertex[2].vertex.x, pVertex[2].vertex.y, pVertex[2].vertex.z); + Vector3 v1 = p2 - p1; + Vector3 v2 = p3 - p1; + + // -- Calculate normal -- + if(pVertex->normal.x == 0 && pVertex->normal.y == 0 && pVertex->normal.z == 0) { + + + Vector3 normal = v1.cross( v2 ); + + normal.normalize(); + + pVertex[0].normal.x = normal.x; + pVertex[0].normal.y = normal.y; + pVertex[0].normal.z = normal.z; + + pVertex[1].normal.x = normal.x; + pVertex[1].normal.y = normal.y; + pVertex[1].normal.z = normal.z; + + pVertex[2].normal.x = normal.x; + pVertex[2].normal.y = normal.y; + pVertex[2].normal.z = normal.z; + } + + // -- Calculate tangent -- + TexCoord st1; // = pVertex[2].texcoord; + TexCoord st2; // = pVertex[1].texcoord; + st1.u = pVertex[1].texcoord.u - pVertex[0].texcoord.u; + st1.v = pVertex[1].texcoord.v - pVertex[0].texcoord.v; + st2.u = pVertex[2].texcoord.u - pVertex[0].texcoord.u; + st2.v = pVertex[2].texcoord.v - pVertex[0].texcoord.v; + double coef = 1/ (st1.u * st2.v - st2.u * st1.v); + + pVertex[0].tangent.x = coef * ((v1.x * st2.v) + (v2.x * -st1.v)); + pVertex[0].tangent.y = coef * ((v1.y * st2.v) + (v2.y * -st1.v)); + pVertex[0].tangent.z = coef * ((v1.z * st2.v) + (v2.z * -st1.v)); + + Vector3 tangent( + coef * ((v1.x * st2.v) + (v2.x * -st1.v)), + coef * ((v1.y * st2.v) + (v2.y * -st1.v)), + coef * ((v1.z * st2.v) + (v2.z * -st1.v)) + ); + + tangent.normalize(); + + pVertex[0].tangent.x = tangent.x; + pVertex[0].tangent.y = tangent.y; + pVertex[0].tangent.z = tangent.z; + pVertex[1].tangent.x = tangent.x; + pVertex[1].tangent.y = tangent.y; + pVertex[1].tangent.z = tangent.z; + pVertex[2].tangent.x = tangent.x; + pVertex[2].tangent.y = tangent.y; + pVertex[2].tangent.z = tangent.z; + + + } + + } + + + if(pFile != NULL) { + munmap(pFile, fileSize); + } + + if(fdFile != 0) { + close(fdFile); + } + + if(pVertices) { + free(pVertices); + } + if(pNormals) { + free(pNormals); + } + if(pTexCoords) { + free(pTexCoords); + } + if(pFaces) { + free(pFaces); + } +} + +KRModel::~KRModel() { + + if(m_pPackFile != NULL) { + munmap(m_pPackFile, m_iPackFileSize); + } + if(m_fdPackFile) { + close(m_fdPackFile); + } else { + // If we didn't load a packed file, then the data was calculated at run time and malloc'ed + if(m_pVertexData != NULL) { + free(m_pVertexData); + } + } + + if(m_pBuffers != NULL) { + glDeleteBuffers(m_cBuffers, m_pBuffers); + delete m_pBuffers; + } +} + +void KRModel::render(KRCamera *pCamera, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &mvpMatrix, Vector3 &cameraPosition, Vector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers) { + for(std::vector::iterator itr = m_materials.begin(); itr != m_materials.end(); itr++) { + + KRMaterial *pMaterial = (*itr)->pMaterial; + + if(pMaterial != NULL) { + if(!bRenderShadowMap) { + pMaterial->bind(pCamera, mvpMatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers); + } + + if(!bRenderShadowMap || !pMaterial->isTransparent()) { + // Exclude transparent and semi-transparent meshes from shadow maps + + int iVertex = (*itr)->start_vertex; + int iBuffer = iVertex / MAX_VBO_SIZE; + iVertex = iVertex % MAX_VBO_SIZE; + int cVertexes = (*itr)->vertex_count; + while(cVertexes > 0) { + glBindBuffer(GL_ARRAY_BUFFER, m_pBuffers[iBuffer]); + + glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_VERTEX, 3, GL_FLOAT, 0, sizeof(VertexData), BUFFER_OFFSET(0)); + glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_VERTEX); + + glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_NORMAL, 3, GL_FLOAT, 0, sizeof(VertexData), BUFFER_OFFSET(sizeof(Vertex3D))); + glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_NORMAL); + + glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TANGENT, 3, GL_FLOAT, 0, sizeof(VertexData), BUFFER_OFFSET(sizeof(Vertex3D) + sizeof(Vector3D))); + glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_TANGENT); + + glVertexAttribPointer(KRShader::KRENGINE_ATTRIB_TEXUV, 2, GL_FLOAT, 0, sizeof(VertexData), BUFFER_OFFSET(sizeof(Vertex3D) + sizeof(Vector3D) * 2)); + glEnableVertexAttribArray(KRShader::KRENGINE_ATTRIB_TEXUV); + + if(iVertex + cVertexes >= MAX_VBO_SIZE) { + glDrawArrays(GL_TRIANGLES, iVertex, (MAX_VBO_SIZE - iVertex)); + cVertexes -= (MAX_VBO_SIZE - iVertex); + iVertex = 0; + iBuffer++; + } else { + glDrawArrays(GL_TRIANGLES, iVertex, cVertexes); + cVertexes = 0; + } + } + } + } + glBindBuffer(GL_ARRAY_BUFFER, 0); + + } + +} + + +GLfloat KRModel::getMaxDimension() { + GLfloat m = 0.0; + if(m_maxx - m_minx > m) m = m_maxx - m_minx; + if(m_maxy - m_miny > m) m = m_maxy - m_miny; + if(m_maxz - m_minz > m) m = m_maxz - m_minz; + return m; +} + +GLfloat KRModel::getMinX() { + return m_minx; +} +GLfloat KRModel::getMaxX() { + return m_maxx; +} +GLfloat KRModel::getMinY() { + return m_miny; +} +GLfloat KRModel::getMaxY() { + return m_maxy; +} +GLfloat KRModel::getMinZ() { + return m_minz; +} +GLfloat KRModel::getMaxZ() { + return m_maxz; +} diff --git a/KREngine/KREngine/Classes/KRModel.h b/KREngine/KREngine/Classes/KRModel.h new file mode 100644 index 0000000..8422ac9 --- /dev/null +++ b/KREngine/KREngine/Classes/KRModel.h @@ -0,0 +1,98 @@ +// +// KRModel.h +// gldemo +// +// Created by Kearwood Gilbert on 10-09-22. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import +#import +#import +#import +#import + +using std::vector; + +#ifndef KRMODEL_I +#define KRMODEL_I + +#import "KRMaterialManager.h" +#import "KRCamera.h" + + +class KRModel { + +public: + KRModel(std::string path, KRMaterialManager *pMaterialManager); + ~KRModel(); + + void render(KRCamera *pCamera, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &mvpMatrix, Vector3 &cameraPosition, Vector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers); + + GLfloat getMaxDimension(); + GLfloat getMinX(); + GLfloat getMaxX(); + GLfloat getMinY(); + GLfloat getMaxY(); + GLfloat getMinZ(); + GLfloat getMaxZ(); + +private: + + void loadWavefront(std::string path, KRMaterialManager *pMaterialManager); + void loadPack(std::string path, KRMaterialManager *pMaterialManager); + + int m_fdPackFile; + void *m_pPackFile; + int m_iPackFileSize; + + typedef struct { + char szTag[16]; + float minx, miny, minz, maxx, maxy, maxz; + int32_t vertex_count; + int32_t material_count; + } pack_header; + + typedef struct { + int32_t start_vertex; + int32_t vertex_count; + char szName[64]; + } pack_material; + + typedef struct { + GLfloat x; + GLfloat y; + GLfloat z; + } Vertex3D, Vector3D; + + typedef struct { + GLfloat u; + GLfloat v; + } TexCoord; + + typedef struct { + Vertex3D vertex; + Vector3D normal; + Vector3D tangent; + TexCoord texcoord; + } VertexData; + + VertexData *m_pVertexData; + + typedef struct { + GLint start_vertex; + GLsizei vertex_count; + KRMaterial *pMaterial; + } Material; + + GLsizei m_cBuffers; + GLuint *m_pBuffers; + + vector m_materials; + + GLfloat m_minx, m_miny, m_minz, m_maxx, m_maxy, m_maxz; + +}; + + +#endif // KRMODEL_I \ No newline at end of file diff --git a/KREngine/KREngine/Classes/KRModelManager.cpp b/KREngine/KREngine/Classes/KRModelManager.cpp new file mode 100644 index 0000000..5237135 --- /dev/null +++ b/KREngine/KREngine/Classes/KRModelManager.cpp @@ -0,0 +1,35 @@ +// +// KRModelManager.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-12-31. +// Copyright 2010 Kearwood Software. All rights reserved. +// + +#include "KRModelManager.h" + +KRModelManager::KRModelManager(KRMaterialManager *pMaterialManager) { + m_pMaterialManager = pMaterialManager; +} + +KRModelManager::~KRModelManager() { + for(map::iterator itr = m_models.begin(); itr != m_models.end(); ++itr){ + delete (*itr).second; + } + m_models.empty(); +} + +KRModel *KRModelManager::loadModel(const char *szName, const char *szPath) { + KRModel *pModel = new KRModel(szPath, m_pMaterialManager); + m_models[szName] = pModel; + return pModel; +} + +KRModel *KRModelManager::getModel(const char *szName) { + return m_models[szName]; +} + +KRModel *KRModelManager::getFirstModel() { + static std::map::iterator model_itr = m_models.begin(); + return (*model_itr).second; +} diff --git a/KREngine/KREngine/Classes/KRModelManager.h b/KREngine/KREngine/Classes/KRModelManager.h new file mode 100644 index 0000000..f91d801 --- /dev/null +++ b/KREngine/KREngine/Classes/KRModelManager.h @@ -0,0 +1,32 @@ +// +// KRModelManager.h +// gldemo +// +// Created by Kearwood Gilbert on 10-12-31. +// Copyright 2010 Kearwood Software. All rights reserved. +// + +#ifndef KRMODELMANAGER_H +#define KRMODELMANAGER_H + +#include "KRModel.h" + +#include +#import +using std::map; + +class KRModelManager { +public: + KRModelManager(KRMaterialManager *pMaterialManager); + ~KRModelManager(); + + KRModel *loadModel(const char *szName, const char *szPath); + KRModel *getModel(const char *szName); + KRModel *getFirstModel(); + +private: + std::map m_models; + KRMaterialManager *m_pMaterialManager; +}; + +#endif diff --git a/KREngine/KREngine/Classes/KRScene.cpp b/KREngine/KREngine/Classes/KRScene.cpp new file mode 100644 index 0000000..1c89186 --- /dev/null +++ b/KREngine/KREngine/Classes/KRScene.cpp @@ -0,0 +1,95 @@ +// +// KRScene.cpp +// KREngine +// +// Created by Kearwood Gilbert on 11-09-29. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#include + +#import "KRVector3.h" +#import "KRMat4.h" + +#import "KRScene.h" + +KRScene::KRScene() { + m_pExtents = NULL; +} +KRScene::~KRScene() { + for(vector::iterator itr = m_instances.begin(); itr != m_instances.end(); ++itr){ + delete *itr; + } + m_instances.empty(); + clearExtents(); +} +KRInstance *KRScene::addInstance(KRModel *pModel, KRMat4 modelMatrix) { + clearExtents(); + KRInstance *pInstance = new KRInstance(pModel, modelMatrix); + m_instances.push_back(pInstance); + return pInstance; +} +void KRScene::render(KRCamera *pCamera, KRBoundingVolume &frustrumVolume, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &viewMatrix, Vector3 &cameraPosition, Vector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers) { + + if(cShadowBuffers > 0 && !bRenderShadowMap) { + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, shadowDepthTextures[0]); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + } + + if(cShadowBuffers > 1 && !bRenderShadowMap) { + glActiveTexture(GL_TEXTURE4); + glBindTexture(GL_TEXTURE_2D, shadowDepthTextures[1]); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + } + + if(cShadowBuffers > 2 && !bRenderShadowMap) { + glActiveTexture(GL_TEXTURE5); + glBindTexture(GL_TEXTURE_2D, shadowDepthTextures[2]); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + } + + + for(vector::iterator itr = m_instances.begin(); itr != m_instances.end(); ++itr){ + KRInstance *pInstance = *itr; + + if(pInstance->getExtents().test_intersect(frustrumVolume) || bRenderShadowMap) { + pInstance->render(pCamera, pMaterialManager, bRenderShadowMap, viewMatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers); + } + } +} + +void KRScene::calcExtents() { + clearExtents(); + for(vector::iterator itr = m_instances.begin(); itr != m_instances.end(); ++itr){ + KRInstance *pInstance = *itr; + if(m_pExtents) { + *m_pExtents = m_pExtents->get_union(pInstance->getExtents()); + } else { + m_pExtents = new KRBoundingVolume(pInstance->getExtents()); + } + } +} + +KRBoundingVolume KRScene::getExtents() { + if(!m_pExtents) { + calcExtents(); + } + return *m_pExtents; +} + +void KRScene::clearExtents() { + if(m_pExtents) { + delete m_pExtents; + m_pExtents = NULL; + } +} diff --git a/KREngine/KREngine/Classes/KRScene.h b/KREngine/KREngine/Classes/KRScene.h new file mode 100644 index 0000000..e374a62 --- /dev/null +++ b/KREngine/KREngine/Classes/KRScene.h @@ -0,0 +1,41 @@ +// +// KRScene.h +// KREngine +// +// Created by Kearwood Gilbert on 11-09-29. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#ifndef KRSCENE_H +#define KRSCENE_H + +#import +#import "KRInstance.h" +#import "KRBoundingVolume.h" +#import "KRMat4.h" +#import "KRModel.h" +#import "KRCamera.h" + +class KRBoundingVolume; +class KRInstance; + +using std::vector; + +class KRScene { +public: + KRScene(); + ~KRScene(); + KRInstance *addInstance(KRModel *pModel, KRMat4 modelMatrix); + void render(KRCamera *pCamera, KRBoundingVolume &frustrumVolume, KRMaterialManager *pMaterialManager, bool bRenderShadowMap, KRMat4 &viewMatrix, Vector3 &cameraPosition, Vector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers); + KRBoundingVolume getExtents(); +private: + vector m_instances; + KRBoundingVolume *m_pExtents; + + void calcExtents(); + void clearExtents(); +}; + + + +#endif diff --git a/KREngine/KREngine/Classes/KRShader.cpp b/KREngine/KREngine/Classes/KRShader.cpp new file mode 100644 index 0000000..c348563 --- /dev/null +++ b/KREngine/KREngine/Classes/KRShader.cpp @@ -0,0 +1,181 @@ +// +// KRShader.cpp +// KREngine +// +// Created by Kearwood Gilbert on 11-08-11. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#include "KRShader.h" + +KRShader::KRShader(std::string options, const GLchar *szVertShaderSource, const GLchar *szFragShaderSource) { + m_iProgram = 0; + GLuint vertexShader = 0, fragShader = 0; + try { + const GLchar *vertSource[2] = {options.c_str(), szVertShaderSource}; + const GLchar *fragSource[2] = {options.c_str(), szFragShaderSource}; + + // Create shader program. + m_iProgram = glCreateProgram(); + + // Create and compile vertex shader. + vertexShader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertexShader, 2, vertSource, NULL); + glCompileShader(vertexShader); + + // Create and compile vertex shader. + fragShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragShader, 2, fragSource, NULL); + glCompileShader(fragShader); + + // Attach vertex shader to program. + glAttachShader(m_iProgram, vertexShader); + + // Attach fragment shader to program. + glAttachShader(m_iProgram, fragShader); + + // Bind attribute locations. + // This needs to be done prior to linking. + glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_VERTEX, "position"); + glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_TEXUV, "inputTextureCoordinate"); + glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_VERTEX, "myVertex"); + glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_NORMAL, "myNormal"); + glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_TANGENT, "myTangent"); + glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_TEXUV, "myUV"); + + // Link program. + glLinkProgram(m_iProgram); + + // Get uniform locations + m_uniforms[KRENGINE_UNIFORM_MATERIAL_AMBIENT] = glGetUniformLocation(m_iProgram, "material_ambient"); + m_uniforms[KRENGINE_UNIFORM_MATERIAL_DIFFUSE] = glGetUniformLocation(m_iProgram, "material_diffuse"); + m_uniforms[KRENGINE_UNIFORM_MATERIAL_SPECULAR] = glGetUniformLocation(m_iProgram, "material_specular"); + m_uniforms[KRENGINE_UNIFORM_MATERIAL_ALPHA] = glGetUniformLocation(m_iProgram, "material_alpha"); + m_uniforms[KRENGINE_UNIFORM_MATERIAL_SHININESS] = glGetUniformLocation(m_iProgram, "material_shininess"); + + m_uniforms[KRENGINE_UNIFORM_MVP] = glGetUniformLocation(m_iProgram, "myMVPMatrix"); + m_uniforms[KRENGINE_UNIFORM_SHADOWMVP1] = glGetUniformLocation(m_iProgram, "myShadowMVPMatrix1"); + m_uniforms[KRENGINE_UNIFORM_SHADOWMVP2] = glGetUniformLocation(m_iProgram, "myShadowMVPMatrix2"); + m_uniforms[KRENGINE_UNIFORM_SHADOWMVP3] = glGetUniformLocation(m_iProgram, "myShadowMVPMatrix3"); + m_uniforms[KRENGINE_UNIFORM_LIGHTDIRECTION] = glGetUniformLocation(m_iProgram, "lightDirection"); + m_uniforms[KRENGINE_UNIFORM_CAMERAPOS] = glGetUniformLocation(m_iProgram, "cameraPosition"); + + m_uniforms[KRENGINE_UNIFORM_DIFFUSETEXTURE] = glGetUniformLocation(m_iProgram, "diffuseTexture"); + m_uniforms[KRENGINE_UNIFORM_SPECULARTEXTURE] = glGetUniformLocation(m_iProgram, "specularTexture"); + m_uniforms[KRENGINE_UNIFORM_NORMALTEXTURE] = glGetUniformLocation(m_iProgram, "normalTexture"); + m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE1] = glGetUniformLocation(m_iProgram, "shadowTexture1"); + m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE2] = glGetUniformLocation(m_iProgram, "shadowTexture2"); + m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE3] = glGetUniformLocation(m_iProgram, "shadowTexture3"); + + + } catch(...) { + if(vertexShader) { + glDeleteShader(vertexShader); + vertexShader = 0; + } + if(fragShader) { + glDeleteShader(fragShader); + fragShader = 0; + } + if(m_iProgram) { + glDeleteProgram(m_iProgram); + m_iProgram = 0; + } + } + + // Release vertex and fragment shaders. + if (vertexShader) { + glDeleteShader(vertexShader); + } + if (fragShader) { + glDeleteShader(fragShader); + } +} + +KRShader::~KRShader() { + if(m_iProgram) { + glDeleteProgram(m_iProgram); + } +} + +void KRShader::bind(KRCamera *pCamera, KRMat4 &mvpMatrix, Vector3 &cameraPosition, Vector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers) { + glUseProgram(m_iProgram); + + + // Bind our modelmatrix variable to be a uniform called mvpmatrix in our shaderprogram + glUniformMatrix4fv(m_uniforms[KRENGINE_UNIFORM_MVP], 1, GL_FALSE, mvpMatrix.getPointer()); + + Vector3 nLightDir = lightDirection; + nLightDir.normalize(); + + // Bind the light direction vector + glUniform3f( + m_uniforms[KRENGINE_UNIFORM_LIGHTDIRECTION], + (GLfloat)nLightDir.x, + (GLfloat)nLightDir.y, + (GLfloat)nLightDir.z + ); + + // Bind the camera position, in model space + glUniform3f( + m_uniforms[KRENGINE_UNIFORM_CAMERAPOS], + (GLfloat)cameraPosition.x, + (GLfloat)cameraPosition.y, + (GLfloat)cameraPosition.z + ); + + // Bind the shadowmap space matrices + for(int iShadow=0; iShadow < cShadowBuffers; iShadow++) { + glUniformMatrix4fv(m_uniforms[KRENGINE_UNIFORM_SHADOWMVP1 + iShadow], 1, GL_FALSE, pShadowMatrices[iShadow].getPointer()); + } + + // Sets the diffuseTexture variable to the first texture unit + glUniform1i(m_uniforms[KRENGINE_UNIFORM_DIFFUSETEXTURE], 0); + + // Sets the specularTexture variable to the second texture unit + glUniform1i(m_uniforms[KRENGINE_UNIFORM_SPECULARTEXTURE], 1); + + // Sets the normalTexture variable to the third texture unit + glUniform1i(m_uniforms[KRENGINE_UNIFORM_NORMALTEXTURE], 2); + + // Sets the shadowTexture variable to the fourth texture unit + glUniform1i(m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE1], 3); + glUniform1i(m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE2], 4); + glUniform1i(m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE3], 5); + + /* +#if defined(DEBUG) + if (![self validateProgram:m_iProgram]) + { + NSLog(@"Failed to validate program: %d", m_shadowShaderProgram); + return; + } +#endif + */ + + + GLint logLength, status; + + glValidateProgram(m_iProgram); + glGetProgramiv(m_iProgram, GL_INFO_LOG_LENGTH, &logLength); + if (logLength > 0) + { + GLchar *log = (GLchar *)malloc(logLength); + glGetProgramInfoLog(m_iProgram, logLength, &logLength, log); + fprintf(stderr, "Program validate log:\n%s", log); + free(log); + } + + /* + glGetProgramiv(m_iProgram, GL_VALIDATE_STATUS, &status); + if (status == 0) { + return false; + } else { + return true; + } + */ +} + +GLuint KRShader::getProgram() { + return m_iProgram; +} diff --git a/KREngine/KREngine/Classes/KRShader.h b/KREngine/KREngine/Classes/KRShader.h new file mode 100644 index 0000000..0394f91 --- /dev/null +++ b/KREngine/KREngine/Classes/KRShader.h @@ -0,0 +1,67 @@ +// +// KRShader.h +// KREngine +// +// Created by Kearwood Gilbert on 11-08-11. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#import +#import +#import +#import +#import + +using std::vector; + +#ifndef KRSHADER_H +#define KRSHADER_H + +#import "KRShader.h" +#import "KRMat4.h" +#import "KRCamera.h" + +class KRShader { +public: + KRShader(std::string options, const GLchar *szVertShaderSource, const GLchar *szFragShaderSource); + ~KRShader(); + GLuint getProgram(); + + void bind(KRCamera *pCamera, KRMat4 &mvpMatrix, Vector3 &cameraPosition, Vector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers); + + enum { + KRENGINE_ATTRIB_VERTEX, + KRENGINE_ATTRIB_NORMAL, + KRENGINE_ATTRIB_TANGENT, + KRENGINE_ATTRIB_TEXUV, + KRENGINE_NUM_ATTRIBUTES + }; + + enum { + KRENGINE_UNIFORM_MATERIAL_AMBIENT, + KRENGINE_UNIFORM_MATERIAL_DIFFUSE, + KRENGINE_UNIFORM_MATERIAL_SPECULAR, + KRENGINE_UNIFORM_MATERIAL_ALPHA, + KRENGINE_UNIFORM_MATERIAL_SHININESS, + KRENGINE_UNIFORM_MVP, + KRENGINE_UNIFORM_LIGHTDIRECTION, + KRENGINE_UNIFORM_CAMERAPOS, + KRENGINE_UNIFORM_DIFFUSETEXTURE, + KRENGINE_UNIFORM_SPECULARTEXTURE, + KRENGINE_UNIFORM_NORMALTEXTURE, + KRENGINE_UNIFORM_SHADOWMVP1, + KRENGINE_UNIFORM_SHADOWMVP2, + KRENGINE_UNIFORM_SHADOWMVP3, + KRENGINE_UNIFORM_SHADOWTEXTURE1, + KRENGINE_UNIFORM_SHADOWTEXTURE2, + KRENGINE_UNIFORM_SHADOWTEXTURE3, + + KRENGINE_NUM_UNIFORMS + }; + GLint m_uniforms[KRENGINE_NUM_UNIFORMS]; + +private: + GLuint m_iProgram; +}; + +#endif diff --git a/KREngine/KREngine/Classes/KRShaderManager.cpp b/KREngine/KREngine/Classes/KRShaderManager.cpp new file mode 100644 index 0000000..c9ce862 --- /dev/null +++ b/KREngine/KREngine/Classes/KRShaderManager.cpp @@ -0,0 +1,68 @@ +// +// KRShaderManager.cpp +// KREngine +// +// Created by Kearwood Gilbert on 11-08-11. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#include "KRShaderManager.h" +#include + +using namespace std; + +KRShaderManager::KRShaderManager(const GLchar *szVertShaderSource, const GLchar *szFragShaderSource) { + m_szFragShaderSource = new GLchar[strlen(szFragShaderSource)+1]; + m_szVertShaderSource = new GLchar[strlen(szVertShaderSource)+1]; + strcpy(m_szFragShaderSource, szFragShaderSource); + strcpy(m_szVertShaderSource, szVertShaderSource); +} + +KRShaderManager::~KRShaderManager() { + delete m_szFragShaderSource; + delete m_szVertShaderSource; +} + + +KRShader *KRShaderManager::getShader(KRCamera *pCamera, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, int iShadowQuality) { + + char szKey[128]; + sprintf(szKey, "%d_%d_%d_%d_%d_%d_%d_%d_%d", pCamera->bEnablePerPixel, bDiffuseMap, bNormalMap, bSpecMap, pCamera->bDebugPSSM, iShadowQuality, /*pCamera->dAmbientR, pCamera->dAmbientG, pCamera->dAmbientB, pCamera->dSunR, pCamera->dSunG, pCamera->dSunB, */pCamera->bEnableAmbient, pCamera->bEnableDiffuse, pCamera->bEnableSpecular); + + + /* + const char *options = "#define HAS_DIFFUSE_MAP 1\n#define HAS_NORMAL_MAP 0\n#define HAS_SPEC_MAP 0\n#define ENABLE_PER_PIXEL 0\n#define DEBUG_PSSM 0\n#define SHADOW_QUALITY 0\n#define SUN_INTENSITY 1.5\n#define AMBIENT_INTENSITY_R 0.25\n#define AMBIENT_INTENSITY_G 0.25\n#define AMBIENT_INTENSITY_B 0.25\n"; + */ + KRShader *pShader = m_shaders[szKey]; + + if(pShader == NULL) { + if(m_shaders.size() > KRENGINE_MAX_SHADER_HANDLES) { + // Keep the size of the shader cache reasonable + std::map::iterator itr = m_shaders.begin(); + delete (*itr).second; + m_shaders.erase(itr); + } + + stringstream stream; + stream.precision(std::numeric_limits::digits10); + + stream << "#define HAS_DIFFUSE_MAP " << (bDiffuseMap ? "1" : "0"); + stream << "\n#define HAS_NORMAL_MAP " << (bNormalMap ? "1" : "0"); + stream << "\n#define HAS_SPEC_MAP " << (bSpecMap ? "1" : "0"); + stream << "\n#define ENABLE_PER_PIXEL " << (pCamera->bEnablePerPixel ? "1" : "0"); + stream << "\n#define DEBUG_PSSM " << (pCamera->bDebugPSSM ? "1" : "0"); + stream << "\n#define SHADOW_QUALITY " << iShadowQuality; + stream << "\n#define ENABLE_AMBIENT " << (pCamera->bEnableAmbient ? "1" : "0"); + stream << "\n#define ENABLE_DIFFUSE " << (pCamera->bEnableDiffuse ? "1" : "0"); + stream << "\n#define ENABLE_SPECULAR " << (pCamera->bEnableSpecular ? "1" : "0"); + stream.setf(ios::fixed,ios::floatfield); + + stream << "\n"; + std::string options = stream.str(); + + pShader = new KRShader(options, m_szVertShaderSource, m_szFragShaderSource); + + m_shaders[szKey] = pShader; + } + return pShader; +} diff --git a/KREngine/KREngine/Classes/KRShaderManager.h b/KREngine/KREngine/Classes/KRShaderManager.h new file mode 100644 index 0000000..7c8586d --- /dev/null +++ b/KREngine/KREngine/Classes/KRShaderManager.h @@ -0,0 +1,43 @@ +// +// KRShaderManager.h +// KREngine +// +// Created by Kearwood Gilbert on 11-08-11. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#import +#import +#import +#import +#import +#import +#import "KRCamera.h" + +using std::map; +using std::vector; + +#include "KRShader.h" + +#ifndef KRSHADERMANAGER_H +#define KRSHADERMANAGER_H + +#define KRENGINE_MAX_SHADER_HANDLES 100 + +class KRShaderManager { +public: + KRShaderManager(const GLchar *szVertShaderSource, const GLchar *szFragShaderSource); + ~KRShaderManager(); + + KRShader *getShader(KRCamera *pCamera, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, int iShadowQuality); + +private: + std::map m_shaders; + + GLchar *m_szFragShaderSource; + GLchar *m_szVertShaderSource; + + KRShader *m_pShader; +}; + +#endif diff --git a/KREngine/KREngine/Classes/KRTexture.cpp b/KREngine/KREngine/Classes/KRTexture.cpp new file mode 100644 index 0000000..5695044 --- /dev/null +++ b/KREngine/KREngine/Classes/KRTexture.cpp @@ -0,0 +1,209 @@ +// +// KRTexture.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-10-23. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#include "KRTexture.h" + +#import +#include +#include +#include +#include +#include +#import +#import + + +#define PVR_TEXTURE_FLAG_TYPE_MASK 0xff + +static char gPVRTexIdentifier[5] = "PVR!"; + +enum +{ + kPVRTextureFlagTypePVRTC_2 = 24, + kPVRTextureFlagTypePVRTC_4 +}; + +typedef struct _PVRTexHeader +{ + uint32_t headerLength; + uint32_t height; + uint32_t width; + uint32_t numMipmaps; + uint32_t flags; + uint32_t dataLength; + uint32_t bpp; + uint32_t bitmaskRed; + uint32_t bitmaskGreen; + uint32_t bitmaskBlue; + uint32_t bitmaskAlpha; + uint32_t pvrTag; + uint32_t numSurfs; +} PVRTexHeader; + +KRTexture::KRTexture() { + m_iName = 0; + m_fdFile = 0; + m_pFile = NULL; + m_fileSize = 0; +} + +KRTexture::~KRTexture() { + if(m_iName != 0) { + glDeleteTextures(1, &m_iName); + } + if(m_pFile != NULL) { + munmap(m_pFile, m_fileSize); + } + if(m_fdFile != 0) { + close(m_fdFile); + } +} + +bool KRTexture::loadFromFile(const char *szFile) { + struct stat statbuf; + m_fdFile = open(szFile, O_RDONLY); + if(m_fdFile < 0) { + return false; + } else { + if(fstat(m_fdFile,&statbuf) < 0) { + return false; + } else { + void *pFile; + if ((pFile = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, m_fdFile, 0)) + == (caddr_t) -1) { + return false; + } else { + m_fileSize = statbuf.st_size; + m_pFile = pFile; + + PVRTexHeader *header = (PVRTexHeader *)pFile; + uint32_t formatFlags = header->flags & PVR_TEXTURE_FLAG_TYPE_MASK; + if (formatFlags == kPVRTextureFlagTypePVRTC_4) { + m_internalFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; + } else if(formatFlags == kPVRTextureFlagTypePVRTC_2) { + m_internalFormat = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; + } else { + assert(false); + } + + uint32_t pvrTag = header->pvrTag; + if (gPVRTexIdentifier[0] != ((pvrTag >> 0) & 0xff) || + gPVRTexIdentifier[1] != ((pvrTag >> 8) & 0xff) || + gPVRTexIdentifier[2] != ((pvrTag >> 16) & 0xff) || + gPVRTexIdentifier[3] != ((pvrTag >> 24) & 0xff)) + { + return false; + } + + m_iWidth = header->width; // Note: call __builtin_bswap32 when needed to switch endianness + m_iHeight = header->height; + m_bHasAlpha = header->bitmaskAlpha; + + uint8_t *bytes = ((uint8_t *)pFile) + sizeof(PVRTexHeader); + uint32_t dataLength = header->dataLength, dataOffset = 0, dataSize = 0; + uint32_t width = m_iWidth, height = m_iHeight, bpp = 4; + uint32_t blockSize = 0, widthBlocks = 0, heightBlocks = 0; + + // Calculate the data size for each texture level and respect the minimum number of blocks + while(dataOffset < dataLength) { + if (formatFlags == kPVRTextureFlagTypePVRTC_4) { + blockSize = 4 * 4; // Pixel by pixel block size for 4bpp + widthBlocks = width / 4; + heightBlocks = height / 4; + bpp = 4; + } else { + blockSize = 8 * 4; // Pixel by pixel block size for 2bpp + widthBlocks = width / 8; + heightBlocks = height / 4; + bpp = 2; + } + + // Clamp to minimum number of blocks + if (widthBlocks < 2) { + widthBlocks = 2; + } + if (heightBlocks < 2) { + heightBlocks = 2; + } + dataSize = widthBlocks * heightBlocks * ((blockSize * bpp) / 8); + + dataBlockStruct newBlock; + newBlock.start = bytes+dataOffset; + newBlock.length = dataSize; + + m_blocks.push_back(newBlock); + + dataOffset += dataSize; + + width = width >> 1; + if(width < 1) { + width = 1; + } + height = height >> 1; + if(height < 1) { + height = 1; + } + } + + return true; + } + } + } +} + +bool KRTexture::createGLTexture() { + int width = m_iWidth; + int height = m_iHeight; + GLenum err; + + if (m_blocks.size() > 0) + { + if (m_iName != 0) { + glDeleteTextures(1, &m_iName); + } + + glGenTextures(1, &m_iName); + glBindTexture(GL_TEXTURE_2D, m_iName); + } + + if (m_blocks.size() > 1) { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + } else { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + } + int i=0; + for(std::list::iterator itr = m_blocks.begin(); itr != m_blocks.end(); itr++) { + dataBlockStruct block = *itr; + glCompressedTexImage2D(GL_TEXTURE_2D, i, m_internalFormat, width, height, 0, block.length, block.start); + + err = glGetError(); + if (err != GL_NO_ERROR) { + return false; + } + + width = width >> 1; + if(width < 1) { + width = 1; + } + height = height >> 1; + if(height < 1) { + height = 1; + } + + i++; + } + + return true; +} + +GLuint KRTexture::getName() { + if(m_iName == 0) { + createGLTexture(); + } + return m_iName; +} \ No newline at end of file diff --git a/KREngine/KREngine/Classes/KRTexture.h b/KREngine/KREngine/Classes/KRTexture.h new file mode 100644 index 0000000..a0bf526 --- /dev/null +++ b/KREngine/KREngine/Classes/KRTexture.h @@ -0,0 +1,49 @@ +// +// KRTexture.h +// gldemo +// +// Created by Kearwood Gilbert on 10-10-23. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import +#import +#import +#import +#import + +using std::list; + +#ifndef KRTEXTURE_H +#define KRTEXTURE_H + +class KRTexture { +public: + KRTexture(); + ~KRTexture(); + + bool loadFromFile(const char *szFile); + bool createGLTexture(); + GLuint getName(); + +private: + + GLuint m_iName; + uint32_t m_iWidth; + uint32_t m_iHeight; + GLenum m_internalFormat; + bool m_bHasAlpha; + + struct dataBlockStruct { + void *start; + uint32_t length; + }; + + std::list m_blocks; + + int m_fdFile; + int m_fileSize; + void *m_pFile; +}; + +#endif diff --git a/KREngine/KREngine/Classes/KRTextureManager.cpp b/KREngine/KREngine/Classes/KRTextureManager.cpp new file mode 100644 index 0000000..7dda2a8 --- /dev/null +++ b/KREngine/KREngine/Classes/KRTextureManager.cpp @@ -0,0 +1,66 @@ +// +// KRTextureManager.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-10-14. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#include "KRTextureManager.h" +#include + +KRTextureManager::KRTextureManager() { + +} + +KRTextureManager::~KRTextureManager() { + for(map::iterator itr = m_textures.begin(); itr != m_textures.end(); ++itr){ + delete (*itr).second; + } +} + +KRTexture *KRTextureManager::loadTexture(const char *szName, const char *szPath) { + KRTexture *pTexture = new KRTexture(); + if(!pTexture->loadFromFile(szPath)) { + delete pTexture; + return NULL; + } + + if(!pTexture->createGLTexture()) { + delete pTexture; + return NULL; + } + + std::string lowerName = szName; + std::transform(lowerName.begin(), lowerName.end(), + lowerName.begin(), ::tolower); + + + + m_textures[lowerName] = pTexture; + return pTexture; +} + +GLuint KRTextureManager::getTextureName(const char *szName) { + KRTexture *pTexture = getTexture(szName); + if(pTexture) { + return pTexture->getName(); + } else { + return NULL; + } +} + +KRTexture *KRTextureManager::getTexture(const char *szName) { + std::string lowerName = szName; + std::transform(lowerName.begin(), lowerName.end(), + lowerName.begin(), ::tolower); + + map::iterator itr = m_textures.find(lowerName); + if(itr == m_textures.end()) { + // Not found + return NULL; + } else { + return (*itr).second; + } + +} \ No newline at end of file diff --git a/KREngine/KREngine/Classes/KRTextureManager.h b/KREngine/KREngine/Classes/KRTextureManager.h new file mode 100644 index 0000000..b291ed8 --- /dev/null +++ b/KREngine/KREngine/Classes/KRTextureManager.h @@ -0,0 +1,32 @@ +// +// KRTextureManager.h +// gldemo +// +// Created by Kearwood Gilbert on 10-10-14. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#ifndef KRTEXTUREMANAGER_H +#define KRTEXTUREMANAGER_H + +#include "KRTexture.h" + +#include +#import + +using std::map; + +class KRTextureManager { +public: + KRTextureManager(); + ~KRTextureManager(); + + KRTexture *loadTexture(const char *szName, const char *szPath); + GLuint getTextureName(const char *szName); + KRTexture *getTexture(const char *szFile); + +private: + std::map m_textures; +}; + +#endif diff --git a/KREngine/KREngine/Classes/KRVector3.cpp b/KREngine/KREngine/Classes/KRVector3.cpp new file mode 100644 index 0000000..220a70c --- /dev/null +++ b/KREngine/KREngine/Classes/KRVector3.cpp @@ -0,0 +1,97 @@ +// +// KRVector3.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-12-31. +// Copyright 2010 Kearwood Software. All rights reserved. +// + +#include "KRVector3.h" + +//default constructor +Vector3::Vector3() +{ + x = 0.0f; + y = 0.0f; + z = 0.0f; +} + +Vector3::Vector3(float X = 0, float Y = 0, float Z = 0) +{ + x = X; + y = Y; + z = Z; +} + +Vector3::Vector3(const Vector3& p) { + x = p.x; + y = p.y; + z = p.z; +} + +Vector3& Vector3::operator = ( const Vector3& p ) { + x = p.x; + y = p.y; + z = p.z; + + return *this; +} + +Vector3::~Vector3() +{ +} + +//calculate and return the magnitude of this vector +float Vector3::GetMagnitude() +{ + return sqrtf(x * x + y * y + z * z); +} + +//multiply this vector by a scalar +Vector3 Vector3::operator*(float num) const +{ + return Vector3(x * num, y * num, z * num); +} + +//pass in a vector, pass in a scalar, return the product +/* +Vector3 Vector3::operator*(float num, Vector3 const &vec) +{ + return Vector3(vec.x * num, vec.y * num, vec.z * num); +} + */ + +//add two vectors +Vector3 Vector3::operator+(const Vector3 &vec) const +{ + return Vector3(x + vec.x, y + vec.y, z + vec.z); +} + +//subtract two vectors +Vector3 Vector3::operator-(const Vector3 &vec) const +{ + return Vector3(x - vec.x, y - vec.y, z - vec.z); +} + +//normalize this vector +void Vector3::normalize() +{ + float magnitude = sqrtf(x * x + y * y + z * z); + x /= magnitude; + y /= magnitude; + z /= magnitude; +} + +//calculate and return dot product +float Vector3::dot(const Vector3 &vec) const +{ + return x * vec.x + y * vec.y + z * vec.z; +} + +//calculate and return cross product +Vector3 Vector3::cross(const Vector3 &vec) const +{ + return Vector3(y * vec.z - z * vec.y, + z * vec.x - x * vec.z, + x * vec.y - y * vec.x); +} diff --git a/KREngine/KREngine/Classes/KRVector3.h b/KREngine/KREngine/Classes/KRVector3.h new file mode 100644 index 0000000..b56aa81 --- /dev/null +++ b/KREngine/KREngine/Classes/KRVector3.h @@ -0,0 +1,54 @@ +// +// KRVector3.h +// gldemo +// +// Created by Kearwood Gilbert on 10-12-31. +// Copyright 2010 Kearwood Software. All rights reserved. +// + +#ifndef KRVECTOR3 +#define KRVECTOR3 +#include + +class Vector3 +{ + +public: + float x, y, z; + + //default constructor + + Vector3(float X, float Y, float Z); + Vector3(); + ~Vector3(); + + Vector3(const Vector3& p); + Vector3& operator = ( const Vector3& p ); + + + //calculate and return the magnitude of this vector + float GetMagnitude(); + + //multiply this vector by a scalar + Vector3 operator*(float num) const; + + //pass in a vector, pass in a scalar, return the product + //friend Vector3 operator*(float num, Vector3 const &vec); + + //add two vectors + Vector3 operator+(const Vector3 &vec) const; + + //subtract two vectors + Vector3 operator-(const Vector3 &vec) const; + + //normalize this vector + void normalize(); + + //calculate and return dot product + float dot(const Vector3 &vec) const; + + //calculate and return cross product + Vector3 cross(const Vector3 &vec) const; +}; + +#endif \ No newline at end of file diff --git a/KREngine/KREngine/KREngine-Prefix.pch b/KREngine/KREngine/KREngine-Prefix.pch new file mode 100644 index 0000000..debee03 --- /dev/null +++ b/KREngine/KREngine/KREngine-Prefix.pch @@ -0,0 +1,7 @@ +// +// Prefix header for all source files of the 'KREngine' target in the 'KREngine' project +// + +#ifdef __OBJC__ + #import +#endif diff --git a/KREngine/KREngine/Shaders/ObjectShader.fsh b/KREngine/KREngine/Shaders/ObjectShader.fsh new file mode 100644 index 0000000..932cddf --- /dev/null +++ b/KREngine/KREngine/Shaders/ObjectShader.fsh @@ -0,0 +1,154 @@ +uniform lowp vec3 material_ambient, material_diffuse, material_specular; +uniform lowp float material_alpha; + +#if ENABLE_PER_PIXEL == 1 +uniform mediump float material_shininess; +#endif + +#if HAS_DIFFUSE_MAP == 1 +uniform sampler2D diffuseTexture; +#endif + +#if HAS_SPEC_MAP == 1 +uniform sampler2D specularTexture; +#endif + +#if HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1 +uniform sampler2D normalTexture; +#endif + +#if SHADOW_QUALITY >= 1 +uniform sampler2D shadowTexture1; +varying highp vec4 shadowMapCoord1; +#endif + +#if SHADOW_QUALITY >= 2 +uniform sampler2D shadowTexture2; +varying highp vec4 shadowMapCoord2; +#endif + +#if SHADOW_QUALITY >= 3 +uniform sampler2D shadowTexture3; +varying highp vec4 shadowMapCoord3; +#endif + +#if HAS_DIFFUSE_MAP == 1 || (HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1) || HAS_SPEC_MAP == 1 +varying mediump vec2 texCoord; +#endif + +#if HAS_NORMAL_MAP == 0 && ENABLE_PER_PIXEL == 1 +varying mediump vec3 normal; +#endif + +#if ENABLE_PER_PIXEL == 1 +varying mediump vec3 lightVec; +varying mediump vec3 halfVec; +#else +varying mediump float lamberFactor; +varying mediump float specularFactor; +#endif + +void main() +{ +#if HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1 + // lookup normal from normal map, move from [0,1] to [-1, 1] range, normalize + mediump vec3 normal = normalize(2.0 * texture2D(normalTexture,texCoord).rgb - 1.0); +#endif + +#if ENABLE_PER_PIXEL == 1 + mediump float lamberFactor = max(0.0,dot(lightVec, normal)); + mediump float specularFactor = 0.0; + if(material_shininess > 0.0) { + specularFactor = max(0.0,pow(dot(halfVec,normal), material_shininess)); + } +#endif + +#if HAS_DIFFUSE_MAP == 1 + mediump vec4 diffuseMaterial = vec4(vec3(texture2D(diffuseTexture, texCoord)), material_alpha); +#else + mediump vec4 diffuseMaterial = vec4(vec3(1.0), material_alpha); +#endif + +#if SHADOW_QUALITY == 1 + highp float shadowMapDepth = 1.0; + highp float vertexShadowDepth = 1.0; + highp vec2 shadowMapPos = ((shadowMapCoord1 / shadowMapCoord1.w + 1.0) / 2.0).st; + + if(shadowMapCoord1.x >= -1.0 && shadowMapCoord1.x <= 1.0 && shadowMapCoord1.y >= -1.0 && shadowMapCoord1.y <= 1.0 && shadowMapCoord1.z >= 0.0 && shadowMapCoord1.z <= 1.0) { +#if DEBUG_PSSM == 1 + diffuseMaterial = diffuseMaterial * vec4(0.75, 0.75, 0.5, 1.0) + vec4(0.0, 0.0, 0.5, 0.0); +#endif + shadowMapDepth = texture2D(shadowTexture1, shadowMapPos).z; // unpack(texture2D(shadowTexture1, shadowMapPos)); + vertexShadowDepth = ((shadowMapCoord1 / shadowMapCoord1.w + 1.0) / 2.0).z; + } +#endif + +#if SHADOW_QUALITY >= 2 + + highp float shadowMapDepth = 1.0; + highp float vertexShadowDepth = 1.0; + + if(shadowMapCoord1.x >= -1.0 && shadowMapCoord1.x <= 1.0 && shadowMapCoord1.y >= -1.0 && shadowMapCoord1.y <= 1.0 && shadowMapCoord1.z >= 0.0 && shadowMapCoord1.z <= 1.0) { +#if DEBUG_PSSM == 1 + diffuseMaterial = diffuseMaterial * vec4(0.75, 0.75, 0.5, 1.0) + vec4(0.0, 0.0, 0.5, 0.0); +#endif + + highp vec2 shadowMapPos = ((shadowMapCoord1 / shadowMapCoord1.w + 1.0) / 2.0).st; + shadowMapDepth = texture2D(shadowTexture1, shadowMapPos).z; + vertexShadowDepth = ((shadowMapCoord1 / shadowMapCoord1.w + 1.0) / 2.0).z; + } + + else if(shadowMapCoord2.s >= -1.0 && shadowMapCoord2.s <= 1.0 && shadowMapCoord2.t >= -1.0 && shadowMapCoord2.t <= 1.0 && shadowMapCoord2.z >= 0.0 && shadowMapCoord2.z <= 1.0) { +#if DEBUG_PSSM == 1 + diffuseMaterial = diffuseMaterial * vec4(0.75, 0.50, 0.75, 1.0) + vec4(0.0, 0.5, 0.0, 0.0); +#endif + highp vec2 shadowMapPos = ((shadowMapCoord2 / shadowMapCoord2.w + 1.0) / 2.0).st; + shadowMapDepth = texture2D(shadowTexture2, shadowMapPos).z; + vertexShadowDepth = ((shadowMapCoord2 / shadowMapCoord2.w + 1.0) / 2.0).z; + } + +#if SHADOW_QUALITY >= 3 + + else if(shadowMapCoord3.s >= -1.0 && shadowMapCoord3.s <= 1.0 && shadowMapCoord3.t >= -1.0 && shadowMapCoord3.t <= 1.0 && shadowMapCoord3.z >= 0.0 && shadowMapCoord3.z <= 1.0) { +#if DEBUG_PSSM == 1 + diffuseMaterial = diffuseMaterial * vec4(0.50, 0.75, 0.75, 1.0) + vec4(0.5, 0.0, 0.0, 0.0); +#endif + highp vec2 shadowMapPos = ((shadowMapCoord3 / shadowMapCoord3.w + 1.0) / 2.0).st; + shadowMapDepth = texture2D(shadowTexture3, shadowMapPos).z; + vertexShadowDepth = ((shadowMapCoord3 / shadowMapCoord3.w + 1.0) / 2.0).z; + } + +#endif +#endif + +#if SHADOW_QUALITY >= 1 + if(vertexShadowDepth >= shadowMapDepth && shadowMapDepth < 1.0) { + lamberFactor = 0.0; + specularFactor = 0.0; + } +#endif + +#if ENABLE_AMBIENT + // -------------------- Add ambient light and alpha component -------------------- + gl_FragColor = vec4(vec3(diffuseMaterial) * material_ambient, material_alpha); +#else + gl_FragColor = vec4(0.0, 0.0, 0.0, material_alpha); +#endif + +#if ENABLE_DIFFUSE + // -------------------- Add diffuse light -------------------- + gl_FragColor += vec4(vec3(diffuseMaterial) * material_diffuse * lamberFactor, 0.0); +#endif + +#if ENABLE_SPECULAR + + // -------------------- Add specular light -------------------- +#if HAS_SPEC_MAP == 1 + gl_FragColor += vec4(material_specular * vec3(texture2D(specularTexture, texCoord)) * specularFactor, 0.0); +#else + gl_FragColor += vec4(material_specular * specularFactor, 0.0); +#endif + +#endif + +} diff --git a/KREngine/KREngine/Shaders/ObjectShader.vsh b/KREngine/KREngine/Shaders/ObjectShader.vsh new file mode 100644 index 0000000..d93b2de --- /dev/null +++ b/KREngine/KREngine/Shaders/ObjectShader.vsh @@ -0,0 +1,101 @@ +// +// Shader.vsh +// gldemo +// +// Created by Kearwood Gilbert on 10-09-16. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +attribute highp vec3 myVertex, myNormal; +attribute highp vec3 myTangent; +attribute mediump vec2 myUV; +uniform highp mat4 myMVPMatrix, myShadowMVPMatrix1,myShadowMVPMatrix2,myShadowMVPMatrix3; // mvpmatrix is the result of multiplying the model, view, and projection matrices +// uniform lowp vec3 material_ambient, material_diffuse, material_specular; +uniform highp vec3 lightDirection; // Must be normalized before entering shader +uniform highp vec3 cameraPosition; + +#if ENABLE_PER_PIXEL == 0 +uniform mediump float material_shininess; +#endif + +#if HAS_DIFFUSE_MAP == 1 || (HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1) || HAS_SPEC_MAP == 1 +varying mediump vec2 texCoord; +#endif + +#if ENABLE_PER_PIXEL == 1 +varying mediump vec3 lightVec; +varying mediump vec3 halfVec; +#else +varying mediump float lamberFactor; +varying mediump float specularFactor; +#endif + +#if HAS_NORMAL_MAP == 0 && ENABLE_PER_PIXEL == 1 +varying mediump vec3 normal; +#endif + +#if SHADOW_QUALITY >= 1 +varying highp vec4 shadowMapCoord1; +#endif + +#if SHADOW_QUALITY >= 2 +varying highp vec4 shadowMapCoord2; +#endif + +#if SHADOW_QUALITY >= 3 +varying highp vec4 shadowMapCoord3; +#endif + +void main() +{ + // Transform position + gl_Position = myMVPMatrix * vec4(myVertex,1.0); + + + +#if HAS_DIFFUSE_MAP == 1 || (HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1) || HAS_SPEC_MAP == 1 + // Pass UV co-ordinates + texCoord = myUV.st; +#endif + +#if SHADOW_QUALITY >= 1 + shadowMapCoord1 = myShadowMVPMatrix1 * vec4(myVertex,1.0); +#endif + +#if SHADOW_QUALITY >= 2 + shadowMapCoord2 = myShadowMVPMatrix2 * vec4(myVertex,1.0); +#endif + +#if SHADOW_QUALITY >= 3 + shadowMapCoord3 = myShadowMVPMatrix3 * vec4(myVertex,1.0); +#endif + + + // ----------- Directional Light (Sun) ----------- + +#if HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1 + // ----- Calculate per-pixel lighting in tangent space, for normal mapping ------ + mediump vec3 eyeVec = normalize(cameraPosition - myVertex); + mediump vec3 a_bitangent = cross(myNormal, myTangent); + lightVec = normalize(vec3(dot(lightDirection, myTangent), dot(lightDirection, a_bitangent), dot(lightDirection, myNormal))); + halfVec = normalize(vec3(dot(eyeVec, myTangent), dot(eyeVec, a_bitangent), dot(eyeVec, myNormal))); + halfVec = normalize(halfVec + lightVec); // Normalizing anyways, no need to divide by 2 + +#else + +#if ENABLE_PER_PIXEL == 1 + // ------ Calculate per-pixel lighting without normal mapping ------ + normal = myNormal; + lightVec = lightDirection; + halfVec = normalize((normalize(cameraPosition - myVertex) + lightVec)); // Normalizing anyways, no need to divide by 2 +#else + // ------ Calculate per-vertex lighting ------ + mediump vec3 halfVec = normalize((normalize(cameraPosition - myVertex) + lightDirection)); // Normalizing anyways, no need to divide by 2 + lamberFactor = max(0.0,dot(lightDirection, myNormal)); + specularFactor = 0.0; + if(material_shininess > 0.0) { + specularFactor = max(0.0,pow(dot(halfVec,myNormal), material_shininess)); + } +#endif +#endif +} diff --git a/KREngine/KREngine/Shaders/PostShader.fsh b/KREngine/KREngine/Shaders/PostShader.fsh new file mode 100644 index 0000000..bd794bb --- /dev/null +++ b/KREngine/KREngine/Shaders/PostShader.fsh @@ -0,0 +1,153 @@ +#define ENABLE_VIDEO_BG 0 +#define PIXEL_SHIFT_1 0.001 +#define PIXEL_SHIFT_2 0.002 +#define PIXEL_SHIFT_3 0.003 +#define PIXEL_SHIFT_4 0.004 + + +varying mediump vec2 textureCoordinate; +precision lowp float; + +#if ENABLE_VIDEO_BG == 0 +uniform lowp sampler2D videoFrame; +#endif + +uniform lowp sampler2D renderFrame; +uniform lowp sampler2D depthFrame; + +void main() +{ + + lowp vec4 renderColor = texture2D(renderFrame, textureCoordinate); +#if DOF_QUALITY > 0 || ENABLE_FLASH == 1 + mediump float depth = texture2D(depthFrame, textureCoordinate).r; +#endif + mediump vec4 pixelColor = renderColor; + + +#if DOF_QUALITY == 2 + + // Render high quality circle of confusion + // __XXX__ + // _XXXXX_ + // _XXXXX_ + // _XXXXX_ + // __XXX__ + mediump float cf1 = PIXEL_SHIFT_1; + mediump float cf2 = PIXEL_SHIFT_2; + + mediump float bx1 = textureCoordinate.s + cf1; + mediump float bx2 = textureCoordinate.s + cf2; + mediump float bxm1 = textureCoordinate.s - cf1; + mediump float bxm2 = textureCoordinate.s - cf2; + + mediump float by1 = textureCoordinate.t + cf1; + mediump float by2 = textureCoordinate.t + cf2; + mediump float bym1 = textureCoordinate.t - cf1; + mediump float bym2 = textureCoordinate.t - cf2; + + pixelColor += texture2D(renderFrame, vec2(bx1, textureCoordinate.t)); + pixelColor += texture2D(renderFrame, vec2(bxm1, textureCoordinate.t)); + pixelColor += texture2D(renderFrame, vec2(bx2, textureCoordinate.t)); + pixelColor += texture2D(renderFrame, vec2(bxm2, textureCoordinate.t)); + + pixelColor += texture2D(renderFrame, vec2(textureCoordinate.s, by1)); + pixelColor += texture2D(renderFrame, vec2(bx1, by1)); + pixelColor += texture2D(renderFrame, vec2(bxm1, by1)); + pixelColor += texture2D(renderFrame, vec2(bx2, by1)); + pixelColor += texture2D(renderFrame, vec2(bxm2, by1)); + + pixelColor += texture2D(renderFrame, vec2(textureCoordinate.s, by2)); + pixelColor += texture2D(renderFrame, vec2(bx1, by2)); + pixelColor += texture2D(renderFrame, vec2(bxm1, by2)); + + pixelColor += texture2D(renderFrame, vec2(textureCoordinate.s,bym1)); + pixelColor += texture2D(renderFrame, vec2(bx1,bym1)); + pixelColor += texture2D(renderFrame, vec2(bxm1,bym1)); + pixelColor += texture2D(renderFrame, vec2(bx2,bym1)); + pixelColor += texture2D(renderFrame, vec2(bxm2,bym1)); + + pixelColor += texture2D(renderFrame, vec2(bx1, bym2)); + pixelColor += texture2D(renderFrame, vec2(bx1, bym2)); + pixelColor += texture2D(renderFrame, vec2(bxm1, bym2)); + pixelColor /= 21.0; + +#endif +// DOF_QUALITY == 2 + +#if DOF_QUALITY == 1 + + // Render low quality circle of confusion + // ___X___ + // __XXX__ + // _XXXXX_ + // __XXX__ + // ___X___ + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(0, -PIXEL_SHIFT_2)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(-PIXEL_SHIFT_1, -PIXEL_SHIFT_1)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(0, -PIXEL_SHIFT_1)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(+PIXEL_SHIFT_1, -PIXEL_SHIFT_1)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(-PIXEL_SHIFT_2, 0)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(-PIXEL_SHIFT_1, 0)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(+PIXEL_SHIFT_1, 0)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(+PIXEL_SHIFT_2, 0)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(-PIXEL_SHIFT_1, -PIXEL_SHIFT_1)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(0, -PIXEL_SHIFT_1)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(+PIXEL_SHIFT_1, -PIXEL_SHIFT_1)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(0, -PIXEL_SHIFT_2)); + pixelColor /= 13.0; + +#endif +// DOF_QUALITY == 1 + +#if DOF_QUALITY > 0 + mediump float focusDepth = texture2D(depthFrame, vec2(0.5, 0.5)).r; + mediump float blurAmount = clamp((depth - DOF_DEPTH - focusDepth) / DOF_FALLOFF, 0.0, 1.0); + pixelColor = pixelColor * blurAmount + renderColor * (1.0 - blurAmount); +#endif + + // ---- DEPTH_OF_FIELD END ---- + + + + + // ---- NIGHT_TIME START ---- +#if ENABLE_FLASH == 1 + // Un-comment to enable night time / flash effect + pixelColor *= vec4(vec3(1.0 - clamp((depth - FLASH_DEPTH) / FLASH_FALLOFF, 0.0, 1.0)) * FLASH_INTENSITY, 1.0); + //pixelColor *= vec4(vec3(clamp(1.0 / (depth - FLASH_DEPTH) * FLASH_FALLOFF, 0.0, 1.0) * FLASH_INTENSITY), 1.0); + +#endif + // ---- NIGHT_TIME END ---- + + // ---- VIDEO_BG START ---- + + /* + // Overlay video background + if(depth == 1.0) { + //FILTER COLOR BY CALCULATING PER PIXEL DOT PRODUCT + pixelColor = vec4(dot(vec3(texture2D(videoFrame, textureCoordinate)), vec3(.222, .707, .071))); + } + */ + + // ---- VIDEO_BG END ---- + + + // ---- VIGNETTE START ---- + + // Render vignette effect + +#if ENABLE_VIGNETTE + pixelColor *= vec4(vec3(clamp(1.0 - (distance(textureCoordinate, vec2(0.5, 0.5)) - VIGNETTE_RADIUS) / VIGNETTE_FALLOFF, 0.0, 1.0)), 1.0); +#endif + // ---- VIGNETTE END ---- + + gl_FragColor = pixelColor; + + + //PASSTHROUGH STATEMENT + // gl_FragColor = texture2D(depthFrame, textureCoordinate); + + //gl_FragColor = vec4(vec3(blurAmount), 1.0); + +} diff --git a/KREngine/KREngine/Shaders/PostShader.vsh b/KREngine/KREngine/Shaders/PostShader.vsh new file mode 100644 index 0000000..6121696 --- /dev/null +++ b/KREngine/KREngine/Shaders/PostShader.vsh @@ -0,0 +1,10 @@ +attribute vec4 position; +attribute lowp vec4 inputTextureCoordinate; + +varying mediump vec2 textureCoordinate; + +void main() +{ + gl_Position = position; + textureCoordinate = inputTextureCoordinate.xy; +} \ No newline at end of file diff --git a/KREngine/KREngine/Shaders/ShadowShader.fsh b/KREngine/KREngine/Shaders/ShadowShader.fsh new file mode 100644 index 0000000..f0066f7 --- /dev/null +++ b/KREngine/KREngine/Shaders/ShadowShader.fsh @@ -0,0 +1,8 @@ +uniform sampler2D diffuseTexture; +varying mediump vec2 texCoord; + + +void main() +{ + gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +} diff --git a/KREngine/KREngine/Shaders/ShadowShader.vsh b/KREngine/KREngine/Shaders/ShadowShader.vsh new file mode 100644 index 0000000..d41ad71 --- /dev/null +++ b/KREngine/KREngine/Shaders/ShadowShader.vsh @@ -0,0 +1,26 @@ +#define SHADOW_BIAS 0.01 + + +attribute highp vec3 myVertex; +/* +attribute mediump vec2 myUV; +varying mediump vec2 texCoord; +*/ +uniform highp mat4 myShadowMVPMatrix1; // Shadowmvpmatrix is the result of multiplying the model, view, and projection matrices + + + +void main() +{ + // Transform position + /* + position = myShadowMVPMatrix1 * vec4(myVertex,1.0); + */ + gl_Position = myShadowMVPMatrix1 * vec4(myVertex,1.0); + gl_Position.z += SHADOW_BIAS; + /* + // Pass UV co-ordinates + texCoord = myUV.st; + */ + +} diff --git a/krengine.xcworkspace/contents.xcworkspacedata b/krengine.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..deb7c47 --- /dev/null +++ b/krengine.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,13 @@ + + + + + + + + + diff --git a/objpack/objpack.xcodeproj/project.pbxproj b/objpack/objpack.xcodeproj/project.pbxproj new file mode 100644 index 0000000..8254c90 --- /dev/null +++ b/objpack/objpack.xcodeproj/project.pbxproj @@ -0,0 +1,215 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + E404C89D136B8E2F00B6C99B /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E404C89C136B8E2F00B6C99B /* main.cpp */; }; + E404C8A6136B901400B6C99B /* KROBJPacker.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E404C8A4136B901400B6C99B /* KROBJPacker.cpp */; }; + E404C8A9136B919F00B6C99B /* KRVector3.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E404C8A7136B919F00B6C99B /* KRVector3.cpp */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + E404C896136B8E2F00B6C99B /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + E404C898136B8E2F00B6C99B /* objpack */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = objpack; sourceTree = BUILT_PRODUCTS_DIR; }; + E404C89C136B8E2F00B6C99B /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; + E404C89E136B8E2F00B6C99B /* objpack.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = objpack.1; sourceTree = ""; }; + E404C8A4136B901400B6C99B /* KROBJPacker.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = KROBJPacker.cpp; sourceTree = ""; }; + E404C8A5136B901400B6C99B /* KROBJPacker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KROBJPacker.h; sourceTree = ""; }; + E404C8A7136B919F00B6C99B /* KRVector3.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = KRVector3.cpp; sourceTree = ""; }; + E404C8A8136B919F00B6C99B /* KRVector3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KRVector3.h; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + E404C895136B8E2F00B6C99B /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + E404C88D136B8E2F00B6C99B = { + isa = PBXGroup; + children = ( + E404C89B136B8E2F00B6C99B /* objpack */, + E404C899136B8E2F00B6C99B /* Products */, + ); + sourceTree = ""; + }; + E404C899136B8E2F00B6C99B /* Products */ = { + isa = PBXGroup; + children = ( + E404C898136B8E2F00B6C99B /* objpack */, + ); + name = Products; + sourceTree = ""; + }; + E404C89B136B8E2F00B6C99B /* objpack */ = { + isa = PBXGroup; + children = ( + E404C89C136B8E2F00B6C99B /* main.cpp */, + E404C89E136B8E2F00B6C99B /* objpack.1 */, + E404C8A4136B901400B6C99B /* KROBJPacker.cpp */, + E404C8A5136B901400B6C99B /* KROBJPacker.h */, + E404C8A7136B919F00B6C99B /* KRVector3.cpp */, + E404C8A8136B919F00B6C99B /* KRVector3.h */, + ); + path = objpack; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + E404C897136B8E2F00B6C99B /* objpack */ = { + isa = PBXNativeTarget; + buildConfigurationList = E404C8A1136B8E3000B6C99B /* Build configuration list for PBXNativeTarget "objpack" */; + buildPhases = ( + E404C894136B8E2F00B6C99B /* Sources */, + E404C895136B8E2F00B6C99B /* Frameworks */, + E404C896136B8E2F00B6C99B /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = objpack; + productName = objpack; + productReference = E404C898136B8E2F00B6C99B /* objpack */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + E404C88F136B8E2F00B6C99B /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0420; + ORGANIZATIONNAME = "Kearwood Software"; + }; + buildConfigurationList = E404C892136B8E2F00B6C99B /* Build configuration list for PBXProject "objpack" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = E404C88D136B8E2F00B6C99B; + productRefGroup = E404C899136B8E2F00B6C99B /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + E404C897136B8E2F00B6C99B /* objpack */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + E404C894136B8E2F00B6C99B /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + E404C89D136B8E2F00B6C99B /* main.cpp in Sources */, + E404C8A6136B901400B6C99B /* KROBJPacker.cpp in Sources */, + E404C8A9136B919F00B6C99B /* KRVector3.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + E404C89F136B8E3000B6C99B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ARCHS = "$(ARCHS_STANDARD_32_64_BIT)"; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = DEBUG; + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_VERSION = com.apple.compilers.llvm.clang.1_0; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.6; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + E404C8A0136B8E3000B6C99B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ARCHS = "$(ARCHS_STANDARD_32_64_BIT)"; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_VERSION = com.apple.compilers.llvm.clang.1_0; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.6; + SDKROOT = macosx; + }; + name = Release; + }; + E404C8A2136B8E3000B6C99B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + COPY_PHASE_STRIP = NO; + GCC_DYNAMIC_NO_PIC = NO; + GCC_ENABLE_OBJC_EXCEPTIONS = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + E404C8A3136B8E3000B6C99B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + COPY_PHASE_STRIP = YES; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + GCC_ENABLE_OBJC_EXCEPTIONS = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + E404C892136B8E2F00B6C99B /* Build configuration list for PBXProject "objpack" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + E404C89F136B8E3000B6C99B /* Debug */, + E404C8A0136B8E3000B6C99B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + E404C8A1136B8E3000B6C99B /* Build configuration list for PBXNativeTarget "objpack" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + E404C8A2136B8E3000B6C99B /* Debug */, + E404C8A3136B8E3000B6C99B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = E404C88F136B8E2F00B6C99B /* Project object */; +} diff --git a/objpack/objpack.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/objpack/objpack.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..6f68d8a --- /dev/null +++ b/objpack/objpack.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/objpack/objpack/KROBJPacker.cpp b/objpack/objpack/KROBJPacker.cpp new file mode 100644 index 0000000..dcef483 --- /dev/null +++ b/objpack/objpack/KROBJPacker.cpp @@ -0,0 +1,491 @@ +// +// objpacker.cpp +// objpack +// +// Created by Kearwood Gilbert on 11-04-29. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#import +#include +#include +#include +#include +#include +#include +#include +#include + +#include "KROBJPacker.h" + +#include "KRVector3.h" + +KROBJPacker::KROBJPacker() { + +} + +KROBJPacker::~KROBJPacker() { + +} + +void KROBJPacker::pack(const char *szPath) { + int fdFile = 0; + int fileSize = 0; + void *pFile = NULL; + char szSymbol[16][64]; + + std::vector materials; + + Vertex3D *pVertices = NULL; + Vector3D *pNormals = NULL; + TexCoord *pTexCoords = NULL; + int *pFaces = NULL; + + vector m_materials; + VertexData *m_pVertexData = NULL; + + pack_header header; + strcpy(header.szTag, "KROBJPACK1.0 "); + + struct stat statbuf; + fdFile = open(szPath, O_RDONLY); + if(fdFile >= 0) { + if(fstat(fdFile, &statbuf) >= 0) { + if ((pFile = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fdFile, 0)) == (caddr_t) -1) { + } else { + fileSize = statbuf.st_size; + + // Pass 1 - Get counts + cout << " Pass 1 - Getting Counts\n"; + + int cVertices = 0; + int cNormals = 0; + int cTexCoords = 0; + int cVertexData = 0; + + + cVertices = 0; + int cFaces = 1; + int cMaterialFaceStart = 1; + + char *pScan = (char *)pFile; + char *pEnd = (char *)pFile + fileSize; + while(pScan < pEnd) { + + // Scan through whitespace + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t' || *pScan == '\r' || *pScan == '\n')) { + pScan++; + } + + if(*pScan == '#') { + // Line is a comment line + + // Scan to the end of the line + while(pScan < pEnd && *pScan != '\r' && *pScan != '\n') { + pScan++; + } + } else { + int cSymbols = 0; + while(pScan < pEnd && *pScan != '\n' && *pScan != '\r') { + + char *pDest = szSymbol[cSymbols++]; + while(pScan < pEnd && *pScan != ' ' && *pScan != '\n' && *pScan != '\r') { + *pDest++ = *pScan++; + } + *pDest = '\0'; + + // Scan through whitespace, but don't advance to next line + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t')) { + pScan++; + } + } + + if(strcmp(szSymbol[0], "v") == 0) { + // Vertex (v) + cVertices++; + } else if(strcmp(szSymbol[0], "vt") == 0) { + // Vertex Texture UV Coordinate (vt) + cTexCoords++; + } else if(strcmp(szSymbol[0], "vn") == 0) { + // Vertex Normal (vn) + cNormals++; + } else if(strcmp(szSymbol[0], "f") == 0) { + // Face (f) + int cFaceVertexes = (cSymbols - 3) * 3; // 3 vertexes per triangle. Triangles have 4 symbols. Quads have 5 symbols and generate two triangles. + cVertexData += cFaceVertexes; + cFaces += cFaceVertexes * 3 + 1; // Allocate space for count of vertices, Vertex Index, Texture Coordinate Index, and Normal Index + + } else if(strcmp(szSymbol[0], "usemtl") == 0) { + // Use Material (usemtl) + if(cMaterialFaceStart - cFaces > 0) { + cFaces++; + + } + materials.push_back(std::string(szSymbol[1])); + } + + } + } + + + // Pass 2 - Populate vertexes and faces + cout << " Pass 2 - Populate vertexes and faces\n"; + + Vertex3D *pVertices = (Vertex3D *)malloc(sizeof(Vertex3D) * cVertices); + Vector3D *pNormals = (Vector3D *)malloc(sizeof(Vector3D) *cNormals); + TexCoord *pTexCoords = (TexCoord *)malloc(sizeof(TexCoord) * cTexCoords); + int *pFaces = (int *)malloc(sizeof(int *) * (cFaces + 1)); + + + Vertex3D *pVertice = pVertices; + Vector3D *pNormal = pNormals; + TexCoord *pTexCoord = pTexCoords; + int *pFace = pFaces; + int *pMaterialFaces = pFace++; + *pMaterialFaces = 0; + + + std::vector::iterator material_itr = materials.begin(); + + // -------- + + pScan = (char *)pFile; + while(pScan < pEnd) { + + // Scan through whitespace + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t' || *pScan == '\r' || *pScan == '\n')) { + pScan++; + } + + if(*pScan == '#') { + // Line is a comment line + + // Scan to the end of the line + while(pScan < pEnd && *pScan != '\r' && *pScan != '\n') { + pScan++; + } + } else { + int cSymbols = 0; + while(pScan < pEnd && *pScan != '\n' && *pScan != '\r') { + + char *pDest = szSymbol[cSymbols++]; + while(pScan < pEnd && *pScan != ' ' && *pScan != '\n' && *pScan != '\r') { + *pDest++ = *pScan++; + } + *pDest = '\0'; + + // Scan through whitespace, but don't advance to next line + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t')) { + pScan++; + } + } + + if(strcmp(szSymbol[0], "v") == 0) { + // Vertex (v) + char *pChar = szSymbol[1]; + pVertice -> x = strtof(pChar, &pChar); + pChar = szSymbol[2]; + pVertice -> y = strtof(pChar, &pChar); + pChar = szSymbol[3]; + pVertice -> z = strtof(pChar, &pChar); + pVertice++; + } else if(strcmp(szSymbol[0], "vt") == 0) { + // Vertex Texture UV Coordinate (vt) + char *pChar = szSymbol[1]; + pTexCoord -> u = strtof(pChar, &pChar); + pChar = szSymbol[2]; + pTexCoord -> v = strtof(pChar, &pChar); + pTexCoord++; + } else if(strcmp(szSymbol[0], "vn") == 0) { + // Vertex Normal (vn) + char *pChar = szSymbol[1]; + pNormal -> x = strtof(pChar, &pChar); + pChar = szSymbol[2]; + pNormal -> y = strtof(pChar, &pChar); + pChar = szSymbol[3]; + pNormal -> z = strtof(pChar, &pChar); + pNormal++; + } else if(strcmp(szSymbol[0], "f") == 0) { + // Face (f) + int cFaceVertices = cSymbols - 1; + + *pFace++ = cFaceVertices; + for(int iSymbol=1; iSymbol < cSymbols; iSymbol++) { + char *pChar = szSymbol[iSymbol]; + if(*pChar == '.' || (*pChar >= '0' && *pChar <= '9')) { + *pFace++ = strtol(pChar, &pChar, 10) - 1; // Vertex Index + + if(*pChar == '/') { + pChar++; + if(*pChar == '/') { + *pFace++ = -1; + } else { + *pFace++ = strtol(pChar, &pChar, 10) - 1; // Texture Coordinate Index + } + } else { + *pFace++ = -1; + } + + if(*pChar == '/') { + pChar++; + if(*pChar == '/') { + *pFace++ = -1; + } else { + *pFace++ = strtol(pChar, &pChar, 10) - 1; // Normal Index + } + } else { + *pFace++ = -1; + } + while(*pChar == '/') { + pChar++; + strtol(pChar, &pChar, 10); + } + } + } + + + } else if(strcmp(szSymbol[0], "usemtl") == 0) { + // Use Material (usemtl) + if(pFace - pMaterialFaces > 1) { + *pMaterialFaces = pFace - pMaterialFaces - 1; + pMaterialFaces = pFace++; + } + } + } + } + + + *pMaterialFaces = pFace - pMaterialFaces - 1; + *pFace++ = 0; + + + m_pVertexData = (VertexData *)malloc(sizeof(VertexData) * cVertexData); + + VertexData *pData = m_pVertexData; + + int iVertex = 0; + pack_material *pMaterial = new pack_material(); + pMaterial->start_vertex = iVertex; + pMaterial->vertex_count = 0; + memset(pMaterial->szName, 64, 0); + if(material_itr < materials.end()) { + strncpy(pMaterial->szName, (*material_itr++).c_str(), 64); + } + m_materials.push_back(pMaterial); + + + pFace = pFaces; + while(*pFace != 0 && iVertex < cVertexData) { + pMaterial->start_vertex = iVertex; + + int *pMaterialEndFace = pFace + *pFace++; + while(pFace < pMaterialEndFace && iVertex < cVertexData) { + int cFaceVertexes = *pFace; + VertexData *pFirstFaceVertex = NULL; + VertexData *pPrevFaceVertex = NULL; + for(int iFaceVertex=0; iFaceVertex < cFaceVertexes; iFaceVertex++) { + if(iFaceVertex > 2) { + // There have already been 3 vertices. Now we need to split the quad into a second triangle composed of the 1st, 3rd, and 4th vertices + memcpy(pData++, pFirstFaceVertex, sizeof(VertexData)); + memcpy(pData++, pPrevFaceVertex, sizeof(VertexData)); + iVertex+=2; + } + Vertex3D *pVertex = pVertices + pFace[iFaceVertex*3+1]; + if(iFaceVertex==0) { + pFirstFaceVertex = pData; + } + pPrevFaceVertex = pData; + pData->vertex.x = pVertex -> x; + pData->vertex.y = pVertex -> y; + pData->vertex.z = pVertex -> z; + + if(pFace[iFaceVertex*3+2] >= 0) { + TexCoord *pTexCoord = pTexCoords + pFace[iFaceVertex*3+2]; + pData->texcoord.u = pTexCoord -> u; + pData->texcoord.v = pTexCoord -> v; + } else { + pData->texcoord.u = 0; + pData->texcoord.v = 0; + } + + if(pFace[iFaceVertex*3+3] >= 0){ + Vector3D *pNormal = pNormals + pFace[iFaceVertex*3+3]; + pData->normal.x = pNormal -> x; + pData->normal.y = pNormal -> y; + pData->normal.z = pNormal -> z; + } else { + pData->normal.x = 0; + pData->normal.y = 0; + pData->normal.z = 0; + } + + pData++; + iVertex++; + } + pFace += cFaceVertexes * 3 + 1; + } + pMaterial->vertex_count = iVertex - pMaterial->start_vertex; + if(*pFace != 0) { + pMaterial = new pack_material(); + pMaterial->start_vertex = iVertex; + pMaterial->vertex_count = 0; + memset(pMaterial->szName, 64, 0); + + if(material_itr < materials.end()) { + strncpy(pMaterial->szName, (*material_itr++).c_str(), 64); + } + m_materials.push_back(pMaterial); + } + } + + + header.minx = 0.0; + header.miny = 0.0; + header.minz = 0.0; + header.maxx = 0.0; + header.maxy = 0.0; + header.maxz = 0.0; + + // Calculate surface normals and tangents + cout << " Pass 3 - Calculate surface normals and tangents\n"; + // http://www.terathon.com/code/tangent.html + // http://www.fabiensanglard.net/bumpMapping/index.php + + for(std::vector::iterator itr = m_materials.begin(); itr != m_materials.end(); itr++) { + VertexData *pStart = m_pVertexData + (*itr)->start_vertex; + VertexData *pEnd = pStart + (*itr)->vertex_count; + for(VertexData *pVertex = pStart; pVertex < pEnd; pVertex+=3) { + if(pVertex->vertex.x < header.minx) header.minx = pVertex->vertex.x; + if(pVertex->vertex.x > header.maxx) header.maxx = pVertex->vertex.x; + if(pVertex->vertex.y < header.miny) header.miny = pVertex->vertex.y; + if(pVertex->vertex.y > header.maxy) header.maxy = pVertex->vertex.y; + if(pVertex->vertex.z < header.minz) header.minz = pVertex->vertex.z; + if(pVertex->vertex.z > header.maxz) header.maxz = pVertex->vertex.z; + } + + + for(VertexData *pVertex = pStart; pVertex < pEnd; pVertex+=3) { + Vector3 p1(pVertex[0].vertex.x, pVertex[0].vertex.y, pVertex[0].vertex.z); + Vector3 p2(pVertex[1].vertex.x, pVertex[1].vertex.y, pVertex[1].vertex.z); + Vector3 p3(pVertex[2].vertex.x, pVertex[2].vertex.y, pVertex[2].vertex.z); + Vector3 v1 = p2 - p1; + Vector3 v2 = p3 - p1; + + // -- Calculate normal -- + if(pVertex->normal.x == 0 && pVertex->normal.y == 0 && pVertex->normal.z == 0) { + + + Vector3 normal = v1.cross( v2 ); + + normal.normalize(); + + pVertex[0].normal.x = normal.x; + pVertex[0].normal.y = normal.y; + pVertex[0].normal.z = normal.z; + + pVertex[1].normal.x = normal.x; + pVertex[1].normal.y = normal.y; + pVertex[1].normal.z = normal.z; + + pVertex[2].normal.x = normal.x; + pVertex[2].normal.y = normal.y; + pVertex[2].normal.z = normal.z; + } + + // -- Calculate tangent -- + TexCoord st1; // = pVertex[2].texcoord; + TexCoord st2; // = pVertex[1].texcoord; + st1.u = pVertex[1].texcoord.u - pVertex[0].texcoord.u; + st1.v = pVertex[1].texcoord.v - pVertex[0].texcoord.v; + st2.u = pVertex[2].texcoord.u - pVertex[0].texcoord.u; + st2.v = pVertex[2].texcoord.v - pVertex[0].texcoord.v; + double coef = 1/ (st1.u * st2.v - st2.u * st1.v); + + pVertex[0].tangent.x = coef * ((v1.x * st2.v) + (v2.x * -st1.v)); + pVertex[0].tangent.y = coef * ((v1.y * st2.v) + (v2.y * -st1.v)); + pVertex[0].tangent.z = coef * ((v1.z * st2.v) + (v2.z * -st1.v)); + + Vector3 tangent( + coef * ((v1.x * st2.v) + (v2.x * -st1.v)), + coef * ((v1.y * st2.v) + (v2.y * -st1.v)), + coef * ((v1.z * st2.v) + (v2.z * -st1.v)) + ); + + tangent.normalize(); + + pVertex[0].tangent.x = tangent.x; + pVertex[0].tangent.y = tangent.y; + pVertex[0].tangent.z = tangent.z; + pVertex[1].tangent.x = tangent.x; + pVertex[1].tangent.y = tangent.y; + pVertex[1].tangent.z = tangent.z; + pVertex[2].tangent.x = tangent.x; + pVertex[2].tangent.y = tangent.y; + pVertex[2].tangent.z = tangent.z; + + + } + + + } + + // Write output file + std::string out_file_name = szPath; + out_file_name.append(".pack"); + cout << " Writing obj.pack file:" << out_file_name << " ... \n"; + FILE *out_file = fopen(out_file_name.c_str(), "w"); + + header.material_count = 0; + for(std::vector::iterator itr = m_materials.begin(); itr != m_materials.end(); itr++) { + pack_material *pMaterial = (*itr); + if(pMaterial->vertex_count) { // Skip materials with no vertices + header.material_count++; + } + } + + header.vertex_count = cVertexData; + + fwrite(&header, sizeof(header), 1, out_file); + + for(std::vector::iterator itr = m_materials.begin(); itr != m_materials.end(); itr++) { + pack_material *pMaterial = (*itr); + if(pMaterial->vertex_count) { // Skip materials with no vertices + fwrite(pMaterial, sizeof(pack_material), 1, out_file); + cout << " " << pMaterial->szName << ": " << pMaterial->vertex_count << " vertices\n"; + } + } + + + fwrite(m_pVertexData, sizeof(VertexData), cVertexData, out_file); + + fclose(out_file); + + } + } + } + + if(pFile != NULL) { + munmap(pFile, fileSize); + } + + if(fdFile != 0) { + close(fdFile); + } + + if(m_pVertexData) { + free(m_pVertexData); + } + + if(pVertices) { + free(pVertices); + } + if(pNormals) { + free(pNormals); + } + if(pTexCoords) { + free(pTexCoords); + } + if(pFaces) { + free(pFaces); + } +} diff --git a/objpack/objpack/KROBJPacker.h b/objpack/objpack/KROBJPacker.h new file mode 100644 index 0000000..3e762e0 --- /dev/null +++ b/objpack/objpack/KROBJPacker.h @@ -0,0 +1,55 @@ +// +// objpacker.h +// objpack +// +// Created by Kearwood Gilbert on 11-04-29. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#import +#import + +#ifndef OBJPACKER_H +#define OBJPACKER_H + + +class KROBJPacker { +public: + KROBJPacker(); + ~KROBJPacker(); + void pack(const char *szPath); + +private: + typedef struct { + char szTag[16]; + float minx, miny, minz, maxx, maxy, maxz; + int32_t vertex_count; + int32_t material_count; + } pack_header; + + typedef struct { + int32_t start_vertex; + int32_t vertex_count; + char szName[64]; + } pack_material; + + typedef struct { + float x; + float y; + float z; + } Vertex3D, Vector3D; + + typedef struct { + float u; + float v; + } TexCoord; + + typedef struct { + Vertex3D vertex; + Vector3D normal; + Vector3D tangent; + TexCoord texcoord; + } VertexData; +}; + +#endif // OBJPACKER_H \ No newline at end of file diff --git a/objpack/objpack/KRVector3.cpp b/objpack/objpack/KRVector3.cpp new file mode 100644 index 0000000..6f122cc --- /dev/null +++ b/objpack/objpack/KRVector3.cpp @@ -0,0 +1,76 @@ +// +// KRVector3.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-12-31. +// Copyright 2010 Kearwood Software. All rights reserved. +// + +#include "KRVector3.h" + +//default constructor +Vector3::Vector3(float X = 0, float Y = 0, float Z = 0) +{ + x = X; + y = Y; + z = Z; +} + +Vector3::~Vector3() +{ +} + +//calculate and return the magnitude of this vector +float Vector3::GetMagnitude() +{ + return sqrtf(x * x + y * y + z * z); +} + +//multiply this vector by a scalar +Vector3 Vector3::operator*(float num) const +{ + return Vector3(x * num, y * num, z * num); +} + +//pass in a vector, pass in a scalar, return the product +/* + Vector3 Vector3::operator*(float num, Vector3 const &vec) + { + return Vector3(vec.x * num, vec.y * num, vec.z * num); + } + */ + +//add two vectors +Vector3 Vector3::operator+(const Vector3 &vec) const +{ + return Vector3(x + vec.x, y + vec.y, z + vec.z); +} + +//subtract two vectors +Vector3 Vector3::operator-(const Vector3 &vec) const +{ + return Vector3(x - vec.x, y - vec.y, z - vec.z); +} + +//normalize this vector +void Vector3::normalize() +{ + float magnitude = sqrtf(x * x + y * y + z * z); + x /= magnitude; + y /= magnitude; + z /= magnitude; +} + +//calculate and return dot product +float Vector3::dot(const Vector3 &vec) const +{ + return x * vec.x + y * vec.y + z * vec.z; +} + +//calculate and return cross product +Vector3 Vector3::cross(const Vector3 &vec) const +{ + return Vector3(y * vec.z - z * vec.y, + z * vec.x - x * vec.z, + x * vec.y - y * vec.x); +} diff --git a/objpack/objpack/KRVector3.h b/objpack/objpack/KRVector3.h new file mode 100644 index 0000000..2e947a5 --- /dev/null +++ b/objpack/objpack/KRVector3.h @@ -0,0 +1,49 @@ +// +// KRVector3.h +// gldemo +// +// Created by Kearwood Gilbert on 10-12-31. +// Copyright 2010 Kearwood Software. All rights reserved. +// + +#ifndef KRVECTOR3 +#define KRVECTOR3 +#include + +class Vector3 +{ + +public: + float x, y, z; + + //default constructor + + Vector3(float X, float Y, float Z); + ~Vector3(); + + //calculate and return the magnitude of this vector + float GetMagnitude(); + + //multiply this vector by a scalar + Vector3 operator*(float num) const; + + //pass in a vector, pass in a scalar, return the product + //friend Vector3 operator*(float num, Vector3 const &vec); + + //add two vectors + Vector3 operator+(const Vector3 &vec) const; + + //subtract two vectors + Vector3 operator-(const Vector3 &vec) const; + + //normalize this vector + void normalize(); + + //calculate and return dot product + float dot(const Vector3 &vec) const; + + //calculate and return cross product + Vector3 cross(const Vector3 &vec) const; +}; + +#endif diff --git a/objpack/objpack/main.cpp b/objpack/objpack/main.cpp new file mode 100644 index 0000000..d74bc01 --- /dev/null +++ b/objpack/objpack/main.cpp @@ -0,0 +1,29 @@ +// +// main.cpp +// objpack +// +// Created by Kearwood Gilbert on 11-04-29. +// Copyright 2011 Kearwood Software. All rights reserved. +// + +#include + +#import "KROBJPacker.h" + +int main (int argc, const char * argv[]) +{ + + if(argc < 2) { + std::cout << "You must pass an .obj file as a parameter. An .obj.pack file will be written for each .obj file.\n"; + } else { + KROBJPacker p; + for(int i=1; i < argc; i++) { + std::cout << "Packing " << argv[i] << " ...\n"; + p.pack(argv[i]); + } + std::cout << "Done.\n"; + } + + return 0; +} + diff --git a/objpack/objpack/objpack.1 b/objpack/objpack/objpack.1 new file mode 100644 index 0000000..ba9cc29 --- /dev/null +++ b/objpack/objpack/objpack.1 @@ -0,0 +1,79 @@ +.\"Modified from man(1) of FreeBSD, the NetBSD mdoc.template, and mdoc.samples. +.\"See Also: +.\"man mdoc.samples for a complete listing of options +.\"man mdoc for the short list of editing options +.\"/usr/share/misc/mdoc.template +.Dd 11-04-29 \" DATE +.Dt objpack 1 \" Program name and manual section number +.Os Darwin +.Sh NAME \" Section Header - required - don't modify +.Nm objpack, +.\" The following lines are read in generating the apropos(man -k) database. Use only key +.\" words here as the database is built based on the words here and in the .ND line. +.Nm Other_name_for_same_program(), +.Nm Yet another name for the same program. +.\" Use .Nm macro to designate other names for the documented program. +.Nd This line parsed for whatis database. +.Sh SYNOPSIS \" Section Header - required - don't modify +.Nm +.Op Fl abcd \" [-abcd] +.Op Fl a Ar path \" [-a path] +.Op Ar file \" [file] +.Op Ar \" [file ...] +.Ar arg0 \" Underlined argument - use .Ar anywhere to underline +arg2 ... \" Arguments +.Sh DESCRIPTION \" Section Header - required - don't modify +Use the .Nm macro to refer to your program throughout the man page like such: +.Nm +Underlining is accomplished with the .Ar macro like this: +.Ar underlined text . +.Pp \" Inserts a space +A list of items with descriptions: +.Bl -tag -width -indent \" Begins a tagged list +.It item a \" Each item preceded by .It macro +Description of item a +.It item b +Description of item b +.El \" Ends the list +.Pp +A list of flags and their descriptions: +.Bl -tag -width -indent \" Differs from above in tag removed +.It Fl a \"-a flag as a list item +Description of -a flag +.It Fl b +Description of -b flag +.El \" Ends the list +.Pp +.\" .Sh ENVIRONMENT \" May not be needed +.\" .Bl -tag -width "ENV_VAR_1" -indent \" ENV_VAR_1 is width of the string ENV_VAR_1 +.\" .It Ev ENV_VAR_1 +.\" Description of ENV_VAR_1 +.\" .It Ev ENV_VAR_2 +.\" Description of ENV_VAR_2 +.\" .El +.Sh FILES \" File used or created by the topic of the man page +.Bl -tag -width "/Users/joeuser/Library/really_long_file_name" -compact +.It Pa /usr/share/file_name +FILE_1 description +.It Pa /Users/joeuser/Library/really_long_file_name +FILE_2 description +.El \" Ends the list +.\" .Sh DIAGNOSTICS \" May not be needed +.\" .Bl -diag +.\" .It Diagnostic Tag +.\" Diagnostic informtion here. +.\" .It Diagnostic Tag +.\" Diagnostic informtion here. +.\" .El +.Sh SEE ALSO +.\" List links in ascending order by section, alphabetically within a section. +.\" Please do not reference files that do not exist without filing a bug report +.Xr a 1 , +.Xr b 1 , +.Xr c 1 , +.Xr a 2 , +.Xr b 2 , +.Xr a 3 , +.Xr b 3 +.\" .Sh BUGS \" Document known, unremedied bugs +.\" .Sh HISTORY \" Document history if command behaves in a unique manner \ No newline at end of file diff --git a/objview/Classes/KRObjViewAppDelegate.h b/objview/Classes/KRObjViewAppDelegate.h new file mode 100644 index 0000000..d88b15f --- /dev/null +++ b/objview/Classes/KRObjViewAppDelegate.h @@ -0,0 +1,21 @@ +// +// KRObjViewAppDelegate.h +// KRObjView +// +// Created by Mac on 11-04-29. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import + +@class KRObjViewViewController; + +@interface KRObjViewAppDelegate : NSObject { + UIWindow *window; + KRObjViewViewController *viewController; +} + +@property (nonatomic, retain) IBOutlet UIWindow *window; +@property (nonatomic, retain) IBOutlet KRObjViewViewController *viewController; + +@end diff --git a/objview/Classes/KRObjViewAppDelegate.mm b/objview/Classes/KRObjViewAppDelegate.mm new file mode 100644 index 0000000..ec2bb42 --- /dev/null +++ b/objview/Classes/KRObjViewAppDelegate.mm @@ -0,0 +1,34 @@ +// +// KRObjViewAppDelegate.m +// KRObjView +// +// Created by Mac on 11-04-29. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "KRObjViewAppDelegate.h" +#import "KRObjViewViewController.h" + +@implementation KRObjViewAppDelegate + +@synthesize window; +@synthesize viewController; + +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + + // Add the view controller's view to the window and display. + [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade]; + [self.window addSubview:viewController.view]; + [self.window makeKeyAndVisible]; + + return YES; +} + +- (void)dealloc { + [viewController release]; + [window release]; + [super dealloc]; +} + +@end diff --git a/objview/Classes/KRObjViewGLView.h b/objview/Classes/KRObjViewGLView.h new file mode 100644 index 0000000..3e00c8a --- /dev/null +++ b/objview/Classes/KRObjViewGLView.h @@ -0,0 +1,45 @@ +// +// KRObjViewGLView.h +// KRObjView +// +// Created by Mac on 11-05-01. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import + +#import +#import +#import + +#import +#import +#import + +@interface KRObjViewGLView : UIView { + + /* The pixel dimensions of the backbuffer */ + GLint backingWidth, backingHeight; + + EAGLContext *context; + + /* OpenGL names for the renderbuffer and framebuffers used to render to this view */ + GLuint viewFramebuffer, viewRenderbuffer; + + KREngine *renderEngine; + + KRScene m_scene; + +} + +// OpenGL drawing +- (BOOL)createFramebuffers; +- (void)destroyFramebuffer; +- (void)setDisplayFramebuffer; +- (BOOL)presentFramebuffer; +- (KREngine *)getEngine; +- (KRScene *)getScene; +- (BOOL)loadObjects; + + +@end diff --git a/objview/Classes/KRObjViewGLView.mm b/objview/Classes/KRObjViewGLView.mm new file mode 100644 index 0000000..50f900e --- /dev/null +++ b/objview/Classes/KRObjViewGLView.mm @@ -0,0 +1,217 @@ +// +// KRObjViewGLView.m +// KRObjView +// +// Created by Mac on 11-05-01. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "KRObjViewGLView.h" + +#import +#import + +@implementation KRObjViewGLView + +// Override the class method to return the OpenGL layer, as opposed to the normal CALayer ++ (Class) layerClass +{ + return [CAEAGLLayer class]; +} + +- (id)initWithFrame:(CGRect)frame { + + self = [super initWithFrame:frame]; + if (self) { + + // Do OpenGL Core Animation layer setup + CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; + + // Set scaling to account for Retina display + if ([self respondsToSelector:@selector(setContentScaleFactor:)]) + { + self.contentScaleFactor = [[UIScreen mainScreen] scale]; + } + + eaglLayer.opaque = YES; + eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; + context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; + + if (!context) + { + [self release]; + return nil; + } + + if (![EAGLContext setCurrentContext:context]) + { + [self release]; + return nil; + } + + if (![self createFramebuffers]) + { + [self release]; + return nil; + } + + // Initialize KREngine + renderEngine = [[KREngine alloc] initForWidth: backingWidth Height: backingHeight]; + [self loadObjects]; + } + return self; +} + +- (void)dealloc { + if(renderEngine) { + [renderEngine release]; + renderEngine = nil; + } + + [super dealloc]; +} + +#pragma mark - +#pragma mark OpenGL drawing + +- (BOOL)loadObjects +{ + NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; + NSFileManager* fileManager = [NSFileManager defaultManager]; + + for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { + if([fileName hasSuffix: @".pvr"]) { + NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; + [renderEngine loadResource: path]; + } + } + + for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { + if([fileName hasSuffix: @".mtl"]) { + NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; + [renderEngine loadResource: path]; + } + } + + for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { + if([fileName hasSuffix: @".krobject"]) { + NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; + [renderEngine loadResource: path]; + } + } + + KRModelManager *pModelManager = [renderEngine getModelManager]; + m_scene.addInstance(pModelManager->getModel("fachwerkhaus12"), KRMat4()); + + [renderEngine setNearZ: 25.0]; + [renderEngine setFarZ: 5000.0]; + /* + + startPos 156.0 -55.0 -825.0 + touchScale 95.0 + nearZ 25.0 + farZ 5000.0 + + */ + + // [renderEngine setParameterValueWithName: @]; + return TRUE; +} + +- (BOOL)createFramebuffers +{ + + // ===== Create onscreen framebuffer object ===== + glGenFramebuffers(1, &viewFramebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer); + + // ----- Create color buffer for viewFramebuffer ----- + glGenRenderbuffers(1, &viewRenderbuffer); + glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer); + [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer]; + glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth); + glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight); + NSLog(@"Backing width: %d, height: %d", backingWidth, backingHeight); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, viewRenderbuffer); + + + + if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + NSLog(@"Failure with depth buffer generation"); + return NO; + } + + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if (status != GL_FRAMEBUFFER_COMPLETE) { + NSLog(@"Incomplete FBO: %d", status); + exit(1); + } + + + return TRUE; +} + +- (void)destroyFramebuffer; +{ + if (viewFramebuffer) + { + glDeleteFramebuffers(1, &viewFramebuffer); + viewFramebuffer = 0; + } + + if (viewRenderbuffer) + { + glDeleteRenderbuffers(1, &viewRenderbuffer); + viewRenderbuffer = 0; + } +} + +- (void)setDisplayFramebuffer; +{ + if (context) + { + if (!viewFramebuffer) + { + [self createFramebuffers]; + } + + glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer); + + glViewport(0, 0, backingWidth, backingHeight); + + /* + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + */ + } +} + +- (BOOL)presentFramebuffer; +{ + BOOL success = FALSE; + + if (context) + { + glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer); + + success = [context presentRenderbuffer:GL_RENDERBUFFER]; + } + + return success; +} + +- (KREngine *)getEngine; +{ + return renderEngine; +} + +- (KRScene *)getScene; +{ + return &m_scene; +} + +#pragma mark - +#pragma mark Accessors + + +@end diff --git a/objview/Classes/KRObjViewViewController.h b/objview/Classes/KRObjViewViewController.h new file mode 100644 index 0000000..7a4ac82 --- /dev/null +++ b/objview/Classes/KRObjViewViewController.h @@ -0,0 +1,54 @@ +// +// KRObjViewViewController.h +// KRObjView +// +// Created by Mac on 11-04-29. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "KRObjViewGLView.h" +#import +#import +#import + +@interface KRObjViewViewController : UIViewController { + + CALayer *overlayLayer; + + KRObjViewGLView *glView; + + float heading; + + Vector3 camera_position; + double camera_pitch; + double camera_yaw; + + double leftStickStartX; + double leftStickStartY; + double rightStickStartX; + double rightStickStartY; + + double leftStickDeltaX; + double leftStickDeltaY; + double rightStickDeltaX; + double rightStickDeltaY; + double dLeftSlider; + double dRightSlider; + + bool bUpdateParam; + bool bLoadedTestInstances; + int cParamDisplayFrames; + + + id displayLink; + +} + +@property (nonatomic, retain) IBOutlet CALayer *overlayLayer; +@property (readonly) KRObjViewGLView *glView; + +// OpenGL ES 2.0 setup methods +- (void)drawView:(id)sender; + +@end diff --git a/objview/Classes/KRObjViewViewController.mm b/objview/Classes/KRObjViewViewController.mm new file mode 100644 index 0000000..8a68258 --- /dev/null +++ b/objview/Classes/KRObjViewViewController.mm @@ -0,0 +1,257 @@ +// +// KRObjViewViewController.m +// KRObjView +// +// Created by Mac on 11-04-29. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "KRObjViewViewController.h" +#import +#import + +@implementation KRObjViewViewController + +@synthesize overlayLayer; +@synthesize glView; + +// Handle Touch Events +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + for(id touch in touches) { + CGPoint touchPoint = [touch locationInView:self.view]; + if(!leftStickStartY && !rightStickStartY && touchPoint.y < CGRectGetMinY(self.view.frame) + CGRectGetHeight(self.view.frame) * 0.05) { + dRightSlider = (touchPoint.x - CGRectGetMinX(self.view.frame)) / CGRectGetWidth(self.view.frame); + cParamDisplayFrames = 30; + bUpdateParam = true; + } else if(!leftStickStartY && !rightStickStartY && touchPoint.y > CGRectGetMinY(self.view.frame) + CGRectGetHeight(self.view.frame) * 0.95) { + dLeftSlider = (touchPoint.x - CGRectGetMinX(self.view.frame)) / CGRectGetWidth(self.view.frame); + cParamDisplayFrames = 30; + } else if(touchPoint.y > CGRectGetMidY(self.view.frame)) { + leftStickStartX = touchPoint.x; + leftStickStartY = touchPoint.y; + leftStickDeltaX = 0.0f; + leftStickDeltaY = 0.0f; + // NSLog(@"Left Stick Pressed"); + } else { + rightStickStartX = touchPoint.x; + rightStickStartY = touchPoint.y; + rightStickDeltaX = 0.0f; + rightStickDeltaY = 0.0f; + // NSLog(@"Right Stick Pressed"); + } + } + +} + +- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { + for(id touch in touches) { + CGPoint touchPoint = [touch locationInView:self.view]; + if(!leftStickStartY && !rightStickStartY && touchPoint.y < CGRectGetMinY(self.view.frame) + CGRectGetHeight(self.view.frame) * 0.10) { + dRightSlider = (touchPoint.x - CGRectGetMinX(self.view.frame)) / CGRectGetWidth(self.view.frame); + cParamDisplayFrames = 30; + bUpdateParam = true; + } else if(!leftStickStartY && !rightStickStartY && touchPoint.y > CGRectGetMinY(self.view.frame) + CGRectGetHeight(self.view.frame) * 0.90) { + dLeftSlider = (touchPoint.x - CGRectGetMinX(self.view.frame)) / CGRectGetWidth(self.view.frame); + cParamDisplayFrames = 30; + } else if(touchPoint.y > CGRectGetMidY(self.view.frame)) { + if(leftStickStartX > 0.0f) { // Avoid interpreting touches sliding across center of screen + leftStickDeltaX = (leftStickStartX - touchPoint.x) / (CGRectGetWidth(self.view.frame) * 0.25); + leftStickDeltaY = (leftStickStartY - touchPoint.y) / (CGRectGetHeight(self.view.frame) * 0.25); + // clamp values + if(leftStickDeltaX < -1.0f) { + leftStickDeltaX = -1.0f; + } else if(leftStickDeltaX > 1.0f) { + leftStickDeltaX = 1.0f; + } + if(leftStickDeltaY < -1.0f) { + leftStickDeltaY = -1.0f; + } else if(leftStickDeltaY > 1.0f) { + leftStickDeltaY = 1.0f; + } + } + } else { + if(rightStickStartX > 0.0f) { // Avoid interpreting touches sliding across center of screen + rightStickDeltaX = (rightStickStartX - touchPoint.x) / (CGRectGetWidth(self.view.frame) * 0.25); + rightStickDeltaY = (rightStickStartY - touchPoint.y) / (CGRectGetHeight(self.view.frame) * 0.25); + // clamp values + if(rightStickDeltaX < -1.0f) { + rightStickDeltaX = -1.0f; + } else if(rightStickDeltaX > 1.0f) { + rightStickDeltaX = 1.0f; + } + if(rightStickDeltaY < -1.0f) { + rightStickDeltaY = -1.0f; + } else if(rightStickDeltaY > 1.0f) { + rightStickDeltaY = 1.0f; + } + } + } + } +} + +- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { + for(id touch in touches) { + CGPoint touchPoint = [touch locationInView:self.view]; + if(touchPoint.y > CGRectGetMidY(self.view.frame)) { + leftStickStartX = 0.0f; + leftStickStartY = 0.0f; + leftStickDeltaX = 0.0f; + leftStickDeltaY = 0.0f; + } else { + rightStickStartX = 0.0f; + rightStickStartY = 0.0f; + rightStickDeltaX = 0.0f; + rightStickDeltaY = 0.0f; + } + } +} +-(void)loadView { + + CGRect mainScreenFrame = [[UIScreen mainScreen] applicationFrame]; + UIView *primaryView = [[UIView alloc] initWithFrame:mainScreenFrame]; + self.view = primaryView; + [primaryView release]; + + glView = [[KRObjViewGLView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, mainScreenFrame.size.width, mainScreenFrame.size.height)]; + glView.multipleTouchEnabled = YES; + + [self.view addSubview:glView]; + [glView release]; + + camera_yaw = -4.0; + camera_pitch = 0.1; + + leftStickStartX = 0.0f; + leftStickStartY = 0.0f; + rightStickStartX = 0.0f; + rightStickStartY = 0.0f; + leftStickDeltaX = 0.0f; + leftStickDeltaY = 0.0f; + rightStickDeltaX = 0.0f; + rightStickDeltaY = 0.0f; + + + bUpdateParam = false; + dRightSlider = 0.0f; + dLeftSlider = 0.0f; + bLoadedTestInstances = false; + cParamDisplayFrames = 0; + + camera_position = Vector3(-850, -10, -700); + + displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawView:)]; + [displayLink setFrameInterval:1]; // Maximum 60fps + [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; +} + +- (void)dealloc +{ + [displayLink invalidate]; + + [super dealloc]; +} + + +- (void)drawView:(id)sender +{ + const GLfloat PI = 3.14159265; + const GLfloat d2r = PI * 2 / 360; + + + KREngine *engine = [glView getEngine]; + int iParam = int(dLeftSlider * ([engine getParameterCount] + 1)); + if(iParam > [engine getParameterCount]) { + iParam = [engine getParameterCount]; + } + + if(cParamDisplayFrames && iParam < [engine getParameterCount]) { + cParamDisplayFrames--; + char szText[256]; + const char *szName = [[engine getParameterLabelWithIndex: iParam] UTF8String]; + double dValue = [engine getParameterValueWithIndex: iParam]; + switch([engine getParameterTypeWithIndex: iParam]) { + case KRENGINE_PARAMETER_INT: + sprintf(szText, "%s: %i", szName, (int)dValue); + break; + case KRENGINE_PARAMETER_BOOL: + sprintf(szText, "%s: %s", szName, dValue == 0.0 ? "false" : "true"); + break; + case KRENGINE_PARAMETER_FLOAT: + sprintf(szText, "%s: %f", szName, dValue); + break; + } + NSString *debug_text = [[NSString alloc] initWithUTF8String:szText]; + [engine setDebugText: debug_text]; + } else { + [engine setDebugText: @""]; + } + + + + if(bUpdateParam) { + bUpdateParam = false; + if(iParam == [engine getParameterCount] && dRightSlider > 0.9) { + if(!bLoadedTestInstances) { + bLoadedTestInstances = true; + + KRModelManager *pModelManager = [engine getModelManager]; + + KRScene *scene =[glView getScene]; + + srand ( time(NULL) ); + for(int iHouse=1; iHouse < 12; iHouse++) { + for(int iInstance=0; iInstance < 20; iInstance++) { + char szName[100]; + sprintf(szName, "fachwerkhaus%i", iHouse); + KRMat4 pos; + + pos.rotate((double)rand() / (double)RAND_MAX * 6.282, Y_AXIS); + pos.translate((double)rand() / (double)RAND_MAX * 10000 - 5000, 0.0, (double)rand() / (double)RAND_MAX * 10000 - 5000); + + scene->addInstance(pModelManager->getModel(szName), pos); + } + } + + } + + } else { + double dValue = dRightSlider * ([engine getParameterMaxWithIndex: iParam] - [engine getParameterMinWithIndex: iParam]) + [engine getParameterMinWithIndex: iParam]; + switch([engine getParameterTypeWithIndex: iParam]) { + case KRENGINE_PARAMETER_INT: + dValue = dRightSlider * ([engine getParameterMaxWithIndex: iParam] + 0.5 - [engine getParameterMinWithIndex: iParam]) + [engine getParameterMinWithIndex: iParam]; + [engine setParameterValueWithIndex: iParam Value: dValue]; + break; + case KRENGINE_PARAMETER_BOOL: + [engine setParameterValueWithIndex: iParam Value: 1.0 - dValue]; + break; + case KRENGINE_PARAMETER_FLOAT: + [engine setParameterValueWithIndex: iParam Value: dValue]; + break; + } + + + } + } + + + + [glView setDisplayFramebuffer]; + + + + //double dScaleFactor = [engine getModelManager]->getFirstModel()->getMaxDimension() / 100.0f; + double dScaleFactor = 10.0f; + + camera_position.z += (-cos(camera_pitch) * cos(camera_yaw) * leftStickDeltaX + -cos(camera_pitch) * cos(camera_yaw - 90.0f * d2r) * -leftStickDeltaY) * dScaleFactor; + camera_position.x += (cos(camera_pitch) * sin(camera_yaw) * leftStickDeltaX + cos(camera_pitch) * sin(camera_yaw - 90.0f * d2r) * -leftStickDeltaY) * dScaleFactor; + camera_position.y += sin(camera_pitch) * leftStickDeltaX * dScaleFactor; + camera_yaw += rightStickDeltaY * 4.0 * d2r; + camera_pitch += rightStickDeltaX * 4.0 * d2r; + + [engine renderScene: [glView getScene] WithPosition:camera_position Yaw: camera_yaw Pitch: camera_pitch Roll:0.0f]; + + + [glView presentFramebuffer]; +} + +@end diff --git a/objview/Classes/krengine/KREngine.h b/objview/Classes/krengine/KREngine.h new file mode 100644 index 0000000..a7a3212 --- /dev/null +++ b/objview/Classes/krengine/KREngine.h @@ -0,0 +1,67 @@ +// +// KREngine.h +// gldemo +// +// Created by Kearwood Gilbert on 10-09-16. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import +#import + +// #import "KRTextureManager.h" +#import +#import +#import "KRMat4.h" +#import "KRModel.h" +#import "KRTextureManager.h" +#import "KRMaterialManager.h" + +using std::map; + +@interface KREngine : NSObject +{ +@private + + GLint backingWidth, backingHeight; + + GLuint compositeFramebuffer, compositeDepthTexture, compositeColorTexture; + GLuint shadowFramebuffer, shadowDepthTexture; + + // uniform index + enum { + KRENGINE_UNIFORM_MATERIAL_AMBIENT, + KRENGINE_UNIFORM_MATERIAL_DIFFUSE, + KRENGINE_UNIFORM_MATERIAL_SPECULAR, + KRENGINE_UNIFORM_MVP, + KRENGINE_UNIFORM_MODEL, + KRENGINE_UNIFORM_MODELIT, // Inverse Transform + KRENGINE_NUM_UNIFORMS + }; + GLint m_uniforms[KRENGINE_NUM_UNIFORMS]; + + // attribute index + enum { + KRENGINE_ATTRIB_VERTEX, + KRENGINE_ATTRIB_NORMAL, + KRENGINE_ATTRIB_TANGENT, + KRENGINE_ATTRIB_TEXUV, + KRENGINE_NUM_ATTRIBUTES + }; + + + GLuint m_objectShaderProgram; + GLuint m_postShaderProgram; + GLuint m_shadowShaderProgram; + + std::map m_models; + KRTextureManager *m_pTextureManager; + KRMaterialManager *m_pMaterialManager; +} +- (id)initForWidth: (GLuint)width Height: (GLuint)height; +- (void)renderWithModelMatrix: (KRMat4)modelMatrix; +- (BOOL)loadVertexShader:(NSString *)vertexShaderName fragmentShader:(NSString *)fragmentShaderName forProgram:(GLuint *)programPointer; +- (BOOL)loadResource:(NSString *)path; + +@end + diff --git a/objview/Classes/krengine/KREngine.mm b/objview/Classes/krengine/KREngine.mm new file mode 100644 index 0000000..792a9a9 --- /dev/null +++ b/objview/Classes/krengine/KREngine.mm @@ -0,0 +1,566 @@ +// +// KREngine.mm +// gldemo +// +// Created by Kearwood Gilbert on 10-09-16. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import "KREngine.h" + + + + +@interface KREngine (PrivateMethods) +//- (BOOL)loadObjects; +- (BOOL)loadShaders; +- (BOOL)createBuffers; +- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file; +- (BOOL)linkProgram:(GLuint)prog; +- (BOOL)validateProgram:(GLuint)prog; +- (void)renderPost; +- (BOOL)loadResource:(NSString *)path; +@end + +@implementation KREngine + +- (id)initForWidth: (GLuint)width Height: (GLuint)height +{ + backingWidth = width; + backingHeight = height; + + if ((self = [super init])) + { + + m_pTextureManager = new KRTextureManager(); + m_pMaterialManager = new KRMaterialManager(m_pTextureManager); + + if (![self createBuffers] || ![self loadShaders]/* || ![self loadObjects] */) + { + [self release]; + return nil; + } + } + + return self; +} + +- (BOOL)createBuffers +{ + // ===== Create offscreen compositing framebuffer object ===== + glGenFramebuffers(1, &compositeFramebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, compositeFramebuffer); + + // ----- Create texture color buffer for compositeFramebuffer ----- + glGenTextures(1, &compositeColorTexture); + glBindTexture(GL_TEXTURE_2D, compositeColorTexture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // This is necessary for non-power-of-two textures + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // This is necessary for non-power-of-two textures + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, backingWidth, backingHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, compositeColorTexture, 0); + + // ----- Create Depth Texture for compositeFramebuffer ----- + glGenTextures(1, &compositeDepthTexture); + glBindTexture(GL_TEXTURE_2D, compositeDepthTexture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // This is necessary for non-power-of-two textures + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // This is necessary for non-power-of-two textures + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, backingWidth, backingHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, compositeDepthTexture, 0); + + + + // ===== Create offscreen shadow framebuffer object ===== + glGenFramebuffers(1, &shadowFramebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, shadowFramebuffer); + + // ----- Create Depth Texture for shadowFramebuffer ----- + glGenTextures(1, &shadowDepthTexture); + glBindTexture(GL_TEXTURE_2D, shadowDepthTexture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // This is necessary for non-power-of-two textures + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // This is necessary for non-power-of-two textures + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, backingWidth, backingHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowDepthTexture, 0); + + return TRUE; +} + +- (void)destroyBuffers +{ + if (compositeDepthTexture) { + glDeleteTextures(1, &compositeDepthTexture); + compositeDepthTexture = 0; + } + + if (compositeColorTexture) { + glDeleteTextures(1, &compositeColorTexture); + compositeColorTexture = 0; + } + + if (compositeFramebuffer) { + glDeleteFramebuffers(1, &compositeFramebuffer); + compositeFramebuffer = 0; + } + + if (shadowDepthTexture) { + glDeleteTextures(1, &shadowDepthTexture); + shadowDepthTexture = 0; + } + + if (shadowFramebuffer) { + glDeleteFramebuffers(1, &shadowFramebuffer); + shadowFramebuffer = 0; + } +} + +- (void)renderWithModelMatrix: (KRMat4)modelMatrix +{ + /* An identity matrix we use to perform the equivalant of glLoadIdentity */ + KRMat4 identitymatrix; + + KRMat4 projectionmatrix; /* Our projection matrix starts with all 0s */ + // KRMat4 modelmatrix; /* Our model matrix */ + KRMat4 mvpmatrix; /* Our MVP matrix */ + + + /* Create our projection matrix with a 45 degree field of view + * a width to height ratio of 1 and view from .1 to 800 infront of us */ + + projectionmatrix.perspective(45.0f, 1.3333, 0.01f, 800.0f); + + // Replace the implementation of this method to do your own custom drawing + static std::map::iterator model_itr; + + model_itr = m_models.begin(); + + + + glBindFramebuffer(GL_FRAMEBUFFER, compositeFramebuffer); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + + // Enable backface culling + glCullFace(GL_BACK); + glEnable(GL_CULL_FACE); + + // Enable z-buffer test + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glDepthRangef(0.0, 1.0); + + // Enable alpha blending + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + + + + // Use shader program + glUseProgram(m_objectShaderProgram); + + // Sets the diffuseTexture variable to the first texture unit + glUniform1i(glGetUniformLocation(m_objectShaderProgram, "diffuseTexture"), 0); + + // Sets the specularTexture variable to the second texture unit + glUniform1i(glGetUniformLocation(m_objectShaderProgram, "specularTexture"), 1); + + // Sets the normalTexture variable to the third texture unit + glUniform1i(glGetUniformLocation(m_objectShaderProgram, "normalTexture"), 2); + + // Validate program before drawing. This is a good check, but only really necessary in a debug build. + // DEBUG macro must be defined in your debug configurations if that's not already the case. +#if defined(DEBUG) + if (![self validateProgram:m_objectShaderProgram]) + { + NSLog(@"Failed to validate program: %d", m_objectShaderProgram); + return; + } +#endif + + // ----- Render Model ----- + + KRMat4 modelmatrix = identitymatrix; + // Load the identity matrix into modelmatrix. rotate the model, and move it back 3 + KRModel *pModel = (*model_itr).second; + modelmatrix.translate(pModel->getMinX() - pModel->getMaxX(), pModel->getMinY() - pModel->getMaxY(), pModel->getMinZ() - pModel->getMaxZ()); + modelmatrix.scale(1.0/pModel-> getMaxDimension()); + modelmatrix.translate(0.15, 0.1, -0.4); + + + modelmatrix *= modelMatrix; + + mvpmatrix = modelmatrix; + + + + // multiply our modelmatrix and our projectionmatrix. + mvpmatrix *= projectionmatrix; + mvpmatrix.rotate(-90 * 0.0174532925199, Z_AXIS); + + + // Bind our modelmatrix variable to be a uniform called mvpmatrix in our shaderprogram + glUniformMatrix4fv(m_uniforms[KRENGINE_UNIFORM_MVP], 1, GL_FALSE, mvpmatrix.getPointer()); + glUniformMatrix4fv(m_uniforms[KRENGINE_UNIFORM_MODEL], 1, GL_FALSE, modelmatrix.getPointer()); + glUniformMatrix3fv(m_uniforms[KRENGINE_UNIFORM_MODELIT], 1, GL_FALSE, modelmatrix.getPointer()); + + + + (*model_itr).second -> render(m_objectShaderProgram, (GLuint)KRENGINE_ATTRIB_VERTEX, (GLuint)KRENGINE_ATTRIB_NORMAL, (GLuint)KRENGINE_ATTRIB_TANGENT, (GLuint)KRENGINE_ATTRIB_TEXUV, m_pMaterialManager); + + // This application only creates a single color renderbuffer which is already bound at this point. + // This call is redundant, but needed if dealing with multiple renderbuffers. + /* + glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); + [context presentRenderbuffer:GL_RENDERBUFFER]; + */ + + [self renderPost]; +} + +- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file +{ + GLint status; + const GLchar *source; + + source = (GLchar *)[[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil] UTF8String]; + if (!source) + { + NSLog(@"Failed to load vertex shader"); + return FALSE; + } + + *shader = glCreateShader(type); + glShaderSource(*shader, 1, &source, NULL); + glCompileShader(*shader); + +#if defined(DEBUG) + GLint logLength; + glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &logLength); + if (logLength > 0) + { + GLchar *log = (GLchar *)malloc(logLength); + glGetShaderInfoLog(*shader, logLength, &logLength, log); + NSLog(@"Shader compile log:\n%s", log); + free(log); + } +#endif + + glGetShaderiv(*shader, GL_COMPILE_STATUS, &status); + if (status == 0) + { + glDeleteShader(*shader); + return FALSE; + } + + return TRUE; +} + +- (BOOL)linkProgram:(GLuint)prog +{ + GLint status; + + glLinkProgram(prog); + +#if defined(DEBUG) + GLint logLength; + glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength); + if (logLength > 0) + { + GLchar *log = (GLchar *)malloc(logLength); + glGetProgramInfoLog(prog, logLength, &logLength, log); + NSLog(@"Program link log:\n%s", log); + free(log); + } +#endif + + glGetProgramiv(prog, GL_LINK_STATUS, &status); + if (status == 0) + return FALSE; + + return TRUE; +} + +- (BOOL)validateProgram:(GLuint)prog +{ + GLint logLength, status; + + glValidateProgram(prog); + glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength); + if (logLength > 0) + { + GLchar *log = (GLchar *)malloc(logLength); + glGetProgramInfoLog(prog, logLength, &logLength, log); + NSLog(@"Program validate log:\n%s", log); + free(log); + } + + glGetProgramiv(prog, GL_VALIDATE_STATUS, &status); + if (status == 0) + return FALSE; + + return TRUE; +} + + +- (BOOL)loadVertexShader:(NSString *)vertexShaderName fragmentShader:(NSString *)fragmentShaderName forProgram:(GLuint *)programPointer; +{ + GLuint vertexShader, fragShader; + + NSString *vertShaderPathname, *fragShaderPathname; + + // Create shader program. + *programPointer = glCreateProgram(); + + // Create and compile vertex shader. + vertShaderPathname = [[NSBundle mainBundle] pathForResource:vertexShaderName ofType:@"vsh"]; + if (![self compileShader:&vertexShader type:GL_VERTEX_SHADER file:vertShaderPathname]) + { + NSLog(@"Failed to compile vertex shader"); + return FALSE; + } + + // Create and compile fragment shader. + fragShaderPathname = [[NSBundle mainBundle] pathForResource:fragmentShaderName ofType:@"fsh"]; + if (![self compileShader:&fragShader type:GL_FRAGMENT_SHADER file:fragShaderPathname]) + { + NSLog(@"Failed to compile fragment shader"); + return FALSE; + } + + // Attach vertex shader to program. + glAttachShader(*programPointer, vertexShader); + + // Attach fragment shader to program. + glAttachShader(*programPointer, fragShader); + + // Bind attribute locations. + // This needs to be done prior to linking. + glBindAttribLocation(*programPointer, KRENGINE_ATTRIB_VERTEX, "position"); + glBindAttribLocation(*programPointer, KRENGINE_ATTRIB_TEXUV, "inputTextureCoordinate"); + + + glBindAttribLocation(*programPointer, KRENGINE_ATTRIB_VERTEX, "myVertex"); + glBindAttribLocation(*programPointer, KRENGINE_ATTRIB_NORMAL, "myNormal"); + glBindAttribLocation(*programPointer, KRENGINE_ATTRIB_TANGENT, "myTangent"); + glBindAttribLocation(*programPointer, KRENGINE_ATTRIB_TEXUV, "myUV"); + + // Link program. + if (![self linkProgram:*programPointer]) + { + NSLog(@"Failed to link program: %d", *programPointer); + + if (vertexShader) + { + glDeleteShader(vertexShader); + vertexShader = 0; + } + if (fragShader) + { + glDeleteShader(fragShader); + fragShader = 0; + } + if (*programPointer) + { + glDeleteProgram(*programPointer); + *programPointer = 0; + } + + return FALSE; + } + + // Release vertex and fragment shaders. + if (vertexShader) + { + glDeleteShader(vertexShader); + } + if (fragShader) + { + glDeleteShader(fragShader); + } + + return TRUE; +} + +- (BOOL)loadShaders +{ + [self loadVertexShader:@"PostShader" fragmentShader:@"PostShader" forProgram:&m_postShaderProgram]; + [self loadVertexShader:@"ShadowShader" fragmentShader:@"ShadowShader" forProgram:&m_shadowShaderProgram]; + [self loadVertexShader:@"ObjectShader" fragmentShader:@"ObjectShader" forProgram:&m_objectShaderProgram]; + + // Get uniform locations + m_uniforms[KRENGINE_UNIFORM_MATERIAL_AMBIENT] = glGetUniformLocation(m_objectShaderProgram, "material_ambient"); + m_uniforms[KRENGINE_UNIFORM_MATERIAL_DIFFUSE] = glGetUniformLocation(m_objectShaderProgram, "material_diffuse"); + m_uniforms[KRENGINE_UNIFORM_MATERIAL_SPECULAR] = glGetUniformLocation(m_objectShaderProgram, "material_specular"); + m_uniforms[KRENGINE_UNIFORM_MVP] = glGetUniformLocation(m_objectShaderProgram, "myMVPMatrix"); + m_uniforms[KRENGINE_UNIFORM_MODEL] = glGetUniformLocation(m_objectShaderProgram, "myModelView"); + m_uniforms[KRENGINE_UNIFORM_MODELIT] = glGetUniformLocation(m_objectShaderProgram, "myModelViewIT"); + + return TRUE; +} + +- (BOOL)loadResource:(NSString *)path +{ + NSString *name = [[path lastPathComponent] stringByDeletingPathExtension]; + if([path hasSuffix: @".pack"]) { + NSLog(@"object: %@", path); + m_models[[name UTF8String]] = new KRModel([path UTF8String], m_pMaterialManager); + } else if([path hasSuffix: @".pvr"]) { + NSLog(@"texture: %@", path); + m_pTextureManager->loadTexture([name UTF8String], [path UTF8String]); + } else if([path hasSuffix: @".mtl"]) { + NSLog(@"material: %@", path); + m_pMaterialManager->loadFile([path UTF8String]); + } + + return TRUE; +} + +/* +- (BOOL)loadObjects +{ + NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; + NSFileManager* fileManager = [NSFileManager defaultManager]; + + for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { + if([fileName hasSuffix: @".pvr"]) { + NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; + [self loadResource: path]; + } + } + + for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { + if([fileName hasSuffix: @".mtl"]) { + NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; + [self loadResource: path]; + } + } + + for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { + if([fileName hasSuffix: @".pack"]) { + NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; + [self loadResource: path]; + } + } + + + + return TRUE; +} +*/ + +/* + NSArray *paths = [[NSBundle mainBundle] pathsForResourcesOfType:@"pack" inDirectory:nil]; + for (NSString *path in paths) { + [self loadResource: path]; + } + + NSArray *paths = [[NSBundle mainBundle] pathsForResourcesOfType:@"pvr" inDirectory:nil]; + for (NSString *path in paths) { + [self loadResource: path]; + } + + paths = [[NSBundle mainBundle] pathsForResourcesOfType:@"mtl" inDirectory:nil]; + for (NSString *path in paths) { + [self loadResource: path]; + } + */ + +- (void)dealloc +{ + if (m_objectShaderProgram) { + glDeleteProgram(m_objectShaderProgram); + m_objectShaderProgram = 0; + } + + for(std::map::iterator itr=m_models.begin(); itr != m_models.end(); itr++) { + delete (*itr).second; + } + m_models.empty(); + + if(m_pTextureManager) { + delete m_pTextureManager; + m_pTextureManager = NULL; + } + + if(m_pMaterialManager) { + delete m_pMaterialManager; + m_pMaterialManager = NULL; + } + + + [self destroyBuffers]; + [super dealloc]; +} + +- (void)renderPost +{ + glBindFramebuffer(GL_FRAMEBUFFER, 1); // renderFramebuffer + + // Replace the implementation of this method to do your own custom drawing. + static const GLfloat squareVertices[] = { + -1.0f, -1.0f, + 1.0f, -1.0f, + -1.0f, 1.0f, + 1.0f, 1.0f, + }; + + static const GLfloat textureVertices[] = { + 0.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 1.0f, + 1.0f, 1.0f, + }; + /* + static const GLfloat textureVertices[] = { + 1.0f, 1.0f, + 1.0f, 0.0f, + 0.0f, 1.0f, + 0.0f, 0.0f, + }; + */ + + + glDisable(GL_DEPTH_TEST); + glUseProgram(m_postShaderProgram); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, compositeDepthTexture); + glUniform1i(glGetUniformLocation(m_postShaderProgram, "depthFrame"), 0); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, compositeColorTexture); + glUniform1i(glGetUniformLocation(m_postShaderProgram, "renderFrame"), 1); + + /* + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, videoFrameTexture); + glUniform1i(glGetUniformLocation(m_postShaderProgram, "videoFrame"), 2); + */ + + // Update attribute values. + glVertexAttribPointer(KRENGINE_ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices); + glEnableVertexAttribArray(KRENGINE_ATTRIB_VERTEX); + glVertexAttribPointer(KRENGINE_ATTRIB_TEXUV, 2, GL_FLOAT, 0, 0, textureVertices); + glEnableVertexAttribArray(KRENGINE_ATTRIB_TEXUV); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, 0); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, 0); + + /* + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, 0); + */ +} + +@end diff --git a/objview/Classes/krengine/KRMat4.cpp b/objview/Classes/krengine/KRMat4.cpp new file mode 100644 index 0000000..2b17a43 --- /dev/null +++ b/objview/Classes/krengine/KRMat4.cpp @@ -0,0 +1,133 @@ +// +// KRMat4.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-09-21. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#include +#include +#include + +#include "KRMat4.h" + +KRMat4::KRMat4() { + // Default constructor - Initialize with an identity matrix + static const GLfloat IDENTITY_MATRIX[] = { + 1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0 + }; + memcpy(m_mat, IDENTITY_MATRIX, sizeof(GLfloat) * 16); + +} + +KRMat4::~KRMat4() { + +} + +GLfloat *KRMat4::getPointer() { + return m_mat; +} + +// Copy constructor +KRMat4::KRMat4(const KRMat4 &m) { + + memcpy(m_mat, m.m_mat, sizeof(GLfloat) * 16); +} + +KRMat4& KRMat4::operator=(const KRMat4 &m) { + if(this != &m) { // Prevent self-assignment. + memcpy(m_mat, m.m_mat, sizeof(GLfloat) * 16); + } + return *this; +} + +// Overload compound multiply operator +KRMat4& KRMat4::operator*=(const KRMat4 &m) { + GLfloat temp[16]; + + int x,y; + + for (x=0; x < 4; x++) + { + for(y=0; y < 4; y++) + { + temp[y + (x*4)] = (m_mat[x*4] * m.m_mat[y]) + + (m_mat[(x*4)+1] * m.m_mat[y+4]) + + (m_mat[(x*4)+2] * m.m_mat[y+8]) + + (m_mat[(x*4)+3] * m.m_mat[y+12]); + } + } + + memcpy(m_mat, temp, sizeof(GLfloat) << 4); + return *this; +} + +// Overload multiply operator +KRMat4& KRMat4::operator*(const KRMat4 &m) { + KRMat4 result = *this; + result *= m; + return result; +} + + + +/* Generate a perspective view matrix using a field of view angle fov, + * window aspect ratio, near and far clipping planes */ +void KRMat4::perspective(GLfloat fov, GLfloat aspect, GLfloat nearz, GLfloat farz) { + GLfloat range; + + range = tan(fov * 0.00872664625) * nearz; /* 0.00872664625 = PI/360 */ + memset(m_mat, 0, sizeof(GLfloat) * 16); + m_mat[0] = (2 * nearz) / ((range * aspect) - (-range * aspect)); + m_mat[5] = (2 * nearz) / (2 * range); + m_mat[10] = -(farz + nearz) / (farz - nearz); + m_mat[11] = -1; + m_mat[14] = -(2 * farz * nearz) / (farz - nearz); +} + +/* Perform translation operations on a matrix */ +void KRMat4::translate(GLfloat x, GLfloat y, GLfloat z) { + KRMat4 newMatrix; // Create new identity matrix + + newMatrix.m_mat[12] = x; + newMatrix.m_mat[13] = y; + newMatrix.m_mat[14] = z; + + *this *= newMatrix; +} + +/* Rotate a matrix by an angle on a X, Y, or Z axis */ +void KRMat4::rotate(GLfloat angle, AXIS axis) { + // const GLfloat d2r = 0.0174532925199; /* PI / 180 */ + const int cos1[3] = { 5, 0, 0 }; + const int cos2[3] = { 10, 10, 5 }; + const int sin1[3] = { 6, 2, 1 }; + const int sin2[3] = { 9, 8, 4 }; + + KRMat4 newMatrix; // Create new identity matrix + + newMatrix.m_mat[cos1[axis]] = cos(angle); + newMatrix.m_mat[sin1[axis]] = -sin(angle); + newMatrix.m_mat[sin2[axis]] = -newMatrix.m_mat[sin1[axis]]; + newMatrix.m_mat[cos2[axis]] = newMatrix.m_mat[cos1[axis]]; + + *this *= newMatrix; +} + +void KRMat4::scale(GLfloat x, GLfloat y, GLfloat z) { + KRMat4 newMatrix; // Create new identity matrix + + newMatrix.m_mat[0] = x; + newMatrix.m_mat[5] = y; + newMatrix.m_mat[10] = z; + + *this *= newMatrix; +} + +void KRMat4::scale(GLfloat s) { + scale(s,s,s); +} diff --git a/objview/Classes/krengine/KRMat4.h b/objview/Classes/krengine/KRMat4.h new file mode 100644 index 0000000..3869804 --- /dev/null +++ b/objview/Classes/krengine/KRMat4.h @@ -0,0 +1,69 @@ +// +// KRMat4.h +// gldemo +// +// Created by Kearwood Gilbert on 10-09-21. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import +#import + +#ifndef KRMAT4_I +#define KRMAT4_I + + +#define EMPTY_MATRIX4 { 0.0, 0.0, 0.0, 0.0,\ +0.0, 0.0, 0.0, 0.0,\ +0.0, 0.0, 0.0, 0.0,\ +0.0, 0.0, 0.0, 0.0 } + +#define IDENTITY_MATRIX4 { 1.0, 0.0, 0.0, 0.0,\ +0.0, 1.0, 0.0, 0.0,\ +0.0, 0.0, 1.0, 0.0,\ +0.0, 0.0, 0.0, 1.0 } + +typedef enum { + X_AXIS, + Y_AXIS, + Z_AXIS +} AXIS; + + +class KRMat4 { + + GLfloat m_mat[16]; + +public: + + // Default constructor - Creates an identity matrix + KRMat4(); + + // Destructor + ~KRMat4(); + + // Copy constructor + KRMat4(const KRMat4 &m); + + // Overload assignment operator + KRMat4& operator=(const KRMat4 &m); + + // Overload compound multiply operator + KRMat4& operator*=(const KRMat4 &m); + + // Overload multiply operator + KRMat4& operator*(const KRMat4 &m); + + GLfloat *getPointer(); + + void perspective(GLfloat fov, GLfloat aspect, GLfloat nearz, GLfloat farz); + void translate(GLfloat x, GLfloat y, GLfloat z); + void scale(GLfloat x, GLfloat y, GLfloat z); + void scale(GLfloat s); + void rotate(GLfloat angle, AXIS axis); + +}; + + + +#endif // KRMAT4_I \ No newline at end of file diff --git a/objview/Classes/krengine/KRMaterial.cpp b/objview/Classes/krengine/KRMaterial.cpp new file mode 100644 index 0000000..b87e135 --- /dev/null +++ b/objview/Classes/krengine/KRMaterial.cpp @@ -0,0 +1,148 @@ +// +// KRMaterial.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-10-24. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#include "KRMaterial.h" + +KRMaterial::KRMaterial() { + m_pAmbientMap = NULL; + m_pDiffuseMap = NULL; + m_pSpecularMap = NULL; + m_pNormalMap = NULL; + m_ka_r = (GLfloat)0.0f; + m_ka_g = (GLfloat)0.0f; + m_ka_b = (GLfloat)0.0f; + m_kd_r = (GLfloat)1.0f; + m_kd_g = (GLfloat)1.0f; + m_kd_b = (GLfloat)1.0f; + m_ks_r = (GLfloat)1.0f; + m_ks_g = (GLfloat)1.0f; + m_ks_b = (GLfloat)1.0f; +} + +KRMaterial::~KRMaterial() { + +} + +void KRMaterial::setAmbientMap(KRTexture *pTexture) { + m_pAmbientMap = pTexture; +} + +void KRMaterial::setDiffuseMap(KRTexture *pTexture) { + m_pDiffuseMap = pTexture; +} + +void KRMaterial::setSpecularMap(KRTexture *pTexture) { + m_pSpecularMap = pTexture; +} + +void KRMaterial::setNormalMap(KRTexture *pTexture) { + m_pNormalMap = pTexture; +} + +void KRMaterial::setAmbient(GLfloat r, GLfloat g, GLfloat b) { + m_ka_r = r; + m_ka_g = g; + m_ka_b = b; +} + +void KRMaterial::setDiffuse(GLfloat r, GLfloat g, GLfloat b) { + m_kd_r = r; + m_kd_g = g; + m_kd_b = b; +} + +void KRMaterial::setSpecular(GLfloat r, GLfloat g, GLfloat b) { + m_ks_r = r; + m_ks_g = g; + m_ks_b = b; +} + +void KRMaterial::bind(GLuint program) { + + GLuint uniform_material_ambient = glGetUniformLocation(program, "material_ambient"); + GLuint uniform_material_diffuse = glGetUniformLocation(program, "material_diffuse"); + GLuint uniform_material_specular = glGetUniformLocation(program, "material_specular"); + + glUniform3f( + uniform_material_ambient, + m_ka_r, // iMaterial % 2 ? (GLfloat)0.9f : (GLfloat)0.5f, + m_ka_g, // iMaterial % 4 ? (GLfloat)0.9f : (GLfloat)0.5f, + m_ka_b // iMaterial % 8 ? (GLfloat)0.9f : (GLfloat)0.5f, + ); + + glUniform3f( + uniform_material_diffuse, + m_kd_r + 1.0f, // 1.0 added so there will not be complete darkness + m_kd_g + 1.0f, // 1.0 added so there will not be complete darkness + m_kd_b + 1.0f // 1.0 added so there will not be complete darkness + ); + + glUniform3f( + uniform_material_specular, + m_ks_r, // iMaterial % 2 ? (GLfloat)0.9f : (GLfloat)0.5f, + m_ks_g, // iMaterial % 4 ? (GLfloat)0.9f : (GLfloat)0.5f, + m_ks_b // iMaterial % 8 ? (GLfloat)0.9f : (GLfloat)0.5f, + ); + + + + int iTextureName = 0; + if(m_pDiffuseMap != NULL) { + iTextureName = m_pDiffuseMap->getName(); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, iTextureName); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + } + + + iTextureName = 0; + if(m_pSpecularMap != NULL) { + iTextureName = m_pSpecularMap->getName(); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, iTextureName); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + } + + + iTextureName = 0; + if(m_pNormalMap != NULL) { + iTextureName = m_pNormalMap->getName(); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, iTextureName); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + } + + + + +//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); +//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + +/* + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + */ +/* + if (_anisotropySupported) + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, _anisotropyTexParam); + */ +/* + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + */ + + + + +} \ No newline at end of file diff --git a/objview/Classes/krengine/KRMaterial.h b/objview/Classes/krengine/KRMaterial.h new file mode 100644 index 0000000..a318632 --- /dev/null +++ b/objview/Classes/krengine/KRMaterial.h @@ -0,0 +1,46 @@ +// +// KRMaterial.h +// gldemo +// +// Created by Kearwood Gilbert on 10-10-24. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import +#import +#import +#import +using std::list; + +#ifndef KRMATERIAL_H +#define KRMATRIAL_H + +#import "KRTexture.h" + +class KRMaterial { +public: + KRMaterial(); + ~KRMaterial(); + + void setAmbientMap(KRTexture *pTexture); + void setDiffuseMap(KRTexture *pTexture); + void setSpecularMap(KRTexture *pTexture); + void setNormalMap(KRTexture *pTexture); + void setAmbient(GLfloat r, GLfloat g, GLfloat b); + void setDiffuse(GLfloat r, GLfloat g, GLfloat b); + void setSpecular(GLfloat r, GLfloat g, GLfloat b); + + void bind(GLuint program); + +private: + KRTexture *m_pAmbientMap; // mtl map_Ka value + KRTexture *m_pDiffuseMap; // mtl map_Kd value + KRTexture *m_pSpecularMap; // mtl map_Ks value + KRTexture *m_pNormalMap; // mtl map_Normal value + + GLfloat m_ka_r, m_ka_g, m_ka_b; // Ambient rgb + GLfloat m_kd_r, m_kd_g, m_kd_b; // Diffuse rgb + GLfloat m_ks_r, m_ks_g, m_ks_b; // Specular rgb +}; + +#endif diff --git a/objview/Classes/krengine/KRMaterialManager.cpp b/objview/Classes/krengine/KRMaterialManager.cpp new file mode 100644 index 0000000..5326b72 --- /dev/null +++ b/objview/Classes/krengine/KRMaterialManager.cpp @@ -0,0 +1,185 @@ +// +// KRMaterialManager.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-10-24. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#include "KRMaterialManager.h" + +#import +#include +#include +#include +#include +#include + + +KRMaterialManager::KRMaterialManager(KRTextureManager *pTextureManager) { + m_pTextureManager = pTextureManager; +} + +KRMaterialManager::~KRMaterialManager() { + +} + + +KRMaterial *KRMaterialManager::getMaterial(const char *szName) { + map::iterator itr = m_materials.find(szName); + if(itr == m_materials.end()) { + // Not found + return NULL; + } else { + return (*itr).second; + } +} + +bool KRMaterialManager::loadFile(const char *szPath) { + bool bSuccess = false; + + int fdFile = 0; + int fileSize = 0; + void *pFile = NULL; + KRMaterial *pMaterial = NULL; + char szSymbol[16][64]; + + struct stat statbuf; + fdFile = open(szPath, O_RDONLY); + if(fdFile >= 0) { + if(fstat(fdFile, &statbuf) >= 0) { + if ((pFile = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fdFile, 0)) == (caddr_t) -1) { + } else { + fileSize = statbuf.st_size; + + char *pScan = (char *)pFile; + char *pEnd = (char *)pFile + fileSize; + while(pScan < pEnd) { + + // Scan through whitespace + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t' || *pScan == '\r' || *pScan == '\n')) { + pScan++; + } + + if(*pScan == '#') { + // Line is a comment line + + // Scan to the end of the line + while(pScan < pEnd && *pScan != '\r' && *pScan != '\n') { + pScan++; + } + } else { + int cSymbols = 0; + while(pScan < pEnd && *pScan != '\n' && *pScan != '\r') { + + char *pDest = szSymbol[cSymbols++]; + while(pScan < pEnd && *pScan != ' ' && *pScan != '\n' && *pScan != '\r') { + *pDest++ = *pScan++; + } + *pDest = '\0'; + + // Scan through whitespace, but don't advance to next line + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t')) { + pScan++; + } + } + + if(strcmp(szSymbol[0], "newmtl") == 0 && cSymbols >= 2) { + + pMaterial = new KRMaterial(); + m_materials[szSymbol[1]] = pMaterial; + } if(pMaterial != NULL) { + if(strcmp(szSymbol[0], "Ka") == 0) { + char *pScan2 = szSymbol[1]; + double r = strtof(pScan2, &pScan2); + if(cSymbols == 2) { + pMaterial->setAmbient(r, r, r); + } else if(cSymbols == 4) { + pScan2 = szSymbol[2]; + double g = strtof(pScan2, &pScan2); + pScan2 = szSymbol[3]; + double b = strtof(pScan2, &pScan2); + pMaterial->setAmbient(r, g, b); + } + } else if(strcmp(szSymbol[0], "Kd") == 0) { + char *pScan2 = szSymbol[1]; + double r = strtof(pScan2, &pScan2); + if(cSymbols == 2) { + pMaterial->setDiffuse(r, r, r); + } else if(cSymbols == 4) { + pScan2 = szSymbol[2]; + double g = strtof(pScan2, &pScan2); + pScan2 = szSymbol[3]; + double b = strtof(pScan2, &pScan2); + pMaterial->setDiffuse(r, g, b); + } + } else if(strcmp(szSymbol[0], "Ks") == 0) { + char *pScan2 = szSymbol[1]; + double r = strtof(pScan2, &pScan2); + if(cSymbols == 2) { + pMaterial->setSpecular(r, r, r); + } else if(cSymbols == 4) { + pScan2 = szSymbol[2]; + double g = strtof(pScan2, &pScan2); + pScan2 = szSymbol[3]; + double b = strtof(pScan2, &pScan2); + pMaterial->setSpecular(r, g, b); + } + } else if(strncmp(szSymbol[0], "map", 3) == 0) { + // Truncate file extension + char *pScan2 = szSymbol[1]; + char *pLastPeriod = NULL; + while(*pScan2 != '\0') { + if(*pScan2 == '.') { + pLastPeriod = pScan2; + } + pScan2++; + } + if(pLastPeriod) { + *pLastPeriod = '\0'; + } + + KRTexture *pTexture = m_pTextureManager->getTexture(szSymbol[1]); + if(pTexture) { + if(strcmp(szSymbol[0], "map_Ka") == 0) { + pMaterial->setAmbientMap(pTexture); + } else if(strcmp(szSymbol[0], "map_Kd") == 0) { + pMaterial->setDiffuseMap(pTexture); + } else if(strcmp(szSymbol[0], "map_Ks") == 0) { + pMaterial->setSpecularMap(pTexture); + } else if(strcmp(szSymbol[0], "map_Normal") == 0) { + pMaterial->setNormalMap(pTexture); + } + } + } + } + + } + + } + + bSuccess = true; + } + } + } + + if(pFile != NULL) { + munmap(pFile, fileSize); + } + + if(fdFile != 0) { + close(fdFile); + } + + /* + KRMaterial *pMaterial = new KRMaterial(); + + if(!pMaterial->loadFromFile(szPath)) { + delete pMaterial; + return NULL; + } + m_materials[szName] = pMaterial; + */ + + return bSuccess; +} diff --git a/objview/Classes/krengine/KRMaterialManager.h b/objview/Classes/krengine/KRMaterialManager.h new file mode 100644 index 0000000..70071ed --- /dev/null +++ b/objview/Classes/krengine/KRMaterialManager.h @@ -0,0 +1,32 @@ +// +// KRMaterialManager.h +// gldemo +// +// Created by Kearwood Gilbert on 10-10-24. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#ifndef KRMATERIALMANAGER_H +#define KRMATERIALMANAGER_H + +#include "KRMaterial.h" +#include "KRTextureManager.h" + +#include +using std::map; + +class KRMaterialManager { +public: + KRMaterialManager(KRTextureManager *pTextureManager); + ~KRMaterialManager(); + + bool loadFile(const char *szPath); + KRMaterial *getMaterial(const char *szName); + +private: + map m_materials; + KRTextureManager *m_pTextureManager; +}; + +#endif + diff --git a/objview/Classes/krengine/KRModel.cpp b/objview/Classes/krengine/KRModel.cpp new file mode 100644 index 0000000..f711b34 --- /dev/null +++ b/objview/Classes/krengine/KRModel.cpp @@ -0,0 +1,615 @@ +// +// KRModel.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-09-22. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import +#include +#include +#include +#include +#include +#include +#include + + +#include "KRModel.h" + +#include "KRVector3.h" + +#define MAX_VBO_SIZE 65535 +// MAX_VBO_SIZE must be divisible by 3 so triangles aren't split across VBO objects... + +#define BUFFER_OFFSET(i) ((char *)NULL + (i)) + +KRModel::KRModel(std::string path, KRMaterialManager *pMaterialManager) { + + m_fdPackFile = 0; + m_pPackFile = NULL; + m_iPackFileSize = 0; + m_cBuffers = 0; + m_pBuffers = NULL; + + // loadWavefront(path, pMaterialManager); + loadPack(path, pMaterialManager); +} + +void KRModel::loadPack(std::string path, KRMaterialManager *pMaterialManager) { + struct stat statbuf; + m_fdPackFile = open(path.c_str(), O_RDONLY); + if(m_fdPackFile >= 0) { + if(fstat(m_fdPackFile, &statbuf) >= 0) { + if ((m_pPackFile = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, m_fdPackFile, 0)) == (caddr_t) -1) { + } else { + m_iPackFileSize = statbuf.st_size; + + + pack_header *pHeader = (pack_header *)m_pPackFile; + + m_minx = pHeader->minx; + m_miny = pHeader->miny; + m_minz = pHeader->minz; + m_maxx = pHeader->maxx; + m_maxy = pHeader->maxy; + m_maxz = pHeader->maxz; + + pack_material *pPackMaterials = (pack_material *)(pHeader+1); + + for(int iMaterial=0; iMaterial < pHeader->material_count; iMaterial++) { + pack_material *pPackMaterial = pPackMaterials + iMaterial; + + Material *pMaterial = new Material(); + pMaterial->start_vertex = pPackMaterial->start_vertex; + pMaterial->vertex_count = pPackMaterial->vertex_count; + pMaterial->pMaterial = pMaterialManager->getMaterial(pPackMaterial->szName); + m_materials.push_back(pMaterial); + } + + m_pVertexData = (VertexData *)(pPackMaterials + pHeader->material_count); + + + m_cBuffers = (pHeader->vertex_count + MAX_VBO_SIZE - 1) / MAX_VBO_SIZE; + m_pBuffers = new GLuint[m_cBuffers]; + glGenBuffers(m_cBuffers, m_pBuffers); + for(GLsizei iBuffer=0; iBuffer < m_cBuffers; iBuffer++) { +// if(iBuffer < 30) { + GLsizei cVertexes = iBuffer < m_cBuffers - 1 ? MAX_VBO_SIZE : pHeader->vertex_count % MAX_VBO_SIZE; + glBindBuffer(GL_ARRAY_BUFFER, m_pBuffers[iBuffer]); + glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData) * cVertexes, m_pVertexData + iBuffer * MAX_VBO_SIZE, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); +// } + + } + } + } + } +} + +void KRModel::loadWavefront(std::string path, KRMaterialManager *pMaterialManager) { + const char *szPath = path.c_str(); + + int fdFile = 0; + int fileSize = 0; + void *pFile = NULL; + char szSymbol[16][64]; + + std::vector materials; + + + Vertex3D *pVertices = NULL; + Vector3D *pNormals = NULL; + TexCoord *pTexCoords = NULL; + int *pFaces = NULL; + + struct stat statbuf; + fdFile = open(szPath, O_RDONLY); + if(fdFile >= 0) { + if(fstat(fdFile, &statbuf) >= 0) { + if ((pFile = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fdFile, 0)) == (caddr_t) -1) { + } else { + fileSize = statbuf.st_size; + + + + // Pass 1 - Get counts + + int cVertices = 0; + int cNormals = 0; + int cTexCoords = 0; + int cVertexData = 0; + + + cVertices = 0; + int cFaces = 1; + GLint cMaterialFaceStart = 1; + + + + // --------- + + + char *pScan = (char *)pFile; + char *pEnd = (char *)pFile + fileSize; + while(pScan < pEnd) { + + // Scan through whitespace + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t' || *pScan == '\r' || *pScan == '\n')) { + pScan++; + } + + if(*pScan == '#') { + // Line is a comment line + + // Scan to the end of the line + while(pScan < pEnd && *pScan != '\r' && *pScan != '\n') { + pScan++; + } + } else { + int cSymbols = 0; + while(pScan < pEnd && *pScan != '\n' && *pScan != '\r') { + + char *pDest = szSymbol[cSymbols++]; + while(pScan < pEnd && *pScan != ' ' && *pScan != '\n' && *pScan != '\r') { + *pDest++ = *pScan++; + } + *pDest = '\0'; + + // Scan through whitespace, but don't advance to next line + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t')) { + pScan++; + } + } + + if(strcmp(szSymbol[0], "v") == 0) { + // Vertex (v) + cVertices++; + } else if(strcmp(szSymbol[0], "vt") == 0) { + // Vertex Texture UV Coordinate (vt) + cTexCoords++; + } else if(strcmp(szSymbol[0], "vn") == 0) { + // Vertex Normal (vn) + cNormals++; + } else if(strcmp(szSymbol[0], "f") == 0) { + // Face (f) + int cFaceVertexes = (cSymbols - 3) * 3; // 3 vertexes per triangle. Triangles have 4 symbols. Quads have 5 symbols and generate two triangles. + cVertexData += cFaceVertexes; + cFaces += cFaceVertexes * 3 + 1; // Allocate space for count of vertices, Vertex Index, Texture Coordinate Index, and Normal Index + + } else if(strcmp(szSymbol[0], "usemtl") == 0) { + // Use Material (usemtl) + if(cMaterialFaceStart - cFaces > 0) { + cFaces++; + + } + materials.push_back(pMaterialManager->getMaterial(szSymbol[1])); + } + + } + } + + + // Pass 2 - Populate vertexes and faces + Vertex3D *pVertices = (Vertex3D *)malloc(sizeof(Vertex3D) * cVertices); + Vector3D *pNormals = (Vector3D *)malloc(sizeof(Vector3D) *cNormals); + TexCoord *pTexCoords = (TexCoord *)malloc(sizeof(TexCoord) * cTexCoords); + int *pFaces = (int *)malloc(sizeof(int *) * (cFaces + 1)); + + + Vertex3D *pVertice = pVertices; + Vector3D *pNormal = pNormals; + TexCoord *pTexCoord = pTexCoords; + int *pFace = pFaces; + int *pMaterialFaces = pFace++; + *pMaterialFaces = 0; + + + std::vector::iterator material_itr = materials.begin(); + + // -------- + + pScan = (char *)pFile; + while(pScan < pEnd) { + + // Scan through whitespace + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t' || *pScan == '\r' || *pScan == '\n')) { + pScan++; + } + + if(*pScan == '#') { + // Line is a comment line + + // Scan to the end of the line + while(pScan < pEnd && *pScan != '\r' && *pScan != '\n') { + pScan++; + } + } else { + int cSymbols = 0; + while(pScan < pEnd && *pScan != '\n' && *pScan != '\r') { + + char *pDest = szSymbol[cSymbols++]; + while(pScan < pEnd && *pScan != ' ' && *pScan != '\n' && *pScan != '\r') { + *pDest++ = *pScan++; + } + *pDest = '\0'; + + // Scan through whitespace, but don't advance to next line + while(pScan < pEnd && (*pScan == ' ' || *pScan == '\t')) { + pScan++; + } + } + + if(strcmp(szSymbol[0], "v") == 0) { + // Vertex (v) + char *pChar = szSymbol[1]; + pVertice -> x = strtof(pChar, &pChar); + pChar = szSymbol[2]; + pVertice -> y = strtof(pChar, &pChar); + pChar = szSymbol[3]; + pVertice -> z = strtof(pChar, &pChar); + pVertice++; + } else if(strcmp(szSymbol[0], "vt") == 0) { + // Vertex Texture UV Coordinate (vt) + char *pChar = szSymbol[1]; + pTexCoord -> u = strtof(pChar, &pChar); + pChar = szSymbol[2]; + pTexCoord -> v = strtof(pChar, &pChar); + pTexCoord++; + } else if(strcmp(szSymbol[0], "vn") == 0) { + // Vertex Normal (vn) + char *pChar = szSymbol[1]; + pNormal -> x = strtof(pChar, &pChar); + pChar = szSymbol[2]; + pNormal -> y = strtof(pChar, &pChar); + pChar = szSymbol[3]; + pNormal -> z = strtof(pChar, &pChar); + pNormal++; + } else if(strcmp(szSymbol[0], "f") == 0) { + // Face (f) + GLint cFaceVertices = cSymbols - 1; + + *pFace++ = cFaceVertices; + for(int iSymbol=1; iSymbol < cSymbols; iSymbol++) { + char *pChar = szSymbol[iSymbol]; + if(*pChar == '.' || (*pChar >= '0' && *pChar <= '9')) { + *pFace++ = strtol(pChar, &pChar, 10) - 1; // Vertex Index + + if(*pChar == '/') { + pChar++; + if(*pChar == '/') { + *pFace++ = -1; + } else { + *pFace++ = strtol(pChar, &pChar, 10) - 1; // Texture Coordinate Index + } + } else { + *pFace++ = -1; + } + + if(*pChar == '/') { + pChar++; + if(*pChar == '/') { + *pFace++ = -1; + } else { + *pFace++ = strtol(pChar, &pChar, 10) - 1; // Normal Index + } + } else { + *pFace++ = -1; + } + while(*pChar == '/') { + pChar++; + strtol(pChar, &pChar, 10); + } + } + } + + + } else if(strcmp(szSymbol[0], "usemtl") == 0) { + // Use Material (usemtl) + if(pFace - pMaterialFaces > 1) { + *pMaterialFaces = pFace - pMaterialFaces - 1; + pMaterialFaces = pFace++; + } + } + } + } + + + *pMaterialFaces = pFace - pMaterialFaces - 1; + *pFace++ = 0; + + + m_pVertexData = (VertexData *)malloc(sizeof(VertexData) * cVertexData); + + VertexData *pData = m_pVertexData; + + int iVertex = 0; + Material *pMaterial = new Material(); + pMaterial->start_vertex = iVertex; + pMaterial->vertex_count = 0; + if(material_itr < materials.end()) { + pMaterial->pMaterial = *material_itr++; + } else { + pMaterial->pMaterial = NULL; + } + m_materials.push_back(pMaterial); + + + pFace = pFaces; + while(*pFace != 0 && iVertex < cVertexData) { + pMaterial->start_vertex = iVertex; + + int *pMaterialEndFace = pFace + *pFace++; + while(pFace < pMaterialEndFace) { + int cFaceVertexes = *pFace; + VertexData *pFirstFaceVertex = NULL; + VertexData *pPrevFaceVertex = NULL; + for(int iFaceVertex=0; iFaceVertex < cFaceVertexes; iFaceVertex++) { + if(iFaceVertex > 2) { + // There have already been 3 vertices. Now we need to split the quad into a second triangle composed of the 1st, 3rd, and 4th vertices + memcpy(pData++, pFirstFaceVertex, sizeof(VertexData)); + memcpy(pData++, pPrevFaceVertex, sizeof(VertexData)); + iVertex+=2; + } + Vertex3D *pVertex = pVertices + pFace[iFaceVertex*3+1]; + if(iFaceVertex==0) { + pFirstFaceVertex = pData; + } + pPrevFaceVertex = pData; + pData->vertex.x = pVertex -> x; + pData->vertex.y = pVertex -> y; + pData->vertex.z = pVertex -> z; + + if(pFace[iFaceVertex*3+2] >= 0) { + TexCoord *pTexCoord = pTexCoords + pFace[iFaceVertex*3+2]; + pData->texcoord.u = pTexCoord -> u; + pData->texcoord.v = pTexCoord -> v; + } else { + pData->texcoord.u = 0; + pData->texcoord.v = 0; + } + + if(pFace[iFaceVertex*3+3] >= 0){ + Vector3D *pNormal = pNormals + pFace[iFaceVertex*3+3]; + pData->normal.x = pNormal -> x; + pData->normal.y = pNormal -> y; + pData->normal.z = pNormal -> z; + } else { + pData->normal.x = 0; + pData->normal.y = 0; + pData->normal.z = 0; + } + + pData++; + iVertex++; + } + pFace += cFaceVertexes * 3 + 1; + } + pMaterial->vertex_count = iVertex - pMaterial->start_vertex; + if(*pFace != 0) { + pMaterial = new Material(); + pMaterial->start_vertex = iVertex; + pMaterial->vertex_count = 0; + if(material_itr < materials.end()) { + pMaterial->pMaterial = *material_itr++; + } else { + pMaterial->pMaterial = NULL; + } + m_materials.push_back(pMaterial); + } + } + } + } + } + + + m_minx = 0.0; + m_miny = 0.0; + m_minz = 0.0; + m_maxx = 0.0; + m_maxy = 0.0; + m_maxz = 0.0; + + + + // Calculate surface normals and tangents + // http://www.terathon.com/code/tangent.html + // http://www.fabiensanglard.net/bumpMapping/index.php + + for(std::vector::iterator itr = m_materials.begin(); itr != m_materials.end(); itr++) { + VertexData *pStart = m_pVertexData + (*itr)->start_vertex; + VertexData *pEnd = pStart + (*itr)->vertex_count; + for(VertexData *pVertex = pStart; pVertex < pEnd; pVertex+=3) { + if(pVertex->vertex.x < m_minx) m_minx = pVertex->vertex.x; + if(pVertex->vertex.x > m_maxx) m_maxx = pVertex->vertex.x; + if(pVertex->vertex.y < m_miny) m_miny = pVertex->vertex.y; + if(pVertex->vertex.y > m_maxy) m_maxy = pVertex->vertex.y; + if(pVertex->vertex.z < m_minz) m_minz = pVertex->vertex.z; + if(pVertex->vertex.z > m_maxz) m_maxz = pVertex->vertex.z; + } + + + for(VertexData *pVertex = pStart; pVertex < pEnd; pVertex+=3) { + Vector3 p1(pVertex[0].vertex.x, pVertex[0].vertex.y, pVertex[0].vertex.z); + Vector3 p2(pVertex[1].vertex.x, pVertex[1].vertex.y, pVertex[1].vertex.z); + Vector3 p3(pVertex[2].vertex.x, pVertex[2].vertex.y, pVertex[2].vertex.z); + Vector3 v1 = p2 - p1; + Vector3 v2 = p3 - p1; + + // -- Calculate normal -- + if(pVertex->normal.x == 0 && pVertex->normal.y == 0 && pVertex->normal.z == 0) { + + + Vector3 normal = v1.cross( v2 ); + + normal.normalize(); + + pVertex[0].normal.x = normal.x; + pVertex[0].normal.y = normal.y; + pVertex[0].normal.z = normal.z; + + pVertex[1].normal.x = normal.x; + pVertex[1].normal.y = normal.y; + pVertex[1].normal.z = normal.z; + + pVertex[2].normal.x = normal.x; + pVertex[2].normal.y = normal.y; + pVertex[2].normal.z = normal.z; + } + + // -- Calculate tangent -- + TexCoord st1; // = pVertex[2].texcoord; + TexCoord st2; // = pVertex[1].texcoord; + st1.u = pVertex[1].texcoord.u - pVertex[0].texcoord.u; + st1.v = pVertex[1].texcoord.v - pVertex[0].texcoord.v; + st2.u = pVertex[2].texcoord.u - pVertex[0].texcoord.u; + st2.v = pVertex[2].texcoord.v - pVertex[0].texcoord.v; + double coef = 1/ (st1.u * st2.v - st2.u * st1.v); + + pVertex[0].tangent.x = coef * ((v1.x * st2.v) + (v2.x * -st1.v)); + pVertex[0].tangent.y = coef * ((v1.y * st2.v) + (v2.y * -st1.v)); + pVertex[0].tangent.z = coef * ((v1.z * st2.v) + (v2.z * -st1.v)); + + Vector3 tangent( + coef * ((v1.x * st2.v) + (v2.x * -st1.v)), + coef * ((v1.y * st2.v) + (v2.y * -st1.v)), + coef * ((v1.z * st2.v) + (v2.z * -st1.v)) + ); + + tangent.normalize(); + + pVertex[0].tangent.x = tangent.x; + pVertex[0].tangent.y = tangent.y; + pVertex[0].tangent.z = tangent.z; + pVertex[1].tangent.x = tangent.x; + pVertex[1].tangent.y = tangent.y; + pVertex[1].tangent.z = tangent.z; + pVertex[2].tangent.x = tangent.x; + pVertex[2].tangent.y = tangent.y; + pVertex[2].tangent.z = tangent.z; + + + } + + } + + + if(pFile != NULL) { + munmap(pFile, fileSize); + } + + if(fdFile != 0) { + close(fdFile); + } + + if(pVertices) { + free(pVertices); + } + if(pNormals) { + free(pNormals); + } + if(pTexCoords) { + free(pTexCoords); + } + if(pFaces) { + free(pFaces); + } +} + +KRModel::~KRModel() { + + if(m_pPackFile != NULL) { + munmap(m_pPackFile, m_iPackFileSize); + } + if(m_fdPackFile) { + close(m_fdPackFile); + } else { + // If we didn't load a packed file, then the data was calculated at run time and malloc'ed + if(m_pVertexData != NULL) { + free(m_pVertexData); + } + } + + if(m_pBuffers != NULL) { + glDeleteBuffers(m_cBuffers, m_pBuffers); + delete m_pBuffers; + } +} + +void KRModel::render(GLuint program, GLuint iVertexAttrib, GLuint iNormalAttrib, GLuint iTangentAttrib, GLuint iTexCoordAttrib, KRMaterialManager *pMaterialManager) { + for(std::vector::iterator itr = m_materials.begin(); itr != m_materials.end(); itr++) { + + KRMaterial *pMaterial = (*itr)->pMaterial; + + if(pMaterial != NULL) { + pMaterial->bind(program); + } else { + pMaterial = new KRMaterial(); + pMaterial->bind(program); + delete pMaterial; + } + + int iVertex = (*itr)->start_vertex; + int iBuffer = iVertex / MAX_VBO_SIZE; + iVertex = iVertex % MAX_VBO_SIZE; + int cVertexes = (*itr)->vertex_count; + while(cVertexes > 0) { + glBindBuffer(GL_ARRAY_BUFFER, m_pBuffers[iBuffer]); + + glVertexAttribPointer(iVertexAttrib, 3, GL_FLOAT, 0, sizeof(VertexData), BUFFER_OFFSET(0)); + glEnableVertexAttribArray(iVertexAttrib); + + glVertexAttribPointer(iNormalAttrib, 3, GL_FLOAT, 0, sizeof(VertexData), BUFFER_OFFSET(sizeof(Vertex3D))); + glEnableVertexAttribArray(iNormalAttrib); + + glVertexAttribPointer(iTangentAttrib, 3, GL_FLOAT, 0, sizeof(VertexData), BUFFER_OFFSET(sizeof(Vertex3D) + sizeof(Vector3D))); + glEnableVertexAttribArray(iTangentAttrib); + + glVertexAttribPointer(iTexCoordAttrib, 2, GL_FLOAT, 0, sizeof(VertexData), BUFFER_OFFSET(sizeof(Vertex3D) + sizeof(Vector3D) * 2)); + glEnableVertexAttribArray(iTexCoordAttrib); + + if(iVertex + cVertexes >= MAX_VBO_SIZE) { + glDrawArrays(GL_TRIANGLES, iVertex, (MAX_VBO_SIZE - iVertex)); + cVertexes -= (MAX_VBO_SIZE - iVertex); + iVertex = 0; + iBuffer++; + } else { + glDrawArrays(GL_TRIANGLES, iVertex, cVertexes); + cVertexes = 0; + } + + } + glBindBuffer(GL_ARRAY_BUFFER, 0); + + } + +} + + +GLfloat KRModel::getMaxDimension() { + GLfloat m = 0.0; + if(m_maxx - m_minx > m) m = m_maxx - m_minx; + if(m_maxy - m_miny > m) m = m_maxy - m_miny; + if(m_maxz - m_minz > m) m = m_maxz - m_minz; + return m; +} + +GLfloat KRModel::getMinX() { + return m_minx; +} +GLfloat KRModel::getMaxX() { + return m_maxx; +} +GLfloat KRModel::getMinY() { + return m_miny; +} +GLfloat KRModel::getMaxY() { + return m_maxy; +} +GLfloat KRModel::getMinZ() { + return m_minz; +} +GLfloat KRModel::getMaxZ() { + return m_maxz; +} diff --git a/objview/Classes/krengine/KRModel.h b/objview/Classes/krengine/KRModel.h new file mode 100644 index 0000000..7820303 --- /dev/null +++ b/objview/Classes/krengine/KRModel.h @@ -0,0 +1,97 @@ +// +// KRModel.h +// gldemo +// +// Created by Kearwood Gilbert on 10-09-22. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import +#import +#import +#import + +using std::vector; + +#ifndef KRMODEL_I +#define KRMODEL_I + +#import "KRMaterialManager.h" + + + +class KRModel { + +public: + KRModel(std::string path, KRMaterialManager *pMaterialManager); + ~KRModel(); + + void render(GLuint program, GLuint iVertexAttrib, GLuint iNormalAttrib, GLuint iTangentAttrib, GLuint iTexCoordAttrib, KRMaterialManager *pMaterialManager); + + GLfloat getMaxDimension(); + GLfloat getMinX(); + GLfloat getMaxX(); + GLfloat getMinY(); + GLfloat getMaxY(); + GLfloat getMinZ(); + GLfloat getMaxZ(); + +private: + + void loadWavefront(std::string path, KRMaterialManager *pMaterialManager); + void loadPack(std::string path, KRMaterialManager *pMaterialManager); + + int m_fdPackFile; + void *m_pPackFile; + int m_iPackFileSize; + + typedef struct { + char szTag[16]; + float minx, miny, minz, maxx, maxy, maxz; + int32_t vertex_count; + int32_t material_count; + } pack_header; + + typedef struct { + int32_t start_vertex; + int32_t vertex_count; + char szName[64]; + } pack_material; + + typedef struct { + GLfloat x; + GLfloat y; + GLfloat z; + } Vertex3D, Vector3D; + + typedef struct { + GLfloat u; + GLfloat v; + } TexCoord; + + typedef struct { + Vertex3D vertex; + Vector3D normal; + Vector3D tangent; + TexCoord texcoord; + } VertexData; + + VertexData *m_pVertexData; + + typedef struct { + GLint start_vertex; + GLsizei vertex_count; + KRMaterial *pMaterial; + } Material; + + GLsizei m_cBuffers; + GLuint *m_pBuffers; + + vector m_materials; + + GLfloat m_minx, m_miny, m_minz, m_maxx, m_maxy, m_maxz; + +}; + + +#endif // KRMODEL_I \ No newline at end of file diff --git a/objview/Classes/krengine/KRModelManager.cpp b/objview/Classes/krengine/KRModelManager.cpp new file mode 100644 index 0000000..8ce0012 --- /dev/null +++ b/objview/Classes/krengine/KRModelManager.cpp @@ -0,0 +1,9 @@ +// +// KRModelManager.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-12-31. +// Copyright 2010 Kearwood Software. All rights reserved. +// + +#include "KRModelManager.h" diff --git a/objview/Classes/krengine/KRModelManager.h b/objview/Classes/krengine/KRModelManager.h new file mode 100644 index 0000000..069b126 --- /dev/null +++ b/objview/Classes/krengine/KRModelManager.h @@ -0,0 +1,7 @@ +// +// KRModelManager.h +// gldemo +// +// Created by Kearwood Gilbert on 10-12-31. +// Copyright 2010 Kearwood Software. All rights reserved. +// diff --git a/objview/Classes/krengine/KRTexture.cpp b/objview/Classes/krengine/KRTexture.cpp new file mode 100644 index 0000000..8190b53 --- /dev/null +++ b/objview/Classes/krengine/KRTexture.cpp @@ -0,0 +1,206 @@ +// +// KRTexture.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-10-23. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#include "KRTexture.h" + +#import +#include +#include +#include +#include +#include +#import + + +#define PVR_TEXTURE_FLAG_TYPE_MASK 0xff + +static char gPVRTexIdentifier[5] = "PVR!"; + +enum +{ + kPVRTextureFlagTypePVRTC_2 = 24, + kPVRTextureFlagTypePVRTC_4 +}; + +typedef struct _PVRTexHeader +{ + uint32_t headerLength; + uint32_t height; + uint32_t width; + uint32_t numMipmaps; + uint32_t flags; + uint32_t dataLength; + uint32_t bpp; + uint32_t bitmaskRed; + uint32_t bitmaskGreen; + uint32_t bitmaskBlue; + uint32_t bitmaskAlpha; + uint32_t pvrTag; + uint32_t numSurfs; +} PVRTexHeader; + +KRTexture::KRTexture() { + m_iName = 0; + m_fdFile = 0; + m_pFile = NULL; + m_fileSize = 0; +} + +KRTexture::~KRTexture() { + if(m_iName != 0) { + glDeleteTextures(1, &m_iName); + } + if(m_pFile != NULL) { + munmap(m_pFile, m_fileSize); + } + if(m_fdFile != 0) { + close(m_fdFile); + } +} + +bool KRTexture::loadFromFile(const char *szFile) { + struct stat statbuf; + m_fdFile = open(szFile, O_RDONLY); + if(m_fdFile < 0) { + return false; + } else { + if(fstat(m_fdFile,&statbuf) < 0) { + return false; + } else { + void *pFile; + if ((pFile = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, m_fdFile, 0)) + == (caddr_t) -1) { + return false; + } else { + m_fileSize = statbuf.st_size; + m_pFile = pFile; + + PVRTexHeader *header = (PVRTexHeader *)pFile; + uint32_t formatFlags = header->flags & PVR_TEXTURE_FLAG_TYPE_MASK; + if (formatFlags == kPVRTextureFlagTypePVRTC_4) { + m_internalFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; + } else if(formatFlags == kPVRTextureFlagTypePVRTC_2) { + m_internalFormat = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; + } + + uint32_t pvrTag = header->pvrTag; + if (gPVRTexIdentifier[0] != ((pvrTag >> 0) & 0xff) || + gPVRTexIdentifier[1] != ((pvrTag >> 8) & 0xff) || + gPVRTexIdentifier[2] != ((pvrTag >> 16) & 0xff) || + gPVRTexIdentifier[3] != ((pvrTag >> 24) & 0xff)) + { + return false; + } + + m_iWidth = header->width; // Note: call __builtin_bswap32 when needed to switch endianness + m_iHeight = header->height; + m_bHasAlpha = header->bitmaskAlpha; + + uint8_t *bytes = ((uint8_t *)pFile) + sizeof(PVRTexHeader); + uint32_t dataLength = header->dataLength, dataOffset = 0, dataSize = 0; + uint32_t width = m_iWidth, height = m_iHeight, bpp = 4; + uint32_t blockSize = 0, widthBlocks = 0, heightBlocks = 0; + + // Calculate the data size for each texture level and respect the minimum number of blocks + while(dataOffset < dataLength) { + if (formatFlags == kPVRTextureFlagTypePVRTC_4) { + blockSize = 4 * 4; // Pixel by pixel block size for 4bpp + widthBlocks = width / 4; + heightBlocks = height / 4; + bpp = 4; + } else { + blockSize = 8 * 4; // Pixel by pixel block size for 2bpp + widthBlocks = width / 8; + heightBlocks = height / 4; + bpp = 2; + } + + // Clamp to minimum number of blocks + if (widthBlocks < 2) { + widthBlocks = 2; + } + if (heightBlocks < 2) { + heightBlocks = 2; + } + dataSize = widthBlocks * heightBlocks * ((blockSize * bpp) / 8); + + dataBlockStruct newBlock; + newBlock.start = bytes+dataOffset; + newBlock.length = dataSize; + + m_blocks.push_back(newBlock); + + dataOffset += dataSize; + + width = width >> 1; + if(width < 1) { + width = 1; + } + height = height >> 1; + if(height < 1) { + height = 1; + } + } + + return true; + } + } + } +} + +bool KRTexture::createGLTexture() { + int width = m_iWidth; + int height = m_iHeight; + GLenum err; + + if (m_blocks.size() > 0) + { + if (m_iName != 0) { + glDeleteTextures(1, &m_iName); + } + + glGenTextures(1, &m_iName); + glBindTexture(GL_TEXTURE_2D, m_iName); + } + + if (m_blocks.size() > 1) { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + } else { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + } + int i=0; + for(std::list::iterator itr = m_blocks.begin(); itr != m_blocks.end(); itr++) { + dataBlockStruct block = *itr; + glCompressedTexImage2D(GL_TEXTURE_2D, i, m_internalFormat, width, height, 0, block.length, block.start); + + err = glGetError(); + if (err != GL_NO_ERROR) { + return false; + } + + width = width >> 1; + if(width < 1) { + width = 1; + } + height = height >> 1; + if(height < 1) { + height = 1; + } + + i++; + } + + return true; +} + +GLuint KRTexture::getName() { + if(m_iName == 0) { + createGLTexture(); + } + return m_iName; +} \ No newline at end of file diff --git a/objview/Classes/krengine/KRTexture.h b/objview/Classes/krengine/KRTexture.h new file mode 100644 index 0000000..a28cb59 --- /dev/null +++ b/objview/Classes/krengine/KRTexture.h @@ -0,0 +1,47 @@ +// +// KRTexture.h +// gldemo +// +// Created by Kearwood Gilbert on 10-10-23. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#import +#import +#import +#import +using std::list; + +#ifndef KRTEXTURE_H +#define KRTEXTURE_H + +class KRTexture { +public: + KRTexture(); + ~KRTexture(); + + bool loadFromFile(const char *szFile); + bool createGLTexture(); + GLuint getName(); + +private: + + GLuint m_iName; + uint32_t m_iWidth; + uint32_t m_iHeight; + GLenum m_internalFormat; + bool m_bHasAlpha; + + struct dataBlockStruct { + void *start; + uint32_t length; + }; + + list m_blocks; + + int m_fdFile; + int m_fileSize; + void *m_pFile; +}; + +#endif diff --git a/objview/Classes/krengine/KRTextureManager.cpp b/objview/Classes/krengine/KRTextureManager.cpp new file mode 100644 index 0000000..7dda2a8 --- /dev/null +++ b/objview/Classes/krengine/KRTextureManager.cpp @@ -0,0 +1,66 @@ +// +// KRTextureManager.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-10-14. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#include "KRTextureManager.h" +#include + +KRTextureManager::KRTextureManager() { + +} + +KRTextureManager::~KRTextureManager() { + for(map::iterator itr = m_textures.begin(); itr != m_textures.end(); ++itr){ + delete (*itr).second; + } +} + +KRTexture *KRTextureManager::loadTexture(const char *szName, const char *szPath) { + KRTexture *pTexture = new KRTexture(); + if(!pTexture->loadFromFile(szPath)) { + delete pTexture; + return NULL; + } + + if(!pTexture->createGLTexture()) { + delete pTexture; + return NULL; + } + + std::string lowerName = szName; + std::transform(lowerName.begin(), lowerName.end(), + lowerName.begin(), ::tolower); + + + + m_textures[lowerName] = pTexture; + return pTexture; +} + +GLuint KRTextureManager::getTextureName(const char *szName) { + KRTexture *pTexture = getTexture(szName); + if(pTexture) { + return pTexture->getName(); + } else { + return NULL; + } +} + +KRTexture *KRTextureManager::getTexture(const char *szName) { + std::string lowerName = szName; + std::transform(lowerName.begin(), lowerName.end(), + lowerName.begin(), ::tolower); + + map::iterator itr = m_textures.find(lowerName); + if(itr == m_textures.end()) { + // Not found + return NULL; + } else { + return (*itr).second; + } + +} \ No newline at end of file diff --git a/objview/Classes/krengine/KRTextureManager.h b/objview/Classes/krengine/KRTextureManager.h new file mode 100644 index 0000000..8c3016f --- /dev/null +++ b/objview/Classes/krengine/KRTextureManager.h @@ -0,0 +1,30 @@ +// +// KRTextureManager.h +// gldemo +// +// Created by Kearwood Gilbert on 10-10-14. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// + +#ifndef KRTEXTUREMANAGER_H +#define KRTEXTUREMANAGER_H + +#include "KRTexture.h" + +#include +using std::map; + +class KRTextureManager { +public: + KRTextureManager(); + ~KRTextureManager(); + + KRTexture *loadTexture(const char *szName, const char *szPath); + GLuint getTextureName(const char *szName); + KRTexture *getTexture(const char *szFile); + +private: + map m_textures; +}; + +#endif diff --git a/objview/Classes/krengine/KRVector3.cpp b/objview/Classes/krengine/KRVector3.cpp new file mode 100644 index 0000000..0ee4957 --- /dev/null +++ b/objview/Classes/krengine/KRVector3.cpp @@ -0,0 +1,76 @@ +// +// KRVector3.cpp +// gldemo +// +// Created by Kearwood Gilbert on 10-12-31. +// Copyright 2010 Kearwood Software. All rights reserved. +// + +#include "KRVector3.h" + +//default constructor +Vector3::Vector3(float X = 0, float Y = 0, float Z = 0) +{ + x = X; + y = Y; + z = Z; +} + +Vector3::~Vector3() +{ +} + +//calculate and return the magnitude of this vector +float Vector3::GetMagnitude() +{ + return sqrtf(x * x + y * y + z * z); +} + +//multiply this vector by a scalar +Vector3 Vector3::operator*(float num) const +{ + return Vector3(x * num, y * num, z * num); +} + +//pass in a vector, pass in a scalar, return the product +/* + Vector3 Vector3::operator*(float num, Vector3 const &vec) +{ + return Vector3(vec.x * num, vec.y * num, vec.z * num); +} + */ + +//add two vectors +Vector3 Vector3::operator+(const Vector3 &vec) const +{ + return Vector3(x + vec.x, y + vec.y, z + vec.z); +} + +//subtract two vectors +Vector3 Vector3::operator-(const Vector3 &vec) const +{ + return Vector3(x - vec.x, y - vec.y, z - vec.z); +} + +//normalize this vector +void Vector3::normalize() +{ + float magnitude = sqrtf(x * x + y * y + z * z); + x /= magnitude; + y /= magnitude; + z /= magnitude; +} + +//calculate and return dot product +float Vector3::dot(const Vector3 &vec) const +{ + return x * vec.x + y * vec.y + z * vec.z; +} + +//calculate and return cross product +Vector3 Vector3::cross(const Vector3 &vec) const +{ + return Vector3(y * vec.z - z * vec.y, + z * vec.x - x * vec.z, + x * vec.y - y * vec.x); +} diff --git a/objview/Classes/krengine/KRVector3.h b/objview/Classes/krengine/KRVector3.h new file mode 100644 index 0000000..df77a79 --- /dev/null +++ b/objview/Classes/krengine/KRVector3.h @@ -0,0 +1,49 @@ +// +// KRVector3.h +// gldemo +// +// Created by Kearwood Gilbert on 10-12-31. +// Copyright 2010 Kearwood Software. All rights reserved. +// + +#ifndef KRVECTOR3 +#define KRVECTOR3 +#include + +class Vector3 +{ + +public: + float x, y, z; + + //default constructor + + Vector3(float X, float Y, float Z); + ~Vector3(); + + //calculate and return the magnitude of this vector + float GetMagnitude(); + + //multiply this vector by a scalar + Vector3 operator*(float num) const; + + //pass in a vector, pass in a scalar, return the product + //friend Vector3 operator*(float num, Vector3 const &vec); + + //add two vectors + Vector3 operator+(const Vector3 &vec) const; + + //subtract two vectors + Vector3 operator-(const Vector3 &vec) const; + + //normalize this vector + void normalize(); + + //calculate and return dot product + float dot(const Vector3 &vec) const; + + //calculate and return cross product + Vector3 cross(const Vector3 &vec) const; +}; + +#endif \ No newline at end of file diff --git a/objview/Classes/krengine/Shaders/ObjectShader.fsh b/objview/Classes/krengine/Shaders/ObjectShader.fsh new file mode 100644 index 0000000..8e96236 --- /dev/null +++ b/objview/Classes/krengine/Shaders/ObjectShader.fsh @@ -0,0 +1,168 @@ +// +// Shader.fsh +// gldemo +// +// Based on http://www.fabiensanglard.net/bumpMapping/index.php + + +/* + +// -- Per vertex lighting + +uniform sampler2D diffuseTexture, specularTexture, normalTexture; +varying mediump vec2 texCoord; +varying mediump vec3 diffuse; +varying mediump vec3 specular; +void main (void) +{ + mediump vec3 texColour = vec3(texture2D(diffuseTexture, texCoord)); + mediump vec3 specColor = vec3(texture2D(specularTexture, texCoord)); + mediump vec3 normalVal = vec3(texture2D(normalTexture, texCoord)); + //mediump vec3 colour = (texColour * diffuse) + specular; + mediump vec3 colour = (texColour * diffuse) + (specColor * specular); + gl_FragColor = vec4(colour, 1.0); +} +*/ + +/* + +// -- Per Pixel lighting, test 1 -- + +uniform sampler2D diffuseTexture, specularTexture, normalTexture; +uniform mediump vec3 material_ambient, material_diffuse, material_specular; + +varying mediump vec2 texCoord; +varying mediump vec3 normal; +varying mediump vec3 ePos; + +const mediump float shininess = 4.0; +const mediump vec3 LightPos = vec3(20.0, 00.0, 10.0); +const mediump vec3 LightCol = vec3(2.0,2.0,2.0); + + +//directional light function // +// spFlg flag using specular or not. +// nrml nrml vector in the eye coordinate. +// ePos vertex position in the eye coordinate. +void DirectionalLight(inout mediump vec3 diffuse, inout mediump vec3 specular, in mediump int spFlg, in mediump vec3 nrml, in mediump vec3 ePos){ + // calculate the light direction vector. + mediump vec3 lightDir = normalize(LightPos); + // calculate the half vector between eye position and light position. + mediump vec3 halfV = normalize(-ePos + LightPos); + // calculate diffuse light intensity. + mediump float dVP = max(dot(nrml,lightDir), 0.0); + // calculate approximated specular light base intensity. + mediump float dHV = max(dot(nrml,halfV),0.0); + // if the diffuse is not zero and spFlg is On, + // calculate specular light intensity with shininess, + // or turn off the specular light. + mediump float pf; + if (dVP>.0 && spFlg==1) pf = pow(dHV, shininess); + else pf = 0.0; + diffuse += dVP*LightCol; + specular += pf*LightCol; +} + +void main (void) +{ + + mediump vec3 texColour = vec3(texture2D(diffuseTexture, texCoord)); + mediump vec3 specColor = vec3(texture2D(specularTexture, texCoord)); + mediump vec3 normalVal = vec3(texture2D(normalTexture, texCoord)); + + + // initalize light intensity parameter. + mediump vec3 diffuse = vec3(0.75); + mediump vec3 specular = vec3(0.0); + + + DirectionalLight(diffuse, specular, 1, normalize(normal + normalVal), ePos); + + specular *= material_specular; + + diffuse = diffuse * material_diffuse + material_ambient; + + + //mediump vec3 colour = (texColour * diffuse) + specular; + mediump vec3 colour = (texColour * diffuse) + (specColor * specular); + gl_FragColor = vec4(colour, 1.0); + +} + +*/ + +/* +// -- Per Pixel lighting, test 2 -- +uniform sampler2D diffuseTexture, specularTexture, normalTexture; +varying mediump vec2 texCoord; +varying mediump vec3 lightVec; +varying mediump vec3 halfVec; +//varying mediump vec3 eyeVec; +uniform mediump vec3 material_ambient, material_diffuse, material_specular; + +void main() +{ + // lookup normal from normal map, move from [0,1] to [-1, 1] range, normalize + mediump vec3 normal = normalize(2.0 * texture2D(normalTexture,texCoord).rgb - 1.0); + + mediump float lamberFactor= max (dot (lightVec, normal), 0.0); + + + gl_FragColor = vec4(0.0); + + if (lamberFactor > 0.0) + { + // compute diffuse lighting + mediump vec4 diffuseMaterial = texture2D(diffuseTexture, texCoord); + mediump vec4 diffuseLight = vec4(material_diffuse, 1.0) + 1.0; // 1.0 added so there will not be complete darkness + + // compute specular lighting + mediump vec4 specularLight = texture2D(specularTexture, texCoord); // Specular value comes from a texture + mediump float shininess = pow (max (dot (halfVec, normal), 0.0), 2.0); + + gl_FragColor = diffuseMaterial * diffuseLight * lamberFactor ; + gl_FragColor += vec4(material_specular, 1.0) * specularLight * shininess; + + } + + // compute ambient + gl_FragColor += vec4(material_ambient, 1.0);; +} + */ + + +// -- Per Pixel lighting, test 3 -- +uniform sampler2D diffuseTexture, specularTexture, normalTexture; +varying mediump vec2 texCoord; +varying mediump vec3 lightVec; +varying mediump vec3 halfVec; +uniform lowp vec3 material_ambient, material_diffuse, material_specular; + +void main() +{ + // compute diffuse lighting + mediump vec4 diffuseMaterial = texture2D(diffuseTexture, texCoord); + + /* + // lookup normal from normal map, move from [0,1] to [-1, 1] range, normalize + mediump vec3 normal = normalize(2.0 * texture2D(normalTexture,texCoord).rgb - 1.0); + + mediump float lamberFactor= max (dot (lightVec, normal), 0.0); + + // Add ambient light and alpha component from diffuse texture map + gl_FragColor = vec4(material_ambient, diffuseMaterial.w); + + // compute specular lighting + mediump vec3 specularLight = vec3(texture2D(specularTexture, texCoord)); // Specular value comes from a texture + mediump float shininess = pow (max (dot (halfVec, normal), 0.0), 2.0); + + // Add diffuse light + gl_FragColor += vec4(vec3(diffuseMaterial) * material_diffuse * lamberFactor, 0.0); + + // Add specular light + gl_FragColor += vec4(material_specular * specularLight * shininess, 0.0); + */ + + //gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); + gl_FragColor = diffuseMaterial; +} diff --git a/objview/Classes/krengine/Shaders/ObjectShader.vsh b/objview/Classes/krengine/Shaders/ObjectShader.vsh new file mode 100644 index 0000000..cf30b5c --- /dev/null +++ b/objview/Classes/krengine/Shaders/ObjectShader.vsh @@ -0,0 +1,171 @@ +// +// Shader.vsh +// gldemo +// +// Created by Kearwood Gilbert on 10-09-16. +// Copyright (c) 2010 Kearwood Software. All rights reserved. +// +/* + + // -- Per vertex lighting + + attribute highp vec3 myVertex, myNormal; + attribute mediump vec2 myUV; + uniform mediump mat4 myMVPMatrix, myModelView; // mvpmatrix is the result of multiplying the model, view, and projection matrices + uniform mediump mat3 myModelViewIT; + uniform mediump vec3 material_ambient, material_diffuse, material_specular; + + varying mediump vec2 texCoord; + varying mediump vec3 diffuse, specular; + varying mediump vec3 normal; + const mediump float shininess = 4.0; + const mediump float cutoff = 0.975, exp = 100.0; + const mediump vec3 LightPos = vec3(20.0, 00.0, 10.0); + const mediump vec3 LightCol = vec3(2.0,2.0,2.0); + + //directional light function // + // spFlg flag using specular or not. + // nrml nrml vector in the eye coordinate. + // ePos vertex position in the eye coordinate. + void DirectionalLight(in mediump int spFlg, in mediump vec3 nrml, in mediump vec3 ePos){ + // calculate the light direction vector. + mediump vec3 lightDir = normalize(LightPos); + // calculate the half vector between eye position and light position. + mediump vec3 halfV = normalize(-ePos + LightPos); + // calculate diffuse light intensity. + mediump float dVP = max(dot(nrml,lightDir), 0.0); + // calculate approximated specular light base intensity. + mediump float dHV = max(dot(nrml,halfV),0.0); + // if the diffuse is not zero and spFlg is On, + // calculate specular light intensity with shininess, + // or turn off the specular light. + mediump float pf; + if (dVP>.0 && spFlg==1) pf = pow(dHV, shininess); + else pf = 0.0; + diffuse += dVP*LightCol; + specular += pf*LightCol; + } + + // main function // + void main(void){ + // transform the normal vector from the model coordinate to the eye coordinate. + normal = normalize(myModelViewIT * myNormal); + // calculate the vertex position in the eye coordinate. + highp vec3 ePos = vec3(myModelView * vec4(myVertex,1.0)); + // initalize light intensity parameter. + //diffuse = material_ambient; + diffuse = vec3(0.75); + + specular = vec3(0.0); + + DirectionalLight(1, normal, ePos); + + specular *= material_specular; + + diffuse = diffuse * material_diffuse + material_ambient; + + + // Transform position + gl_Position = myMVPMatrix * vec4(myVertex,1.0); + // Pass UV co-ordinates + texCoord = myUV.st; + } + +*/ + +/* + + // -- Per Pixel lighting, test 1 -- + +attribute highp vec3 myVertex, myNormal, myTangent; +attribute mediump vec2 myUV; +uniform mediump mat4 myMVPMatrix, myModelView; // mvpmatrix is the result of multiplying the model, view, and projection matrices +uniform mediump mat3 myModelViewIT; +uniform mediump vec3 material_ambient, material_diffuse, material_specular; + +varying mediump vec2 texCoord; +varying mediump vec3 normal; +varying mediump vec3 ePos; + + +// main function // +void main(void){ + // Transform position + gl_Position = myMVPMatrix * vec4(myVertex,1.0); + // Pass UV co-ordinates + texCoord = myUV.st; + + // transform the normal vector from the model coordinate to the eye coordinate. + normal = normalize(myModelViewIT * myNormal); + + // calculate the vertex position in the eye coordinate. + ePos = vec3(myModelView * vec4(myVertex,1.0)); + + //mat_ambient = material_ambient; + //mat_diffuse = material_diffuse; + //mat_specular = material_specular; +} + +*/ + + +// -- Per Pixel lighting, test 2 -- + +const mediump vec3 LightPos = vec3(40, 20.0, -90.0); + +attribute highp vec3 myVertex, myNormal; +attribute highp vec3 myTangent; +attribute mediump vec2 myUV; +uniform highp mat4 myMVPMatrix, myModelView; // mvpmatrix is the result of multiplying the model, view, and projection matrices +uniform highp mat3 myModelViewIT; +uniform lowp vec3 material_ambient, material_diffuse, material_specular; + +varying mediump vec2 texCoord; +/* +varying mediump vec3 lightVec; +varying mediump vec3 halfVec; +*/ + + +//varying mediump vec3 eyeVec; + +void main() +{ + // Transform position + gl_Position = myMVPMatrix * vec4(myVertex,1.0); + + // Pass UV co-ordinates + texCoord = myUV.st; + + /* + + // Building the matrix Eye Space -> Tangent Space + vec3 n = normalize(vec3(myModelView * vec4(myNormal, 1.0))); + vec3 t = normalize(vec3(myModelView * vec4(myTangent, 1.0))); + vec3 b = cross(n, t); + + vec3 vertexPosition = vec3(myMVPMatrix * vec4(myVertex, 1.0)); + vec3 lightDir = normalize(LightPos - vertexPosition); + + // transform light and half angle vectors by tangent basis + vec3 v; + v.x = dot(lightDir, t); + v.y = dot(lightDir, b); + v.z = dot(lightDir, n); + lightVec = normalize(v); + +// v.x = dot(vertexPosition, t); +// v.y = dot(vertexPosition, b); +// v.z = dot(vertexPosition, n); +// eyeVec = normalize(v); + + vertexPosition = normalize(vertexPosition); + + // Normalize the halfVector to pass it to the fragment shader + vec3 halfVector = normalize((vertexPosition + lightDir) / 2.0); + v.x = dot (halfVector, t); + v.y = dot (halfVector, b); + v.z = dot (halfVector, n); + halfVec = normalize (v); + */ +} diff --git a/objview/Classes/krengine/Shaders/PostShader.fsh b/objview/Classes/krengine/Shaders/PostShader.fsh new file mode 100644 index 0000000..c86ef29 --- /dev/null +++ b/objview/Classes/krengine/Shaders/PostShader.fsh @@ -0,0 +1,150 @@ +#define DOF_QUALITY 0 +#define ENABLE_VIDEO_BG 0 +#define PIXEL_SHIFT_1 0.001 +#define PIXEL_SHIFT_2 0.002 +#define PIXEL_SHIFT_3 0.003 +#define PIXEL_SHIFT_4 0.004 + + +varying mediump vec2 textureCoordinate; +precision lowp float; + +#if ENABLE_VIDEO_BG == 0 +uniform lowp sampler2D videoFrame; +#endif + +uniform lowp sampler2D renderFrame; +uniform lowp sampler2D depthFrame; + +void main() +{ + + lowp vec4 renderColor = texture2D(renderFrame, textureCoordinate); + mediump float depth = texture2D(depthFrame, textureCoordinate).r; + + mediump vec4 pixelColor = renderColor; + + +#if DOF_QUALITY == 2 + + // Render high quality circle of confusion + // __XXX__ + // _XXXXX_ + // _XXXXX_ + // _XXXXX_ + // __XXX__ + mediump float cf1 = PIXEL_SHIFT_1; + mediump float cf2 = PIXEL_SHIFT_2; + + mediump float bx1 = textureCoordinate.s + cf1; + mediump float bx2 = textureCoordinate.s + cf2; + mediump float bxm1 = textureCoordinate.s - cf1; + mediump float bxm2 = textureCoordinate.s - cf2; + + mediump float by1 = textureCoordinate.t + cf1; + mediump float by2 = textureCoordinate.t + cf2; + mediump float bym1 = textureCoordinate.t - cf1; + mediump float bym2 = textureCoordinate.t - cf2; + + pixelColor += texture2D(renderFrame, vec2(bx1, textureCoordinate.t)); + pixelColor += texture2D(renderFrame, vec2(bxm1, textureCoordinate.t)); + pixelColor += texture2D(renderFrame, vec2(bx2, textureCoordinate.t)); + pixelColor += texture2D(renderFrame, vec2(bxm2, textureCoordinate.t)); + + pixelColor += texture2D(renderFrame, vec2(textureCoordinate.s, by1)); + pixelColor += texture2D(renderFrame, vec2(bx1, by1)); + pixelColor += texture2D(renderFrame, vec2(bxm1, by1)); + pixelColor += texture2D(renderFrame, vec2(bx2, by1)); + pixelColor += texture2D(renderFrame, vec2(bxm2, by1)); + + pixelColor += texture2D(renderFrame, vec2(textureCoordinate.s, by2)); + pixelColor += texture2D(renderFrame, vec2(bx1, by2)); + pixelColor += texture2D(renderFrame, vec2(bxm1, by2)); + + pixelColor += texture2D(renderFrame, vec2(textureCoordinate.s,bym1)); + pixelColor += texture2D(renderFrame, vec2(bx1,bym1)); + pixelColor += texture2D(renderFrame, vec2(bxm1,bym1)); + pixelColor += texture2D(renderFrame, vec2(bx2,bym1)); + pixelColor += texture2D(renderFrame, vec2(bxm2,bym1)); + + pixelColor += texture2D(renderFrame, vec2(bx1, bym2)); + pixelColor += texture2D(renderFrame, vec2(bx1, bym2)); + pixelColor += texture2D(renderFrame, vec2(bxm1, bym2)); + pixelColor /= 21.0; + +#endif +// DOF_QUALITY == 2 + +#if DOF_QUALITY == 1 + + // Render low quality circle of confusion + // ___X___ + // __XXX__ + // _XXXXX_ + // __XXX__ + // ___X___ + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(0, -PIXEL_SHIFT_2)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(-PIXEL_SHIFT_1, -PIXEL_SHIFT_1)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(0, -PIXEL_SHIFT_1)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(+PIXEL_SHIFT_1, -PIXEL_SHIFT_1)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(-PIXEL_SHIFT_2, 0)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(-PIXEL_SHIFT_1, 0)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(+PIXEL_SHIFT_1, 0)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(+PIXEL_SHIFT_2, 0)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(-PIXEL_SHIFT_1, -PIXEL_SHIFT_1)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(0, -PIXEL_SHIFT_1)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(+PIXEL_SHIFT_1, -PIXEL_SHIFT_1)); + pixelColor += texture2D(renderFrame, textureCoordinate + vec2(0, -PIXEL_SHIFT_2)); + pixelColor /= 13.0; + +#endif +// DOF_QUALITY == 1 + +#if DOF_QUALITY > 0 + mediump float focusDepth = texture2D(depthFrame, vec2(0.5, 0.5)).r; + mediump float blurAmount = clamp((depth - focusDepth) * 10.0, 0.0, 1.0); + pixelColor = pixelColor * blurAmount + renderColor * (1.0 - blurAmount); +#endif + + // ---- DEPTH_OF_FIELD END ---- + + + + + // ---- NIGHT_TIME START ---- + + // Un-comment to enable night time / flash effect + // pixelColor *= 1.0 - clamp((depth - 0.7) * 2.0, 0.0, 1.0); + + // ---- NIGHT_TIME END ---- + + // ---- VIDEO_BG START ---- + + /* + // Overlay video background + if(depth == 1.0) { + //FILTER COLOR BY CALCULATING PER PIXEL DOT PRODUCT + pixelColor = vec4(dot(vec3(texture2D(videoFrame, textureCoordinate)), vec3(.222, .707, .071))); + } + */ + + // ---- VIDEO_BG END ---- + + + // ---- VIGNETTE START ---- + + // Render vignette effect + + // pixelColor *= 1.0 - clamp((distance(textureCoordinate, vec2(0.5, 0.5)) - 0.4), 0.0, 1.0); + + // ---- VIGNETTE END ---- + + gl_FragColor = pixelColor; + + + //PASSTHROUGH STATEMENT + // gl_FragColor = texture2D(depthFrame, textureCoordinate); + + //gl_FragColor = vec4(vec3(blurAmount), 1.0); + +} diff --git a/objview/Classes/krengine/Shaders/PostShader.vsh b/objview/Classes/krengine/Shaders/PostShader.vsh new file mode 100644 index 0000000..6121696 --- /dev/null +++ b/objview/Classes/krengine/Shaders/PostShader.vsh @@ -0,0 +1,10 @@ +attribute vec4 position; +attribute lowp vec4 inputTextureCoordinate; + +varying mediump vec2 textureCoordinate; + +void main() +{ + gl_Position = position; + textureCoordinate = inputTextureCoordinate.xy; +} \ No newline at end of file diff --git a/objview/Classes/krengine/Shaders/ShadowShader.fsh b/objview/Classes/krengine/Shaders/ShadowShader.fsh new file mode 100644 index 0000000..c223ed3 --- /dev/null +++ b/objview/Classes/krengine/Shaders/ShadowShader.fsh @@ -0,0 +1,35 @@ +// -- Per Pixel lighting, test 3 -- +uniform sampler2D diffuseTexture, specularTexture, normalTexture; +varying mediump vec2 texCoord; +varying mediump vec3 lightVec; +varying mediump vec3 halfVec; +uniform lowp vec3 material_ambient, material_diffuse, material_specular; + +void main() +{ + // compute diffuse lighting + mediump vec4 diffuseMaterial = texture2D(diffuseTexture, texCoord); + + /* + // lookup normal from normal map, move from [0,1] to [-1, 1] range, normalize + mediump vec3 normal = normalize(2.0 * texture2D(normalTexture,texCoord).rgb - 1.0); + + mediump float lamberFactor= max (dot (lightVec, normal), 0.0); + + // Add ambient light and alpha component from diffuse texture map + gl_FragColor = vec4(material_ambient, diffuseMaterial.w); + + // compute specular lighting + mediump vec3 specularLight = vec3(texture2D(specularTexture, texCoord)); // Specular value comes from a texture + mediump float shininess = pow (max (dot (halfVec, normal), 0.0), 2.0); + + // Add diffuse light + gl_FragColor += vec4(vec3(diffuseMaterial) * material_diffuse * lamberFactor, 0.0); + + // Add specular light + gl_FragColor += vec4(material_specular * specularLight * shininess, 0.0); + */ + + //gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); + gl_FragColor = diffuseMaterial; +} \ No newline at end of file diff --git a/objview/Classes/krengine/Shaders/ShadowShader.vsh b/objview/Classes/krengine/Shaders/ShadowShader.vsh new file mode 100644 index 0000000..4633925 --- /dev/null +++ b/objview/Classes/krengine/Shaders/ShadowShader.vsh @@ -0,0 +1,58 @@ +const mediump vec3 LightPos = vec3(40, 20.0, -90.0); + +attribute highp vec3 myVertex, myNormal; +attribute highp vec3 myTangent; +attribute mediump vec2 myUV; +uniform highp mat4 myMVPMatrix, myModelView; // mvpmatrix is the result of multiplying the model, view, and projection matrices +uniform highp mat3 myModelViewIT; +uniform lowp vec3 material_ambient, material_diffuse, material_specular; + +varying mediump vec2 texCoord; +/* + varying mediump vec3 lightVec; + varying mediump vec3 halfVec; + */ + + +//varying mediump vec3 eyeVec; + +void main() +{ + // Transform position + gl_Position = myMVPMatrix * vec4(myVertex,1.0); + + // Pass UV co-ordinates + texCoord = myUV.st; + + /* + + // Building the matrix Eye Space -> Tangent Space + vec3 n = normalize(vec3(myModelView * vec4(myNormal, 1.0))); + vec3 t = normalize(vec3(myModelView * vec4(myTangent, 1.0))); + vec3 b = cross(n, t); + + vec3 vertexPosition = vec3(myMVPMatrix * vec4(myVertex, 1.0)); + vec3 lightDir = normalize(LightPos - vertexPosition); + + // transform light and half angle vectors by tangent basis + vec3 v; + v.x = dot(lightDir, t); + v.y = dot(lightDir, b); + v.z = dot(lightDir, n); + lightVec = normalize(v); + + // v.x = dot(vertexPosition, t); + // v.y = dot(vertexPosition, b); + // v.z = dot(vertexPosition, n); + // eyeVec = normalize(v); + + vertexPosition = normalize(vertexPosition); + + // Normalize the halfVector to pass it to the fragment shader + vec3 halfVector = normalize((vertexPosition + lightDir) / 2.0); + v.x = dot (halfVector, t); + v.y = dot (halfVector, b); + v.z = dot (halfVector, n); + halfVec = normalize (v); + */ +} \ No newline at end of file diff --git a/objview/KRObjView-Info.plist b/objview/KRObjView-Info.plist new file mode 100644 index 0000000..868a072 --- /dev/null +++ b/objview/KRObjView-Info.plist @@ -0,0 +1,38 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleDisplayName + ${PRODUCT_NAME} + UIFileSharingEnabled + + CFBundleExecutable + ${EXECUTABLE_NAME} + CFBundleIconFile + + CFBundleIdentifier + com.kearwood.${PRODUCT_NAME:rfc1034identifier} + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + ${PRODUCT_NAME} + CFBundlePackageType + APPL + CFBundleSignature + ???? + CFBundleVersion + 1.0 + LSRequiresIPhoneOS + + NSMainNibFile + MainWindow + NSMainNibFile~ipad + MainWindow-iPad + UISupportedInterfaceOrientations + + UIInterfaceOrientationLandscapeLeft + + + diff --git a/objview/KRObjView.xcodeproj/applemac.mode1v3 b/objview/KRObjView.xcodeproj/applemac.mode1v3 new file mode 100644 index 0000000..fdbec79 --- /dev/null +++ b/objview/KRObjView.xcodeproj/applemac.mode1v3 @@ -0,0 +1,1442 @@ + + + + + ActivePerspectiveName + Project + AllowedModules + + + BundleLoadPath + + MaxInstances + n + Module + PBXSmartGroupTreeModule + Name + Groups and Files Outline View + + + BundleLoadPath + + MaxInstances + n + Module + PBXNavigatorGroup + Name + Editor + + + BundleLoadPath + + MaxInstances + n + Module + XCTaskListModule + Name + Task List + + + BundleLoadPath + + MaxInstances + n + Module + XCDetailModule + Name + File and Smart Group Detail Viewer + + + BundleLoadPath + + MaxInstances + 1 + Module + PBXBuildResultsModule + Name + Detailed Build Results Viewer + + + BundleLoadPath + + MaxInstances + 1 + Module + PBXProjectFindModule + Name + Project Batch Find Tool + + + BundleLoadPath + + MaxInstances + n + Module + XCProjectFormatConflictsModule + Name + Project Format Conflicts List + + + BundleLoadPath + + MaxInstances + n + Module + PBXBookmarksModule + Name + Bookmarks Tool + + + BundleLoadPath + + MaxInstances + n + Module + PBXClassBrowserModule + Name + Class Browser + + + BundleLoadPath + + MaxInstances + n + Module + PBXCVSModule + Name + Source Code Control Tool + + + BundleLoadPath + + MaxInstances + n + Module + PBXDebugBreakpointsModule + Name + Debug Breakpoints Tool + + + BundleLoadPath + + MaxInstances + n + Module + XCDockableInspector + Name + Inspector + + + BundleLoadPath + + MaxInstances + n + Module + PBXOpenQuicklyModule + Name + Open Quickly Tool + + + BundleLoadPath + + MaxInstances + 1 + Module + PBXDebugSessionModule + Name + Debugger + + + BundleLoadPath + + MaxInstances + 1 + Module + PBXDebugCLIModule + Name + Debug Console + + + BundleLoadPath + + MaxInstances + n + Module + XCSnapshotModule + Name + Snapshots Tool + + + BundlePath + /Developer/Library/PrivateFrameworks/DevToolsInterface.framework/Resources + Description + DefaultDescriptionKey + DockingSystemVisible + + Extension + mode1v3 + FavBarConfig + + PBXProjectModuleGUID + 1063FCC4136D865B00EE555B + XCBarModuleItemNames + + XCBarModuleItems + + + FirstTimeWindowDisplayed + + Identifier + com.apple.perspectives.project.mode1v3 + MajorVersion + 33 + MinorVersion + 0 + Name + Default + Notifications + + + XCObserverAutoDisconnectKey + + XCObserverDefintionKey + + PBXStatusErrorsKey + 0 + + XCObserverFactoryKey + XCPerspectivesSpecificationIdentifier + XCObserverGUIDKey + XCObserverProjectIdentifier + XCObserverNotificationKey + PBXStatusBuildStateMessageNotification + XCObserverTargetKey + XCMainBuildResultsModuleGUID + XCObserverTriggerKey + awakenModuleWithObserver: + XCObserverValidationKey + + PBXStatusErrorsKey + 2 + + + + OpenEditors + + PerspectiveWidths + + -1 + -1 + + Perspectives + + + ChosenToolbarItems + + active-combo-popup + action + NSToolbarFlexibleSpaceItem + debugger-enable-breakpoints + clean + build + com.apple.ide.PBXToolbarStopButton + get-info + toggle-editor + NSToolbarFlexibleSpaceItem + com.apple.pbx.toolbar.searchfield + + ControllerClassBaseName + + IconName + WindowOfProjectWithEditor + Identifier + perspective.project + IsVertical + + Layout + + + BecomeActive + + ContentConfiguration + + PBXBottomSmartGroupGIDs + + 1C37FBAC04509CD000000102 + 1C37FAAC04509CD000000102 + 1C37FABC05509CD000000102 + 1C37FABC05539CD112110102 + E2644B35053B69B200211256 + 1C37FABC04509CD000100104 + 1CC0EA4004350EF90044410B + 1CC0EA4004350EF90041110B + + PBXProjectModuleGUID + 1CE0B1FE06471DED0097A5F4 + PBXProjectModuleLabel + Files + PBXProjectStructureProvided + yes + PBXSmartGroupTreeModuleColumnData + + PBXSmartGroupTreeModuleColumnWidthsKey + + 294 + + PBXSmartGroupTreeModuleColumnsKey_v4 + + MainColumn + + + PBXSmartGroupTreeModuleOutlineStateKey_v7 + + PBXSmartGroupTreeModuleOutlineStateExpansionKey + + 29B97314FDCFA39411CA2CEA + 080E96DDFE201D6D7F000001 + 1063FC5B136D5EC200EE555B + 29B97315FDCFA39411CA2CEA + 1C37FABC05509CD000000102 + + PBXSmartGroupTreeModuleOutlineStateSelectionKey + + + 7 + 1 + 0 + + + PBXSmartGroupTreeModuleOutlineStateVisibleRectKey + {{0, 0}, {294, 705}} + + PBXTopSmartGroupGIDs + + XCIncludePerspectivesSwitch + + XCSharingToken + com.apple.Xcode.GFSharingToken + + GeometryConfiguration + + Frame + {{0, 0}, {311, 723}} + GroupTreeTableConfiguration + + MainColumn + 294 + + RubberWindowFrame + 62 272 1440 764 0 0 1920 1058 + + Module + PBXSmartGroupTreeModule + Proportion + 311pt + + + Dock + + + ContentConfiguration + + PBXProjectModuleGUID + 1CE0B20306471E060097A5F4 + PBXProjectModuleLabel + KRObjViewGLView.m + PBXSplitModuleInNavigatorKey + + Split0 + + PBXProjectModuleGUID + 1CE0B20406471E060097A5F4 + PBXProjectModuleLabel + KRObjViewGLView.m + _historyCapacity + 0 + bookmark + 1063FD44136E8D0C00EE555B + history + + 1063FCAF136D865B00EE555B + 1063FCB0136D865B00EE555B + 1063FCB2136D865B00EE555B + 1063FCB6136D865B00EE555B + 1063FD3A136E8D0C00EE555B + 1063FD3B136E8D0C00EE555B + 1063FD3C136E8D0C00EE555B + 1063FD3D136E8D0C00EE555B + 1063FD3E136E8D0C00EE555B + 1063FD3F136E8D0C00EE555B + 1063FD40136E8D0C00EE555B + 1063FD41136E8D0C00EE555B + 1063FD42136E8D0C00EE555B + 1063FD43136E8D0C00EE555B + + + SplitCount + 1 + + StatusBarVisibility + + + GeometryConfiguration + + Frame + {{0, 0}, {1124, 718}} + RubberWindowFrame + 62 272 1440 764 0 0 1920 1058 + + Module + PBXNavigatorGroup + Proportion + 718pt + + + ContentConfiguration + + PBXProjectModuleGUID + 1CE0B20506471E060097A5F4 + PBXProjectModuleLabel + Detail + + GeometryConfiguration + + Frame + {{0, 723}, {1124, 0}} + RubberWindowFrame + 62 272 1440 764 0 0 1920 1058 + + Module + XCDetailModule + Proportion + 0pt + + + Proportion + 1124pt + + + Name + Project + ServiceClasses + + XCModuleDock + PBXSmartGroupTreeModule + XCModuleDock + PBXNavigatorGroup + XCDetailModule + + TableOfContents + + 1063FCC2136D865B00EE555B + 1CE0B1FE06471DED0097A5F4 + 1063FCC3136D865B00EE555B + 1CE0B20306471E060097A5F4 + 1CE0B20506471E060097A5F4 + + ToolbarConfigUserDefaultsMinorVersion + 2 + ToolbarConfiguration + xcode.toolbar.config.defaultV3 + + + ControllerClassBaseName + + IconName + WindowOfProject + Identifier + perspective.morph + IsVertical + 0 + Layout + + + BecomeActive + 1 + ContentConfiguration + + PBXBottomSmartGroupGIDs + + 1C37FBAC04509CD000000102 + 1C37FAAC04509CD000000102 + 1C08E77C0454961000C914BD + 1C37FABC05509CD000000102 + 1C37FABC05539CD112110102 + E2644B35053B69B200211256 + 1C37FABC04509CD000100104 + 1CC0EA4004350EF90044410B + 1CC0EA4004350EF90041110B + + PBXProjectModuleGUID + 11E0B1FE06471DED0097A5F4 + PBXProjectModuleLabel + Files + PBXProjectStructureProvided + yes + PBXSmartGroupTreeModuleColumnData + + PBXSmartGroupTreeModuleColumnWidthsKey + + 186 + + PBXSmartGroupTreeModuleColumnsKey_v4 + + MainColumn + + + PBXSmartGroupTreeModuleOutlineStateKey_v7 + + PBXSmartGroupTreeModuleOutlineStateExpansionKey + + 29B97314FDCFA39411CA2CEA + 1C37FABC05509CD000000102 + + PBXSmartGroupTreeModuleOutlineStateSelectionKey + + + 0 + + + PBXSmartGroupTreeModuleOutlineStateVisibleRectKey + {{0, 0}, {186, 337}} + + PBXTopSmartGroupGIDs + + XCIncludePerspectivesSwitch + 1 + XCSharingToken + com.apple.Xcode.GFSharingToken + + GeometryConfiguration + + Frame + {{0, 0}, {203, 355}} + GroupTreeTableConfiguration + + MainColumn + 186 + + RubberWindowFrame + 373 269 690 397 0 0 1440 878 + + Module + PBXSmartGroupTreeModule + Proportion + 100% + + + Name + Morph + PreferredWidth + 300 + ServiceClasses + + XCModuleDock + PBXSmartGroupTreeModule + + TableOfContents + + 11E0B1FE06471DED0097A5F4 + + ToolbarConfiguration + xcode.toolbar.config.default.shortV3 + + + PerspectivesBarVisible + + ShelfIsVisible + + SourceDescription + file at '/Developer/Library/PrivateFrameworks/DevToolsInterface.framework/Resources/XCPerspectivesSpecificationMode1.xcperspec' + StatusbarIsVisible + + TimeStamp + 326012172.37813801 + ToolbarConfigUserDefaultsMinorVersion + 2 + ToolbarDisplayMode + 1 + ToolbarIsVisible + + ToolbarSizeMode + 2 + Type + Perspectives + UpdateMessage + The Default Workspace in this version of Xcode now includes support to hide and show the detail view (what has been referred to as the "Metro-Morph" feature). You must discard your current Default Workspace settings and update to the latest Default Workspace in order to gain this feature. Do you wish to update to the latest Workspace defaults for project '%@'? + WindowJustification + 5 + WindowOrderList + + 1063FCC5136D865B00EE555B + 1063FCC6136D865B00EE555B + 1C78EAAD065D492600B07095 + 1CD10A99069EF8BA00B06720 + 1063FBD2136BA3B000EE555B + /Users/applemac/Desktop/KRObjView/KRObjView.xcodeproj + + WindowString + 62 272 1440 764 0 0 1920 1058 + WindowToolsV3 + + + FirstTimeWindowDisplayed + + Identifier + windowTool.build + IsVertical + + Layout + + + Dock + + + ContentConfiguration + + PBXProjectModuleGUID + 1CD0528F0623707200166675 + PBXProjectModuleLabel + KRObjViewViewController.m + StatusBarVisibility + + + GeometryConfiguration + + Frame + {{0, 0}, {831, 237}} + RubberWindowFrame + 42 493 831 519 0 0 1920 1058 + + Module + PBXNavigatorGroup + Proportion + 237pt + + + BecomeActive + + ContentConfiguration + + PBXProjectModuleGUID + XCMainBuildResultsModuleGUID + PBXProjectModuleLabel + Build Results + XCBuildResultsTrigger_Collapse + 1021 + XCBuildResultsTrigger_Open + 1012 + + GeometryConfiguration + + Frame + {{0, 242}, {831, 236}} + RubberWindowFrame + 42 493 831 519 0 0 1920 1058 + + Module + PBXBuildResultsModule + Proportion + 236pt + + + Proportion + 478pt + + + Name + Build Results + ServiceClasses + + PBXBuildResultsModule + + StatusbarIsVisible + + TableOfContents + + 1063FBD2136BA3B000EE555B + 1063FBD3136BA3B000EE555B + 1CD0528F0623707200166675 + XCMainBuildResultsModuleGUID + + ToolbarConfiguration + xcode.toolbar.config.buildV3 + WindowContentMinSize + 486 300 + WindowString + 42 493 831 519 0 0 1920 1058 + WindowToolGUID + 1063FBD2136BA3B000EE555B + WindowToolIsVisible + + + + FirstTimeWindowDisplayed + + Identifier + windowTool.debugger + IsVertical + + Layout + + + Dock + + + ContentConfiguration + + Debugger + + HorizontalSplitView + + _collapsingFrameDimension + 0.0 + _indexOfCollapsedView + 0 + _percentageOfCollapsedView + 0.0 + isCollapsed + yes + sizes + + {{0, 0}, {316, 185}} + {{316, 0}, {378, 185}} + + + VerticalSplitView + + _collapsingFrameDimension + 0.0 + _indexOfCollapsedView + 0 + _percentageOfCollapsedView + 0.0 + isCollapsed + yes + sizes + + {{0, 0}, {694, 185}} + {{0, 185}, {694, 196}} + + + + LauncherConfigVersion + 8 + PBXProjectModuleGUID + 1C162984064C10D400B95A72 + PBXProjectModuleLabel + Debug - GLUTExamples (Underwater) + + GeometryConfiguration + + DebugConsoleVisible + None + DebugConsoleWindowFrame + {{200, 200}, {500, 300}} + DebugSTDIOWindowFrame + {{200, 200}, {500, 300}} + Frame + {{0, 0}, {694, 381}} + PBXDebugSessionStackFrameViewKey + + DebugVariablesTableConfiguration + + Name + 120 + Value + 85 + Summary + 148 + + Frame + {{316, 0}, {378, 185}} + RubberWindowFrame + 63 590 694 422 0 0 1920 1058 + + RubberWindowFrame + 63 590 694 422 0 0 1920 1058 + + Module + PBXDebugSessionModule + Proportion + 381pt + + + Proportion + 381pt + + + Name + Debugger + ServiceClasses + + PBXDebugSessionModule + + StatusbarIsVisible + + TableOfContents + + 1CD10A99069EF8BA00B06720 + 1063FBD4136BA3B000EE555B + 1C162984064C10D400B95A72 + 1063FBD5136BA3B000EE555B + 1063FBD6136BA3B000EE555B + 1063FBD7136BA3B000EE555B + 1063FBD8136BA3B000EE555B + 1063FBD9136BA3B000EE555B + + ToolbarConfiguration + xcode.toolbar.config.debugV3 + WindowString + 63 590 694 422 0 0 1920 1058 + WindowToolGUID + 1CD10A99069EF8BA00B06720 + WindowToolIsVisible + + + + Identifier + windowTool.find + Layout + + + Dock + + + Dock + + + ContentConfiguration + + PBXProjectModuleGUID + 1CDD528C0622207200134675 + PBXProjectModuleLabel + <No Editor> + PBXSplitModuleInNavigatorKey + + Split0 + + PBXProjectModuleGUID + 1CD0528D0623707200166675 + + SplitCount + 1 + + StatusBarVisibility + 1 + + GeometryConfiguration + + Frame + {{0, 0}, {781, 167}} + RubberWindowFrame + 62 385 781 470 0 0 1440 878 + + Module + PBXNavigatorGroup + Proportion + 781pt + + + Proportion + 50% + + + BecomeActive + 1 + ContentConfiguration + + PBXProjectModuleGUID + 1CD0528E0623707200166675 + PBXProjectModuleLabel + Project Find + + GeometryConfiguration + + Frame + {{8, 0}, {773, 254}} + RubberWindowFrame + 62 385 781 470 0 0 1440 878 + + Module + PBXProjectFindModule + Proportion + 50% + + + Proportion + 428pt + + + Name + Project Find + ServiceClasses + + PBXProjectFindModule + + StatusbarIsVisible + 1 + TableOfContents + + 1C530D57069F1CE1000CFCEE + 1C530D58069F1CE1000CFCEE + 1C530D59069F1CE1000CFCEE + 1CDD528C0622207200134675 + 1C530D5A069F1CE1000CFCEE + 1CE0B1FE06471DED0097A5F4 + 1CD0528E0623707200166675 + + WindowString + 62 385 781 470 0 0 1440 878 + WindowToolGUID + 1C530D57069F1CE1000CFCEE + WindowToolIsVisible + 0 + + + Identifier + MENUSEPARATOR + + + FirstTimeWindowDisplayed + + Identifier + windowTool.debuggerConsole + IsVertical + + Layout + + + Dock + + + BecomeActive + + ContentConfiguration + + PBXProjectModuleGUID + 1C78EAAC065D492600B07095 + PBXProjectModuleLabel + Debugger Console + + GeometryConfiguration + + Frame + {{0, 0}, {650, 209}} + RubberWindowFrame + 63 762 650 250 0 0 1920 1058 + + Module + PBXDebugCLIModule + Proportion + 209pt + + + Proportion + 209pt + + + Name + Debugger Console + ServiceClasses + + PBXDebugCLIModule + + StatusbarIsVisible + + TableOfContents + + 1C78EAAD065D492600B07095 + 1063FBDA136BA3B000EE555B + 1C78EAAC065D492600B07095 + + ToolbarConfiguration + xcode.toolbar.config.consoleV3 + WindowString + 63 762 650 250 0 0 1920 1058 + WindowToolGUID + 1C78EAAD065D492600B07095 + WindowToolIsVisible + + + + Identifier + windowTool.snapshots + Layout + + + Dock + + + Module + XCSnapshotModule + Proportion + 100% + + + Proportion + 100% + + + Name + Snapshots + ServiceClasses + + XCSnapshotModule + + StatusbarIsVisible + Yes + ToolbarConfiguration + xcode.toolbar.config.snapshots + WindowString + 315 824 300 550 0 0 1440 878 + WindowToolIsVisible + Yes + + + Identifier + windowTool.scm + Layout + + + Dock + + + ContentConfiguration + + PBXProjectModuleGUID + 1C78EAB2065D492600B07095 + PBXProjectModuleLabel + <No Editor> + PBXSplitModuleInNavigatorKey + + Split0 + + PBXProjectModuleGUID + 1C78EAB3065D492600B07095 + + SplitCount + 1 + + StatusBarVisibility + 1 + + GeometryConfiguration + + Frame + {{0, 0}, {452, 0}} + RubberWindowFrame + 743 379 452 308 0 0 1280 1002 + + Module + PBXNavigatorGroup + Proportion + 0pt + + + BecomeActive + 1 + ContentConfiguration + + PBXProjectModuleGUID + 1CD052920623707200166675 + PBXProjectModuleLabel + SCM + + GeometryConfiguration + + ConsoleFrame + {{0, 259}, {452, 0}} + Frame + {{0, 7}, {452, 259}} + RubberWindowFrame + 743 379 452 308 0 0 1280 1002 + TableConfiguration + + Status + 30 + FileName + 199 + Path + 197.0950012207031 + + TableFrame + {{0, 0}, {452, 250}} + + Module + PBXCVSModule + Proportion + 262pt + + + Proportion + 266pt + + + Name + SCM + ServiceClasses + + PBXCVSModule + + StatusbarIsVisible + 1 + TableOfContents + + 1C78EAB4065D492600B07095 + 1C78EAB5065D492600B07095 + 1C78EAB2065D492600B07095 + 1CD052920623707200166675 + + ToolbarConfiguration + xcode.toolbar.config.scm + WindowString + 743 379 452 308 0 0 1280 1002 + + + Identifier + windowTool.breakpoints + IsVertical + 0 + Layout + + + Dock + + + BecomeActive + 1 + ContentConfiguration + + PBXBottomSmartGroupGIDs + + 1C77FABC04509CD000000102 + + PBXProjectModuleGUID + 1CE0B1FE06471DED0097A5F4 + PBXProjectModuleLabel + Files + PBXProjectStructureProvided + no + PBXSmartGroupTreeModuleColumnData + + PBXSmartGroupTreeModuleColumnWidthsKey + + 168 + + PBXSmartGroupTreeModuleColumnsKey_v4 + + MainColumn + + + PBXSmartGroupTreeModuleOutlineStateKey_v7 + + PBXSmartGroupTreeModuleOutlineStateExpansionKey + + 1C77FABC04509CD000000102 + + PBXSmartGroupTreeModuleOutlineStateSelectionKey + + + 0 + + + PBXSmartGroupTreeModuleOutlineStateVisibleRectKey + {{0, 0}, {168, 350}} + + PBXTopSmartGroupGIDs + + XCIncludePerspectivesSwitch + 0 + + GeometryConfiguration + + Frame + {{0, 0}, {185, 368}} + GroupTreeTableConfiguration + + MainColumn + 168 + + RubberWindowFrame + 315 424 744 409 0 0 1440 878 + + Module + PBXSmartGroupTreeModule + Proportion + 185pt + + + ContentConfiguration + + PBXProjectModuleGUID + 1CA1AED706398EBD00589147 + PBXProjectModuleLabel + Detail + + GeometryConfiguration + + Frame + {{190, 0}, {554, 368}} + RubberWindowFrame + 315 424 744 409 0 0 1440 878 + + Module + XCDetailModule + Proportion + 554pt + + + Proportion + 368pt + + + MajorVersion + 3 + MinorVersion + 0 + Name + Breakpoints + ServiceClasses + + PBXSmartGroupTreeModule + XCDetailModule + + StatusbarIsVisible + 1 + TableOfContents + + 1CDDB66807F98D9800BB5817 + 1CDDB66907F98D9800BB5817 + 1CE0B1FE06471DED0097A5F4 + 1CA1AED706398EBD00589147 + + ToolbarConfiguration + xcode.toolbar.config.breakpointsV3 + WindowString + 315 424 744 409 0 0 1440 878 + WindowToolGUID + 1CDDB66807F98D9800BB5817 + WindowToolIsVisible + 1 + + + Identifier + windowTool.debugAnimator + Layout + + + Dock + + + Module + PBXNavigatorGroup + Proportion + 100% + + + Proportion + 100% + + + Name + Debug Visualizer + ServiceClasses + + PBXNavigatorGroup + + StatusbarIsVisible + 1 + ToolbarConfiguration + xcode.toolbar.config.debugAnimatorV3 + WindowString + 100 100 700 500 0 0 1280 1002 + + + Identifier + windowTool.bookmarks + Layout + + + Dock + + + Module + PBXBookmarksModule + Proportion + 100% + + + Proportion + 100% + + + Name + Bookmarks + ServiceClasses + + PBXBookmarksModule + + StatusbarIsVisible + 0 + WindowString + 538 42 401 187 0 0 1280 1002 + + + Identifier + windowTool.projectFormatConflicts + Layout + + + Dock + + + Module + XCProjectFormatConflictsModule + Proportion + 100% + + + Proportion + 100% + + + Name + Project Format Conflicts + ServiceClasses + + XCProjectFormatConflictsModule + + StatusbarIsVisible + 0 + WindowContentMinSize + 450 300 + WindowString + 50 850 472 307 0 0 1440 877 + + + Identifier + windowTool.classBrowser + Layout + + + Dock + + + BecomeActive + 1 + ContentConfiguration + + OptionsSetName + Hierarchy, all classes + PBXProjectModuleGUID + 1CA6456E063B45B4001379D8 + PBXProjectModuleLabel + Class Browser - NSObject + + GeometryConfiguration + + ClassesFrame + {{0, 0}, {374, 96}} + ClassesTreeTableConfiguration + + PBXClassNameColumnIdentifier + 208 + PBXClassBookColumnIdentifier + 22 + + Frame + {{0, 0}, {630, 331}} + MembersFrame + {{0, 105}, {374, 395}} + MembersTreeTableConfiguration + + PBXMemberTypeIconColumnIdentifier + 22 + PBXMemberNameColumnIdentifier + 216 + PBXMemberTypeColumnIdentifier + 97 + PBXMemberBookColumnIdentifier + 22 + + PBXModuleWindowStatusBarHidden2 + 1 + RubberWindowFrame + 385 179 630 352 0 0 1440 878 + + Module + PBXClassBrowserModule + Proportion + 332pt + + + Proportion + 332pt + + + Name + Class Browser + ServiceClasses + + PBXClassBrowserModule + + StatusbarIsVisible + 0 + TableOfContents + + 1C0AD2AF069F1E9B00FABCE6 + 1C0AD2B0069F1E9B00FABCE6 + 1CA6456E063B45B4001379D8 + + ToolbarConfiguration + xcode.toolbar.config.classbrowser + WindowString + 385 179 630 352 0 0 1440 878 + WindowToolGUID + 1C0AD2AF069F1E9B00FABCE6 + WindowToolIsVisible + 0 + + + Identifier + windowTool.refactoring + IncludeInToolsMenu + 0 + Layout + + + Dock + + + BecomeActive + 1 + GeometryConfiguration + + Frame + {0, 0}, {500, 335} + RubberWindowFrame + {0, 0}, {500, 335} + + Module + XCRefactoringModule + Proportion + 100% + + + Proportion + 100% + + + Name + Refactoring + ServiceClasses + + XCRefactoringModule + + WindowString + 200 200 500 356 0 0 1920 1200 + + + + diff --git a/objview/KRObjView.xcodeproj/project.pbxproj b/objview/KRObjView.xcodeproj/project.pbxproj new file mode 100644 index 0000000..5058721 --- /dev/null +++ b/objview/KRObjView.xcodeproj/project.pbxproj @@ -0,0 +1,349 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 45; + objects = { + +/* Begin PBXBuildFile section */ + 1063FBAC136BA13F00EE555B /* CoreMedia.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1063FBAB136BA13F00EE555B /* CoreMedia.framework */; }; + 1063FBAE136BA13F00EE555B /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1063FBAD136BA13F00EE555B /* CoreVideo.framework */; }; + 1063FBB0136BA13F00EE555B /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1063FBAF136BA13F00EE555B /* OpenGLES.framework */; }; + 1063FBB2136BA13F00EE555B /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1063FBB1136BA13F00EE555B /* QuartzCore.framework */; }; + 1063FC77136D6A1B00EE555B /* KRObjViewGLView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1063FC76136D6A1B00EE555B /* KRObjViewGLView.mm */; }; + 1D3623260D0F684500981E51 /* KRObjViewAppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1D3623250D0F684500981E51 /* KRObjViewAppDelegate.mm */; }; + 1D60589B0D05DD56006BFB54 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; }; + 1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; }; + 1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */; }; + 288765A50DF7441C002DB57D /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 288765A40DF7441C002DB57D /* CoreGraphics.framework */; }; + 28AD733F0D9D9553002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD733E0D9D9553002E5188 /* MainWindow.xib */; }; + 28D7ACF80DDB3853001CB0EB /* KRObjViewViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 28D7ACF70DDB3853001CB0EB /* KRObjViewViewController.mm */; }; + E43A7A6E13CA2BA2000A565E /* libKREngine.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E43A7A6D13CA2BA2000A565E /* libKREngine.a */; }; + E46FED2D13C9A49F009F5814 /* ShadowShader.vsh in Resources */ = {isa = PBXBuildFile; fileRef = E46FED2113C9A488009F5814 /* ShadowShader.vsh */; }; + E46FED2E13C9A49F009F5814 /* ShadowShader.fsh in Resources */ = {isa = PBXBuildFile; fileRef = E46FED2213C9A488009F5814 /* ShadowShader.fsh */; }; + E46FED2F13C9A49F009F5814 /* PostShader.fsh in Resources */ = {isa = PBXBuildFile; fileRef = E46FED2313C9A488009F5814 /* PostShader.fsh */; }; + E46FED3013C9A49F009F5814 /* ObjectShader.fsh in Resources */ = {isa = PBXBuildFile; fileRef = E46FED2413C9A488009F5814 /* ObjectShader.fsh */; }; + E46FED3113C9A49F009F5814 /* ObjectShader.vsh in Resources */ = {isa = PBXBuildFile; fileRef = E46FED2513C9A488009F5814 /* ObjectShader.vsh */; }; + E46FED3213C9A49F009F5814 /* PostShader.vsh in Resources */ = {isa = PBXBuildFile; fileRef = E46FED2613C9A488009F5814 /* PostShader.vsh */; }; + E49EB29C13806C5D00A4E727 /* MainWindow-iPad.xib in Resources */ = {isa = PBXBuildFile; fileRef = E49EB29B13806C5D00A4E727 /* MainWindow-iPad.xib */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 1063FBAB136BA13F00EE555B /* CoreMedia.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMedia.framework; path = System/Library/Frameworks/CoreMedia.framework; sourceTree = SDKROOT; }; + 1063FBAD136BA13F00EE555B /* CoreVideo.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreVideo.framework; path = System/Library/Frameworks/CoreVideo.framework; sourceTree = SDKROOT; }; + 1063FBAF136BA13F00EE555B /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; }; + 1063FBB1136BA13F00EE555B /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; + 1063FC75136D6A1B00EE555B /* KRObjViewGLView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KRObjViewGLView.h; sourceTree = ""; }; + 1063FC76136D6A1B00EE555B /* KRObjViewGLView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = KRObjViewGLView.mm; sourceTree = ""; }; + 1D30AB110D05D00D00671497 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; + 1D3623240D0F684500981E51 /* KRObjViewAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KRObjViewAppDelegate.h; sourceTree = ""; }; + 1D3623250D0F684500981E51 /* KRObjViewAppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = KRObjViewAppDelegate.mm; sourceTree = ""; }; + 1D6058910D05DD3D006BFB54 /* KRObjView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KRObjView.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; + 288765A40DF7441C002DB57D /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; + 28AD733E0D9D9553002E5188 /* MainWindow.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MainWindow.xib; sourceTree = ""; }; + 28D7ACF60DDB3853001CB0EB /* KRObjViewViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KRObjViewViewController.h; sourceTree = ""; }; + 28D7ACF70DDB3853001CB0EB /* KRObjViewViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = KRObjViewViewController.mm; sourceTree = ""; }; + 29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + 32CA4F630368D1EE00C91783 /* KRObjView_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KRObjView_Prefix.pch; sourceTree = ""; }; + 8D1107310486CEB800E47090 /* KRObjView-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "KRObjView-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = ""; }; + E43A7A6D13CA2BA2000A565E /* libKREngine.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libKREngine.a; sourceTree = BUILT_PRODUCTS_DIR; }; + E46FED2113C9A488009F5814 /* ShadowShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ShadowShader.vsh; path = ../KREngine/KREngine/Shaders/ShadowShader.vsh; sourceTree = ""; }; + E46FED2213C9A488009F5814 /* ShadowShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ShadowShader.fsh; path = ../KREngine/KREngine/Shaders/ShadowShader.fsh; sourceTree = ""; }; + E46FED2313C9A488009F5814 /* PostShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = PostShader.fsh; path = ../KREngine/KREngine/Shaders/PostShader.fsh; sourceTree = ""; }; + E46FED2413C9A488009F5814 /* ObjectShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ObjectShader.fsh; path = ../KREngine/KREngine/Shaders/ObjectShader.fsh; sourceTree = ""; }; + E46FED2513C9A488009F5814 /* ObjectShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ObjectShader.vsh; path = ../KREngine/KREngine/Shaders/ObjectShader.vsh; sourceTree = ""; }; + E46FED2613C9A488009F5814 /* PostShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = PostShader.vsh; path = ../KREngine/KREngine/Shaders/PostShader.vsh; sourceTree = ""; }; + E49EB29B13806C5D00A4E727 /* MainWindow-iPad.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = "MainWindow-iPad.xib"; path = "iPad/MainWindow-iPad.xib"; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 1D60588F0D05DD3D006BFB54 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + E43A7A6E13CA2BA2000A565E /* libKREngine.a in Frameworks */, + 1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */, + 1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */, + 288765A50DF7441C002DB57D /* CoreGraphics.framework in Frameworks */, + 1063FBAC136BA13F00EE555B /* CoreMedia.framework in Frameworks */, + 1063FBAE136BA13F00EE555B /* CoreVideo.framework in Frameworks */, + 1063FBB0136BA13F00EE555B /* OpenGLES.framework in Frameworks */, + 1063FBB2136BA13F00EE555B /* QuartzCore.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 080E96DDFE201D6D7F000001 /* Classes */ = { + isa = PBXGroup; + children = ( + 1D3623240D0F684500981E51 /* KRObjViewAppDelegate.h */, + 1D3623250D0F684500981E51 /* KRObjViewAppDelegate.mm */, + 28D7ACF60DDB3853001CB0EB /* KRObjViewViewController.h */, + 28D7ACF70DDB3853001CB0EB /* KRObjViewViewController.mm */, + 1063FC75136D6A1B00EE555B /* KRObjViewGLView.h */, + 1063FC76136D6A1B00EE555B /* KRObjViewGLView.mm */, + ); + path = Classes; + sourceTree = ""; + }; + 19C28FACFE9D520D11CA2CBB /* Products */ = { + isa = PBXGroup; + children = ( + 1D6058910D05DD3D006BFB54 /* KRObjView.app */, + ); + name = Products; + sourceTree = ""; + }; + 29B97314FDCFA39411CA2CEA /* CustomTemplate */ = { + isa = PBXGroup; + children = ( + E43A7A6D13CA2BA2000A565E /* libKREngine.a */, + 080E96DDFE201D6D7F000001 /* Classes */, + 29B97315FDCFA39411CA2CEA /* Other Sources */, + 29B97317FDCFA39411CA2CEA /* Resources */, + E49EB29A13806C5B00A4E727 /* iPad */, + 29B97323FDCFA39411CA2CEA /* Frameworks */, + 19C28FACFE9D520D11CA2CBB /* Products */, + ); + name = CustomTemplate; + sourceTree = ""; + }; + 29B97315FDCFA39411CA2CEA /* Other Sources */ = { + isa = PBXGroup; + children = ( + 32CA4F630368D1EE00C91783 /* KRObjView_Prefix.pch */, + 29B97316FDCFA39411CA2CEA /* main.m */, + ); + name = "Other Sources"; + sourceTree = ""; + }; + 29B97317FDCFA39411CA2CEA /* Resources */ = { + isa = PBXGroup; + children = ( + E400687C1373AB6400B3D28B /* Assets */, + 28AD733E0D9D9553002E5188 /* MainWindow.xib */, + 8D1107310486CEB800E47090 /* KRObjView-Info.plist */, + ); + name = Resources; + sourceTree = ""; + }; + 29B97323FDCFA39411CA2CEA /* Frameworks */ = { + isa = PBXGroup; + children = ( + 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */, + 1D30AB110D05D00D00671497 /* Foundation.framework */, + 288765A40DF7441C002DB57D /* CoreGraphics.framework */, + 1063FBAB136BA13F00EE555B /* CoreMedia.framework */, + 1063FBAD136BA13F00EE555B /* CoreVideo.framework */, + 1063FBAF136BA13F00EE555B /* OpenGLES.framework */, + 1063FBB1136BA13F00EE555B /* QuartzCore.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; + E400687C1373AB6400B3D28B /* Assets */ = { + isa = PBXGroup; + children = ( + E46FED2013C9A472009F5814 /* Shaders */, + ); + name = Assets; + sourceTree = ""; + }; + E46FED2013C9A472009F5814 /* Shaders */ = { + isa = PBXGroup; + children = ( + E46FED2113C9A488009F5814 /* ShadowShader.vsh */, + E46FED2213C9A488009F5814 /* ShadowShader.fsh */, + E46FED2313C9A488009F5814 /* PostShader.fsh */, + E46FED2413C9A488009F5814 /* ObjectShader.fsh */, + E46FED2513C9A488009F5814 /* ObjectShader.vsh */, + E46FED2613C9A488009F5814 /* PostShader.vsh */, + ); + name = Shaders; + sourceTree = ""; + }; + E49EB29A13806C5B00A4E727 /* iPad */ = { + isa = PBXGroup; + children = ( + E49EB29B13806C5D00A4E727 /* MainWindow-iPad.xib */, + ); + name = iPad; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 1D6058900D05DD3D006BFB54 /* KRObjView */ = { + isa = PBXNativeTarget; + buildConfigurationList = 1D6058960D05DD3E006BFB54 /* Build configuration list for PBXNativeTarget "KRObjView" */; + buildPhases = ( + 1D60588D0D05DD3D006BFB54 /* Resources */, + 1D60588E0D05DD3D006BFB54 /* Sources */, + 1D60588F0D05DD3D006BFB54 /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = KRObjView; + productName = AVCapTest; + productReference = 1D6058910D05DD3D006BFB54 /* KRObjView.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 29B97313FDCFA39411CA2CEA /* Project object */ = { + isa = PBXProject; + buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "KRObjView" */; + compatibilityVersion = "Xcode 3.1"; + developmentRegion = English; + hasScannedForEncodings = 1; + knownRegions = ( + English, + Japanese, + French, + German, + en, + ); + mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 1D6058900D05DD3D006BFB54 /* KRObjView */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 1D60588D0D05DD3D006BFB54 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + E46FED2D13C9A49F009F5814 /* ShadowShader.vsh in Resources */, + E46FED2E13C9A49F009F5814 /* ShadowShader.fsh in Resources */, + E46FED2F13C9A49F009F5814 /* PostShader.fsh in Resources */, + E46FED3013C9A49F009F5814 /* ObjectShader.fsh in Resources */, + E46FED3113C9A49F009F5814 /* ObjectShader.vsh in Resources */, + E46FED3213C9A49F009F5814 /* PostShader.vsh in Resources */, + 28AD733F0D9D9553002E5188 /* MainWindow.xib in Resources */, + E49EB29C13806C5D00A4E727 /* MainWindow-iPad.xib in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 1D60588E0D05DD3D006BFB54 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 1D60589B0D05DD56006BFB54 /* main.m in Sources */, + 1D3623260D0F684500981E51 /* KRObjViewAppDelegate.mm in Sources */, + 28D7ACF80DDB3853001CB0EB /* KRObjViewViewController.mm in Sources */, + 1063FC77136D6A1B00EE555B /* KRObjViewGLView.mm in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 1D6058940D05DD3E006BFB54 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = YES; + COPY_PHASE_STRIP = NO; + GCC_DYNAMIC_NO_PIC = NO; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = KRObjView_Prefix.pch; + "HEADER_SEARCH_PATHS[arch=*]" = /Users/kearwood/Documents/dev/svn/sarofax/tools/krengine/trunk/KREngine/KREngine/Classes; + INFOPLIST_FILE = "KRObjView-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 4.3; + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + "\"$(SRCROOT)\"", + ); + PRODUCT_NAME = KRObjView; + TARGETED_DEVICE_FAMILY = "1,2"; + USER_HEADER_SEARCH_PATHS = "/Users/applemac/circa1948/circa1948_circa1948/tools/krengine/**"; + }; + name = Debug; + }; + 1D6058950D05DD3E006BFB54 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = YES; + COPY_PHASE_STRIP = YES; + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = KRObjView_Prefix.pch; + "HEADER_SEARCH_PATHS[arch=*]" = /Users/kearwood/Documents/dev/svn/sarofax/tools/krengine/trunk/KREngine/KREngine/Classes; + INFOPLIST_FILE = "KRObjView-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 4.3; + LIBRARY_SEARCH_PATHS = ( + "$(inherited)", + "\"$(SRCROOT)\"", + ); + PRODUCT_NAME = KRObjView; + TARGETED_DEVICE_FAMILY = "1,2"; + USER_HEADER_SEARCH_PATHS = "/Users/applemac/circa1948/circa1948_circa1948/tools/krengine/**"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + C01FCF4F08A954540054247B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ARCHS = "$(ARCHS_STANDARD_32_BIT)"; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + GCC_C_LANGUAGE_STANDARD = c99; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + PREBINDING = NO; + SDKROOT = iphoneos; + }; + name = Debug; + }; + C01FCF5008A954540054247B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ARCHS = "$(ARCHS_STANDARD_32_BIT)"; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + GCC_C_LANGUAGE_STANDARD = c99; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1"; + PREBINDING = NO; + SDKROOT = iphoneos; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 1D6058960D05DD3E006BFB54 /* Build configuration list for PBXNativeTarget "KRObjView" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 1D6058940D05DD3E006BFB54 /* Debug */, + 1D6058950D05DD3E006BFB54 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + C01FCF4E08A954540054247B /* Build configuration list for PBXProject "KRObjView" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + C01FCF4F08A954540054247B /* Debug */, + C01FCF5008A954540054247B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 29B97313FDCFA39411CA2CEA /* Project object */; +} diff --git a/objview/KRObjView.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/objview/KRObjView.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..3dd1fb7 --- /dev/null +++ b/objview/KRObjView.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/objview/KRObjView_Prefix.pch b/objview/KRObjView_Prefix.pch new file mode 100644 index 0000000..35309c6 --- /dev/null +++ b/objview/KRObjView_Prefix.pch @@ -0,0 +1,8 @@ +// +// Prefix header for all source files of the 'KRObjView' target in the 'KRObjView' project +// + +#ifdef __OBJC__ + #import + #import +#endif diff --git a/objview/MainWindow.xib b/objview/MainWindow.xib new file mode 100644 index 0000000..dfed42c --- /dev/null +++ b/objview/MainWindow.xib @@ -0,0 +1,262 @@ + + + + 1024 + 10K540 + 1305 + 1038.36 + 461.00 + + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + 300 + + + YES + IBUICustomObject + IBUIWindow + IBUIViewController + IBProxyObject + + + YES + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + + + YES + + YES + + + + + YES + + IBFilesOwner + IBCocoaTouchFramework + + + IBFirstResponder + IBCocoaTouchFramework + + + IBCocoaTouchFramework + + + KRObjViewViewController + + + 1 + 1 + + IBCocoaTouchFramework + NO + + + + 292 + {320, 480} + + 1 + MSAxIDEAA + + NO + NO + + IBCocoaTouchFramework + YES + + + + + YES + + + delegate + + + + 4 + + + + viewController + + + + 11 + + + + window + + + + 14 + + + + + YES + + 0 + + + + + + -1 + + + File's Owner + + + 3 + + + KRObjView App Delegate + + + -2 + + + + + 10 + + + + + 12 + + + + + + + YES + + YES + -1.CustomClassName + -2.CustomClassName + 10.CustomClassName + 10.IBEditorWindowLastContentRect + 10.IBPluginDependency + 12.IBEditorWindowLastContentRect + 12.IBPluginDependency + 3.CustomClassName + 3.IBPluginDependency + + + YES + UIApplication + UIResponder + KRObjViewViewController + {{234, 376}, {320, 480}} + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + {{525, 346}, {320, 480}} + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + KRObjViewAppDelegate + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + + + + YES + + + + + + YES + + + + + 15 + + + + YES + + KRObjViewAppDelegate + NSObject + + YES + + YES + viewController + window + + + YES + KRObjViewViewController + UIWindow + + + + YES + + YES + viewController + window + + + YES + + viewController + KRObjViewViewController + + + window + UIWindow + + + + + IBProjectSource + ./Classes/KRObjViewAppDelegate.h + + + + KRObjViewViewController + UIViewController + + overlayLayer + CALayer + + + overlayLayer + + overlayLayer + CALayer + + + + IBProjectSource + ./Classes/KRObjViewViewController.h + + + + CALayer + NSObject + + IBProjectSource + ./Classes/CALayer.h + + + + + 0 + IBCocoaTouchFramework + + com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS + + + + com.apple.InterfaceBuilder.CocoaTouchPlugin.InterfaceBuilder3 + + + YES + 3 + 300 + + diff --git a/objview/iPad/MainWindow-iPad.xib b/objview/iPad/MainWindow-iPad.xib new file mode 100644 index 0000000..7a66a79 --- /dev/null +++ b/objview/iPad/MainWindow-iPad.xib @@ -0,0 +1,274 @@ + + + + 1056 + 10J869 + 1305 + 1038.35 + 461.00 + + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + 300 + + + YES + IBUICustomObject + IBUIWindow + IBUIViewController + IBProxyObject + + + YES + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + + + YES + + YES + + + + + YES + + IBFilesOwner + IBIPadFramework + + + IBFirstResponder + IBIPadFramework + + + IBIPadFramework + + + KRObjViewViewController + + 2 + + + 1 + 1 + + IBIPadFramework + NO + + + + 292 + {768, 1024} + + 1 + MSAxIDEAA + + NO + NO + + IBIPadFramework + YES + + + + + YES + + + delegate + + + + 4 + + + + viewController + + + + 11 + + + + window + + + + 14 + + + + + YES + + 0 + + + + + + -1 + + + File's Owner + + + 3 + + + KRObjView App Delegate + + + -2 + + + + + 10 + + + + + 12 + + + + + + + YES + + YES + -1.CustomClassName + -2.CustomClassName + 10.CustomClassName + 10.IBEditorWindowLastContentRect + 10.IBLastUsedUIStatusBarStylesToTargetRuntimesMap + 10.IBPluginDependency + 12.IBEditorWindowLastContentRect + 12.IBLastUsedUIStatusBarStylesToTargetRuntimesMap + 12.IBPluginDependency + 3.CustomClassName + 3.IBPluginDependency + + + YES + UIApplication + UIResponder + KRObjViewViewController + {{234, 376}, {320, 480}} + + IBCocoaTouchFramework + + + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + {{525, 346}, {320, 480}} + + IBCocoaTouchFramework + + + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + KRObjViewAppDelegate + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + + + + YES + + + + + + YES + + + + + 15 + + + + YES + + KRObjViewAppDelegate + NSObject + + YES + + YES + viewController + window + + + YES + KRObjViewViewController + UIWindow + + + + YES + + YES + viewController + window + + + YES + + viewController + KRObjViewViewController + + + window + UIWindow + + + + + IBProjectSource + ./Classes/KRObjViewAppDelegate.h + + + + KRObjViewViewController + UIViewController + + overlayLayer + CALayer + + + overlayLayer + + overlayLayer + CALayer + + + + IBProjectSource + ./Classes/KRObjViewViewController.h + + + + CALayer + NSObject + + IBProjectSource + ./Classes/CALayer.h + + + + + 0 + IBIPadFramework + + com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS + + + + com.apple.InterfaceBuilder.CocoaTouchPlugin.InterfaceBuilder3 + + + YES + 3 + 300 + + diff --git a/objview/main.m b/objview/main.m new file mode 100644 index 0000000..71d4332 --- /dev/null +++ b/objview/main.m @@ -0,0 +1,17 @@ +// +// main.m +// KRObjView +// +// Created by Mac on 11-04-29. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import + +int main(int argc, char *argv[]) { + + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + int retVal = UIApplicationMain(argc, argv, nil, nil); + [pool release]; + return retVal; +}