Skip to content

Commit

Permalink
Renderer|DGL: Configure fog via DGL
Browse files Browse the repository at this point in the history
DGL fog parameters affect both `dgl.draw` and model shaders.
  • Loading branch information
skyjake committed Apr 24, 2017
1 parent 826448c commit 193d26d
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 154 deletions.
19 changes: 17 additions & 2 deletions doomsday/apps/api/api_gl.h
Expand Up @@ -47,7 +47,11 @@ enum {
DGL_CURRENT_COLOR_A,
DGL_CURRENT_COLOR_RGBA,

DGL_MODULATE_TEXTURE_COLOR,
DGL_FOG_MODE,
DGL_FOG_START,
DGL_FOG_END,
DGL_FOG_DENSITY,
DGL_FOG_COLOR,

// Matrices
DGL_MODELVIEW = 0x4000,
Expand Down Expand Up @@ -94,7 +98,9 @@ enum {
DGL_CLAMP_TO_EDGE,
DGL_REPEAT,
DGL_LINE_WIDTH,
DGL_POINT_SIZE
DGL_POINT_SIZE,
DGL_EXP,
DGL_EXP2,
};

// Types.
Expand Down Expand Up @@ -328,9 +334,15 @@ DENG_API_TYPEDEF(GL)
* be created dynamically.
*/
int (*Bind)(DGLuint texture);

void (*DeleteTextures)(int num, const DGLuint* names);

void (*Fogi)(DGLenum property, int value);
void (*Fogf)(DGLenum property, float value);
void (*Fogfv)(DGLenum property, float const *values);

void (*UseFog)(int yes);

void (*SetFilter)(dd_bool enable);
void (*SetFilterColor)(float r, float g, float b, float a);
void (*ConfigureBorderedProjection2)(dgl_borderedprojectionstate_t* bp, int flags, int width, int height, int availWidth, int availHeight, scalemode_t overrideMode, float stretchEpsilon);
Expand Down Expand Up @@ -409,6 +421,9 @@ DENG_API_T(GL);
#define DGL_NewTextureWithParams _api_GL.NewTextureWithParams
#define DGL_Bind _api_GL.Bind
#define DGL_DeleteTextures _api_GL.DeleteTextures
#define DGL_Fogi _api_GL.Fogi
#define DGL_Fogf _api_GL.Fogf
#define DGL_Fogfv _api_GL.Fogfv
#define GL_UseFog _api_GL.UseFog
#define GL_SetFilter _api_GL.SetFilter
#define GL_SetFilterColor _api_GL.SetFilterColor
Expand Down
10 changes: 0 additions & 10 deletions doomsday/apps/client/include/gl/gl_deferredapi.h
Expand Up @@ -43,19 +43,9 @@
extern "C" {
#endif

//#define glEnable(x) Deferred_glEnable(x)
//#define glDisable(x) Deferred_glDisable(x)
//#define glDeleteTextures(x, y) Deferred_glDeleteTextures(x, y)
//#define glFogi(x, y) Deferred_glFogi(x, y)
//#define glFogf(x, y) Deferred_glFogf(x, y)
//#define glFogfv(x, y) Deferred_glFogfv(x, y)

void Deferred_glEnable(GLenum e);
void Deferred_glDisable(GLenum e);
void Deferred_glDeleteTextures(GLsizei num, const GLuint* names);
void Deferred_glFogi(GLenum p, GLint v);
void Deferred_glFogf(GLenum p, GLfloat v);
void Deferred_glFogfv(GLenum p, const GLfloat* v);

#ifdef __cplusplus
} // extern "C"
Expand Down
1 change: 1 addition & 0 deletions doomsday/apps/client/include/gl/gl_main.h
Expand Up @@ -315,6 +315,7 @@ void DGL_CurrentColor(float *rgba);
void DGL_ModulateTexture(int mode);
void DGL_SetModulationColor(de::Vector4f const &modColor);
de::Vector4f DGL_ModulationColor();
void DGL_FogParams(de::GLUniform &fogRange, de::GLUniform &fogColor);

// Console commands ---------------------------------------------------------------------

Expand Down
6 changes: 0 additions & 6 deletions doomsday/apps/client/include/gl/sys_opengl.h
Expand Up @@ -80,19 +80,13 @@
* High-level GL state information.
*/
typedef struct gl_state_s {
/// Global config:
//int multisampleFormat;

/// Current state:
dd_bool currentUseFog;
float currentLineWidth;
float currentPointSize;

/// Feature (abstract) availability bits:
/// Vendor and implementation agnostic.
struct {
//uint blendSubtract : 1;
//uint genMipmap : 1;
uint texCompression : 1;
uint texFilterAniso : 1;
} features;
Expand Down
@@ -1,5 +1,7 @@
# Shader for the DGL drawing routines that emulate OpenGL 1.x behavior.
shader dgl.draw {
path.vertex = "dgl_draw.vsh"

include.fragment <../include/fog.glsl>
path.fragment = "dgl_draw.fsh"
}
Expand Up @@ -23,6 +23,7 @@
uniform int uTexEnabled;
uniform int uTexMode;
uniform vec4 uTexModeColor;
uniform float uAlphaLimit;
uniform sampler2D uTex0;
uniform sampler2D uTex1;

Expand Down Expand Up @@ -54,11 +55,44 @@ void main()
break;
case 2:
// Texture interpolation and modulation with primary color.
out_FragColor.rgb = mix(texColor[0].rgb, texColor[1].rgb, vColor.a) * vColor.rgb;
out_FragColor.rgb *= mix(texColor[0].rgb, texColor[1].rgb, uTexModeColor.a);
break;
case 3:
// Texture interpolation.
out_FragColor.rgb = mix(texColor[0].rgb, texColor[1].rgb, vColor.a);
out_FragColor.rgb = mix(texColor[0].rgb, texColor[1].rgb, uTexModeColor.a);
break;
case 4:
// Sector light, dynamic light, and texture.
out_FragColor.rgb += texColor[0].a * uTexModeColor.rgb;
out_FragColor *= texColor[1];
break;
case 6:
// Simple dynlight addition (add to primary color).
out_FragColor.rgb += texColor[0].a * uTexModeColor.rgb;
break;
case 8:
// Texture and Detail.
out_FragColor *= texColor[0];
out_FragColor.rgb *= texColor[1].rgb * 2.0;
break;
case 10:
// Sector light * texture + dynamic light.
out_FragColor *= texColor[0];
out_FragColor.rgb += texColor[1].rgb * uTexModeColor.rgb;
break;
case 11:
// Normal modulation, alpha of 2nd stage.
// Tex0: texture
// Tex1: shiny texture
out_FragColor.rgb *= texColor[1].rgb;
out_FragColor.a *= texColor[0].a;
break;
}

// Alpha test.
if (out_FragColor.a < uAlphaLimit) {
discard;
}

applyFog();
}
133 changes: 112 additions & 21 deletions doomsday/apps/client/src/gl/dgl_common.cpp
Expand Up @@ -27,6 +27,7 @@
#include <de/concurrency.h>
#include <de/GLInfo>
#include <de/GLState>
#include <de/GLUniform>
#include <de/Matrix>
#include <doomsday/res/Textures>

