Skip to content

Commit

Permalink
OpenGL2: Add normalScale and parallaxDepth stage keywords and helper …
Browse files Browse the repository at this point in the history
…cvars.
  • Loading branch information
SmileTheory committed Mar 4, 2014
1 parent ea2810c commit ee67d0a
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 45 deletions.
17 changes: 10 additions & 7 deletions code/renderergl2/glsl/lightall_fp.glsl
Expand Up @@ -25,7 +25,8 @@ uniform samplerCube u_CubeMap;
#endif

#if defined(USE_NORMALMAP) || defined(USE_DELUXEMAP) || defined(USE_SPECULARMAP) || defined(USE_CUBEMAP)
uniform vec4 u_EnableTextures; // x = normal, y = deluxe, z = specular, w = cube
// y = deluxe, w = cube
uniform vec4 u_EnableTextures;
#endif

#if defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
Expand All @@ -39,7 +40,8 @@ uniform vec3 u_PrimaryLightAmbient;
#endif

#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
uniform vec2 u_MaterialInfo;
uniform vec4 u_NormalScale;
uniform vec4 u_SpecularScale;
#endif

varying vec4 var_TexCoords;
Expand Down Expand Up @@ -360,7 +362,7 @@ void main()
#if defined(USE_PARALLAXMAP)
vec3 offsetDir = normalize(E * tangentToWorld);

offsetDir.xy *= -0.05 / offsetDir.z;
offsetDir.xy *= -u_NormalScale.a / offsetDir.z;

texCoords += offsetDir.xy * RayIntersectDisplaceMap(texCoords, offsetDir.xy, u_NormalMap);
#endif
Expand All @@ -378,7 +380,7 @@ void main()
#else
N.xy = texture2D(u_NormalMap, texCoords).rg - vec2(0.5);
#endif
N.xy *= u_EnableTextures.x;
N.xy *= u_NormalScale.xy;
N.z = sqrt(clamp((0.25 - N.x * N.x) - N.y * N.y, 0.0, 1.0));
N = tangentToWorld * N;
#else
Expand Down Expand Up @@ -425,15 +427,16 @@ void main()
NL = clamp(dot(N, L), 0.0, 1.0);
NE = clamp(dot(N, E), 0.0, 1.0);

vec4 specular = vec4(1.0);
#if defined(USE_SPECULARMAP)
specular += texture2D(u_SpecularMap, texCoords) * u_EnableTextures.z - u_EnableTextures.zzzz;
vec4 specular = texture2D(u_SpecularMap, texCoords);
#if defined(USE_GAMMA2_TEXTURES)
specular.rgb *= specular.rgb;
#endif
#else
vec4 specular = vec4(1.0);
#endif

specular *= u_MaterialInfo.xxxy;
specular *= u_SpecularScale;

float gloss = specular.a;
float shininess = exp2(gloss * 13.0);
Expand Down
7 changes: 4 additions & 3 deletions code/renderergl2/tr_glsl.c
Expand Up @@ -123,9 +123,10 @@ static uniformInfo_t uniformsInfo[] =
{ "u_ModelMatrix", GLSL_MAT16 },
{ "u_ModelViewProjectionMatrix", GLSL_MAT16 },

{ "u_Time", GLSL_FLOAT },
{ "u_VertexLerp" , GLSL_FLOAT },
{ "u_MaterialInfo", GLSL_VEC2 },
{ "u_Time", GLSL_FLOAT },
{ "u_VertexLerp" , GLSL_FLOAT },
{ "u_NormalScale", GLSL_VEC4 },
{ "u_SpecularScale", GLSL_VEC4 },

{ "u_ViewInfo", GLSL_VEC4 },
{ "u_ViewOrigin", GLSL_VEC3 },
Expand Down
6 changes: 6 additions & 0 deletions code/renderergl2/tr_init.c
Expand Up @@ -135,6 +135,9 @@ cvar_t *r_parallaxMapping;
cvar_t *r_cubeMapping;
cvar_t *r_deluxeSpecular;
cvar_t *r_specularIsMetallic;
cvar_t *r_baseNormalX;
cvar_t *r_baseNormalY;
cvar_t *r_baseParallax;
cvar_t *r_baseSpecular;
cvar_t *r_baseGloss;
cvar_t *r_recalcMD3Normals;
Expand Down Expand Up @@ -1188,6 +1191,9 @@ void R_Register( void )
r_cubeMapping = ri.Cvar_Get( "r_cubeMapping", "0", CVAR_ARCHIVE | CVAR_LATCH );
r_deluxeSpecular = ri.Cvar_Get( "r_deluxeSpecular", "0.3", CVAR_ARCHIVE | CVAR_LATCH );
r_specularIsMetallic = ri.Cvar_Get( "r_specularIsMetallic", "0", CVAR_ARCHIVE | CVAR_LATCH );
r_baseNormalX = ri.Cvar_Get( "r_baseNormalX", "1.0", CVAR_ARCHIVE | CVAR_LATCH );
r_baseNormalY = ri.Cvar_Get( "r_baseNormalY", "1.0", CVAR_ARCHIVE | CVAR_LATCH );
r_baseParallax = ri.Cvar_Get( "r_baseParallax", "0.05", CVAR_ARCHIVE | CVAR_LATCH );
r_baseSpecular = ri.Cvar_Get( "r_baseSpecular", "0.04", CVAR_ARCHIVE | CVAR_LATCH );
r_baseGloss = ri.Cvar_Get( "r_baseGloss", "0.3", CVAR_ARCHIVE | CVAR_LATCH );
r_dlightMode = ri.Cvar_Get( "r_dlightMode", "0", CVAR_ARCHIVE | CVAR_LATCH );
Expand Down
11 changes: 9 additions & 2 deletions code/renderergl2/tr_local.h
Expand Up @@ -400,7 +400,10 @@ typedef struct {
stageType_t type;
struct shaderProgram_s *glslShaderGroup;
int glslShaderIndex;
vec2_t materialInfo;

vec4_t normalScale;
vec4_t specularScale;

} shaderStage_t;

