#include "glheader.h"
#include "colormac.h"
#include "context.h"
#include "macros.h"
#include "s_context.h"
#include "s_fog.h"
GLfloat
_swrast_z_to_fogfactor(GLcontext *ctx, GLfloat z)
{
GLfloat d, f;
switch (ctx->Fog.Mode) {
case GL_LINEAR:
if (ctx->Fog.Start == ctx->Fog.End)
d = 1.0F;
else
d = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
f = (ctx->Fog.End - z) * d;
return CLAMP(f, 0.0F, 1.0F);
case GL_EXP:
d = ctx->Fog.Density;
f = EXPF(-d * z);
f = CLAMP(f, 0.0F, 1.0F);
return f;
case GL_EXP2:
d = ctx->Fog.Density;
f = EXPF(-(d * d * z * z));
f = CLAMP(f, 0.0F, 1.0F);
return f;
default:
_mesa_problem(ctx, "Bad fog mode in _swrast_z_to_fogfactor");
return 0.0;
}
}
#define FOG_LOOP(TYPE, COMPUTE_F) \
do { \
const GLfloat fogStep = span->fogStep; \
GLfloat fogCoord = span->fog; \
const GLfloat wStep = haveW ? span->dwdx : 0.0F; \
GLfloat w = haveW ? span->w : 1.0F; \
GLuint i; \
for (i = 0; i < span->end; i++) { \
GLfloat f, oneMinusF; \
COMPUTE_F; \
f = CLAMP(f, 0.0F, 1.0F); \
oneMinusF = 1.0F - f; \
rgba[i][RCOMP] = (TYPE) (f * rgba[i][RCOMP] + oneMinusF * rFog); \
rgba[i][GCOMP] = (TYPE) (f * rgba[i][GCOMP] + oneMinusF * gFog); \
rgba[i][BCOMP] = (TYPE) (f * rgba[i][BCOMP] + oneMinusF * bFog); \
fogCoord += fogStep; \
w += wStep; \
} \
} while (0)
void
_swrast_fog_rgba_span( const GLcontext *ctx, SWspan *span )
{
const SWcontext *swrast = SWRAST_CONTEXT(ctx);
GLfloat rFog, gFog, bFog;
const GLuint haveW = (span->interpMask & SPAN_W);
ASSERT(swrast->_FogEnabled);
ASSERT((span->interpMask | span->arrayMask) & SPAN_FOG);
ASSERT(span->arrayMask & SPAN_RGBA);
if (span->array->ChanType == GL_UNSIGNED_BYTE) {
rFog = ctx->Fog.Color[RCOMP] * 255.0;
gFog = ctx->Fog.Color[GCOMP] * 255.0;
bFog = ctx->Fog.Color[BCOMP] * 255.0;
}
else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
rFog = ctx->Fog.Color[RCOMP] * 65535.0;
gFog = ctx->Fog.Color[GCOMP] * 65535.0;
bFog = ctx->Fog.Color[BCOMP] * 65535.0;
}
else {
rFog = ctx->Fog.Color[RCOMP];
gFog = ctx->Fog.Color[GCOMP];
bFog = ctx->Fog.Color[BCOMP];
}
if (swrast->_PreferPixelFog) {
const GLfloat fogEnd = ctx->Fog.End;
const GLfloat fogScale = (ctx->Fog.Start == ctx->Fog.End)
? 1.0F : 1.0F / (ctx->Fog.End - ctx->Fog.Start);
const GLfloat density = -ctx->Fog.Density;
const GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density;
switch (swrast->_FogMode) {
case GL_LINEAR:
#define COMPUTE_F f = (fogEnd - FABSF(fogCoord) / w) * fogScale;
if (span->array->ChanType == GL_UNSIGNED_BYTE) {
GLubyte (*rgba)[4] = span->array->color.sz1.rgba;
FOG_LOOP(GLubyte, COMPUTE_F);
}
else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
GLushort (*rgba)[4] = span->array->color.sz2.rgba;
FOG_LOOP(GLushort, COMPUTE_F);
}
else {
GLfloat (*rgba)[4] = span->array->color.sz4.rgba;
ASSERT(span->array->ChanType == GL_FLOAT);
FOG_LOOP(GLfloat, COMPUTE_F);
}
#undef COMPUTE_F
break;
case GL_EXP:
#define COMPUTE_F f = EXPF(density * FABSF(fogCoord) / w);
if (span->array->ChanType == GL_UNSIGNED_BYTE) {
GLubyte (*rgba)[4] = span->array->color.sz1.rgba;
FOG_LOOP(GLubyte, COMPUTE_F);
}
else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
GLushort (*rgba)[4] = span->array->color.sz2.rgba;
FOG_LOOP(GLushort, COMPUTE_F);
}
else {
GLfloat (*rgba)[4] = span->array->color.sz4.rgba;
ASSERT(span->array->ChanType == GL_FLOAT);
FOG_LOOP(GLfloat, COMPUTE_F);
}
#undef COMPUTE_F
break;
case GL_EXP2:
#define COMPUTE_F const GLfloat coord = fogCoord / w; \
GLfloat tmp = negDensitySquared * coord * coord; \
if (tmp < FLT_MIN_10_EXP) \
tmp = FLT_MIN_10_EXP; \
f = EXPF(tmp);
if (span->array->ChanType == GL_UNSIGNED_BYTE) {
GLubyte (*rgba)[4] = span->array->color.sz1.rgba;
FOG_LOOP(GLubyte, COMPUTE_F);
}
else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
GLushort (*rgba)[4] = span->array->color.sz2.rgba;
FOG_LOOP(GLushort, COMPUTE_F);
}
else {
GLfloat (*rgba)[4] = span->array->color.sz4.rgba;
ASSERT(span->array->ChanType == GL_FLOAT);
FOG_LOOP(GLfloat, COMPUTE_F);
}
#undef COMPUTE_F
break;
default:
_mesa_problem(ctx, "Bad fog mode in _swrast_fog_rgba_span");
return;
}
}
else if (span->arrayMask & SPAN_FOG) {
GLuint i;
if (span->array->ChanType == GL_UNSIGNED_BYTE) {
GLubyte (*rgba)[4] = span->array->color.sz1.rgba;
for (i = 0; i < span->end; i++) {
const GLfloat f = span->array->fog[i];
const GLfloat oneMinusF = 1.0F - f;
rgba[i][RCOMP] = (GLubyte) (f * rgba[i][RCOMP] + oneMinusF * rFog);
rgba[i][GCOMP] = (GLubyte) (f * rgba[i][GCOMP] + oneMinusF * gFog);
rgba[i][BCOMP] = (GLubyte) (f * rgba[i][BCOMP] + oneMinusF * bFog);
}
}
else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
GLushort (*rgba)[4] = span->array->color.sz2.rgba;
for (i = 0; i < span->end; i++) {
const GLfloat f = span->array->fog[i];
const GLfloat oneMinusF = 1.0F - f;
rgba[i][RCOMP] = (GLushort) (f * rgba[i][RCOMP] + oneMinusF * rFog);
rgba[i][GCOMP] = (GLushort) (f * rgba[i][GCOMP] + oneMinusF * gFog);
rgba[i][BCOMP] = (GLushort) (f * rgba[i][BCOMP] + oneMinusF * bFog);
}
}
else {
GLfloat (*rgba)[4] = span->array->color.sz4.rgba;
ASSERT(span->array->ChanType == GL_FLOAT);
for (i = 0; i < span->end; i++) {
const GLfloat f = span->array->fog[i];
const GLfloat oneMinusF = 1.0F - f;
rgba[i][RCOMP] = f * rgba[i][RCOMP] + oneMinusF * rFog;
rgba[i][GCOMP] = f * rgba[i][GCOMP] + oneMinusF * gFog;
rgba[i][BCOMP] = f * rgba[i][BCOMP] + oneMinusF * bFog;
}
}
}
else {
#define COMPUTE_F f = fogCoord / w;
if (span->array->ChanType == GL_UNSIGNED_BYTE) {
GLubyte (*rgba)[4] = span->array->color.sz1.rgba;
FOG_LOOP(GLubyte, COMPUTE_F);
}
else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
GLushort (*rgba)[4] = span->array->color.sz2.rgba;
FOG_LOOP(GLushort, COMPUTE_F);
}
else {
GLfloat (*rgba)[4] = span->array->color.sz4.rgba;
ASSERT(span->array->ChanType == GL_FLOAT);
FOG_LOOP(GLfloat, COMPUTE_F);
}
#undef COMPUTE_F
}
}
void
_swrast_fog_ci_span( const GLcontext *ctx, SWspan *span )
{
const SWcontext *swrast = SWRAST_CONTEXT(ctx);
const GLuint haveW = (span->interpMask & SPAN_W);
const GLuint fogIndex = (GLuint) ctx->Fog.Index;
GLuint *index = span->array->index;
ASSERT(swrast->_FogEnabled);
ASSERT(span->arrayMask & SPAN_INDEX);
ASSERT((span->interpMask | span->arrayMask) & SPAN_FOG);
if (swrast->_PreferPixelFog) {
switch (ctx->Fog.Mode) {
case GL_LINEAR:
{
const GLfloat fogEnd = ctx->Fog.End;
const GLfloat fogScale = (ctx->Fog.Start == ctx->Fog.End)
? 1.0F : 1.0F / (ctx->Fog.End - ctx->Fog.Start);
const GLfloat fogStep = span->fogStep;
GLfloat fogCoord = span->fog;
const GLfloat wStep = haveW ? span->dwdx : 0.0F;
GLfloat w = haveW ? span->w : 1.0F;
GLuint i;
for (i = 0; i < span->end; i++) {
GLfloat f = (fogEnd - fogCoord / w) * fogScale;
f = CLAMP(f, 0.0F, 1.0F);
index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex);
fogCoord += fogStep;
w += wStep;
}
}
break;
case GL_EXP:
{
const GLfloat density = -ctx->Fog.Density;
const GLfloat fogStep = span->fogStep;
GLfloat fogCoord = span->fog;
const GLfloat wStep = haveW ? span->dwdx : 0.0F;
GLfloat w = haveW ? span->w : 1.0F;
GLuint i;
for (i = 0; i < span->end; i++) {
GLfloat f = EXPF(density * fogCoord / w);
f = CLAMP(f, 0.0F, 1.0F);
index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex);
fogCoord += fogStep;
w += wStep;
}
}
break;
case GL_EXP2:
{
const GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density;
const GLfloat fogStep = span->fogStep;
GLfloat fogCoord = span->fog;
const GLfloat wStep = haveW ? span->dwdx : 0.0F;
GLfloat w = haveW ? span->w : 1.0F;
GLuint i;
for (i = 0; i < span->end; i++) {
const GLfloat coord = fogCoord / w;
GLfloat tmp = negDensitySquared * coord * coord;
GLfloat f;
#if defined(__alpha__) || defined(__alpha)
if (tmp < FLT_MIN_10_EXP)
tmp = FLT_MIN_10_EXP;
#endif
f = EXPF(tmp);
f = CLAMP(f, 0.0F, 1.0F);
index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex);
fogCoord += fogStep;
w += wStep;
}
}
break;
default:
_mesa_problem(ctx, "Bad fog mode in _swrast_fog_ci_span");
return;
}
}
else if (span->arrayMask & SPAN_FOG) {
GLuint i;
for (i = 0; i < span->end; i++) {
const GLfloat f = span->array->fog[i];
index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex);
}
}
else {
const GLfloat fogStep = span->fogStep;
GLfloat fog = span->fog;
const GLfloat wStep = haveW ? span->dwdx : 0.0F;
GLfloat w = haveW ? span->w : 1.0F;
GLuint i;
ASSERT(span->interpMask & SPAN_FOG);
for (i = 0; i < span->end; i++) {
const GLfloat f = fog / w;
index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex);
fog += fogStep;
w += wStep;
}
}
}