From 97d8054cd040ac6e5c6be7bed63f7b8d686f669f Mon Sep 17 00:00:00 2001 From: kearwood Date: Fri, 13 Apr 2012 23:24:07 +0000 Subject: [PATCH] Deferred lighting in progress Now able to dynamically switch between shaders for lights and objects --HG-- extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%4046 --- KREngine/KREngine.xcodeproj/project.pbxproj | 4 +- KREngine/KREngine/Classes/KRContext.cpp | 8 +++- KREngine/KREngine/Classes/KRContext.h | 2 +- KREngine/KREngine/Classes/KREngine.mm | 19 +++++--- KREngine/KREngine/Classes/KRShader.cpp | 6 +-- KREngine/KREngine/Classes/KRShader.h | 2 +- KREngine/KREngine/Classes/KRShaderManager.cpp | 42 ++++++++++++---- KREngine/KREngine/Classes/KRShaderManager.h | 10 ++-- objview/Classes/KRObjViewGLView.mm | 48 +------------------ 9 files changed, 66 insertions(+), 75 deletions(-) diff --git a/KREngine/KREngine.xcodeproj/project.pbxproj b/KREngine/KREngine.xcodeproj/project.pbxproj index 871dc69..deda0fa 100644 --- a/KREngine/KREngine.xcodeproj/project.pbxproj +++ b/KREngine/KREngine.xcodeproj/project.pbxproj @@ -144,8 +144,8 @@ E45772F113C9A13C0037BEEA /* ShadowShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; lineEnding = 0; name = ShadowShader.vsh; path = Shaders/ShadowShader.vsh; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.glsl; }; E45772F213C9A13C0037BEEA /* ShadowShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ShadowShader.fsh; path = Shaders/ShadowShader.fsh; sourceTree = ""; }; E45772F313C9A13C0037BEEA /* PostShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = PostShader.fsh; path = Shaders/PostShader.fsh; sourceTree = ""; }; - E45772F413C9A13C0037BEEA /* ObjectShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ObjectShader.fsh; path = Shaders/ObjectShader.fsh; sourceTree = ""; }; - E45772F513C9A13C0037BEEA /* ObjectShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; lineEnding = 0; name = ObjectShader.vsh; path = Shaders/ObjectShader.vsh; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.glsl; }; + E45772F413C9A13C0037BEEA /* ObjectShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; name = ObjectShader.fsh; path = Shaders/ObjectShader.fsh; sourceTree = ""; }; + E45772F513C9A13C0037BEEA /* ObjectShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lineEnding = 0; name = ObjectShader.vsh; path = Shaders/ObjectShader.vsh; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.glsl; }; E45772F613C9A13C0037BEEA /* PostShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = PostShader.vsh; path = Shaders/PostShader.vsh; sourceTree = ""; }; E461A151152E54B500F2044A /* KRLight.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRLight.h; path = Classes/KRLight.h; sourceTree = ""; }; E461A155152E54F700F2044A /* KRLight.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRLight.cpp; path = Classes/KRLight.cpp; sourceTree = ""; }; diff --git a/KREngine/KREngine/Classes/KRContext.cpp b/KREngine/KREngine/Classes/KRContext.cpp index dcc3978..82ab37f 100644 --- a/KREngine/KREngine/Classes/KRContext.cpp +++ b/KREngine/KREngine/Classes/KRContext.cpp @@ -10,8 +10,8 @@ #include "KRContext.h" -KRContext::KRContext(const GLchar *szVertShaderSource, const GLchar *szFragShaderSource) { - m_pShaderManager = new KRShaderManager(szVertShaderSource, szFragShaderSource); +KRContext::KRContext() { + m_pShaderManager = new KRShaderManager(); m_pTextureManager = new KRTextureManager(); m_pMaterialManager = new KRMaterialManager(m_pTextureManager, m_pShaderManager); m_pModelManager = new KRModelManager(); @@ -73,6 +73,10 @@ void KRContext::loadResource(std::string path) { } else if(extension.compare("pvr") == 0) { m_pTextureManager->loadTexture(name.c_str(), path.c_str()); #endif + } else if(extension.compare("vsh") == 0) { + m_pShaderManager->loadVertexShader(name.c_str(), path.c_str()); + } else if(extension.compare("fsh") == 0) { + m_pShaderManager->loadFragmentShader(name.c_str(), path.c_str()); } else if(extension.compare("mtl") == 0) { m_pMaterialManager->loadFile(path.c_str()); } else { diff --git a/KREngine/KREngine/Classes/KRContext.h b/KREngine/KREngine/Classes/KRContext.h index 65fc651..3f14787 100644 --- a/KREngine/KREngine/Classes/KRContext.h +++ b/KREngine/KREngine/Classes/KRContext.h @@ -17,7 +17,7 @@ class KRContext { public: - KRContext(const GLchar *szVertShaderSource, const GLchar *szFragShaderSource); + KRContext(); ~KRContext(); void loadResource(std::string path); diff --git a/KREngine/KREngine/Classes/KREngine.mm b/KREngine/KREngine/Classes/KREngine.mm index 33c0080..ce9f6b9 100644 --- a/KREngine/KREngine/Classes/KREngine.mm +++ b/KREngine/KREngine/Classes/KREngine.mm @@ -42,7 +42,6 @@ using namespace std; @interface KREngine (PrivateMethods) -//- (BOOL)loadObjects; - (BOOL)loadShaders; - (BOOL)createBuffers; - (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file; @@ -75,14 +74,11 @@ double const PI = 3.141592653589793f; if ((self = [super init])) { - NSString *vertShaderPathname = [[NSBundle mainBundle] pathForResource:@"ObjectShader" ofType:@"vsh"]; - NSString *fragShaderPathname = [[NSBundle mainBundle] pathForResource:@"ObjectShader" ofType:@"fsh"]; - GLchar * szVertShaderSource = (GLchar *)[[NSString stringWithContentsOfFile:vertShaderPathname encoding:NSUTF8StringEncoding error:nil] UTF8String]; - GLchar * szFragShaderSource = (GLchar *)[[NSString stringWithContentsOfFile:fragShaderPathname encoding:NSUTF8StringEncoding error:nil] UTF8String]; - m_pContext = new KRContext(szVertShaderSource, szFragShaderSource); + m_pContext = new KRContext(); + - if (![self createBuffers] || ![self loadShaders] /*|| ![self loadObjects]*/ ) + if (![self createBuffers] || ![self loadShaders] ) { [self release]; return nil; @@ -649,6 +645,15 @@ double const PI = 3.141592653589793f; - (BOOL)loadShaders { + NSFileManager* fileManager = [NSFileManager defaultManager]; + NSString *bundle_directory = [[NSBundle mainBundle] bundlePath]; + for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: bundle_directory error:nil]) { + if([fileName hasSuffix: @".vsh"] || [fileName hasSuffix: @".fsh"]) { + NSString* path = [NSString stringWithFormat:@"%@/%@", bundle_directory, fileName]; + m_pContext->loadResource([path UTF8String]); + } + } + [self loadVertexShader:@"ShadowShader" fragmentShader:@"ShadowShader" forProgram:&m_shadowShaderProgram withOptions: NULL]; m_shadowUniforms[KRENGINE_UNIFORM_SHADOWMVP1] = glGetUniformLocation(m_shadowShaderProgram, "shadow_mvp1"); diff --git a/KREngine/KREngine/Classes/KRShader.cpp b/KREngine/KREngine/Classes/KRShader.cpp index 85a0f51..5e47330 100644 --- a/KREngine/KREngine/Classes/KRShader.cpp +++ b/KREngine/KREngine/Classes/KRShader.cpp @@ -31,13 +31,13 @@ #include "KRShader.h" -KRShader::KRShader(char *szKey, std::string options, const GLchar *szVertShaderSource, const GLchar *szFragShaderSource) { +KRShader::KRShader(char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource) { strcpy(m_szKey, szKey); 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}; + const GLchar *vertSource[2] = {options.c_str(), vertShaderSource.c_str()}; + const GLchar *fragSource[2] = {options.c_str(), fragShaderSource.c_str()}; // Create shader program. m_iProgram = glCreateProgram(); diff --git a/KREngine/KREngine/Classes/KRShader.h b/KREngine/KREngine/Classes/KRShader.h index aee1d83..9aae1ee 100644 --- a/KREngine/KREngine/Classes/KRShader.h +++ b/KREngine/KREngine/Classes/KRShader.h @@ -46,7 +46,7 @@ using std::vector; class KRShader { public: - KRShader(char *szKey, std::string options, const GLchar *szVertShaderSource, const GLchar *szFragShaderSource); + KRShader(char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource); ~KRShader(); GLuint getProgram(); char *getKey(); diff --git a/KREngine/KREngine/Classes/KRShaderManager.cpp b/KREngine/KREngine/Classes/KRShaderManager.cpp index 0dbe4c5..2a1fc16 100644 --- a/KREngine/KREngine/Classes/KRShaderManager.cpp +++ b/KREngine/KREngine/Classes/KRShaderManager.cpp @@ -30,26 +30,24 @@ // #include "KRShaderManager.h" -#include +#include +#include +#include using namespace std; -KRShaderManager::KRShaderManager(const GLchar *szVertShaderSource, const GLchar *szFragShaderSource) { - m_szFragShaderSource = new GLchar[strlen(szFragShaderSource)+1]; - m_szVertShaderSource = new GLchar[strlen(szVertShaderSource)+1]; - strcpy(m_szFragShaderSource, szFragShaderSource); - strcpy(m_szVertShaderSource, szVertShaderSource); +KRShaderManager::KRShaderManager() { + } KRShaderManager::~KRShaderManager() { - delete m_szFragShaderSource; - delete m_szVertShaderSource; + } KRShader *KRShaderManager::getShader(std::string shader_name, KRCamera *pCamera, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, int iShadowQuality, bool bLightMap, bool bDiffuseMapScale,bool bSpecMapScale, bool bNormalMapScale, bool bDiffuseMapOffset, bool bSpecMapOffset, bool bNormalMapOffset, int gBufferPass) { - char szKey[128]; + char szKey[256]; sprintf(szKey, "%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%i_%s", pCamera->bEnablePerPixel, bDiffuseMap, bNormalMap, bSpecMap, pCamera->bDebugPSSM, iShadowQuality, pCamera->bEnableAmbient, pCamera->bEnableDiffuse, pCamera->bEnableSpecular, bLightMap, bDiffuseMapScale, bSpecMapScale, bNormalMapScale, bDiffuseMapOffset, bSpecMapOffset, bNormalMapOffset, gBufferPass, shader_name.c_str()); @@ -92,9 +90,33 @@ KRShader *KRShaderManager::getShader(std::string shader_name, KRCamera *pCamera, stream << "\n"; std::string options = stream.str(); - pShader = new KRShader(szKey, options, m_szVertShaderSource, m_szFragShaderSource); + pShader = new KRShader(szKey, options, m_vertShaderSource[shader_name], m_fragShaderSource[shader_name]); m_shaders[szKey] = pShader; } return pShader; } + +void KRShaderManager::loadFragmentShader(const std::string &name, const std::string &path) { + ifstream ifs(path.c_str(), ios::in | ios::binary | ios::ate); + + ifstream::pos_type fileSize = ifs.tellg(); + ifs.seekg(0, ios::beg); + + vector bytes(fileSize); + ifs.read(&bytes[0], fileSize); + + m_fragShaderSource[name] = string(&bytes[0], fileSize); + +} +void KRShaderManager::loadVertexShader(const std::string &name, const std::string &path) { + ifstream ifs(path.c_str(), ios::in | ios::binary | ios::ate); + + ifstream::pos_type fileSize = ifs.tellg(); + ifs.seekg(0, ios::beg); + + vector bytes(fileSize); + ifs.read(&bytes[0], fileSize); + + m_vertShaderSource[name] = string(&bytes[0], fileSize); +} diff --git a/KREngine/KREngine/Classes/KRShaderManager.h b/KREngine/KREngine/Classes/KRShaderManager.h index 47df476..0cbe2c3 100644 --- a/KREngine/KREngine/Classes/KRShaderManager.h +++ b/KREngine/KREngine/Classes/KRShaderManager.h @@ -47,16 +47,20 @@ using std::vector; class KRShaderManager { public: - KRShaderManager(const GLchar *szVertShaderSource, const GLchar *szFragShaderSource); + KRShaderManager(); ~KRShaderManager(); + void loadFragmentShader(const std::string &name, const std::string &path); + void loadVertexShader(const std::string &name, const std::string &path); + + KRShader *getShader(std::string shader_name, KRCamera *pCamera, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, int iShadowQuality, bool bLightMap, bool bDiffuseMapScale,bool bSpecMapScale, bool bNormalMapScale, bool bDiffuseMapOffset, bool bSpecMapOffset, bool bNormalMapOffset, int gBufferPass); private: std::map m_shaders; - GLchar *m_szFragShaderSource; - GLchar *m_szVertShaderSource; + std::map m_fragShaderSource; + std::map m_vertShaderSource; KRShader *m_pShader; }; diff --git a/objview/Classes/KRObjViewGLView.mm b/objview/Classes/KRObjViewGLView.mm index fe49755..4e2b5fc 100644 --- a/objview/Classes/KRObjViewGLView.mm +++ b/objview/Classes/KRObjViewGLView.mm @@ -107,54 +107,10 @@ NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; [renderEngine loadResource: path]; } -// -// for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { -// if([fileName hasSuffix: @".scene"]) { -// NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; -// [renderEngine loadResource: path]; -// } -// } -// -// for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { -// if([fileName hasSuffix: @".pvr"]) { -// NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; -// [renderEngine loadResource: path]; -// } -// } -// -// for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { -// if([fileName hasSuffix: @".mtl"]) { -// NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; -// [renderEngine loadResource: path]; -// } -// } -// -// for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { -// if([fileName hasSuffix: @".krobject"]) { -// NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; -// [renderEngine loadResource: path]; -// } -// } -// -// for (NSString* fileName in [fileManager contentsOfDirectoryAtPath: documentsDirectory error:nil]) { -// if([fileName hasSuffix: @".krscene"]) { -// NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; -// [renderEngine loadResource: path]; -// } -// } - + [renderEngine setNearZ: 25.0]; [renderEngine setFarZ: 5000.0]; - /* - - startPos 156.0 -55.0 -825.0 - touchScale 95.0 - nearZ 25.0 - farZ 5000.0 - - */ - - // [renderEngine setParameterValueWithName: @]; + return TRUE; }