Expand All @@ -48,6 +49,11 @@ struct DGLState
bool enableFog = false;
int textureModulation = 0;
Vector4f textureModulationColor;
DGLenum fogMode = DGL_LINEAR;
float fogStart = 0;
float fogEnd = 0;
float fogDensity = 0;
Vector4f fogColor;

DGLState()
{
Expand Down Expand Up @@ -123,6 +129,30 @@ Vector4f DGL_ModulationColor()
return dgl.textureModulationColor;
}

void DGL_FogParams(GLUniform &fogRange, GLUniform &fogColor)
{
if (dgl.enableFog)
{
fogColor = Vector4f(dgl.fogColor[0],
dgl.fogColor[1],
dgl.fogColor[2],
1.f);

// TODO: Implement EXP and EXP2 fog modes. This is LINEAR.

Rangef const depthPlanes = GL_DepthClipRange();
float const fogDepth = dgl.fogEnd - dgl.fogStart;
fogRange = Vector4f(dgl.fogStart,
fogDepth,
depthPlanes.start,
depthPlanes.end);
}
else
{
fogColor = Vector4f();
}
}

#if 0
/**
* Requires a texture environment mode that can add and multiply.
Expand Down Expand Up @@ -222,6 +252,11 @@ void DGL_ModulateTexture(int mode)
case 1:
case 2:
case 3:
case 4:
case 6:
case 8:
case 10:
case 11:
break;
}

Expand Down Expand Up @@ -509,7 +544,11 @@ dd_bool DGL_GetIntegerv(int name, int *v)
break;

case DGL_FOG:
*v = GL_state.currentUseFog;
*v = (dgl.enableFog? 1 : 0);
break;

case DGL_FOG_MODE:
*v = int(dgl.fogMode);
break;

case DGL_CURRENT_COLOR_R:
Expand Down Expand Up @@ -611,13 +650,36 @@ dd_bool DGL_GetFloatv(int name, float *v)
break;

case DGL_CURRENT_COLOR_RGBA:
DGL_CurrentColor(color);
DGL_CurrentColor(v);
break;

case DGL_FOG_START:
v[0] = dgl.fogStart;
break;

case DGL_FOG_END:
v[0] = dgl.fogEnd;
break;

case DGL_FOG_DENSITY:
v[0] = dgl.fogDensity;
break;

case DGL_FOG_COLOR:
for (int i = 0; i < 4; ++i)
{
v[i] = color[i];
v[i] = dgl.fogColor[i];
}
break;

case DGL_LINE_WIDTH:
v[0] = GL_state.currentLineWidth;
break;

case DGL_POINT_SIZE:
v[0] = GL_state.currentPointSize;
break;

default:
return false;
}
Expand All @@ -628,17 +690,9 @@ dd_bool DGL_GetFloatv(int name, float *v)
#undef DGL_GetFloat
float DGL_GetFloat(int name)
{
switch(name)
{
case DGL_LINE_WIDTH:
return GL_state.currentLineWidth;

case DGL_POINT_SIZE:
return GL_state.currentPointSize;

default:
return 0;
}
float value = 0.f;
DGL_GetFloatv(name, &value);
return value;
}

#undef DGL_SetFloat
Expand Down Expand Up @@ -690,7 +744,6 @@ int DGL_Enable(int cap)
switch(cap)
{
case DGL_TEXTURE_2D:
//Deferred_glEnable(GL_TEXTURE_2D);
dgl.enableTexture[dgl.activeTexture] = true;
break;

Expand All @@ -705,9 +758,7 @@ int DGL_Enable(int cap)
break;

case DGL_FOG:
//Deferred_glEnable(GL_FOG);
dgl.enableFog = true;
GL_state.currentUseFog = true;
break;

case DGL_SCISSOR_TEST:
Expand Down Expand Up @@ -755,13 +806,10 @@ void DGL_Disable(int cap)
break;

case DGL_FOG:
//Deferred_glDisable(GL_FOG);
dgl.enableFog = false;
GL_state.currentUseFog = false;
break;

case DGL_SCISSOR_TEST:
//glDisable(GL_SCISSOR_TEST);
GLState::current().clearScissor().apply();
break;

Expand Down Expand Up @@ -967,6 +1015,46 @@ void DGL_Ortho(float left, float top, float right, float bottom, float znear, fl
dgl.multMatrix(Matrix4f::ortho(left, right, top, bottom, znear, zfar));
}

#undef DGL_Fogi
void DGL_Fogi(DGLenum property, int value)
{
switch (property)
{
case DGL_FOG_MODE:
dgl.fogMode = DGLenum(value);
break;
}
}

#undef DGL_Fogfv
void DGL_Fogfv(DGLenum property, float const *values)
{
switch (property)
{
case DGL_FOG_START:
dgl.fogStart = values[0];
break;

case DGL_FOG_END:
dgl.fogEnd = values[0];
break;

case DGL_FOG_DENSITY:
dgl.fogDensity = values[0];
break;

case DGL_FOG_COLOR:
dgl.fogColor = Vector4f(values);
break;
}
}

#undef DGL_Fogf
void DGL_Fogf(DGLenum property, float value)
{
DGL_Fogfv(property, &value);
}

#undef DGL_DeleteTextures
void DGL_DeleteTextures(int num, DGLuint const *names)
{
Expand Down Expand Up @@ -1111,12 +1199,15 @@ DENG_DECLARE_API(GL) =
DGL_NewTextureWithParams,
DGL_Bind,
DGL_DeleteTextures,
DGL_Fogi,
DGL_Fogf,
DGL_Fogfv,
GL_UseFog,
GL_SetFilter,
GL_SetFilterColor,
GL_ConfigureBorderedProjection2,
GL_ConfigureBorderedProjection,
GL_BeginBorderedProjection,
GL_EndBorderedProjection,
GL_ResetViewEffects
GL_ResetViewEffects,
};

0 comments on commit 193d26d

Please sign in to comment.