Skip to content

Commit

Permalink
#5909: Load the top half of the diffuse texture transformation matrix…
Browse files Browse the repository at this point in the history
… right into the GLSL program, as two vec4 uniforms
  • Loading branch information
codereader committed Mar 13, 2022
1 parent d14ece8 commit 509ed87
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 15 deletions.
13 changes: 7 additions & 6 deletions install/gl/zfill_alpha_fp.glsl
@@ -1,23 +1,24 @@
#version 120

uniform sampler2D u_diffuse;
uniform float u_alphaTest;
uniform mat4 u_objectTransform;
uniform sampler2D u_Diffuse;
uniform float u_AlphaTest;
uniform mat4 u_ObjectTransform;

// The final diffuse texture coordinate at this vertex, calculated in the vertex shader
varying vec2 var_TexDiffuse;

void main()
{
if (u_alphaTest < 0)
if (u_AlphaTest < 0)
{
gl_FragColor.a = 1.0;
gl_FragColor.rgb = vec3(1.0, 1.0, 0.0);
}
else
{
vec4 tex = texture2D(u_diffuse, var_TexDiffuse);
vec4 tex = texture2D(u_Diffuse, var_TexDiffuse);

if (tex.a <= u_alphaTest)
if (tex.a <= u_AlphaTest)
{
discard;
}
Expand Down
13 changes: 9 additions & 4 deletions install/gl/zfill_vp.glsl
Expand Up @@ -3,18 +3,23 @@
in vec4 attr_Position; // bound to attribute 0 in source, in object space
in vec4 attr_TexCoord; // bound to attribute 8 in source

uniform mat4 u_objectTransform; // object transform (object2world)
uniform mat4 u_ObjectTransform; // object transform (object2world)

// The two top-rows of the diffuse stage texture transformation matrix
uniform vec4 u_DiffuseTextureMatrix[2];

// The final diffuse texture coordinate at this vertex
varying vec2 var_TexDiffuse;

void main()
{
// Apply the supplied object transform to the incoming vertex
// transform vertex position into homogenous clip-space
gl_Position = gl_ModelViewProjectionMatrix * u_objectTransform * attr_Position;
gl_Position = gl_ModelViewProjectionMatrix * u_ObjectTransform * attr_Position;

// transform texcoords
var_TexDiffuse = (gl_TextureMatrix[0] * attr_TexCoord).st;
// Apply the stage texture transform to the incoming tex coord, component wise
var_TexDiffuse.x = dot(u_DiffuseTextureMatrix[0], attr_TexCoord);
var_TexDiffuse.y = dot(u_DiffuseTextureMatrix[1], attr_TexCoord);

// assign color
gl_FrontColor = gl_Color;
Expand Down
18 changes: 16 additions & 2 deletions radiantcore/rendersystem/backend/LightInteractions.cpp
Expand Up @@ -79,6 +79,20 @@ void LightInteractions::fillDepthBuffer(OpenGLState& state, RenderStateFlags glo
// Apply our state to the current state object
depthFillPass->applyState(state, globalFlagsMask, view.getViewer(), renderTime, entity);

auto depthFillProgram = depthFillPass->getDepthFillProgram();

// Set the stage texture transformation matrix to the GLSL uniform
// Since the texture matrix just needs 6 active components, we use two vec3
if (depthFillPass->state().stage0)
{
auto textureMatrix = depthFillPass->state().stage0->getTextureTransform();
depthFillProgram.setDiffuseTextureTransform(textureMatrix);
}
else
{
depthFillProgram.setDiffuseTextureTransform(Matrix4::getIdentity());
}

for (const auto& object : objects)
{
// We submit all objects with an identity matrix in a single multi draw call
Expand All @@ -88,15 +102,15 @@ void LightInteractions::fillDepthBuffer(OpenGLState& state, RenderStateFlags glo
continue;
}

depthFillPass->getDepthFillProgram().setObjectTransform(object.get().getObjectTransform());
depthFillProgram.setObjectTransform(object.get().getObjectTransform());

ObjectRenderer::SubmitGeometry(object.get().getStorageLocation(), GL_TRIANGLES, _store);
++_drawCalls;
}

if (!untransformedObjects.empty())
{
depthFillPass->getDepthFillProgram().setObjectTransform(Matrix4::getIdentity());
depthFillProgram.setObjectTransform(Matrix4::getIdentity());

ObjectRenderer::SubmitGeometry(untransformedObjects, GL_TRIANGLES, _store);
++_drawCalls;
Expand Down
Expand Up @@ -31,13 +31,14 @@ void GLSLDepthFillAlphaProgram::create()

debug::assertNoGlErrors();

_locAlphaTest = glGetUniformLocation(_programObj, "u_alphaTest");
_locObjectTransform = glGetUniformLocation(_programObj, "u_objectTransform");
_locAlphaTest = glGetUniformLocation(_programObj, "u_AlphaTest");
_locObjectTransform = glGetUniformLocation(_programObj, "u_ObjectTransform");
_locDiffuseTextureMatrix = glGetUniformLocation(_programObj, "u_DiffuseTextureMatrix");

glUseProgram(_programObj);
debug::assertNoGlErrors();

GLint samplerLoc = glGetUniformLocation(_programObj, "u_diffuse");
auto samplerLoc = glGetUniformLocation(_programObj, "u_Diffuse");
glUniform1i(samplerLoc, 0);

debug::assertNoGlErrors();
Expand Down Expand Up @@ -79,5 +80,10 @@ void GLSLDepthFillAlphaProgram::setObjectTransform(const Matrix4& transform)
loadMatrixUniform(_locObjectTransform, transform);
}

void GLSLDepthFillAlphaProgram::setDiffuseTextureTransform(const Matrix4& transform)
{
loadTextureMatrixUniform(_locDiffuseTextureMatrix, transform);
}

}

Expand Up @@ -11,13 +11,15 @@ class GLSLDepthFillAlphaProgram :
private:
GLint _locAlphaTest;
GLint _locObjectTransform;
GLint _locDiffuseTextureMatrix;

public:
void create() override;
void enable() override;
void disable() override;

void setObjectTransform(const Matrix4& transform);
void setDiffuseTextureTransform(const Matrix4& transform);

void applyAlphaTest(float alphaTest);
};
Expand Down
28 changes: 28 additions & 0 deletions radiantcore/rendersystem/backend/glprogram/GLSLProgramBase.cpp
Expand Up @@ -42,4 +42,32 @@ void GLSLProgramBase::loadMatrixUniform(GLuint location, const Matrix4& matrix)
glUniformMatrix4fv(location, 1, GL_FALSE, values);
}

void GLSLProgramBase::loadTextureMatrixUniform(GLuint location, const Matrix4& matrix)
{
/* Extract the 6 components that are relevant to texture transforms.
* We load it row-major to be able to use a dot product in GLSL.
*
* | m00 m01 0 m02 |
* | m10 m11 0 m12 |
* | 0 0 1 0 |
* | 0 0 0 1 |
*/
float values[8];

// First vector is the top row
values[0] = static_cast<float>(matrix.xx());
values[1] = static_cast<float>(matrix.yx());
values[2] = 0.0f;
values[3] = static_cast<float>(matrix.tx());

// Second vector is the bottom row
values[4] = static_cast<float>(matrix.xy());
values[5] = static_cast<float>(matrix.yy());
values[6] = 0.0f;
values[7] = static_cast<float>(matrix.ty());

// Load two 4-component vectors into the uniform location
glUniform4fv(location, 2, values);
}

}
Expand Up @@ -25,6 +25,7 @@ class GLSLProgramBase :

protected:
void loadMatrixUniform(GLuint location, const Matrix4& matrix);
void loadTextureMatrixUniform(GLuint location, const Matrix4& matrix);
};

}

0 comments on commit 509ed87

Please sign in to comment.