mirror of https://github.com/aap/librw.git
implemented matfx and cleaned up some whitespace
This commit is contained in:
parent
d3cff5c06c
commit
ad1ea4ca53
18
src/base.cpp
18
src/base.cpp
|
@ -751,15 +751,15 @@ findPointer(void *p, void **list, int32 num)
|
|||
uint8*
|
||||
getFileContents(char *name, uint32 *len)
|
||||
{
|
||||
FILE *cf = fopen(name, "rb");
|
||||
assert(cf != nil);
|
||||
fseek(cf, 0, SEEK_END);
|
||||
*len = ftell(cf);
|
||||
fseek(cf, 0, SEEK_SET);
|
||||
uint8 *data = new uint8[*len];
|
||||
fread(data, *len, 1, cf);
|
||||
fclose(cf);
|
||||
return data;
|
||||
FILE *cf = fopen(name, "rb");
|
||||
assert(cf != nil);
|
||||
fseek(cf, 0, SEEK_END);
|
||||
*len = ftell(cf);
|
||||
fseek(cf, 0, SEEK_SET);
|
||||
uint8 *data = new uint8[*len];
|
||||
fread(data, *len, 1, cf);
|
||||
fclose(cf);
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -591,9 +591,9 @@ registerNativeRaster(void)
|
|||
{
|
||||
nativeRasterOffset = Raster::registerPlugin(sizeof(D3dRaster),
|
||||
ID_RASTERD3D9,
|
||||
createNativeRaster,
|
||||
destroyNativeRaster,
|
||||
copyNativeRaster);
|
||||
createNativeRaster,
|
||||
destroyNativeRaster,
|
||||
copyNativeRaster);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -614,9 +614,9 @@ registerNativeRaster(void)
|
|||
{
|
||||
nativeRasterOffset = Raster::registerPlugin(sizeof(XboxRaster),
|
||||
ID_RASTERXBOX,
|
||||
createNativeRaster,
|
||||
destroyNativeRaster,
|
||||
copyNativeRaster);
|
||||
createNativeRaster,
|
||||
destroyNativeRaster,
|
||||
copyNativeRaster);
|
||||
}
|
||||
|
||||
Texture*
|
||||
|
|
|
@ -115,8 +115,8 @@ skinInstanceCB(Geometry *geo, InstanceDataHeader *header)
|
|||
{
|
||||
defaultInstanceCB(geo, header);
|
||||
|
||||
Skin *skin = *PLUGINOFFSET(Skin*, geo, skinGlobals.offset);
|
||||
if(skin == nil)
|
||||
Skin *skin = *PLUGINOFFSET(Skin*, geo, skinGlobals.offset);
|
||||
if(skin == nil)
|
||||
return;
|
||||
NativeSkin *natskin = new NativeSkin;
|
||||
skin->platformData = natskin;
|
||||
|
@ -169,8 +169,8 @@ skinUninstanceCB(Geometry *geo, InstanceDataHeader *header)
|
|||
{
|
||||
defaultUninstanceCB(geo, header);
|
||||
|
||||
Skin *skin = *PLUGINOFFSET(Skin*, geo, skinGlobals.offset);
|
||||
if(skin == nil)
|
||||
Skin *skin = *PLUGINOFFSET(Skin*, geo, skinGlobals.offset);
|
||||
if(skin == nil)
|
||||
return;
|
||||
NativeSkin *natskin = (NativeSkin*)skin->platformData;
|
||||
|
||||
|
|
|
@ -15,6 +15,9 @@
|
|||
#endif
|
||||
#include "rwgl3.h"
|
||||
#include "rwgl3shader.h"
|
||||
#include "rwgl3plg.h"
|
||||
|
||||
#include "rwgl3impl.h"
|
||||
|
||||
namespace rw {
|
||||
namespace gl3 {
|
||||
|
@ -23,10 +26,13 @@ namespace gl3 {
|
|||
|
||||
// MatFX
|
||||
|
||||
Shader *envShader;
|
||||
|
||||
static void*
|
||||
matfxOpen(void *o, int32, int32)
|
||||
{
|
||||
matFXGlobals.pipelines[PLATFORM_GL3] = makeMatFXPipeline();
|
||||
envShader = Shader::fromFiles("matfx_env.vert", "simple.frag");
|
||||
return o;
|
||||
}
|
||||
|
||||
|
@ -41,6 +47,171 @@ initMatFX(void)
|
|||
{
|
||||
Driver::registerPlugin(PLATFORM_GL3, 0, ID_MATFX,
|
||||
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*
|
||||
|
|
|
@ -114,11 +114,11 @@ copyNativeRaster(void *dst, void *, int32 offset, int32)
|
|||
|
||||
void registerNativeRaster(void)
|
||||
{
|
||||
nativeRasterOffset = Raster::registerPlugin(sizeof(Gl3Raster),
|
||||
ID_RASTERGL3,
|
||||
createNativeRaster,
|
||||
destroyNativeRaster,
|
||||
copyNativeRaster);
|
||||
nativeRasterOffset = Raster::registerPlugin(sizeof(Gl3Raster),
|
||||
ID_RASTERGL3,
|
||||
createNativeRaster,
|
||||
destroyNativeRaster,
|
||||
copyNativeRaster);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -59,6 +59,8 @@ UniformState uniformState;
|
|||
UniformScene uniformScene;
|
||||
UniformObject uniformObject;
|
||||
|
||||
Shader *simpleShader;
|
||||
|
||||
void
|
||||
beginUpdate(Camera *cam)
|
||||
{
|
||||
|
@ -87,9 +89,9 @@ beginUpdate(Camera *cam)
|
|||
setViewMatrix(view);
|
||||
|
||||
// Projection Matrix
|
||||
float32 invwx = 1.0f/cam->viewWindow.x;
|
||||
float32 invwy = 1.0f/cam->viewWindow.y;
|
||||
float32 invz = 1.0f/(cam->farPlane-cam->nearPlane);
|
||||
float32 invwx = 1.0f/cam->viewWindow.x;
|
||||
float32 invwy = 1.0f/cam->viewWindow.y;
|
||||
float32 invz = 1.0f/(cam->farPlane-cam->nearPlane);
|
||||
|
||||
proj[0] = invwx;
|
||||
proj[1] = 0.0f;
|
||||
|
@ -120,12 +122,14 @@ beginUpdate(Camera *cam)
|
|||
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);
|
||||
glDepthFunc(GL_LESS);
|
||||
glDepthFunc(GL_LEQUAL);
|
||||
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
|
@ -228,14 +232,14 @@ setLight(int32 n, Light *light)
|
|||
void
|
||||
setProjectionMatrix(float32 *mat)
|
||||
{
|
||||
memcpy(&uniformScene.proj, mat, 64);
|
||||
memcpy(&uniformScene.proj, mat, 64);
|
||||
sceneDirty = 1;
|
||||
}
|
||||
|
||||
void
|
||||
setViewMatrix(float32 *mat)
|
||||
{
|
||||
memcpy(&uniformScene.view, mat, 64);
|
||||
memcpy(&uniformScene.view, mat, 64);
|
||||
sceneDirty = 1;
|
||||
}
|
||||
|
||||
|
@ -342,56 +346,6 @@ lightingCB(void)
|
|||
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
|
||||
defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
|
||||
{
|
||||
|
@ -412,6 +366,8 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
|
|||
setAlphaTestFunc(1);
|
||||
setAlphaRef(0.2);
|
||||
|
||||
simpleShader->use();
|
||||
|
||||
while(n--){
|
||||
m = inst->material;
|
||||
|
||||
|
|
|
@ -17,17 +17,17 @@ struct AttribDesc
|
|||
|
||||
enum AttribIndices
|
||||
{
|
||||
ATTRIB_POS = 0,
|
||||
ATTRIB_NORMAL,
|
||||
ATTRIB_COLOR,
|
||||
ATTRIB_TEXCOORDS0,
|
||||
ATTRIB_TEXCOORDS1,
|
||||
ATTRIB_TEXCOORDS2,
|
||||
ATTRIB_TEXCOORDS3,
|
||||
ATTRIB_TEXCOORDS4,
|
||||
ATTRIB_TEXCOORDS5,
|
||||
ATTRIB_TEXCOORDS6,
|
||||
ATTRIB_TEXCOORDS7,
|
||||
ATTRIB_POS = 0,
|
||||
ATTRIB_NORMAL,
|
||||
ATTRIB_COLOR,
|
||||
ATTRIB_TEXCOORDS0,
|
||||
ATTRIB_TEXCOORDS1,
|
||||
ATTRIB_TEXCOORDS2,
|
||||
ATTRIB_TEXCOORDS3,
|
||||
ATTRIB_TEXCOORDS4,
|
||||
ATTRIB_TEXCOORDS5,
|
||||
ATTRIB_TEXCOORDS6,
|
||||
ATTRIB_TEXCOORDS7,
|
||||
};
|
||||
|
||||
struct InstanceData
|
||||
|
@ -78,6 +78,8 @@ void setLight(int32 n, Light*);
|
|||
void setTexture(int32 n, Texture *tex);
|
||||
void setVertexAlpha(bool32 enable);
|
||||
|
||||
void flushCache(void);
|
||||
|
||||
class ObjPipeline : public rw::ObjPipeline
|
||||
{
|
||||
public:
|
||||
|
@ -91,12 +93,9 @@ public:
|
|||
void defaultInstanceCB(Geometry *geo, InstanceDataHeader *header);
|
||||
void defaultUninstanceCB(Geometry *geo, InstanceDataHeader *header);
|
||||
void defaultRenderCB(Atomic *atomic, InstanceDataHeader *header);
|
||||
|
||||
void matfxRenderCB(Atomic *atomic, InstanceDataHeader *header);
|
||||
void lightingCB(void);
|
||||
|
||||
ObjPipeline *makeDefaultPipeline(void);
|
||||
ObjPipeline *makeSkinPipeline(void);
|
||||
ObjPipeline *makeMatFXPipeline(void);
|
||||
|
||||
// Native Texture and Raster
|
||||
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
namespace rw {
|
||||
namespace gl3 {
|
||||
|
||||
#ifdef RW_OPENGL
|
||||
|
||||
extern Shader *simpleShader;
|
||||
|
||||
#endif
|
||||
|
||||
void rasterCreate(Raster *raster);
|
||||
uint8 *rasterLock(Raster*, int32 level);
|
||||
void rasterUnlock(Raster*, int32);
|
||||
|
|
|
@ -604,9 +604,9 @@ skinInstanceCB(Geometry *g, int32 i, uint32 offset)
|
|||
header->dataSize = offset*g->numVertices;
|
||||
header->data = new uint8[header->dataSize];
|
||||
|
||||
Skin *skin = *PLUGINOFFSET(Skin*, g, skinGlobals.offset);
|
||||
if(skin == nil)
|
||||
return 8;
|
||||
Skin *skin = *PLUGINOFFSET(Skin*, g, skinGlobals.offset);
|
||||
if(skin == nil)
|
||||
return 8;
|
||||
|
||||
a = &header->attribs[i];
|
||||
uint8 *wgt = header->data + a[0].offset;
|
||||
|
@ -632,8 +632,8 @@ skinUninstanceCB(Geometry *geo)
|
|||
{
|
||||
InstanceDataHeader *header = (InstanceDataHeader*)geo->instData;
|
||||
|
||||
Skin *skin = *PLUGINOFFSET(Skin*, geo, skinGlobals.offset);
|
||||
if(skin == nil)
|
||||
Skin *skin = *PLUGINOFFSET(Skin*, geo, skinGlobals.offset);
|
||||
if(skin == nil)
|
||||
return;
|
||||
|
||||
uint8 *data = skin->data;
|
||||
|
@ -776,9 +776,9 @@ registerNativeRaster(void)
|
|||
{
|
||||
nativeRasterOffset = Raster::registerPlugin(sizeof(GlRaster),
|
||||
ID_RASTERWDGL,
|
||||
createNativeRaster,
|
||||
destroyNativeRaster,
|
||||
copyNativeRaster);
|
||||
createNativeRaster,
|
||||
destroyNativeRaster,
|
||||
copyNativeRaster);
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -168,6 +168,14 @@ MatFX::setEnvTexture(Texture *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
|
||||
MatFX::setEnvCoefficient(float32 coef)
|
||||
{
|
||||
|
|
|
@ -475,9 +475,9 @@ registerNativeRaster(void)
|
|||
{
|
||||
nativeRasterOffset = Raster::registerPlugin(sizeof(Ps2Raster),
|
||||
ID_RASTERPS2,
|
||||
createNativeRaster,
|
||||
destroyNativeRaster,
|
||||
copyNativeRaster);
|
||||
createNativeRaster,
|
||||
destroyNativeRaster,
|
||||
copyNativeRaster);
|
||||
|
||||
Texture::registerPlugin(0, ID_SKYMIPMAP, nil, nil, nil);
|
||||
Texture::registerPluginStream(ID_SKYMIPMAP, readMipmap, writeMipmap, getSizeMipmap);
|
||||
|
|
|
@ -29,7 +29,7 @@ struct Driver
|
|||
|
||||
static PluginList s_plglist[NUM_PLATFORMS];
|
||||
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){
|
||||
return s_plglist[platform].registerPlugin(size, id,
|
||||
ctor, dtor, nil);
|
||||
|
|
|
@ -115,6 +115,7 @@ struct MatFX
|
|||
void setBumpTexture(Texture *t);
|
||||
void setBumpCoefficient(float32 coef);
|
||||
void setEnvTexture(Texture *t);
|
||||
void setEnvFrame(Frame *f);
|
||||
void setEnvCoefficient(float32 coef);
|
||||
void setDualTexture(Texture *t);
|
||||
void setDualSrcBlend(int32 blend);
|
||||
|
|
Loading…
Reference in New Issue