Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
#5909: Migrate bump and specular texture matrices to uniforms
  • Loading branch information
codereader committed Mar 13, 2022
1 parent f512bbb commit 4199e47
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 65 deletions.
38 changes: 9 additions & 29 deletions install/gl/interaction_fp.glsl
@@ -1,28 +1,8 @@
/// ============================================================================
/*
Copyright (C) 2004 Robert Beckebans <trebor_7@users.sourceforge.net>
Please see the file "CONTRIBUTORS" for a list of contributors
#version 120

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/// ============================================================================

uniform sampler2D u_diffusemap;
uniform sampler2D u_bumpmap;
uniform sampler2D u_specularmap;
uniform sampler2D u_Diffusemap;
uniform sampler2D u_Bumpmap;
uniform sampler2D u_Specularmap;
uniform sampler2D u_attenuationmap_xy;
uniform sampler2D u_attenuationmap_z;
uniform vec3 u_view_origin;
Expand All @@ -37,10 +17,10 @@ uniform bool uAmbientLight;

// Texture coords as calculated by the vertex program
varying vec2 var_TexDiffuse;
varying vec2 var_TexBump;
varying vec2 var_TexSpecular;

varying vec3 var_vertex;
varying vec4 var_tex_diffuse_bump;
varying vec2 var_tex_specular;
varying vec4 var_tex_atten_xy_z;
varying mat3 var_mat_os2ts;
varying vec4 var_Colour; // colour to be multiplied on the final fragment
Expand All @@ -57,20 +37,20 @@ void main()
vec3 H = normalize(L + V);

// compute normal in tangent space from bumpmap
vec2 normalRG = texture2D(u_bumpmap, var_tex_diffuse_bump.pq).rg;
vec2 normalRG = texture2D(u_Bumpmap, var_TexBump).rg;
float normalB = sqrt(1.0 - pow(normalRG.r, 2.0) - pow(normalRG.g, 2.0));
vec3 N = 2.0 * (vec3(normalRG, normalB) - 0.5);
N = normalize(N);

// compute the diffuse term
vec4 diffuse = texture2D(u_diffusemap, var_TexDiffuse);
vec4 diffuse = texture2D(u_Diffusemap, var_TexDiffuse);
float lightBrightness = uAmbientLight ? 1.0 : clamp(dot(N, L), 0.0, 1.0);
diffuse.rgb *= u_light_color * u_light_scale * lightBrightness;

// compute the specular term
float specIntensity = clamp(dot(N, H), 0.0, 1.0);
specIntensity = pow(specIntensity, 32.0);
vec3 specular = texture2D(u_specularmap, var_tex_specular.xy).rgb
vec3 specular = texture2D(u_Specularmap, var_TexSpecular).rgb
* u_light_color
* specIntensity;

Expand Down
40 changes: 9 additions & 31 deletions install/gl/interaction_vp.glsl
@@ -1,24 +1,3 @@
/// ============================================================================
/*
Copyright (C) 2004 Robert Beckebans <trebor_7@users.sourceforge.net>
Please see the file "CONTRIBUTORS" for a list of contributors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/// ============================================================================
#version 120

in vec4 attr_Position; // bound to attribute 0 in source, in object space
Expand All @@ -35,13 +14,15 @@ uniform mat4 u_ObjectTransform; // object to world

// Texture Matrices (the two top rows of each)
uniform vec4 u_DiffuseTextureMatrix[2];
uniform vec4 u_BumpTextureMatrix[2];
uniform vec4 u_SpecularTextureMatrix[2];

// Calculated texture coords
varying vec2 var_TexDiffuse;
varying vec2 var_TexBump;
varying vec2 var_TexSpecular;

varying vec3 var_vertex;
varying vec4 var_tex_diffuse_bump;
varying vec2 var_tex_specular;
varying vec4 var_tex_atten_xy_z;
varying mat3 var_mat_os2ts;
varying vec4 var_Colour; // colour to be multiplied on the final fragment
Expand All @@ -59,15 +40,12 @@ void main()
// Apply the texture matrix to get the texture coords for this vertex
var_TexDiffuse.x = dot(u_DiffuseTextureMatrix[0], attr_TexCoord);
var_TexDiffuse.y = dot(u_DiffuseTextureMatrix[1], attr_TexCoord);

var_TexBump.x = dot(u_BumpTextureMatrix[0], attr_TexCoord);
var_TexBump.y = dot(u_BumpTextureMatrix[1], attr_TexCoord);

// transform texcoords into diffusemap texture space
var_tex_diffuse_bump.st = (gl_TextureMatrix[0] * attr_TexCoord).st;

// transform texcoords into bumpmap texture space
var_tex_diffuse_bump.pq = (gl_TextureMatrix[1] * attr_TexCoord).st;

// transform texcoords into specularmap texture space
var_tex_specular = (gl_TextureMatrix[2] * attr_TexCoord).st;
var_TexSpecular.x = dot(u_SpecularTextureMatrix[0], attr_TexCoord);
var_TexSpecular.y = dot(u_SpecularTextureMatrix[1], attr_TexCoord);

// calc light xy,z attenuation in light space
var_tex_atten_xy_z = gl_TextureMatrix[3] * attr_Position;
Expand Down
19 changes: 17 additions & 2 deletions radiantcore/rendersystem/backend/InteractionPass.h
Expand Up @@ -25,12 +25,27 @@ class InteractionPass :

Matrix4 getDiffuseTextureTransform() const
{
const auto& diffuse = state().stage0;
return diffuse ? diffuse->getTextureTransform() : Matrix4::getIdentity();
return GetTextureTransformFromStage(state().stage0);
}

Matrix4 getBumpTextureTransform() const
{
return GetTextureTransformFromStage(state().stage1);
}

Matrix4 getSpecularTextureTransform() const
{
return GetTextureTransformFromStage(state().stage2);
}

// Generates the state with all the required flags for drawing interaction passes
static OpenGLState GenerateInteractionState(GLProgramFactory& programFactory);

private:
inline static Matrix4 GetTextureTransformFromStage(const IShaderLayer::Ptr& stage)
{
return stage ? stage->getTextureTransform() : Matrix4::getIdentity();
}
};

}
2 changes: 2 additions & 0 deletions radiantcore/rendersystem/backend/LightInteractions.cpp
Expand Up @@ -142,6 +142,8 @@ void LightInteractions::drawInteractions(OpenGLState& state, GLSLBumpProgram& pr

// Load stage texture matrices
program.setDiffuseTextureTransform(pass->getDiffuseTextureTransform());
program.setBumpTextureTransform(pass->getBumpTextureTransform());
program.setSpecularTextureTransform(pass->getSpecularTextureTransform());

for (const auto& object : objects)
{
Expand Down
18 changes: 15 additions & 3 deletions radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.cpp
Expand Up @@ -58,6 +58,8 @@ void GLSLBumpProgram::create()
_locObjectTransform = glGetUniformLocation(_programObj, "u_ObjectTransform");

_locDiffuseTextureMatrix = glGetUniformLocation(_programObj, "u_DiffuseTextureMatrix");
_locBumpTextureMatrix = glGetUniformLocation(_programObj, "u_BumpTextureMatrix");
_locSpecularTextureMatrix = glGetUniformLocation(_programObj, "u_SpecularTextureMatrix");

// Set up the texture uniforms. The renderer uses fixed texture units for
// particular textures, so make sure they are correct here.
Expand All @@ -72,13 +74,13 @@ void GLSLBumpProgram::create()

GLint samplerLoc;

samplerLoc = glGetUniformLocation(_programObj, "u_diffusemap");
samplerLoc = glGetUniformLocation(_programObj, "u_Diffusemap");
glUniform1i(samplerLoc, 0);

samplerLoc = glGetUniformLocation(_programObj, "u_bumpmap");
samplerLoc = glGetUniformLocation(_programObj, "u_Bumpmap");
glUniform1i(samplerLoc, 1);

samplerLoc = glGetUniformLocation(_programObj, "u_specularmap");
samplerLoc = glGetUniformLocation(_programObj, "u_Specularmap");
glUniform1i(samplerLoc, 2);

samplerLoc = glGetUniformLocation(_programObj, "u_attenuationmap_xy");
Expand Down Expand Up @@ -206,6 +208,16 @@ void GLSLBumpProgram::setDiffuseTextureTransform(const Matrix4& transform)
loadTextureMatrixUniform(_locDiffuseTextureMatrix, transform);
}

void GLSLBumpProgram::setBumpTextureTransform(const Matrix4& transform)
{
loadTextureMatrixUniform(_locBumpTextureMatrix, transform);
}

void GLSLBumpProgram::setSpecularTextureTransform(const Matrix4& transform)
{
loadTextureMatrixUniform(_locSpecularTextureMatrix, transform);
}

}


Expand Down
4 changes: 4 additions & 0 deletions radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.h
Expand Up @@ -25,6 +25,8 @@ class GLSLBumpProgram :
int _locObjectTransform;

int _locDiffuseTextureMatrix;
int _locBumpTextureMatrix;
int _locSpecularTextureMatrix;

public:

Expand All @@ -37,6 +39,8 @@ class GLSLBumpProgram :
void setObjectTransform(const Matrix4& transform);

void setDiffuseTextureTransform(const Matrix4& transform);
void setBumpTextureTransform(const Matrix4& transform);
void setSpecularTextureTransform(const Matrix4& transform);

void applyRenderParams(const Vector3& viewer,
const Matrix4& localToWorld,
Expand Down

0 comments on commit 4199e47

Please sign in to comment.