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
This commit is contained in:
kearwood
2012-04-13 23:24:07 +00:00
parent 0d7cdffd24
commit 97d8054cd0
9 changed files with 66 additions and 75 deletions

View File

@@ -144,8 +144,8 @@
E45772F113C9A13C0037BEEA /* ShadowShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; lineEnding = 0; name = ShadowShader.vsh; path = Shaders/ShadowShader.vsh; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.glsl; }; E45772F113C9A13C0037BEEA /* ShadowShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; lineEnding = 0; name = ShadowShader.vsh; path = Shaders/ShadowShader.vsh; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.glsl; };
E45772F213C9A13C0037BEEA /* ShadowShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ShadowShader.fsh; path = Shaders/ShadowShader.fsh; 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>"; }; 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>"; }; E45772F413C9A13C0037BEEA /* ObjectShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; name = ObjectShader.fsh; path = Shaders/ObjectShader.fsh; sourceTree = "<group>"; };
E45772F513C9A13C0037BEEA /* ObjectShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; lineEnding = 0; name = ObjectShader.vsh; path = Shaders/ObjectShader.vsh; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.glsl; }; E45772F513C9A13C0037BEEA /* ObjectShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lineEnding = 0; name = ObjectShader.vsh; path = Shaders/ObjectShader.vsh; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.glsl; };
E45772F613C9A13C0037BEEA /* PostShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = PostShader.vsh; path = Shaders/PostShader.vsh; sourceTree = "<group>"; }; E45772F613C9A13C0037BEEA /* PostShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = PostShader.vsh; path = Shaders/PostShader.vsh; sourceTree = "<group>"; };
E461A151152E54B500F2044A /* KRLight.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRLight.h; path = Classes/KRLight.h; sourceTree = "<group>"; }; E461A151152E54B500F2044A /* KRLight.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRLight.h; path = Classes/KRLight.h; sourceTree = "<group>"; };
E461A155152E54F700F2044A /* KRLight.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRLight.cpp; path = Classes/KRLight.cpp; sourceTree = "<group>"; }; E461A155152E54F700F2044A /* KRLight.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRLight.cpp; path = Classes/KRLight.cpp; sourceTree = "<group>"; };

View File

@@ -10,8 +10,8 @@
#include "KRContext.h" #include "KRContext.h"
KRContext::KRContext(const GLchar *szVertShaderSource, const GLchar *szFragShaderSource) { KRContext::KRContext() {
m_pShaderManager = new KRShaderManager(szVertShaderSource, szFragShaderSource); m_pShaderManager = new KRShaderManager();
m_pTextureManager = new KRTextureManager(); m_pTextureManager = new KRTextureManager();
m_pMaterialManager = new KRMaterialManager(m_pTextureManager, m_pShaderManager); m_pMaterialManager = new KRMaterialManager(m_pTextureManager, m_pShaderManager);
m_pModelManager = new KRModelManager(); m_pModelManager = new KRModelManager();
@@ -73,6 +73,10 @@ void KRContext::loadResource(std::string path) {
} else if(extension.compare("pvr") == 0) { } else if(extension.compare("pvr") == 0) {
m_pTextureManager->loadTexture(name.c_str(), path.c_str()); m_pTextureManager->loadTexture(name.c_str(), path.c_str());
#endif #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) { } else if(extension.compare("mtl") == 0) {
m_pMaterialManager->loadFile(path.c_str()); m_pMaterialManager->loadFile(path.c_str());
} else { } else {

View File

@@ -17,7 +17,7 @@
class KRContext { class KRContext {
public: public:
KRContext(const GLchar *szVertShaderSource, const GLchar *szFragShaderSource); KRContext();
~KRContext(); ~KRContext();
void loadResource(std::string path); void loadResource(std::string path);

View File

@@ -42,7 +42,6 @@ using namespace std;
@interface KREngine (PrivateMethods) @interface KREngine (PrivateMethods)
//- (BOOL)loadObjects;
- (BOOL)loadShaders; - (BOOL)loadShaders;
- (BOOL)createBuffers; - (BOOL)createBuffers;
- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file; - (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file;
@@ -75,14 +74,11 @@ double const PI = 3.141592653589793f;
if ((self = [super init])) { 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]; [self release];
return nil; return nil;
@@ -649,6 +645,15 @@ double const PI = 3.141592653589793f;
- (BOOL)loadShaders - (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]; [self loadVertexShader:@"ShadowShader" fragmentShader:@"ShadowShader" forProgram:&m_shadowShaderProgram withOptions: NULL];
m_shadowUniforms[KRENGINE_UNIFORM_SHADOWMVP1] = glGetUniformLocation(m_shadowShaderProgram, "shadow_mvp1"); m_shadowUniforms[KRENGINE_UNIFORM_SHADOWMVP1] = glGetUniformLocation(m_shadowShaderProgram, "shadow_mvp1");

View File

@@ -31,13 +31,13 @@
#include "KRShader.h" #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); strcpy(m_szKey, szKey);
m_iProgram = 0; m_iProgram = 0;
GLuint vertexShader = 0, fragShader = 0; GLuint vertexShader = 0, fragShader = 0;
try { try {
const GLchar *vertSource[2] = {options.c_str(), szVertShaderSource}; const GLchar *vertSource[2] = {options.c_str(), vertShaderSource.c_str()};
const GLchar *fragSource[2] = {options.c_str(), szFragShaderSource}; const GLchar *fragSource[2] = {options.c_str(), fragShaderSource.c_str()};
// Create shader program. // Create shader program.
m_iProgram = glCreateProgram(); m_iProgram = glCreateProgram();

View File

@@ -46,7 +46,7 @@ using std::vector;
class KRShader { class KRShader {
public: 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(); ~KRShader();
GLuint getProgram(); GLuint getProgram();
char *getKey(); char *getKey();

View File

@@ -30,26 +30,24 @@
// //
#include "KRShaderManager.h" #include "KRShaderManager.h"
#include <sstream> #include <iostream>
#include <sstream>
#include <fstream>
using namespace std; using namespace std;
KRShaderManager::KRShaderManager(const GLchar *szVertShaderSource, const GLchar *szFragShaderSource) { KRShaderManager::KRShaderManager() {
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) { 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()); 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"; stream << "\n";
std::string options = stream.str(); 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; m_shaders[szKey] = pShader;
} }
return 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<char> 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<char> bytes(fileSize);
ifs.read(&bytes[0], fileSize);
m_vertShaderSource[name] = string(&bytes[0], fileSize);
}

View File

@@ -47,16 +47,20 @@ using std::vector;
class KRShaderManager { class KRShaderManager {
public: public:
KRShaderManager(const GLchar *szVertShaderSource, const GLchar *szFragShaderSource); KRShaderManager();
~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); 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: private:
std::map<std::string, KRShader *> m_shaders; std::map<std::string, KRShader *> m_shaders;
GLchar *m_szFragShaderSource; std::map<std::string, std::string> m_fragShaderSource;
GLchar *m_szVertShaderSource; std::map<std::string, std::string> m_vertShaderSource;
KRShader *m_pShader; KRShader *m_pShader;
}; };

View File

@@ -107,54 +107,10 @@
NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName]; NSString* path = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName];
[renderEngine loadResource: path]; [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 setNearZ: 25.0];
[renderEngine setFarZ: 5000.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; return TRUE;
} }