Deferred lighting implementation in progress. Now generating view-space normal fragments for the 1st pass G-buffer.

Added transpose() method to KRMat4

--HG--
extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%4043
This commit is contained in:
kearwood
2012-04-13 01:13:18 +00:00
parent 3749d3edfd
commit b88cf8e0cb
16 changed files with 124 additions and 48 deletions

View File

@@ -47,6 +47,7 @@ KRCamera::KRCamera() {
bEnableSpecular = true;
bEnableLightMap = true;
bDebugSuperShiny = false;
bEnableDeferredLighting = false;
dAmbientR = 0.25f;

View File

@@ -54,6 +54,7 @@ public:
bool bEnableAmbient;
bool bEnableDiffuse;
bool bEnableSpecular;
bool bEnableDeferredLighting;
double dSunR;
double dSunG;
double dSunB;

View File

@@ -65,6 +65,7 @@ typedef enum KREngineParameterType {KRENGINE_PARAMETER_INT, KRENGINE_PARAMETER_F
KRENGINE_UNIFORM_MATERIAL_DIFFUSE,
KRENGINE_UNIFORM_MATERIAL_SPECULAR,
KRENGINE_UNIFORM_MVP,
KRENGINE_UNIFORM_M2V,
KRENGINE_UNIFORM_SHADOWMVP1,
KRENGINE_UNIFORM_SHADOWMVP2,
KRENGINE_UNIFORM_SHADOWMVP3,

View File

@@ -746,12 +746,12 @@ double const PI = 3.141592653589793f;
-(int)getParameterCount
{
return 31;
return 32;
}
-(NSString *)getParameterNameWithIndex: (int)i
{
NSString *parameter_names[31] = {
NSString *parameter_names[32] = {
@"camera_fov",
@"sun_direction",
@"sun_attitude",
@@ -782,13 +782,14 @@ double const PI = 3.141592653589793f;
@"debug_enable_ambient",
@"debug_enable_diffuse",
@"debug_enable_specular",
@"debug_super_shiny"
@"debug_super_shiny",
@"enable_deferred_lighting"
};
return parameter_names[i];
}
-(NSString *)getParameterLabelWithIndex: (int)i
{
NSString *parameter_labels[31] = {
NSString *parameter_labels[32] = {
@"Camera FOV",
@"Sun Direction",
@"Sun Attitude",
@@ -819,13 +820,15 @@ double const PI = 3.141592653589793f;
@"Debug - Enable Ambient",
@"Debug - Enable Diffuse",
@"Debug - Enable Specular",
@"Debug - Super Shiny"
@"Debug - Super Shiny",
@"Enable Deferred Lighting"
};
return parameter_labels[i];
}
-(KREngineParameterType)getParameterTypeWithIndex: (int)i
{
KREngineParameterType types[31] = {
KREngineParameterType types[32] = {
KRENGINE_PARAMETER_FLOAT,
KRENGINE_PARAMETER_FLOAT,
KRENGINE_PARAMETER_FLOAT,
@@ -856,13 +859,14 @@ double const PI = 3.141592653589793f;
KRENGINE_PARAMETER_BOOL,
KRENGINE_PARAMETER_BOOL,
KRENGINE_PARAMETER_BOOL,
KRENGINE_PARAMETER_BOOL,
KRENGINE_PARAMETER_BOOL
};
return types[i];
}
-(double)getParameterValueWithIndex: (int)i
{
double values[31] = {
double values[32] = {
m_camera.perspective_fov,
sun_yaw,
sun_pitch,
@@ -893,7 +897,8 @@ double const PI = 3.141592653589793f;
m_camera.bEnableAmbient ? 1.0f : 0.0f,
m_camera.bEnableDiffuse ? 1.0f : 0.0f,
m_camera.bEnableSpecular ? 1.0f : 0.0f,
m_camera.bDebugSuperShiny ? 1.0f : 0.0f
m_camera.bDebugSuperShiny ? 1.0f : 0.0f,
m_camera.bEnableDeferredLighting ? 1.0f : 0.0f
};
return values[i];
}
@@ -1043,18 +1048,22 @@ double const PI = 3.141592653589793f;
m_camera.bDebugSuperShiny = bNewBoolVal;
}
break;
case 31:
if(m_camera.bEnableDeferredLighting != bNewBoolVal) {
m_camera.bEnableDeferredLighting = bNewBoolVal;
}
}
}
-(double)getParameterMinWithIndex: (int)i
{
double minValues[31] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
double minValues[32] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
return minValues[i];
}
-(double)getParameterMaxWithIndex: (int)i
{
double maxValues[31] = {PI, 2.0f * PI, PI, 3.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 2.0f, 1.0f, 1.0f, 1.0f, 5.0f, 1.0f, 0.5f, 1.0f, 2.0f, 2.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
double maxValues[32] = {PI, 2.0f * PI, PI, 3.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 3.0f, 2.0f, 1.0f, 1.0f, 1.0f, 5.0f, 1.0f, 0.5f, 1.0f, 2.0f, 2.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
return maxValues[i];
}

View File

@@ -91,6 +91,9 @@ void KRInstance::render(KRCamera *pCamera, KRContext *pContext, KRBoundingVolume
projectionMatrix = pCamera->getProjectionMatrix();
}
KRMat4 mvpmatrix = m_modelMatrix * viewMatrix * projectionMatrix;
KRMat4 matModelToView = viewMatrix * m_modelMatrix;
matModelToView.transpose();
matModelToView.invert();
// Transform location of camera to object space for calculation of specular halfVec
KRMat4 inverseModelMatrix = m_modelMatrix;
@@ -98,7 +101,7 @@ void KRInstance::render(KRCamera *pCamera, KRContext *pContext, KRBoundingVolume
KRVector3 cameraPosObject = inverseModelMatrix.dot(cameraPosition);
KRVector3 lightDirObject = inverseModelMatrix.dot(lightDirection);
m_pModel->render(pCamera, pContext, bRenderShadowMap, mvpmatrix, cameraPosObject, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, m_pLightMap);
m_pModel->render(pCamera, pContext, bRenderShadowMap, matModelToView, mvpmatrix, cameraPosObject, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, m_pLightMap);
}

View File

@@ -241,6 +241,16 @@ bool KRMat4::invert() {
return true;
}
void KRMat4::transpose() {
GLfloat trans[16];
for(int x=0; x<4; x++) {
for(int y=0; y<4; y++) {
trans[x + y * 4] = m_mat[y + x * 4];
}
}
memcpy(m_mat, trans, sizeof(GLfloat) * 16);
}
/* Dot Product */
KRVector3 KRMat4::dot(const KRVector3 &v) const {
return KRVector3(

View File

@@ -95,6 +95,7 @@ public:
void rotate(GLfloat angle, AXIS axis);
void bias();
bool invert();
void transpose();
KRVector3 dot(const KRVector3 &v) const;
};

View File

@@ -158,7 +158,7 @@ bool KRMaterial::isTransparent() {
return m_tr != 0.0;
}
#if TARGET_OS_IPHONE
void KRMaterial::bind(KRMaterial **prevBoundMaterial, char *szPrevShaderKey, KRCamera *pCamera, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRContext *pContext, KRTexture *pLightMap) {
void KRMaterial::bind(KRMaterial **prevBoundMaterial, char *szPrevShaderKey, KRCamera *pCamera, KRMat4 &matModelToView, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRContext *pContext, KRTexture *pLightMap) {
bool bSameMaterial = *prevBoundMaterial == this;
bool bLightMap = pLightMap && pCamera->bEnableLightMap;
@@ -191,7 +191,7 @@ void KRMaterial::bind(KRMaterial **prevBoundMaterial, char *szPrevShaderKey, KRC
bool bSameShader = strcmp(pShader->getKey(), szPrevShaderKey) == 0;
if(!bSameShader) {
pShader->bind(pCamera, mvpMatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers);
pShader->bind(pCamera, matModelToView, mvpMatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers);
glUniform1f(pShader->m_uniforms[KRShader::KRENGINE_UNIFORM_MATERIAL_SHININESS], pCamera->bDebugSuperShiny ? 20.0 : m_ns );
strcpy(szPrevShaderKey, pShader->getKey());
}

View File

@@ -76,7 +76,7 @@ public:
char *getName();
#if TARGET_OS_IPHONE
void bind(KRMaterial **prevBoundMaterial, char *szPrevShaderKey, KRCamera *pCamera, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRContext *pContext, KRTexture *pLightMap);
void bind(KRMaterial **prevBoundMaterial, char *szPrevShaderKey, KRCamera *pCamera, KRMat4 &matModelToView, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRContext *pContext, KRTexture *pLightMap);
#endif

View File

@@ -67,7 +67,7 @@ KRModel::~KRModel() {
}
#if TARGET_OS_IPHONE
void KRModel::render(KRCamera *pCamera, KRContext *pContext, bool bRenderShadowMap, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRTexture *pLightMap) {
void KRModel::render(KRCamera *pCamera, KRContext *pContext, bool bRenderShadowMap, KRMat4 &matModelToView, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRTexture *pLightMap) {
if(m_materials.size() == 0) {
vector<KRMesh::Submesh *> submeshes = m_pMesh->getSubmeshes();
@@ -104,7 +104,7 @@ void KRModel::render(KRCamera *pCamera, KRContext *pContext, bool bRenderShadowM
KRMaterial *pMaterial = m_materials[iSubmesh];
if(pMaterial != NULL && pMaterial == (*mat_itr)) {
pMaterial->bind(&pPrevBoundMaterial, szPrevShaderKey, pCamera, mvpMatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, pContext, pLightMap);
pMaterial->bind(&pPrevBoundMaterial, szPrevShaderKey, pCamera, matModelToView, mvpMatrix, cameraPosition, lightDirection, pShadowMatrices, shadowDepthTextures, cShadowBuffers, pContext, pLightMap);
m_pMesh->renderSubmesh(iSubmesh, &iPrevBuffer);
}
}

View File

@@ -55,7 +55,7 @@ public:
#if TARGET_OS_IPHONE
void render(KRCamera *pCamera, KRContext *pContext, bool bRenderShadowMap, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRTexture *pLightMap);
void render(KRCamera *pCamera, KRContext *pContext, bool bRenderShadowMap, KRMat4 &matModelToView, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers, KRTexture *pLightMap);
#endif

View File

@@ -79,6 +79,7 @@ KRShader::KRShader(char *szKey, std::string options, const GLchar *szVertShaderS
m_uniforms[KRENGINE_UNIFORM_MATERIAL_SHININESS] = glGetUniformLocation(m_iProgram, "material_shininess");
m_uniforms[KRENGINE_UNIFORM_MVP] = glGetUniformLocation(m_iProgram, "myMVPMatrix");
m_uniforms[KRENGINE_UNIFORM_M2V] = glGetUniformLocation(m_iProgram, "model_to_view");
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");
@@ -133,12 +134,13 @@ KRShader::~KRShader() {
}
}
void KRShader::bind(KRCamera *pCamera, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers) {
void KRShader::bind(KRCamera *pCamera, KRMat4 &matModelToView, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &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());
glUniformMatrix4fv(m_uniforms[KRENGINE_UNIFORM_M2V], 1, GL_FALSE, matModelToView.getPointer());
KRVector3 nLightDir = lightDirection;
nLightDir.normalize();

View File

@@ -51,7 +51,7 @@ public:
GLuint getProgram();
char *getKey();
void bind(KRCamera *pCamera, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers);
void bind(KRCamera *pCamera, KRMat4 &matModelToView, KRMat4 &mvpMatrix, KRVector3 &cameraPosition, KRVector3 &lightDirection, KRMat4 *pShadowMatrices, GLuint *shadowDepthTextures, int cShadowBuffers);
enum {
KRENGINE_ATTRIB_VERTEX,
@@ -69,6 +69,7 @@ public:
KRENGINE_UNIFORM_MATERIAL_ALPHA,
KRENGINE_UNIFORM_MATERIAL_SHININESS,
KRENGINE_UNIFORM_MVP,
KRENGINE_UNIFORM_M2V,
KRENGINE_UNIFORM_LIGHTDIRECTION,
KRENGINE_UNIFORM_CAMERAPOS,
KRENGINE_UNIFORM_DIFFUSETEXTURE,

View File

@@ -50,7 +50,7 @@ KRShaderManager::~KRShaderManager() {
KRShader *KRShaderManager::getShader(KRCamera *pCamera, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, int iShadowQuality, bool bLightMap, bool bDiffuseMapScale,bool bSpecMapScale, bool bNormalMapScale, bool bDiffuseMapOffset, bool bSpecMapOffset, bool bNormalMapOffset) {
char szKey[128];
sprintf(szKey, "%d_%d_%d_%d_%d_%d_%d_%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, bLightMap, bDiffuseMapScale, bSpecMapScale, bNormalMapScale, bDiffuseMapOffset, bSpecMapOffset, bNormalMapOffset);
sprintf(szKey, "%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d", pCamera->bEnablePerPixel, bDiffuseMap, bNormalMap, bSpecMap, pCamera->bDebugPSSM, iShadowQuality, pCamera->bEnableAmbient, pCamera->bEnableDiffuse, pCamera->bEnableSpecular, bLightMap, bDiffuseMapScale, bSpecMapScale, bNormalMapScale, bDiffuseMapOffset, bSpecMapOffset, bNormalMapOffset);
/*

View File

@@ -25,6 +25,16 @@
// or implied, of Kearwood Gilbert.
//
#define GBUFFER_PASS 0
#if GBUFFER_PASS == 1
#if HAS_NORMAL_MAP == 1
varying highp mat3 tangent_to_view;
#else
uniform highp mat4 model_to_view;
#endif
#endif
uniform lowp vec3 material_ambient, material_diffuse, material_specular;
uniform lowp float material_alpha;
@@ -179,6 +189,9 @@ void main()
}
#endif
#if GBUFFER_PASS == 0
#if ENABLE_AMBIENT
// -------------------- Add ambient light and alpha component --------------------
gl_FragColor = vec4(vec3(diffuseMaterial) * material_ambient, material_alpha);
@@ -209,5 +222,19 @@ void main()
mediump vec3 shadowColor = vec3(texture2D(shadowTexture1, shadowCoord));
gl_FragColor = vec4(gl_FragColor.r * shadowColor.r, gl_FragColor.g * shadowColor.g, gl_FragColor.b * shadowColor.b, 1.0);
#endif
#endif
#if GBUFFER_PASS == 1
//gl_FragColor = vec4((normal + 1.0) / 2.0, 1.0f);
#if HAS_NORMAL_MAP == 1
mediump vec3 view_space_normal = tangent_to_view * normal;
#else
mediump vec3 view_space_normal = vec3(model_to_view * vec4(normal, 1.0));
#endif
gl_FragColor = vec4(view_space_normal * 0.5 + 0.5, 1.0);
#endif
}

View File

@@ -29,11 +29,21 @@
// or implied, of Kearwood Gilbert.
//
#define GBUFFER_PASS 0
attribute highp vec3 myVertex, myNormal;
attribute highp vec3 myTangent;
attribute mediump vec2 myUV;
attribute mediump vec2 shadowuv;
uniform highp mat4 myMVPMatrix, myShadowMVPMatrix1,myShadowMVPMatrix2,myShadowMVPMatrix3; // mvpmatrix is the result of multiplying the model, view, and projection matrices
#if GBUFFER_PASS == 1 && HAS_NORMAL_MAP == 1
uniform highp mat4 model_to_view;
varying highp mat3 tangent_to_view;
#endif
// uniform lowp vec3 material_ambient, material_diffuse, material_specular;
uniform highp vec3 lightDirection; // Must be normalized before entering shader
uniform highp vec3 cameraPosition;
@@ -187,12 +197,15 @@ void main()
#endif
// ----------- Directional Light (Sun) -----------
#if (HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1) || GBUFFER_PASS == 1
mediump vec3 a_bitangent = cross(myNormal, myTangent);
#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
@@ -211,4 +224,11 @@ void main()
specularFactor = max(0.0,pow(dot(halfVec,myNormal), material_shininess));
#endif
#endif
#if GBUFFER_PASS == 1 && HAS_NORMAL_MAP == 1
tangent_to_view[0] = vec3(model_to_view * vec4(myTangent, 1.0));
tangent_to_view[1] = vec3(model_to_view * vec4(a_bitangent, 1.0));
tangent_to_view[2] = vec3(model_to_view * vec4(myNormal, 1.0));
#endif
}