Initial import of KREngine
--HG-- extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%404
This commit is contained in:
380
KREngine/KREngine.xcodeproj/project.pbxproj
Normal file
380
KREngine/KREngine.xcodeproj/project.pbxproj
Normal file
@@ -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 = "<group>"; };
|
||||
E414BAE41435558800A668C4 /* KRInstance.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRInstance.cpp; path = Classes/KRInstance.cpp; sourceTree = "<group>"; };
|
||||
E414BAE6143557D200A668C4 /* KRScene.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRScene.h; path = Classes/KRScene.h; sourceTree = "<group>"; };
|
||||
E414BAE81435585A00A668C4 /* KRScene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRScene.cpp; path = Classes/KRScene.cpp; sourceTree = "<group>"; };
|
||||
E414BAEA14355E5500A668C4 /* KRBoundingVolume.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRBoundingVolume.h; path = Classes/KRBoundingVolume.h; sourceTree = "<group>"; };
|
||||
E414BAEC14355EFF00A668C4 /* KRBoundingVolume.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRBoundingVolume.cpp; path = Classes/KRBoundingVolume.cpp; sourceTree = "<group>"; };
|
||||
E45772F113C9A13C0037BEEA /* ShadowShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ShadowShader.vsh; path = Shaders/ShadowShader.vsh; sourceTree = "<group>"; };
|
||||
E45772F213C9A13C0037BEEA /* ShadowShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ShadowShader.fsh; path = Shaders/ShadowShader.fsh; sourceTree = "<group>"; };
|
||||
E45772F313C9A13C0037BEEA /* PostShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = PostShader.fsh; path = Shaders/PostShader.fsh; sourceTree = "<group>"; };
|
||||
E45772F413C9A13C0037BEEA /* ObjectShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ObjectShader.fsh; path = Shaders/ObjectShader.fsh; sourceTree = "<group>"; };
|
||||
E45772F513C9A13C0037BEEA /* ObjectShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ObjectShader.vsh; path = Shaders/ObjectShader.vsh; sourceTree = "<group>"; };
|
||||
E45772F613C9A13C0037BEEA /* PostShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = PostShader.vsh; path = Shaders/PostShader.vsh; sourceTree = "<group>"; };
|
||||
E47C25A113F4F65A00FF4370 /* KRShaderManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRShaderManager.h; path = Classes/KRShaderManager.h; sourceTree = "<group>"; };
|
||||
E47C25A413F4F66F00FF4370 /* KRShader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRShader.h; path = Classes/KRShader.h; sourceTree = "<group>"; };
|
||||
E47C25A613F4F6AB00FF4370 /* KRShaderManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRShaderManager.cpp; path = Classes/KRShaderManager.cpp; sourceTree = "<group>"; };
|
||||
E47C25A813F4F6DD00FF4370 /* KRShader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRShader.cpp; path = Classes/KRShader.cpp; sourceTree = "<group>"; };
|
||||
E48B3CBC14393DF5000C50E2 /* KRCamera.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRCamera.h; path = Classes/KRCamera.h; sourceTree = "<group>"; };
|
||||
E48B3CBF14393E2F000C50E2 /* KRCamera.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRCamera.cpp; path = Classes/KRCamera.cpp; sourceTree = "<group>"; };
|
||||
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 = "<group>"; };
|
||||
E491016F13C99BDC0098455B /* KREngine.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = KREngine.mm; path = Classes/KREngine.mm; sourceTree = "<group>"; };
|
||||
E491017213C99BDC0098455B /* KREngine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KREngine.h; path = Classes/KREngine.h; sourceTree = "<group>"; };
|
||||
E491017613C99BDC0098455B /* KRMat4.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRMat4.h; path = Classes/KRMat4.h; sourceTree = "<group>"; };
|
||||
E491017713C99BDC0098455B /* KRMat4.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRMat4.cpp; path = Classes/KRMat4.cpp; sourceTree = "<group>"; };
|
||||
E491017913C99BDC0098455B /* KRModel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRModel.cpp; path = Classes/KRModel.cpp; sourceTree = "<group>"; };
|
||||
E491017A13C99BDC0098455B /* KRModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRModel.h; path = Classes/KRModel.h; sourceTree = "<group>"; };
|
||||
E491017B13C99BDC0098455B /* KRMaterialManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRMaterialManager.cpp; path = Classes/KRMaterialManager.cpp; sourceTree = "<group>"; };
|
||||
E491017C13C99BDC0098455B /* KRMaterial.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRMaterial.cpp; path = Classes/KRMaterial.cpp; sourceTree = "<group>"; };
|
||||
E491017D13C99BDC0098455B /* KRMaterial.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRMaterial.h; path = Classes/KRMaterial.h; sourceTree = "<group>"; };
|
||||
E491017E13C99BDC0098455B /* KRVector3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRVector3.h; path = Classes/KRVector3.h; sourceTree = "<group>"; };
|
||||
E491017F13C99BDC0098455B /* KRVector3.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRVector3.cpp; path = Classes/KRVector3.cpp; sourceTree = "<group>"; };
|
||||
E491018013C99BDC0098455B /* KRTextureManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRTextureManager.cpp; path = Classes/KRTextureManager.cpp; sourceTree = "<group>"; };
|
||||
E491018113C99BDC0098455B /* KRTexture.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRTexture.cpp; path = Classes/KRTexture.cpp; sourceTree = "<group>"; };
|
||||
E491018213C99BDC0098455B /* KRModelManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRModelManager.cpp; path = Classes/KRModelManager.cpp; sourceTree = "<group>"; };
|
||||
E491018313C99BDC0098455B /* KRModelManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRModelManager.h; path = Classes/KRModelManager.h; sourceTree = "<group>"; };
|
||||
E491018413C99BDC0098455B /* KRMaterialManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRMaterialManager.h; path = Classes/KRMaterialManager.h; sourceTree = "<group>"; };
|
||||
E491018513C99BDC0098455B /* KRTextureManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRTextureManager.h; path = Classes/KRTextureManager.h; sourceTree = "<group>"; };
|
||||
E491018613C99BDC0098455B /* KRTexture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRTexture.h; path = Classes/KRTexture.h; sourceTree = "<group>"; };
|
||||
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 = "<group>";
|
||||
};
|
||||
E491015613C99B9D0098455B = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
E491016613C99B9E0098455B /* KREngine */,
|
||||
E491016313C99B9E0098455B /* Frameworks */,
|
||||
E491016213C99B9E0098455B /* Products */,
|
||||
);
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
E491016213C99B9E0098455B /* Products */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
E491016113C99B9E0098455B /* libKREngine.a */,
|
||||
);
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
E491016313C99B9E0098455B /* Frameworks */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
E491019F13C99BF50098455B /* OpenGLES.framework */,
|
||||
E491016413C99B9E0098455B /* Foundation.framework */,
|
||||
);
|
||||
name = Frameworks;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
E491016613C99B9E0098455B /* KREngine */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
E45772E313C99F160037BEEA /* Shaders */,
|
||||
E491016E13C99BAE0098455B /* Classes */,
|
||||
E491016713C99B9E0098455B /* Supporting Files */,
|
||||
);
|
||||
path = KREngine;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
E491016713C99B9E0098455B /* Supporting Files */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
E491016813C99B9E0098455B /* KREngine-Prefix.pch */,
|
||||
);
|
||||
name = "Supporting Files";
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
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 = "<group>";
|
||||
};
|
||||
/* 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 */;
|
||||
}
|
||||
7
KREngine/KREngine.xcodeproj/project.xcworkspace/contents.xcworkspacedata
generated
Normal file
7
KREngine/KREngine.xcodeproj/project.xcworkspace/contents.xcworkspacedata
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "self:KREngine.xcodeproj">
|
||||
</FileRef>
|
||||
</Workspace>
|
||||
264
KREngine/KREngine/Classes/KRBoundingVolume.cpp
Normal file
264
KREngine/KREngine/Classes/KRBoundingVolume.cpp
Normal file
@@ -0,0 +1,264 @@
|
||||
//
|
||||
// KRBoundingVolume.cpp
|
||||
// KREngine
|
||||
//
|
||||
// Created by Kearwood Gilbert on 11-09-29.
|
||||
// Copyright 2011 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#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;
|
||||
|
||||
}
|
||||
38
KREngine/KREngine/Classes/KRBoundingVolume.h
Normal file
38
KREngine/KREngine/Classes/KRBoundingVolume.h
Normal file
@@ -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
|
||||
65
KREngine/KREngine/Classes/KRCamera.cpp
Normal file
65
KREngine/KREngine/Classes/KRCamera.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// KRSettings.cpp
|
||||
// KREngine
|
||||
//
|
||||
// Created by Kearwood Gilbert on 11-10-02.
|
||||
// Copyright 2011 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#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;
|
||||
}
|
||||
55
KREngine/KREngine/Classes/KRCamera.h
Normal file
55
KREngine/KREngine/Classes/KRCamera.h
Normal file
@@ -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
|
||||
107
KREngine/KREngine/Classes/KREngine.h
Normal file
107
KREngine/KREngine/Classes/KREngine.h
Normal file
@@ -0,0 +1,107 @@
|
||||
//
|
||||
// KREngine.h
|
||||
// gldemo
|
||||
//
|
||||
// Created by Kearwood Gilbert on 10-09-16.
|
||||
// Copyright (c) 2010 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#import <OpenGLES/ES2/gl.h>
|
||||
#import <OpenGLES/ES2/glext.h>
|
||||
|
||||
// #import "KRTextureManager.h"
|
||||
#import <map>
|
||||
#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
|
||||
|
||||
1099
KREngine/KREngine/Classes/KREngine.mm
Normal file
1099
KREngine/KREngine/Classes/KREngine.mm
Normal file
File diff suppressed because it is too large
Load Diff
48
KREngine/KREngine/Classes/KRInstance.cpp
Normal file
48
KREngine/KREngine/Classes/KRInstance.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// KRInstance.cpp
|
||||
// KREngine
|
||||
//
|
||||
// Created by Kearwood Gilbert on 11-09-29.
|
||||
// Copyright 2011 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#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);
|
||||
}
|
||||
42
KREngine/KREngine/Classes/KRInstance.h
Normal file
42
KREngine/KREngine/Classes/KRInstance.h
Normal file
@@ -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 <OpenGLES/ES1/gl.h>
|
||||
#import <OpenGLES/ES1/glext.h>
|
||||
|
||||
#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
|
||||
264
KREngine/KREngine/Classes/KRMat4.cpp
Normal file
264
KREngine/KREngine/Classes/KRMat4.cpp
Normal file
@@ -0,0 +1,264 @@
|
||||
//
|
||||
// KRMat4.cpp
|
||||
// gldemo
|
||||
//
|
||||
// Created by Kearwood Gilbert on 10-09-21.
|
||||
// Copyright (c) 2010 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#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]
|
||||
);
|
||||
}
|
||||
75
KREngine/KREngine/Classes/KRMat4.h
Normal file
75
KREngine/KREngine/Classes/KRMat4.h
Normal file
@@ -0,0 +1,75 @@
|
||||
//
|
||||
// KRMat4.h
|
||||
// gldemo
|
||||
//
|
||||
// Created by Kearwood Gilbert on 10-09-21.
|
||||
// Copyright (c) 2010 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#import <OpenGLES/ES2/gl.h>
|
||||
#import <OpenGLES/ES2/glext.h>
|
||||
|
||||
#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
|
||||
144
KREngine/KREngine/Classes/KRMaterial.cpp
Normal file
144
KREngine/KREngine/Classes/KRMaterial.cpp
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
59
KREngine/KREngine/Classes/KRMaterial.h
Normal file
59
KREngine/KREngine/Classes/KRMaterial.h
Normal file
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// KRMaterial.h
|
||||
// gldemo
|
||||
//
|
||||
// Created by Kearwood Gilbert on 10-10-24.
|
||||
// Copyright (c) 2010 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#import <OpenGLES/ES2/gl.h>
|
||||
#import <OpenGLES/ES2/glext.h>
|
||||
#import <stdint.h>
|
||||
#import <list>
|
||||
#import <string>
|
||||
|
||||
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
|
||||
184
KREngine/KREngine/Classes/KRMaterialManager.cpp
Normal file
184
KREngine/KREngine/Classes/KRMaterialManager.cpp
Normal file
@@ -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 <stdint.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
KRMaterialManager::KRMaterialManager(KRTextureManager *pTextureManager, KRShaderManager *pShaderManager) {
|
||||
m_pTextureManager = pTextureManager;
|
||||
m_pShaderManager = pShaderManager;
|
||||
}
|
||||
|
||||
KRMaterialManager::~KRMaterialManager() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
KRMaterial *KRMaterialManager::getMaterial(const char *szName) {
|
||||
map<std::string, KRMaterial *>::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;
|
||||
}
|
||||
37
KREngine/KREngine/Classes/KRMaterialManager.h
Normal file
37
KREngine/KREngine/Classes/KRMaterialManager.h
Normal file
@@ -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 <map>
|
||||
#import <string>
|
||||
|
||||
using std::map;
|
||||
|
||||
class KRMaterialManager {
|
||||
public:
|
||||
KRMaterialManager(KRTextureManager *pTextureManager, KRShaderManager *pShaderManager);
|
||||
~KRMaterialManager();
|
||||
|
||||
bool loadFile(const char *szPath);
|
||||
KRMaterial *getMaterial(const char *szName);
|
||||
|
||||
private:
|
||||
map<std::string, KRMaterial *> m_materials;
|
||||
KRTextureManager *m_pTextureManager;
|
||||
KRShaderManager *m_pShaderManager;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
617
KREngine/KREngine/Classes/KRModel.cpp
Normal file
617
KREngine/KREngine/Classes/KRModel.cpp
Normal file
@@ -0,0 +1,617 @@
|
||||
//
|
||||
// KRModel.cpp
|
||||
// gldemo
|
||||
//
|
||||
// Created by Kearwood Gilbert on 10-09-22.
|
||||
// Copyright (c) 2010 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#import <stdint.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
|
||||
#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<KRMaterial *> 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<KRMaterial *>::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<Material *>::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<Material *>::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;
|
||||
}
|
||||
98
KREngine/KREngine/Classes/KRModel.h
Normal file
98
KREngine/KREngine/Classes/KRModel.h
Normal file
@@ -0,0 +1,98 @@
|
||||
//
|
||||
// KRModel.h
|
||||
// gldemo
|
||||
//
|
||||
// Created by Kearwood Gilbert on 10-09-22.
|
||||
// Copyright (c) 2010 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#import <OpenGLES/ES2/gl.h>
|
||||
#import <OpenGLES/ES2/glext.h>
|
||||
#import <stdint.h>
|
||||
#import <vector>
|
||||
#import <string>
|
||||
|
||||
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<Material *> m_materials;
|
||||
|
||||
GLfloat m_minx, m_miny, m_minz, m_maxx, m_maxy, m_maxz;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // KRMODEL_I
|
||||
35
KREngine/KREngine/Classes/KRModelManager.cpp
Normal file
35
KREngine/KREngine/Classes/KRModelManager.cpp
Normal file
@@ -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<std::string, KRModel *>::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<std::string, KRModel *>::iterator model_itr = m_models.begin();
|
||||
return (*model_itr).second;
|
||||
}
|
||||
32
KREngine/KREngine/Classes/KRModelManager.h
Normal file
32
KREngine/KREngine/Classes/KRModelManager.h
Normal file
@@ -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 <map>
|
||||
#import <string>
|
||||
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<std::string, KRModel *> m_models;
|
||||
KRMaterialManager *m_pMaterialManager;
|
||||
};
|
||||
|
||||
#endif
|
||||
95
KREngine/KREngine/Classes/KRScene.cpp
Normal file
95
KREngine/KREngine/Classes/KRScene.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
//
|
||||
// KRScene.cpp
|
||||
// KREngine
|
||||
//
|
||||
// Created by Kearwood Gilbert on 11-09-29.
|
||||
// Copyright 2011 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#import "KRVector3.h"
|
||||
#import "KRMat4.h"
|
||||
|
||||
#import "KRScene.h"
|
||||
|
||||
KRScene::KRScene() {
|
||||
m_pExtents = NULL;
|
||||
}
|
||||
KRScene::~KRScene() {
|
||||
for(vector<KRInstance *>::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<KRInstance *>::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<KRInstance *>::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;
|
||||
}
|
||||
}
|
||||
41
KREngine/KREngine/Classes/KRScene.h
Normal file
41
KREngine/KREngine/Classes/KRScene.h
Normal file
@@ -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 <vector>
|
||||
#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<KRInstance *> m_instances;
|
||||
KRBoundingVolume *m_pExtents;
|
||||
|
||||
void calcExtents();
|
||||
void clearExtents();
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
181
KREngine/KREngine/Classes/KRShader.cpp
Normal file
181
KREngine/KREngine/Classes/KRShader.cpp
Normal file
@@ -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;
|
||||
}
|
||||
67
KREngine/KREngine/Classes/KRShader.h
Normal file
67
KREngine/KREngine/Classes/KRShader.h
Normal file
@@ -0,0 +1,67 @@
|
||||
//
|
||||
// KRShader.h
|
||||
// KREngine
|
||||
//
|
||||
// Created by Kearwood Gilbert on 11-08-11.
|
||||
// Copyright 2011 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#import <OpenGLES/ES2/gl.h>
|
||||
#import <OpenGLES/ES2/glext.h>
|
||||
#import <stdint.h>
|
||||
#import <vector>
|
||||
#import <string>
|
||||
|
||||
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
|
||||
68
KREngine/KREngine/Classes/KRShaderManager.cpp
Normal file
68
KREngine/KREngine/Classes/KRShaderManager.cpp
Normal file
@@ -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 <sstream>
|
||||
|
||||
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<std::string, KRShader *>::iterator itr = m_shaders.begin();
|
||||
delete (*itr).second;
|
||||
m_shaders.erase(itr);
|
||||
}
|
||||
|
||||
stringstream stream;
|
||||
stream.precision(std::numeric_limits<long double>::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;
|
||||
}
|
||||
43
KREngine/KREngine/Classes/KRShaderManager.h
Normal file
43
KREngine/KREngine/Classes/KRShaderManager.h
Normal file
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// KRShaderManager.h
|
||||
// KREngine
|
||||
//
|
||||
// Created by Kearwood Gilbert on 11-08-11.
|
||||
// Copyright 2011 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#import <OpenGLES/ES2/gl.h>
|
||||
#import <OpenGLES/ES2/glext.h>
|
||||
#import <stdint.h>
|
||||
#import <vector>
|
||||
#import <map>
|
||||
#import <string>
|
||||
#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<std::string, KRShader *> m_shaders;
|
||||
|
||||
GLchar *m_szFragShaderSource;
|
||||
GLchar *m_szVertShaderSource;
|
||||
|
||||
KRShader *m_pShader;
|
||||
};
|
||||
|
||||
#endif
|
||||
209
KREngine/KREngine/Classes/KRTexture.cpp
Normal file
209
KREngine/KREngine/Classes/KRTexture.cpp
Normal file
@@ -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 <stdint.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#import <stdint.h>
|
||||
#import <assert.h>
|
||||
|
||||
|
||||
#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<dataBlockStruct>::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;
|
||||
}
|
||||
49
KREngine/KREngine/Classes/KRTexture.h
Normal file
49
KREngine/KREngine/Classes/KRTexture.h
Normal file
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// KRTexture.h
|
||||
// gldemo
|
||||
//
|
||||
// Created by Kearwood Gilbert on 10-10-23.
|
||||
// Copyright (c) 2010 Kearwood Software. All rights reserved.
|
||||
//
|
||||
|
||||
#import <OpenGLES/ES1/gl.h>
|
||||
#import <OpenGLES/ES1/glext.h>
|
||||
#import <stdint.h>
|
||||
#import <list>
|
||||
#import <string>
|
||||
|
||||
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<dataBlockStruct> m_blocks;
|
||||
|
||||
int m_fdFile;
|
||||
int m_fileSize;
|
||||
void *m_pFile;
|
||||
};
|
||||
|
||||
#endif
|
||||
66
KREngine/KREngine/Classes/KRTextureManager.cpp
Normal file
66
KREngine/KREngine/Classes/KRTextureManager.cpp
Normal file
@@ -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 <string.h>
|
||||
|
||||
KRTextureManager::KRTextureManager() {
|
||||
|
||||
}
|
||||
|
||||
KRTextureManager::~KRTextureManager() {
|
||||
for(map<std::string, KRTexture *>::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<std::string, KRTexture *>::iterator itr = m_textures.find(lowerName);
|
||||
if(itr == m_textures.end()) {
|
||||
// Not found
|
||||
return NULL;
|
||||
} else {
|
||||
return (*itr).second;
|
||||
}
|
||||
|
||||
}
|
||||
32
KREngine/KREngine/Classes/KRTextureManager.h
Normal file
32
KREngine/KREngine/Classes/KRTextureManager.h
Normal file
@@ -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 <map>
|
||||
#import <string>
|
||||
|
||||
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<std::string, KRTexture *> m_textures;
|
||||
};
|
||||
|
||||
#endif
|
||||
97
KREngine/KREngine/Classes/KRVector3.cpp
Normal file
97
KREngine/KREngine/Classes/KRVector3.cpp
Normal file
@@ -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);
|
||||
}
|
||||
54
KREngine/KREngine/Classes/KRVector3.h
Normal file
54
KREngine/KREngine/Classes/KRVector3.h
Normal file
@@ -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 <math.h>
|
||||
|
||||
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
|
||||
7
KREngine/KREngine/KREngine-Prefix.pch
Normal file
7
KREngine/KREngine/KREngine-Prefix.pch
Normal file
@@ -0,0 +1,7 @@
|
||||
//
|
||||
// Prefix header for all source files of the 'KREngine' target in the 'KREngine' project
|
||||
//
|
||||
|
||||
#ifdef __OBJC__
|
||||
#import <Foundation/Foundation.h>
|
||||
#endif
|
||||
154
KREngine/KREngine/Shaders/ObjectShader.fsh
Normal file
154
KREngine/KREngine/Shaders/ObjectShader.fsh
Normal file
@@ -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
|
||||
|
||||
}
|
||||
101
KREngine/KREngine/Shaders/ObjectShader.vsh
Normal file
101
KREngine/KREngine/Shaders/ObjectShader.vsh
Normal file
@@ -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
|
||||
}
|
||||
153
KREngine/KREngine/Shaders/PostShader.fsh
Normal file
153
KREngine/KREngine/Shaders/PostShader.fsh
Normal file
@@ -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);
|
||||
|
||||
}
|
||||
10
KREngine/KREngine/Shaders/PostShader.vsh
Normal file
10
KREngine/KREngine/Shaders/PostShader.vsh
Normal file
@@ -0,0 +1,10 @@
|
||||
attribute vec4 position;
|
||||
attribute lowp vec4 inputTextureCoordinate;
|
||||
|
||||
varying mediump vec2 textureCoordinate;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = position;
|
||||
textureCoordinate = inputTextureCoordinate.xy;
|
||||
}
|
||||
8
KREngine/KREngine/Shaders/ShadowShader.fsh
Normal file
8
KREngine/KREngine/Shaders/ShadowShader.fsh
Normal file
@@ -0,0 +1,8 @@
|
||||
uniform sampler2D diffuseTexture;
|
||||
varying mediump vec2 texCoord;
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
|
||||
}
|
||||
26
KREngine/KREngine/Shaders/ShadowShader.vsh
Normal file
26
KREngine/KREngine/Shaders/ShadowShader.vsh
Normal file
@@ -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;
|
||||
*/
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user