From dcbbdfe6b217bcb0273a993483c25ba6cff489e4 Mon Sep 17 00:00:00 2001 From: aap Date: Tue, 5 Jul 2016 18:22:22 +0200 Subject: [PATCH] implemented fog for gl3 --- src/camera.cpp | 6 ++++ src/d3d/d3ddriver.cpp | 4 +-- src/engine.cpp | 2 +- src/gl/gl3driver.cpp | 70 +++++++++++++++++++++++++++++++++++++------ src/gl/gl3plugins.cpp | 2 +- src/rwengine.h | 11 ++++--- src/rwobjects.h | 4 +-- 7 files changed, 78 insertions(+), 21 deletions(-) diff --git a/src/camera.cpp b/src/camera.cpp index f8a5e48..2f4b08a 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -124,6 +124,12 @@ Camera::destroy(void) free(this); } +void +Camera::clear(RGBA *col, uint32 mode) +{ + DRIVER->clearCamera(this, col, mode); +} + struct CameraChunkData { V2d viewWindow; diff --git a/src/d3d/d3ddriver.cpp b/src/d3d/d3ddriver.cpp index 8644b6e..1f714c8 100644 --- a/src/d3d/d3ddriver.cpp +++ b/src/d3d/d3ddriver.cpp @@ -230,7 +230,7 @@ beginUpdate(Camera *cam) proj[12] = 0.0f; proj[13] = 0.0f; - proj[14] = -this->nearPlane*this->projMat[10]; + proj[14] = -this->nearPlane*proj[10]; proj[15] = 0.0f; }else{ proj[8] = 0.0f; @@ -240,7 +240,7 @@ beginUpdate(Camera *cam) proj[12] = this->viewOffset.x*invwx; proj[13] = this->viewOffset.y*invwy; - proj[14] = -this->nearPlane*this->projMat[10]; + proj[14] = -this->nearPlane*proj[10]; proj[15] = 1.0f; } device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)proj); diff --git a/src/engine.cpp b/src/engine.cpp index edacdfd..1bcfe6b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -69,8 +69,8 @@ Driver::open(void) namespace null { void beginUpdate(Camera*) { } - void endUpdate(Camera*) { } +void clearCamera(Camera*,RGBA*,uint32) { } void setRenderState(int32, uint32) { } uint32 getRenderState(int32) { return 0; } diff --git a/src/gl/gl3driver.cpp b/src/gl/gl3driver.cpp index 62bc2b7..1744a96 100644 --- a/src/gl/gl3driver.cpp +++ b/src/gl/gl3driver.cpp @@ -19,8 +19,13 @@ namespace gl3 { struct UniformState { - int alphaFunc; + int32 alphaFunc; float32 alphaRef; + int32 fogEnable; + float32 fogStart; + float32 fogEnd; + int32 pad[3]; + RGBAf fogColor; }; struct UniformScene @@ -126,16 +131,29 @@ setRenderState(int32 state, uint32 value) glDepthMask(zwrite); } 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: - uniformState.alphaFunc = value; - stateDirty = 1; + if(uniformState.alphaFunc != value){ + uniformState.alphaFunc = value; + stateDirty = 1; + } break; case ALPHATESTREF: - uniformState.alphaRef = value/255.0f; - stateDirty = 1; - break; - case ZTESTFUNC: + if(uniformState.alphaRef != value/255.0f){ + uniformState.alphaRef = value/255.0f; + stateDirty = 1; + } break; } } @@ -143,6 +161,7 @@ setRenderState(int32 state, uint32 value) uint32 getRenderState(int32 state) { + RGBA rgba; switch(state){ case VERTEXALPHA: return vertexAlpha; @@ -154,14 +173,18 @@ getRenderState(int32 state) return ztest; case ZWRITEENABLE: return zwrite; + case FOGENABLE: + return uniformState.fogEnable; + case FOGCOLOR: + convColor(&rgba, &uniformState.fogColor); + return *(uint32*)&rgba; case ALPHATESTFUNC: return uniformState.alphaFunc; case ALPHATESTREF: return uniformState.alphaRef*255.0f; - case ZTESTFUNC: - break; } + return 0; } void @@ -169,6 +192,9 @@ resetRenderState(void) { uniformState.alphaFunc = ALPHAGREATERTHAN; 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; 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 beginUpdate(Camera *cam) { @@ -353,12 +395,22 @@ beginUpdate(Camera *cam) // TODO } 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 initializeRender(void) { driver[PLATFORM_GL3]->beginUpdate = beginUpdate; + driver[PLATFORM_GL3]->clearCamera = clearCamera; driver[PLATFORM_GL3]->setRenderState = setRenderState; driver[PLATFORM_GL3]->getRenderState = getRenderState; diff --git a/src/gl/gl3plugins.cpp b/src/gl/gl3plugins.cpp index 0326905..281fff1 100644 --- a/src/gl/gl3plugins.cpp +++ b/src/gl/gl3plugins.cpp @@ -32,7 +32,7 @@ static void* matfxOpen(void *o, int32, int32) { matFXGlobals.pipelines[PLATFORM_GL3] = makeMatFXPipeline(); - envShader = Shader::fromFiles("matfx_env.vert", "simple.frag"); + envShader = Shader::fromFiles("matfx_env.vert", "matfx_env.frag"); return o; } diff --git a/src/rwengine.h b/src/rwengine.h index cd03007..7b51613 100644 --- a/src/rwengine.h +++ b/src/rwengine.h @@ -7,8 +7,10 @@ enum RenderState DESTBLEND, ZTESTENABLE, ZWRITEENABLE, + FOGENABLE, + FOGCOLOR, // TODO: - // fog enable, color, type, density + // fog type, density ? // ? cullmode // ? shademode // ???? stencil @@ -16,7 +18,6 @@ enum RenderState // platform specific or opaque? ALPHATESTFUNC, ALPHATESTREF, - ZTESTFUNC, }; enum AlphaTestFunc @@ -42,10 +43,6 @@ enum BlendFunction // TODO: add more perhaps }; -enum ZTestFunc -{ -}; - // This is for platform independent things // TODO: move more stuff into this struct Engine @@ -66,6 +63,7 @@ struct Driver void (*beginUpdate)(Camera*); void (*endUpdate)(Camera*); + void (*clearCamera)(Camera*, RGBA *col, uint32 mode); void (*setRenderState)(int32 state, uint32 value); uint32 (*getRenderState)(int32 state); @@ -97,6 +95,7 @@ inline uint32 getRenderState(int32 state){ namespace null { void beginUpdate(Camera*); void endUpdate(Camera*); + void clearCamera(Camera*, RGBA *col, uint32 mode); void setRenderState(int32 state, uint32 value); uint32 getRenderState(int32 state); diff --git a/src/rwobjects.h b/src/rwobjects.h index 7695f24..6fc0625 100644 --- a/src/rwobjects.h +++ b/src/rwobjects.h @@ -524,6 +524,7 @@ struct Camera : PluginBase { enum { ID = 4 }; enum { PERSPECTIVE = 1, PARALLEL }; + enum { CLEARIMAGE = 0x1, CLEARZ = 0x2}; ObjectWithFrame object; void (*beginUpdateCB)(Camera*); @@ -533,8 +534,6 @@ struct Camera : PluginBase float32 nearPlane, farPlane; float32 fogPlane; int32 projection; - // TODO: remove this? - float32 projMat[16]; // clump link handled by plugin in RW Clump *clump; @@ -556,6 +555,7 @@ struct Camera : PluginBase return LLLinkGetData(lnk, Camera, inClump); } void beginUpdate(void) { this->beginUpdateCB(this); } void endUpdate(void) { this->endUpdateCB(this); } + void clear(RGBA *col, uint32 mode); static Camera *streamRead(Stream *stream); bool streamWrite(Stream *stream); uint32 streamGetSize(void);