implemented matfx and cleaned up some whitespace

This commit is contained in:
aap 2016-06-29 12:53:02 +02:00
parent d3cff5c06c
commit ad1ea4ca53
20 changed files with 342 additions and 201 deletions

View File

@ -751,15 +751,15 @@ findPointer(void *p, void **list, int32 num)
uint8* uint8*
getFileContents(char *name, uint32 *len) getFileContents(char *name, uint32 *len)
{ {
FILE *cf = fopen(name, "rb"); FILE *cf = fopen(name, "rb");
assert(cf != nil); assert(cf != nil);
fseek(cf, 0, SEEK_END); fseek(cf, 0, SEEK_END);
*len = ftell(cf); *len = ftell(cf);
fseek(cf, 0, SEEK_SET); fseek(cf, 0, SEEK_SET);
uint8 *data = new uint8[*len]; uint8 *data = new uint8[*len];
fread(data, *len, 1, cf); fread(data, *len, 1, cf);
fclose(cf); fclose(cf);
return data; return data;
} }
} }

View File

@ -591,9 +591,9 @@ registerNativeRaster(void)
{ {
nativeRasterOffset = Raster::registerPlugin(sizeof(D3dRaster), nativeRasterOffset = Raster::registerPlugin(sizeof(D3dRaster),
ID_RASTERD3D9, ID_RASTERD3D9,
createNativeRaster, createNativeRaster,
destroyNativeRaster, destroyNativeRaster,
copyNativeRaster); copyNativeRaster);
} }
} }

View File

@ -614,9 +614,9 @@ registerNativeRaster(void)
{ {
nativeRasterOffset = Raster::registerPlugin(sizeof(XboxRaster), nativeRasterOffset = Raster::registerPlugin(sizeof(XboxRaster),
ID_RASTERXBOX, ID_RASTERXBOX,
createNativeRaster, createNativeRaster,
destroyNativeRaster, destroyNativeRaster,
copyNativeRaster); copyNativeRaster);
} }
Texture* Texture*

View File

