Use generic vertex attributes for landscape rendering

shapetextures
Armin Burgmeier 2016-01-02 17:20:24 -08:00
parent 7f1ed15344
commit 96c8b51eac
6 changed files with 71 additions and 30 deletions

View File

@ -74,14 +74,14 @@ slice(texture+5)
#ifdef OC_DYNAMIC_LIGHT
// Query light texture
vec2 lightDirCoord = lightCoord.st;
vec2 lightDirCoord = lightCoord;
vec4 lightPx = texture2D(lightTex, lightDirCoord);
float lightBright = maxLightBrightness * max(0.0, (lightPx.a-lightDarknessLevel)/(1.0-lightDarknessLevel));
vec3 lightDir = normalize(vec3(vec2(1.0, 1.0) - lightPx.yz * 3.0, lightDepth));
// Query light color texture (part of the light texture)
vec2 lightColorCoord = lightCoord.st - vec2(0.0, 0.5); // subtract offset for the color texture
vec2 lightColorCoord = lightCoord - vec2(0.0, 0.5); // subtract offset for the color texture
vec3 lightColor = texture2D(lightTex, lightColorCoord.st).rgb;

View File

@ -1,4 +1,10 @@
// Interpolated texture coordinates
varying vec2 landscapeTexCoord;
#ifdef OC_DYNAMIC_LIGHT
varying vec2 lightTexCoord;
#endif
// Input textures
uniform sampler2D landscapeTex[2];
uniform sampler2D scalerTex;
@ -48,7 +54,7 @@ slice(coordinate)
vec2 fullStepX = vec2(fullStep.x, 0.0);
vec2 fullStepY = vec2(0.0, fullStep.y);
vec2 texCoo = gl_TexCoord[0].st;
vec2 texCoo = landscapeTexCoord;
// calculate pixel position in landscape, find center of current pixel
vec2 pixelCoo = texCoo * resolution;
@ -80,6 +86,13 @@ slice(texture)
}
slice(texture+4)
{
#ifdef OC_DYNAMIC_LIGHT
vec2 lightCoord = lightTexCoord;
#endif
}
slice(material)
{

View File

@ -15,10 +15,27 @@
// Default Vertex Shader for the landscape.
attribute vec2 oc_Position;
attribute vec2 oc_LandscapeTexCoord;
attribute vec2 oc_LightTexCoord;
varying vec2 landscapeTexCoord;
#ifdef OC_DYNAMIC_LIGHT
varying vec2 lightTexCoord;
#endif
uniform mat4 projectionMatrix;
slice(position)
{
// model-view matrix is always the identity matrix
gl_Position = projectionMatrix * gl_Vertex;
gl_Position = projectionMatrix * vec4(oc_Position, 0.0, 1.0);
}
slice(texcoord)
{
landscapeTexCoord = oc_LandscapeTexCoord;
#ifdef OC_DYNAMIC_LIGHT
lightTexCoord = oc_LightTexCoord;
#endif
}

View File

@ -239,7 +239,7 @@ bool CStdGL::PrepareSpriteShader(C4Shader& shader, const char* name, int ssc, C4
shader.Clear();
shader.ClearSlices();
// Then load slices for fragment shader
// Start with #defines
shader.AddDefine("OPENCLONK");
shader.AddDefine("OC_SPRITE");
if (ssc & C4SSC_MOD2) shader.AddDefine("OC_CLRMOD_MOD2");
@ -252,6 +252,7 @@ bool CStdGL::PrepareSpriteShader(C4Shader& shader, const char* name, int ssc, C4
for (const char* const* define = additionalDefines; *define != NULL; ++define)
shader.AddDefine(*define);
// Then load slices for fragment and vertex shader
shader.LoadVertexSlices(pGroups, "SpriteVertexShader.glsl");
shader.LoadFragmentSlices(pGroups, "CommonShader.glsl");
shader.LoadFragmentSlices(pGroups, "ObjectShader.glsl");

View File

@ -564,22 +564,27 @@ const char *C4LandscapeRenderGL::UniformNames[C4LRU_Count+1];
bool C4LandscapeRenderGL::LoadShader(C4GroupSet *pGroups, C4Shader& shader, const char* name, int ssc)
{
// Setup #defines
shader.AddDefine("OPENCLONK");
shader.AddDefine("OC_LANDSCAPE");
if(ssc & C4SSC_LIGHT) shader.AddDefine("OC_DYNAMIC_LIGHT"); // sample light from light texture
// Create vertex shader
shader.LoadVertexSlices(pGroups, "LandscapeVertexShader.glsl");
hLandscapeTexCoord = shader.AddTexCoord("landscapeCoord");
if(ssc & C4SSC_LIGHT) hLightTexCoord = shader.AddTexCoord("lightCoord");
// Then load slices for fragment shader
shader.AddFragmentSlice(-1, "#define OPENCLONK");
shader.AddFragmentSlice(-1, "#define OC_LANDSCAPE");
if(ssc & C4SSC_LIGHT) shader.AddFragmentSlice(-1, "#define OC_DYNAMIC_LIGHT"); // sample light from light texture
shader.LoadFragmentSlices(pGroups, "CommonShader.glsl");
shader.LoadFragmentSlices(pGroups, "LandscapeShader.glsl");
// Make attribute name map
const char* AttributeNames[C4LRA_Count + 1];
AttributeNames[C4LRA_Position] = "oc_Position";
AttributeNames[C4LRA_LandscapeTexCoord] = "oc_LandscapeTexCoord";
AttributeNames[C4LRA_LightTexCoord] = "oc_LightTexCoord"; // unused if no dynamic light
AttributeNames[C4LRA_Count] = NULL;
// Initialise!
if (!shader.Init(name, UniformNames, NULL)) {
if (!shader.Init(name, UniformNames, AttributeNames)) {
shader.ClearSlices();
return false;
}
@ -1068,33 +1073,30 @@ void C4LandscapeRenderGL::Draw(const C4TargetFacet &cgo, const C4FoWRegion *Ligh
// Upload vertex data
glBindBuffer(GL_ARRAY_BUFFER, hVBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, nFloats * sizeof(float), vtxData);
glVertexPointer(2, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glClientActiveTexture(hLandscapeTexCoord);
glTexCoordPointer(2, GL_FLOAT, 0, reinterpret_cast<const uint8_t*>(8 * sizeof(float)));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Setup state
glEnableVertexAttribArray(shader->GetAttribute(C4LRA_Position));
glEnableVertexAttribArray(shader->GetAttribute(C4LRA_LandscapeTexCoord));
glVertexAttribPointer(shader->GetAttribute(C4LRA_Position), 2, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(shader->GetAttribute(C4LRA_LandscapeTexCoord), 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const uint8_t*>(8 * sizeof(float)));
if (Light)
{
glClientActiveTexture(hLightTexCoord);
glTexCoordPointer(2, GL_FLOAT, 0, reinterpret_cast<const uint8_t*>(16 * sizeof(float)));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableVertexAttribArray(shader->GetAttribute(C4LRA_LightTexCoord));
glVertexAttribPointer(shader->GetAttribute(C4LRA_LightTexCoord), 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<const uint8_t*>(16 * sizeof(float)));
}
// Do the blit
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Reset state
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableVertexAttribArray(shader->GetAttribute(C4LRA_Position));
glDisableVertexAttribArray(shader->GetAttribute(C4LRA_LandscapeTexCoord));
if (Light)
{
glClientActiveTexture(hLandscapeTexCoord);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
glDisableVertexAttribArray(shader->GetAttribute(C4LRA_LightTexCoord));
glBindBuffer(GL_ARRAY_BUFFER, 0);
ShaderCall.Finish();
}

View File

@ -56,6 +56,15 @@ enum C4LR_Uniforms
C4LRU_Count
};
enum C4LR_Attributes
{
C4LRA_Position,
C4LRA_LandscapeTexCoord,
C4LRA_LightTexCoord,
C4LRA_Count
};
// How much data we want to store per landscape pixel
const int C4LR_BytesPerPx = 3;
@ -108,7 +117,6 @@ private:
C4Shader Shader;
C4Shader ShaderLight;
static const char *UniformNames[];
GLenum hLandscapeTexCoord, hLightTexCoord;
// VBO for landscape vertex data
GLuint hVBO;