implemented fog for gl3

This commit is contained in:
aap 2016-07-05 18:22:22 +02:00
parent abe0bba5af
commit dcbbdfe6b2
7 changed files with 78 additions and 21 deletions

View File

@ -124,6 +124,12 @@ Camera::destroy(void)
free(this); free(this);
} }
void
Camera::clear(RGBA *col, uint32 mode)
{
DRIVER->clearCamera(this, col, mode);
}
struct CameraChunkData struct CameraChunkData
{ {
V2d viewWindow; V2d viewWindow;

View File

@ -230,7 +230,7 @@ beginUpdate(Camera *cam)
proj[12] = 0.0f; proj[12] = 0.0f;
proj[13] = 0.0f; proj[13] = 0.0f;
proj[14] = -this->nearPlane*this->projMat[10]; proj[14] = -this->nearPlane*proj[10];
proj[15] = 0.0f; proj[15] = 0.0f;
}else{ }else{
proj[8] = 0.0f; proj[8] = 0.0f;
@ -240,7 +240,7 @@ beginUpdate(Camera *cam)
proj[12] = this->viewOffset.x*invwx; proj[12] = this->viewOffset.x*invwx;
proj[13] = this->viewOffset.y*invwy; proj[13] = this->viewOffset.y*invwy;
proj[14] = -this->nearPlane*this->projMat[10]; proj[14] = -this->nearPlane*proj[10];
proj[15] = 1.0f; proj[15] = 1.0f;
} }
device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)proj); device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)proj);

View File