@ -115,8 +115,8 @@ skinInstanceCB(Geometry *geo, InstanceDataHeader *header)
{ {
defaultInstanceCB(geo, header); defaultInstanceCB(geo, header);
Skin *skin = *PLUGINOFFSET(Skin*, geo, skinGlobals.offset); Skin *skin = *PLUGINOFFSET(Skin*, geo, skinGlobals.offset);
if(skin == nil) if(skin == nil)
return; return;
NativeSkin *natskin = new NativeSkin; NativeSkin *natskin = new NativeSkin;
skin->platformData = natskin; skin->platformData = natskin;
@ -169,8 +169,8 @@ skinUninstanceCB(Geometry *geo, InstanceDataHeader *header)
{ {
defaultUninstanceCB(geo, header); defaultUninstanceCB(geo, header);
Skin *skin = *PLUGINOFFSET(Skin*, geo, skinGlobals.offset); Skin *skin = *PLUGINOFFSET(Skin*, geo, skinGlobals.offset);
if(skin == nil) if(skin == nil)
return; return;
NativeSkin *natskin = (NativeSkin*)skin->platformData; NativeSkin *natskin = (NativeSkin*)skin->platformData;

View File

@ -15,6 +15,9 @@
#endif #endif
#include "rwgl3.h" #include "rwgl3.h"
#include "rwgl3shader.h" #include "rwgl3shader.h"
#include "rwgl3plg.h"
#include "rwgl3impl.h"
namespace rw { namespace rw {
namespace gl3 { namespace gl3 {
@ -23,10 +26,13 @@ namespace gl3 {
// MatFX // MatFX
Shader *envShader;
static void* 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");
return o; return o;
} }
@ -41,6 +47,171 @@ initMatFX(void)
{ {
Driver::registerPlugin(PLATFORM_GL3, 0, ID_MATFX, Driver::registerPlugin(PLATFORM_GL3, 0, ID_MATFX,
matfxOpen, matfxClose); matfxOpen, matfxClose);
registerUniform("u_texMatrix");
registerUniform("u_coefficient");
}
#define U(s) currentShader->uniformLocations[findUniform(s)]
void
matfxDefaultRender(InstanceDataHeader *header, InstanceData *inst)
{
Material *m;
RGBAf col;
GLfloat surfProps[4];
m = inst->material;
simpleShader->use();
convColor(&col, &m->color);
glUniform4fv(U("u_matColor"), 1, (GLfloat*)&col);
surfProps[0] = m->surfaceProps.ambient;
surfProps[1] = m->surfaceProps.specular;
surfProps[2] = m->surfaceProps.diffuse;
surfProps[3] = 0.0f;
glUniform4fv(U("u_surfaceProps"), 1, surfProps);
setTexture(0, m->texture);
setVertexAlpha(inst->vertexAlpha || m->color.alpha != 0xFF);
flushCache();
glDrawElements(header->primType, inst->numIndex,
GL_UNSIGNED_SHORT, (void*)(uintptr)inst->offset);
}
void
calcEnvTexMatrix(Frame *f, float32 *mat)
{
Matrix cam;
if(f){
// PS2 style - could be simplified by the shader
cam = *((Camera*)engine->currentCamera)->getFrame()->getLTM();
cam.pos = { 0.0, 0.0, 0.0 };
cam.right.x = -cam.right.x;
cam.right.y = -cam.right.y;
cam.right.z = -cam.right.z;
Matrix inv;
Matrix::invert(&inv, f->getLTM());
inv.pos = { -1.0, -1.0, -1.0 };
inv.right.x *= -0.5f;
inv.right.y *= -0.5f;
inv.right.z *= -0.5f;
inv.up.x *= -0.5f;
inv.up.y *= -0.5f;
inv.up.z *= -0.5f;
inv.at.x *= -0.5f;
inv.at.y *= -0.5f;
inv.at.z *= -0.5f;
inv.pos.x *= -0.5f;
inv.pos.y *= -0.5f;
inv.pos.z *= -0.5f;
Matrix m;
Matrix::mult(&m, &inv, &cam);
memcpy(mat, &m, 64);
mat[3] = mat[7] = mat[11] = 0.0f;
mat[15] = 1.0f;
}else{
// D3D - TODO: find out what PS2 does
mat[0] = 0.5f;
mat[1] = 0.0f;
mat[2] = 0.0f;
mat[3] = 0.0f;
mat[4] = 0.0f;
mat[5] = -0.5f;
mat[6] = 0.0f;
mat[7] = 0.0f;
mat[8] = 0.0f;
mat[9] = 0.0f;
mat[10] = 1.0f;
mat[11] = 0.0f;
mat[12] = 0.5f;
mat[13] = 0.5f;
mat[14] = 0.0f;
mat[15] = 0.0f;
}
}
void
matfxEnvRender(InstanceDataHeader *header, InstanceData *inst)
{
Material *m;
RGBAf col;
GLfloat surfProps[4];
float32 texMat[16];
m = inst->material;
matfxDefaultRender(header, inst);
MatFX *fx = MatFX::get(m);
int32 idx = fx->getEffectIndex(MatFX::ENVMAP);
MatFX::Env *env = &fx->fx[idx].env;
envShader->use();
convColor(&col, &m->color);
glUniform4fv(U("u_matColor"), 1, (GLfloat*)&col);
surfProps[0] = m->surfaceProps.ambient;
surfProps[1] = m->surfaceProps.specular;
surfProps[2] = m->surfaceProps.diffuse;
surfProps[3] = 0.0f;
glUniform4fv(U("u_surfaceProps"), 1, surfProps);
glUniform1fv(U("u_coefficient"), 1, &env->coefficient);
calcEnvTexMatrix(env->frame, texMat);
glUniformMatrix4fv(U("u_texMatrix"), 1, GL_FALSE, texMat);
setTexture(0, env->tex);
setVertexAlpha(1);
glBlendFunc(GL_ONE, GL_ONE);
flushCache();
glDrawElements(header->primType, inst->numIndex,
GL_UNSIGNED_SHORT, (void*)(uintptr)inst->offset);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void
matfxRenderCB(Atomic *atomic, InstanceDataHeader *header)
{
setWorldMatrix(atomic->getFrame()->getLTM());
lightingCB();
glBindBuffer(GL_ARRAY_BUFFER, header->vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, header->ibo);
setAttribPointers(header);
InstanceData *inst = header->inst;
int32 n = header->numMeshes;
setAlphaTestFunc(1);
setAlphaRef(0.2);
int32 fx;
while(n--){
fx = MatFX::getEffects(inst->material);
switch(fx){
case MatFX::ENVMAP:
matfxEnvRender(header, inst);
break;
default:
matfxDefaultRender(header, inst);
}
inst++;
}
} }
ObjPipeline* ObjPipeline*

View File

@ -114,11 +114,11 @@ copyNativeRaster(void *dst, void *, int32 offset, int32)
void registerNativeRaster(void) void registerNativeRaster(void)
{ {
nativeRasterOffset = Raster::registerPlugin(sizeof(Gl3Raster), nativeRasterOffset = Raster::registerPlugin(sizeof(Gl3Raster),
ID_RASTERGL3, ID_RASTERGL3,
createNativeRaster, createNativeRaster,
destroyNativeRaster, destroyNativeRaster,
copyNativeRaster); copyNativeRaster);
} }
} }

View File

@ -59,6 +59,8 @@ UniformState uniformState;
UniformScene uniformScene; UniformScene uniformScene;
UniformObject uniformObject; UniformObject uniformObject;
Shader *simpleShader;
void void
beginUpdate(Camera *cam) beginUpdate(Camera *cam)
{ {
@ -87,9 +89,9 @@ beginUpdate(Camera *cam)
setViewMatrix(view); setViewMatrix(view);
// Projection Matrix // Projection Matrix
float32 invwx = 1.0f/cam->viewWindow.x; float32 invwx = 1.0f/cam->viewWindow.x;
float32 invwy = 1.0f/cam->viewWindow.y; float32 invwy = 1.0f/cam->viewWindow.y;
float32 invz = 1.0f/(cam->farPlane-cam->nearPlane); float32 invz = 1.0f/(cam->farPlane-cam->nearPlane);
proj[0] = invwx; proj[0] = invwx;
proj[1] = 0.0f; proj[1] = 0.0f;
@ -120,12 +122,14 @@ beginUpdate(Camera *cam)
void void
initializeRender(void) initializeRender(void)
{ {
driver[PLATFORM_GL3]->beginUpdate = beginUpdate; driver[PLATFORM_GL3]->beginUpdate = beginUpdate;
glClearColor(0.25, 0.25, 0.25, 1.0); simpleShader = Shader::fromFiles("simple.vert", "simple.frag");
glClearColor(0.25, 0.25, 0.25, 1.0);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); glDepthFunc(GL_LEQUAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -228,14 +232,14 @@ setLight(int32 n, Light *light)
void void
setProjectionMatrix(float32 *mat) setProjectionMatrix(float32 *mat)
{ {
memcpy(&uniformScene.proj, mat, 64); memcpy(&uniformScene.proj, mat, 64);
sceneDirty = 1; sceneDirty = 1;
} }
void void
setViewMatrix(float32 *mat) setViewMatrix(float32 *mat)
{ {
memcpy(&uniformScene.view, mat, 64); memcpy(&uniformScene.view, mat, 64);
sceneDirty = 1; sceneDirty = 1;
} }
@ -342,56 +346,6 @@ lightingCB(void)
setAmbientLight(&ambLight); setAmbientLight(&ambLight);
} }
void
matfxRenderCB(Atomic *atomic, InstanceDataHeader *header)
{
setWorldMatrix(atomic->getFrame()->getLTM());
lightingCB();
glBindBuffer(GL_ARRAY_BUFFER, header->vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, header->ibo);
setAttribPointers(header);
Material *m;
RGBAf col;
GLfloat surfProps[4];
int id;
InstanceData *inst = header->inst;
int32 n = header->numMeshes;
setAlphaTestFunc(1);
setAlphaRef(0.2);
while(n--){
m = inst->material;
convColor(&col, &m->color);
glUniform4fv(U("u_matColor"), 1, (GLfloat*)&col);
surfProps[0] = m->surfaceProps.ambient;
surfProps[1] = m->surfaceProps.specular;
surfProps[2] = m->surfaceProps.diffuse;
surfProps[3] = 0.0f;
glUniform4fv(U("u_surfaceProps"), 1, surfProps);
setTexture(0, m->texture);
/*
if(MatFX::getEffects(m) == MatFX::ENVMAP){
MatFX *fx = MatFX::get(m);
int32 idx = fx->getEffectIndex(MatFX::ENVMAP);
setTexture(0, fx->fx[idx].env.tex);
}
*/
setVertexAlpha(inst->vertexAlpha || m->color.alpha != 0xFF);
flushCache();
glDrawElements(header->primType, inst->numIndex,
GL_UNSIGNED_SHORT, (void*)(uintptr)inst->offset);
inst++;
}
}
void void
defaultRenderCB(Atomic *atomic, InstanceDataHeader *header) defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
{ {
@ -412,6 +366,8 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
setAlphaTestFunc(1); setAlphaTestFunc(1);
setAlphaRef(0.2); setAlphaRef(0.2);
simpleShader->use();
while(n--){ while(n--){
m = inst->material; m = inst->material;

View File

@ -17,17 +17,17 @@ struct AttribDesc
enum AttribIndices enum AttribIndices
{ {
ATTRIB_POS = 0, ATTRIB_POS = 0,
ATTRIB_NORMAL, ATTRIB_NORMAL,
ATTRIB_COLOR, ATTRIB_COLOR,
ATTRIB_TEXCOORDS0, ATTRIB_TEXCOORDS0,
ATTRIB_TEXCOORDS1, ATTRIB_TEXCOORDS1,
ATTRIB_TEXCOORDS2, ATTRIB_TEXCOORDS2,
ATTRIB_TEXCOORDS3, ATTRIB_TEXCOORDS3,
ATTRIB_TEXCOORDS4, ATTRIB_TEXCOORDS4,
ATTRIB_TEXCOORDS5, ATTRIB_TEXCOORDS5,
ATTRIB_TEXCOORDS6, ATTRIB_TEXCOORDS6,
ATTRIB_TEXCOORDS7, ATTRIB_TEXCOORDS7,
}; };
struct InstanceData struct InstanceData
@ -78,6 +78,8 @@ void setLight(int32 n, Light*);
void setTexture(int32 n, Texture *tex); void setTexture(int32 n, Texture *tex);
void setVertexAlpha(bool32 enable); void setVertexAlpha(bool32 enable);
void flushCache(void);
class ObjPipeline : public rw::ObjPipeline class ObjPipeline : public rw::ObjPipeline
{ {
public: public:
@ -91,12 +93,9 @@ public:
void defaultInstanceCB(Geometry *geo, InstanceDataHeader *header); void defaultInstanceCB(Geometry *geo, InstanceDataHeader *header);
void defaultUninstanceCB(Geometry *geo, InstanceDataHeader *header); void defaultUninstanceCB(Geometry *geo, InstanceDataHeader *header);
void defaultRenderCB(Atomic *atomic, InstanceDataHeader *header); void defaultRenderCB(Atomic *atomic, InstanceDataHeader *header);
void lightingCB(void);
void matfxRenderCB(Atomic *atomic, InstanceDataHeader *header);
ObjPipeline *makeDefaultPipeline(void); ObjPipeline *makeDefaultPipeline(void);
ObjPipeline *makeSkinPipeline(void);
ObjPipeline *makeMatFXPipeline(void);
// Native Texture and Raster // Native Texture and Raster

View File

@ -1,6 +1,12 @@
namespace rw { namespace rw {
namespace gl3 { namespace gl3 {
#ifdef RW_OPENGL
extern Shader *simpleShader;
#endif
void rasterCreate(Raster *raster); void rasterCreate(Raster *raster);
uint8 *rasterLock(Raster*, int32 level); uint8 *rasterLock(Raster*, int32 level);
void rasterUnlock(Raster*, int32); void rasterUnlock(Raster*, int32);

View File

@ -604,9 +604,9 @@ skinInstanceCB(Geometry *g, int32 i, uint32 offset)
header->dataSize = offset*g->numVertices; header->dataSize = offset*g->numVertices;
header->data = new uint8[header->dataSize]; header->data = new uint8[header->dataSize];
Skin *skin = *PLUGINOFFSET(Skin*, g, skinGlobals.offset); Skin *skin = *PLUGINOFFSET(Skin*, g, skinGlobals.offset);
if(skin == nil) if(skin == nil)
return 8; return 8;
a = &header->attribs[i]; a = &header->attribs[i];
uint8 *wgt = header->data + a[0].offset; uint8 *wgt = header->data + a[0].offset;
@ -632,8 +632,8 @@ skinUninstanceCB(Geometry *geo)
{ {
InstanceDataHeader *header = (InstanceDataHeader*)geo->instData; InstanceDataHeader *header = (InstanceDataHeader*)geo->instData;
Skin *skin = *PLUGINOFFSET(Skin*, geo, skinGlobals.offset); Skin *skin = *PLUGINOFFSET(Skin*, geo, skinGlobals.offset);
if(skin == nil) if(skin == nil)
return; return;
uint8 *data = skin->data; uint8 *data = skin->data;
@ -776,9 +776,9 @@ registerNativeRaster(void)
{ {
nativeRasterOffset = Raster::registerPlugin(sizeof(GlRaster), nativeRasterOffset = Raster::registerPlugin(sizeof(GlRaster),
ID_RASTERWDGL, ID_RASTERWDGL,
createNativeRaster, createNativeRaster,
destroyNativeRaster, destroyNativeRaster,
copyNativeRaster); copyNativeRaster);
} }
void void

View File

@ -168,6 +168,14 @@ MatFX::setEnvTexture(Texture *t)
this->fx[i].env.tex = t; this->fx[i].env.tex = t;
} }
void
MatFX::setEnvFrame(Frame *f)
{
int32 i = this->getEffectIndex(ENVMAP);
if(i >= 0)
this->fx[i].env.frame = f;
}
void void
MatFX::setEnvCoefficient(float32 coef) MatFX::setEnvCoefficient(float32 coef)
{ {

View File

@ -475,9 +475,9 @@ registerNativeRaster(void)
{ {
nativeRasterOffset = Raster::registerPlugin(sizeof(Ps2Raster), nativeRasterOffset = Raster::registerPlugin(sizeof(Ps2Raster),
ID_RASTERPS2, ID_RASTERPS2,
createNativeRaster, createNativeRaster,
destroyNativeRaster, destroyNativeRaster,
copyNativeRaster); copyNativeRaster);
Texture::registerPlugin(0, ID_SKYMIPMAP, nil, nil, nil); Texture::registerPlugin(0, ID_SKYMIPMAP, nil, nil, nil);
Texture::registerPluginStream(ID_SKYMIPMAP, readMipmap, writeMipmap, getSizeMipmap); Texture::registerPluginStream(ID_SKYMIPMAP, readMipmap, writeMipmap, getSizeMipmap);

View File

@ -29,7 +29,7 @@ struct Driver
static PluginList s_plglist[NUM_PLATFORMS]; static PluginList s_plglist[NUM_PLATFORMS];
static void open(void); static void open(void);
static int32 registerPlugin(int32 platform, int32 size, uint32 id, static int32 registerPlugin(int32 platform, int32 size, uint32 id,
Constructor ctor, Destructor dtor){ Constructor ctor, Destructor dtor){
return s_plglist[platform].registerPlugin(size, id, return s_plglist[platform].registerPlugin(size, id,
ctor, dtor, nil); ctor, dtor, nil);

View File

@ -115,6 +115,7 @@ struct MatFX
void setBumpTexture(Texture *t); void setBumpTexture(Texture *t);
void setBumpCoefficient(float32 coef); void setBumpCoefficient(float32 coef);
void setEnvTexture(Texture *t); void setEnvTexture(Texture *t);
void setEnvFrame(Frame *f);
void setEnvCoefficient(float32 coef); void setEnvCoefficient(float32 coef);
void setDualTexture(Texture *t); void setDualTexture(Texture *t);
void setDualSrcBlend(int32 blend); void setDualSrcBlend(int32 blend);