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:
@@ -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; };
|
||||
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; lineEnding = 0; name = ObjectShader.vsh; path = Shaders/ObjectShader.vsh; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.glsl; };
|
||||
E45772F413C9A13C0037BEEA /* ObjectShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; name = ObjectShader.fsh; path = Shaders/ObjectShader.fsh; sourceTree = "<group>"; };
|
||||
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>"; };
|
||||
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>"; };
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
class KRContext {
|
||||
public:
|
||||
KRContext(const GLchar *szVertShaderSource, const GLchar *szFragShaderSource);
|
||||
KRContext();
|
||||
~KRContext();
|
||||
|
||||
void loadResource(std::string path);
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -30,26 +30,24 @@
|
||||
//
|
||||
|
||||
#include "KRShaderManager.h"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
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<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);
|
||||
}
|
||||
|
||||
@@ -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<std::string, KRShader *> m_shaders;
|
||||
|
||||
GLchar *m_szFragShaderSource;
|
||||
GLchar *m_szVertShaderSource;
|
||||
std::map<std::string, std::string> m_fragShaderSource;
|
||||
std::map<std::string, std::string> m_vertShaderSource;
|
||||
|
||||
KRShader *m_pShader;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user