@ -69,8 +69,8 @@ Driver::open(void)
namespace null { namespace null {
void beginUpdate(Camera*) { } void beginUpdate(Camera*) { }
void endUpdate(Camera*) { } void endUpdate(Camera*) { }
void clearCamera(Camera*,RGBA*,uint32) { }
void setRenderState(int32, uint32) { } void setRenderState(int32, uint32) { }
uint32 getRenderState(int32) { return 0; } uint32 getRenderState(int32) { return 0; }

View File

@ -19,8 +19,13 @@ namespace gl3 {
struct UniformState struct UniformState
{ {
int alphaFunc; int32 alphaFunc;
float32 alphaRef; float32 alphaRef;
int32 fogEnable;
float32 fogStart;
float32 fogEnd;
int32 pad[3];
RGBAf fogColor;
}; };
struct UniformScene struct UniformScene
@ -126,16 +131,29 @@ setRenderState(int32 state, uint32 value)
glDepthMask(zwrite); glDepthMask(zwrite);
} }
break; break;
case FOGENABLE:
if(uniformState.fogEnable != value){
uniformState.fogEnable = value;
stateDirty = 1;
}
break;
case FOGCOLOR:
// no cache check here...too lazy
convColor(&uniformState.fogColor, (RGBA*)&value);
stateDirty = 1;
break;
case ALPHATESTFUNC: case ALPHATESTFUNC:
if(uniformState.alphaFunc != value){
uniformState.alphaFunc = value; uniformState.alphaFunc = value;
stateDirty = 1; stateDirty = 1;
}
break; break;
case ALPHATESTREF: case ALPHATESTREF:
if(uniformState.alphaRef != value/255.0f){
uniformState.alphaRef = value/255.0f; uniformState.alphaRef = value/255.0f;
stateDirty = 1; stateDirty = 1;
break; }
case ZTESTFUNC:
break; break;
} }
} }
@ -143,6 +161,7 @@ setRenderState(int32 state, uint32 value)
uint32 uint32
getRenderState(int32 state) getRenderState(int32 state)
{ {
RGBA rgba;
switch(state){ switch(state){
case VERTEXALPHA: case VERTEXALPHA:
return vertexAlpha; return vertexAlpha;
@ -154,14 +173,18 @@ getRenderState(int32 state)
return ztest; return ztest;
case ZWRITEENABLE: case ZWRITEENABLE:
return zwrite; return zwrite;
case FOGENABLE:
return uniformState.fogEnable;
case FOGCOLOR:
convColor(&rgba, &uniformState.fogColor);
return *(uint32*)&rgba;
case ALPHATESTFUNC: case ALPHATESTFUNC:
return uniformState.alphaFunc; return uniformState.alphaFunc;
case ALPHATESTREF: case ALPHATESTREF:
return uniformState.alphaRef*255.0f; return uniformState.alphaRef*255.0f;
case ZTESTFUNC:
break;
} }
return 0;
} }
void void
@ -169,6 +192,9 @@ resetRenderState(void)
{ {
uniformState.alphaFunc = ALPHAGREATERTHAN; uniformState.alphaFunc = ALPHAGREATERTHAN;
uniformState.alphaRef = 10.0f/255.0f; uniformState.alphaRef = 10.0f/255.0f;
uniformState.fogEnable = 0;
uniformState.fogStart = 0.0f;
uniformState.fogColor = { 1.0f, 1.0f, 1.0f, 1.0f };
stateDirty = 1; stateDirty = 1;
vertexAlpha = 0; vertexAlpha = 0;
@ -296,6 +322,22 @@ flushCache(void)
} }
} }
void
clearCamera(Camera *cam, RGBA *col, uint32 mode)
{
RGBAf colf;
GLbitfield mask;
convColor(&colf, col);
glClearColor(colf.red, colf.green, colf.blue, colf.alpha);
mask = 0;
if(mode & Camera::CLEARIMAGE)
mask |= GL_COLOR_BUFFER_BIT;
if(mode & Camera::CLEARZ)
mask |= GL_DEPTH_BUFFER_BIT;
glClear(mask);
}
void void
beginUpdate(Camera *cam) beginUpdate(Camera *cam)
{ {
@ -353,12 +395,22 @@ beginUpdate(Camera *cam)
// TODO // TODO
} }
setProjectionMatrix(proj); setProjectionMatrix(proj);
if(uniformState.fogStart != cam->fogPlane){
uniformState.fogStart = cam->fogPlane;
stateDirty = 1;
}
if(uniformState.fogEnd != cam->farPlane){
uniformState.fogEnd = cam->farPlane;
stateDirty = 1;
}
} }
void void
initializeRender(void) initializeRender(void)
{ {
driver[PLATFORM_GL3]->beginUpdate = beginUpdate; driver[PLATFORM_GL3]->beginUpdate = beginUpdate;
driver[PLATFORM_GL3]->clearCamera = clearCamera;
driver[PLATFORM_GL3]->setRenderState = setRenderState; driver[PLATFORM_GL3]->setRenderState = setRenderState;
driver[PLATFORM_GL3]->getRenderState = getRenderState; driver[PLATFORM_GL3]->getRenderState = getRenderState;

View File

@ -32,7 +32,7 @@ static void*
matfxOpen(void *o, int32, int32) matfxOpen(void *o, int32, int32)
{ {
matFXGlobals.pipelines[PLATFORM_GL3] = makeMatFXPipeline(); matFXGlobals.pipelines[PLATFORM_GL3] = makeMatFXPipeline();
envShader = Shader::fromFiles("matfx_env.vert", "simple.frag"); envShader = Shader::fromFiles("matfx_env.vert", "matfx_env.frag");
return o; return o;
} }

View File

@ -7,8 +7,10 @@ enum RenderState
DESTBLEND, DESTBLEND,
ZTESTENABLE, ZTESTENABLE,
ZWRITEENABLE, ZWRITEENABLE,
FOGENABLE,
FOGCOLOR,
// TODO: // TODO:
// fog enable, color, type, density // fog type, density ?
// ? cullmode // ? cullmode
// ? shademode // ? shademode
// ???? stencil // ???? stencil
@ -16,7 +18,6 @@ enum RenderState
// platform specific or opaque? // platform specific or opaque?
ALPHATESTFUNC, ALPHATESTFUNC,
ALPHATESTREF, ALPHATESTREF,
ZTESTFUNC,
}; };
enum AlphaTestFunc enum AlphaTestFunc
@ -42,10 +43,6 @@ enum BlendFunction
// TODO: add more perhaps // TODO: add more perhaps
}; };
enum ZTestFunc
{
};
// This is for platform independent things // This is for platform independent things
// TODO: move more stuff into this // TODO: move more stuff into this
struct Engine struct Engine
@ -66,6 +63,7 @@ struct Driver
void (*beginUpdate)(Camera*); void (*beginUpdate)(Camera*);
void (*endUpdate)(Camera*); void (*endUpdate)(Camera*);
void (*clearCamera)(Camera*, RGBA *col, uint32 mode);
void (*setRenderState)(int32 state, uint32 value); void (*setRenderState)(int32 state, uint32 value);
uint32 (*getRenderState)(int32 state); uint32 (*getRenderState)(int32 state);
@ -97,6 +95,7 @@ inline uint32 getRenderState(int32 state){
namespace null { namespace null {
void beginUpdate(Camera*); void beginUpdate(Camera*);
void endUpdate(Camera*); void endUpdate(Camera*);
void clearCamera(Camera*, RGBA *col, uint32 mode);
void setRenderState(int32 state, uint32 value); void setRenderState(int32 state, uint32 value);
uint32 getRenderState(int32 state); uint32 getRenderState(int32 state);

View File

@ -524,6 +524,7 @@ struct Camera : PluginBase<Camera>
{ {
enum { ID = 4 }; enum { ID = 4 };
enum { PERSPECTIVE = 1, PARALLEL }; enum { PERSPECTIVE = 1, PARALLEL };
enum { CLEARIMAGE = 0x1, CLEARZ = 0x2};
ObjectWithFrame object; ObjectWithFrame object;
void (*beginUpdateCB)(Camera*); void (*beginUpdateCB)(Camera*);
@ -533,8 +534,6 @@ struct Camera : PluginBase<Camera>
float32 nearPlane, farPlane; float32 nearPlane, farPlane;
float32 fogPlane; float32 fogPlane;
int32 projection; int32 projection;
// TODO: remove this?
float32 projMat[16];
// clump link handled by plugin in RW // clump link handled by plugin in RW
Clump *clump; Clump *clump;
@ -556,6 +555,7 @@ struct Camera : PluginBase<Camera>
return LLLinkGetData(lnk, Camera, inClump); } return LLLinkGetData(lnk, Camera, inClump); }
void beginUpdate(void) { this->beginUpdateCB(this); } void beginUpdate(void) { this->beginUpdateCB(this); }
void endUpdate(void) { this->endUpdateCB(this); } void endUpdate(void) { this->endUpdateCB(this); }
void clear(RGBA *col, uint32 mode);
static Camera *streamRead(Stream *stream); static Camera *streamRead(Stream *stream);
bool streamWrite(Stream *stream); bool streamWrite(Stream *stream);
uint32 streamGetSize(void); uint32 streamGetSize(void);