Completed implementation and debugging of cube map reflections, including cube map reflections for normal mapped textures
--HG-- extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%40127
This commit is contained in:
@@ -70,9 +70,9 @@ KRMaterial::KRMaterial(KRContext &context, const char *szName) : KRResource(cont
|
||||
m_alpha_mode = KRMATERIAL_ALPHA_MODE_OPAQUE;
|
||||
|
||||
|
||||
// FINDME - HACK - Test Code:
|
||||
m_reflectionCube = "skycube";
|
||||
m_reflectionColor = KRVector3(0.75, 0.75, 0.75);
|
||||
// // FINDME - HACK - Test Code:
|
||||
// m_reflectionCube = "skycube";
|
||||
// m_reflectionColor = KRVector3(0.75, 0.75, 0.75);
|
||||
|
||||
}
|
||||
|
||||
@@ -165,9 +165,9 @@ void KRMaterial::setReflectionMap(std::string texture_name, KRVector2 texture_sc
|
||||
|
||||
void KRMaterial::setReflectionCube(std::string texture_name) {
|
||||
m_reflectionCube = texture_name;
|
||||
// FINDME - HACK - Test Code:
|
||||
m_reflectionCube = "skycube";
|
||||
m_reflectionColor = KRVector3(0.75, 0.75, 0.75);
|
||||
// // FINDME - HACK - Test Code:
|
||||
// m_reflectionCube = "skycube";
|
||||
// m_reflectionColor = KRVector3(0.75, 0.75, 0.75);
|
||||
}
|
||||
|
||||
void KRMaterial::setAlphaMode(KRMaterial::alpha_mode_type alpha_mode) {
|
||||
@@ -192,9 +192,9 @@ void KRMaterial::setSpecular(const KRVector3 &c) {
|
||||
|
||||
void KRMaterial::setReflection(const KRVector3 &c) {
|
||||
m_reflectionColor = c;
|
||||
// FINDME - HACK - Test Code:
|
||||
m_reflectionCube = "skycube";
|
||||
m_reflectionColor = KRVector3(0.75, 0.75, 0.75);
|
||||
// // FINDME - HACK - Test Code:
|
||||
// m_reflectionCube = "skycube";
|
||||
// m_reflectionColor = KRVector3(0.75, 0.75, 0.75);
|
||||
}
|
||||
|
||||
void KRMaterial::setTransparency(GLfloat a) {
|
||||
|
||||
@@ -133,6 +133,7 @@ KRShader::KRShader(char *szKey, std::string options, std::string vertShaderSourc
|
||||
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_INVP] = glGetUniformLocation(m_iProgram, "inv_projection_matrix"));
|
||||
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_INVMVP_NO_TRANSLATE] = glGetUniformLocation(m_iProgram, "inv_mvp_matrix_no_translate"));
|
||||
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_MODEL_VIEW_INVERSE_TRANSPOSE] = glGetUniformLocation(m_iProgram, "model_view_inverse_transpose_matrix"));
|
||||
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_MODEL_INVERSE_TRANSPOSE] = glGetUniformLocation(m_iProgram, "model_inverse_transpose_matrix"));
|
||||
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_M2V] = glGetUniformLocation(m_iProgram, "model_view_matrix"));
|
||||
GLDEBUG(m_uniforms[KRENGINE_UNIFORM_MODEL_MATRIX] = glGetUniformLocation(m_iProgram, "model_matrix"));
|
||||
|
||||
@@ -223,6 +224,11 @@ bool KRShader::bind(KRCamera *pCamera, KRMat4 &matModel, KRMat4 &matView, KRMat4
|
||||
|
||||
GLDEBUG(glUniformMatrix4fv(m_uniforms[KRENGINE_UNIFORM_MODEL_VIEW_INVERSE_TRANSPOSE], 1, GL_FALSE, matModelViewInverseTranspose.getPointer()));
|
||||
|
||||
KRMat4 matModelInverseTranspose = matModel;
|
||||
matModelInverseTranspose.transpose();
|
||||
matModelInverseTranspose.invert();
|
||||
|
||||
GLDEBUG(glUniformMatrix4fv(m_uniforms[KRENGINE_UNIFORM_MODEL_INVERSE_TRANSPOSE], 1, GL_FALSE, matModelInverseTranspose.getPointer()));
|
||||
|
||||
KRMat4 matInvProjection;
|
||||
matInvProjection = pCamera->getProjectionMatrix();
|
||||
|
||||
@@ -91,6 +91,7 @@ public:
|
||||
KRENGINE_UNIFORM_INVMVP,
|
||||
KRENGINE_UNIFORM_INVMVP_NO_TRANSLATE,
|
||||
KRENGINE_UNIFORM_MODEL_VIEW_INVERSE_TRANSPOSE,
|
||||
KRENGINE_UNIFORM_MODEL_INVERSE_TRANSPOSE,
|
||||
KRENGINE_UNIFORM_M2V,
|
||||
KRENGINE_UNIFORM_V2M,
|
||||
KRENGINE_UNIFORM_MODEL_MATRIX,
|
||||
|
||||
@@ -89,6 +89,7 @@
|
||||
uniform lowp vec3 material_reflection;
|
||||
uniform samplerCube reflectionCubeTexture;
|
||||
#if HAS_NORMAL_MAP == 1
|
||||
varying highp mat3 tangent_to_world_matrix;
|
||||
varying mediump vec3 eyeVec;
|
||||
uniform highp mat4 model_matrix;
|
||||
#else
|
||||
@@ -286,8 +287,9 @@ void main()
|
||||
// -------------------- Add reflected light --------------------
|
||||
#if HAS_NORMAL_MAP == 1
|
||||
// Calculate reflection vector as I - 2.0 * dot(N, I) * N
|
||||
mediump vec3 normalizedEyeVec = normalize(eyeVec);
|
||||
mediump vec3 reflectionVec = mat3(model_matrix) * (normalizedEyeVec - 2.0 * dot(normal, normalizedEyeVec) * normal);
|
||||
mediump vec3 incidenceVec = -normalize(eyeVec);
|
||||
highp vec3 world_space_normal = tangent_to_world_matrix * normal;
|
||||
mediump vec3 reflectionVec = mat3(model_matrix) * (incidenceVec - 2.0 * dot(world_space_normal, incidenceVec) * world_space_normal);
|
||||
#endif
|
||||
#if HAS_REFLECTION_MAP == 1
|
||||
gl_FragColor += vec4(material_reflection, 0.0) * texture2D(reflectionTexture, reflection_uv) * textureCube(reflectionCubeTexture, reflectionVec);
|
||||
|
||||
@@ -125,7 +125,9 @@ uniform highp mat4 mvp_matrix; // mvp_matrix is the result of multiplying t
|
||||
|
||||
#if HAS_REFLECTION_CUBE_MAP == 1
|
||||
#if HAS_NORMAL_MAP == 1
|
||||
uniform highp mat4 model_inverse_transpose_matrix;
|
||||
varying mediump vec3 eyeVec;
|
||||
varying highp mat3 tangent_to_world_matrix;
|
||||
#else
|
||||
uniform highp mat4 model_matrix;
|
||||
varying mediump vec3 reflectionVec;
|
||||
@@ -208,7 +210,8 @@ void main()
|
||||
#else
|
||||
// Calculate reflection vector as I - 2.0 * dot(N, I) * N
|
||||
mediump vec3 eyeVec = normalize(cameraPosition - vertex_position);
|
||||
reflectionVec = mat3(model_matrix) * (eyeVec - 2.0 * dot(vertex_normal, eyeVec) * vertex_normal);
|
||||
mediump vec3 incidenceVec = -eyeVec;
|
||||
reflectionVec = mat3(model_matrix) * (incidenceVec - 2.0 * dot(vertex_normal, incidenceVec) * vertex_normal);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -265,6 +268,10 @@ void main()
|
||||
#if HAS_REFLECTION_CUBE_MAP == 0
|
||||
// The cube map reflections also require an eyeVec as a varying attribute when normal mapping, so only re-calculate here when needed
|
||||
mediump vec3 eyeVec = normalize(cameraPosition - vertex_position);
|
||||
#else
|
||||
tangent_to_world_matrix[0] = vec3(model_inverse_transpose_matrix * vec4(vertex_tangent, 1.0));
|
||||
tangent_to_world_matrix[1] = vec3(model_inverse_transpose_matrix * vec4(a_bitangent, 1.0));
|
||||
tangent_to_world_matrix[2] = vec3(model_inverse_transpose_matrix * vec4(vertex_normal, 1.0));
|
||||
#endif
|
||||
|
||||
lightVec = normalize(vec3(dot(light_direction, vertex_tangent), dot(light_direction, a_bitangent), dot(light_direction, vertex_normal)));
|
||||
|
||||
Reference in New Issue
Block a user