struct shaderCommands_s;
Expand Down Expand Up @@ -676,7 +679,8 @@ typedef enum

UNIFORM_TIME,
UNIFORM_VERTEXLERP,
UNIFORM_MATERIALINFO,
UNIFORM_NORMALSCALE,
UNIFORM_SPECULARSCALE,

UNIFORM_VIEWINFO, // znear, zfar, width/2, height/2
UNIFORM_VIEWORIGIN,
Expand Down Expand Up @@ -1794,6 +1798,9 @@ extern cvar_t *r_parallaxMapping;
extern cvar_t *r_cubeMapping;
extern cvar_t *r_deluxeSpecular;
extern cvar_t *r_specularIsMetallic;
extern cvar_t *r_baseNormalX;
extern cvar_t *r_baseNormalY;
extern cvar_t *r_baseParallax;
extern cvar_t *r_baseSpecular;
extern cvar_t *r_baseGloss;
extern cvar_t *r_dlightMode;
Expand Down
31 changes: 29 additions & 2 deletions code/renderergl2/tr_shade.c
Expand Up @@ -811,7 +811,8 @@ static void ForwardDlight( void ) {

GLSL_SetUniformFloat(sp, UNIFORM_LIGHTRADIUS, radius);

GLSL_SetUniformVec2(sp, UNIFORM_MATERIALINFO, pStage->materialInfo);
GLSL_SetUniformVec4(sp, UNIFORM_NORMALSCALE, pStage->normalScale);
GLSL_SetUniformVec4(sp, UNIFORM_SPECULARSCALE, pStage->specularScale);

// include GLS_DEPTHFUNC_EQUAL so alpha tested surfaces don't add light
// where they aren't rendered
Expand All @@ -822,11 +823,36 @@ static void ForwardDlight( void ) {
if (pStage->bundle[TB_DIFFUSEMAP].image[0])
R_BindAnimatedImageToTMU( &pStage->bundle[TB_DIFFUSEMAP], TB_DIFFUSEMAP);

// bind textures that are sampled and used in the glsl shader, and
// bind whiteImage to textures that are sampled but zeroed in the glsl shader
//
// alternatives:
// - use the last bound texture
// -> costs more to sample a higher res texture then throw out the result
// - disable texture sampling in glsl shader with #ifdefs, as before
// -> increases the number of shaders that must be compiled
//

if (pStage->bundle[TB_NORMALMAP].image[0])
{
R_BindAnimatedImageToTMU( &pStage->bundle[TB_NORMALMAP], TB_NORMALMAP);
}
else if (r_normalMapping->integer)
GL_BindToTMU( tr.whiteImage, TB_NORMALMAP );

if (pStage->bundle[TB_SPECULARMAP].image[0])
{
R_BindAnimatedImageToTMU( &pStage->bundle[TB_SPECULARMAP], TB_SPECULARMAP);
}
else if (r_specularMapping->integer)
GL_BindToTMU( tr.whiteImage, TB_SPECULARMAP );

{
vec4_t enableTextures;

VectorSet4(enableTextures, 0.0f, 0.0f, 0.0f, 0.0f);
GLSL_SetUniformVec4(sp, UNIFORM_ENABLETEXTURES, enableTextures);
}

if (r_dlightMode->integer >= 2)
{
Expand Down Expand Up @@ -1222,7 +1248,8 @@ static void RB_IterateStagesGeneric( shaderCommands_t *input )

GLSL_SetUniformMat4(sp, UNIFORM_MODELMATRIX, backEnd.or.transformMatrix);

GLSL_SetUniformVec2(sp, UNIFORM_MATERIALINFO, pStage->materialInfo);
GLSL_SetUniformVec4(sp, UNIFORM_NORMALSCALE, pStage->normalScale);
GLSL_SetUniformVec4(sp, UNIFORM_SPECULARSCALE, pStage->specularScale);

//GLSL_SetUniformFloat(sp, UNIFORM_MAPLIGHTSCALE, backEnd.refdef.mapLightScale);

Expand Down

0 comments on commit ee67d0a

Please sign in to comment.