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

@ -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

@ -59,6 +59,8 @@ UniformState uniformState;
UniformScene uniformScene; UniformScene uniformScene;
UniformObject uniformObject; UniformObject uniformObject;
Shader *simpleShader;
void void
beginUpdate(Camera *cam) beginUpdate(Camera *cam)
{ {
@ -122,10 +124,12 @@ initializeRender(void)
{ {
driver[PLATFORM_GL3]->beginUpdate = beginUpdate; driver[PLATFORM_GL3]->beginUpdate = beginUpdate;
simpleShader = Shader::fromFiles("simple.vert", "simple.frag");
glClearColor(0.25, 0.25, 0.25, 1.0); 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);
@ -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

@ -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

@ -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

@ -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);