implemented im3d for gl3 and d3d

This commit is contained in:
aap
2017-08-29 10:12:56 +02:00
parent b7f643a632
commit d00ab2f526
41 changed files with 1447 additions and 761 deletions

View File

@@ -5,9 +5,10 @@
#include "../rwbase.h"
#include "../rwerror.h"
#include "../rwplg.h"
#include "../rwrender.h"
#include "../rwengine.h"
#include "../rwpipeline.h"
#include "../rwobjects.h"
#include "../rwengine.h"
#ifdef RW_OPENGL
#include <GL/glew.h>
#include <GLFW/glfw3.h>
@@ -467,7 +468,7 @@ beginUpdate(Camera *cam)
}
static int
startGLFW(EngineStartParams *startparams)
openGLFW(EngineStartParams *startparams)
{
GLenum status;
GLFWwindow *win;
@@ -512,7 +513,7 @@ startGLFW(EngineStartParams *startparams)
}
static int
stopGLFW(void)
closeGLFW(void)
{
glfwDestroyWindow(glfwwindow);
glfwTerminate();
@@ -564,14 +565,24 @@ initOpenGL(void)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1,
0, GL_RGBA, GL_UNSIGNED_BYTE, &whitepixel);
#include "shaders/simple_gl3.inc"
#include "shaders/simple_vs_gl3.inc"
#include "shaders/simple_fs_gl3.inc"
simpleShader = Shader::fromStrings(simple_vert_src, simple_frag_src);
openIm2D();
openIm3D();
return 1;
}
static int
termOpenGL(void)
{
closeIm3D();
closeIm2D();
return 1;
}
static int
finalizeOpenGL(void)
{
@@ -582,14 +593,18 @@ static int
deviceSystem(DeviceReq req, void *arg0)
{
switch(req){
case DEVICESTART:
return startGLFW((EngineStartParams*)arg0);
case DEVICEOPEN:
return openGLFW((EngineStartParams*)arg0);
case DEVICECLOSE:
return closeGLFW();
case DEVICEINIT:
return initOpenGL();
case DEVICETERM:
return termOpenGL();
case DEVICEFINALIZE:
return finalizeOpenGL();
case DEVICESTOP:
return stopGLFW();
}
return 1;
}
@@ -603,6 +618,9 @@ Device renderdevice = {
gl3::setRenderState,
gl3::getRenderState,
gl3::im2DRenderIndexedPrimitive,
gl3::im3DTransform,
gl3::im3DRenderIndexed,
gl3::im3DEnd,
gl3::deviceSystem
};

View File

@@ -1,104 +0,0 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "../rwbase.h"
#include "../rwerror.h"
#include "../rwplg.h"
#include "../rwpipeline.h"
#include "../rwobjects.h"
#include "../rwengine.h"
#ifdef RW_OPENGL
#include <GL/glew.h>
#include "rwgl3.h"
#include "rwgl3shader.h"
namespace rw {
namespace gl3 {
static uint32 im2DVbo, im2DIbo;
static int32 u_xform;
#define STARTINDICES 10000
#define STARTVERTICES 10000
static Shader *im2dShader;
static AttribDesc attribDesc[3] = {
{ ATTRIB_POS, GL_FLOAT, GL_FALSE, 4,
sizeof(Im2DVertex), 0 },
{ ATTRIB_COLOR, GL_UNSIGNED_BYTE, GL_TRUE, 4,
sizeof(Im2DVertex), offsetof(Im2DVertex, r) },
{ ATTRIB_TEXCOORDS0, GL_FLOAT, GL_FALSE, 2,
sizeof(Im2DVertex), offsetof(Im2DVertex, u) },
};
static int primTypeMap[] = {
GL_POINTS, // invalid
GL_LINES,
GL_LINE_STRIP,
GL_TRIANGLES,
GL_TRIANGLE_STRIP,
GL_TRIANGLE_FAN,
GL_POINTS
};
void
openIm2D(void)
{
u_xform = registerUniform("u_xform");
#include "shaders/im2d_gl3.inc"
im2dShader = Shader::fromStrings(im2d_vert_src, im2d_frag_src);
glGenBuffers(1, &im2DIbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im2DIbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, STARTINDICES*2,
nil, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glGenBuffers(1, &im2DVbo);
glBindBuffer(GL_ARRAY_BUFFER, im2DVbo);
glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im2DVertex),
nil, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void
im2DRenderIndexedPrimitive(PrimitiveType primType,
void *vertices, int32 numVertices,
void *indices, int32 numIndices)
{
GLfloat xform[4];
Camera *cam;
cam = (Camera*)engine->currentCamera;
// TODO: fixed size
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im2DIbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices*2,
indices, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, im2DVbo);
glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Im2DVertex),
vertices, GL_DYNAMIC_DRAW);
xform[0] = 2.0f/cam->frameBuffer->width;
xform[1] = -2.0f/cam->frameBuffer->height;
xform[2] = -1.0f;
xform[3] = 1.0f;
im2dShader->use();
setAttribPointers(attribDesc, 3);
glUniform4fv(currentShader->uniformLocations[u_xform], 1, xform);
setTexture(0, engine->imtexture);
flushCache();
glDrawElements(primTypeMap[primType], numIndices,
GL_UNSIGNED_SHORT, nil);
disableAttribPointers(attribDesc, 3);
}
}
}
#endif

199
src/gl/gl3immed.cpp Normal file
View File

@@ -0,0 +1,199 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "../rwbase.h"
#include "../rwerror.h"
#include "../rwplg.h"
#include "../rwpipeline.h"
#include "../rwobjects.h"
#include "../rwengine.h"
#ifdef RW_OPENGL
#include <GL/glew.h>
#include "rwgl3.h"
#include "rwgl3shader.h"
namespace rw {
namespace gl3 {
static uint32 im2DVbo, im2DIbo;
static int32 u_xform;
#define STARTINDICES 10000
#define STARTVERTICES 10000
static Shader *im2dShader;
static AttribDesc im2dattribDesc[3] = {
{ ATTRIB_POS, GL_FLOAT, GL_FALSE, 4,
sizeof(Im2DVertex), 0 },
{ ATTRIB_COLOR, GL_UNSIGNED_BYTE, GL_TRUE, 4,
sizeof(Im2DVertex), offsetof(Im2DVertex, r) },
{ ATTRIB_TEXCOORDS0, GL_FLOAT, GL_FALSE, 2,
sizeof(Im2DVertex), offsetof(Im2DVertex, u) },
};
static int primTypeMap[] = {
GL_POINTS, // invalid
GL_LINES,
GL_LINE_STRIP,
GL_TRIANGLES,
GL_TRIANGLE_STRIP,
GL_TRIANGLE_FAN,
GL_POINTS
};
void
openIm2D(void)
{
u_xform = registerUniform("u_xform");
#include "shaders/im2d_gl3.inc"
#include "shaders/simple_fs_gl3.inc"
im2dShader = Shader::fromStrings(im2d_vert_src, simple_frag_src);
glGenBuffers(1, &im2DIbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im2DIbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, STARTINDICES*2,
nil, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glGenBuffers(1, &im2DVbo);
glBindBuffer(GL_ARRAY_BUFFER, im2DVbo);
glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im2DVertex),
nil, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void
closeIm2D(void)
{
glDeleteBuffers(1, &im2DIbo);
glDeleteBuffers(1, &im2DVbo);
im2dShader->destroy();
im2dShader = nil;
}
void
im2DRenderIndexedPrimitive(PrimitiveType primType,
void *vertices, int32 numVertices,
void *indices, int32 numIndices)
{
GLfloat xform[4];
Camera *cam;
cam = (Camera*)engine->currentCamera;
// TODO: fixed size
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im2DIbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices*2,
indices, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, im2DVbo);
glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Im2DVertex),
vertices, GL_DYNAMIC_DRAW);
xform[0] = 2.0f/cam->frameBuffer->width;
xform[1] = -2.0f/cam->frameBuffer->height;
xform[2] = -1.0f;
xform[3] = 1.0f;
im2dShader->use();
setAttribPointers(im2dattribDesc, 3);
glUniform4fv(currentShader->uniformLocations[u_xform], 1, xform);
setTexture(0, engine->imtexture);
flushCache();
glDrawElements(primTypeMap[primType], numIndices,
GL_UNSIGNED_SHORT, nil);
disableAttribPointers(im2dattribDesc, 3);
}
// Im3D
static Shader *im3dShader;
static AttribDesc im3dattribDesc[3] = {
{ ATTRIB_POS, GL_FLOAT, GL_FALSE, 3,
sizeof(Im3DVertex), 0 },
{ ATTRIB_COLOR, GL_UNSIGNED_BYTE, GL_TRUE, 4,
sizeof(Im3DVertex), offsetof(Im3DVertex, r) },
{ ATTRIB_TEXCOORDS0, GL_FLOAT, GL_FALSE, 2,
sizeof(Im3DVertex), offsetof(Im3DVertex, u) },
};
static uint32 im3DVbo, im3DIbo;
static int32 num3DVertices; // not actually needed here
void
openIm3D(void)
{
#include "shaders/im3d_gl3.inc"
#include "shaders/simple_fs_gl3.inc"
im3dShader = Shader::fromStrings(im3d_vert_src, simple_frag_src);
glGenBuffers(1, &im3DIbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im3DIbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, STARTINDICES*2,
nil, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glGenBuffers(1, &im3DVbo);
glBindBuffer(GL_ARRAY_BUFFER, im3DVbo);
glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im3DVertex),
nil, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void
closeIm3D(void)
{
glDeleteBuffers(1, &im3DIbo);
glDeleteBuffers(1, &im3DVbo);
im3dShader->destroy();
im3dShader = nil;
}
void
im3DTransform(void *vertices, int32 numVertices, Matrix *world)
{
if(world == nil){
Matrix ident;
ident.setIdentity();
world = &ident;
}
setWorldMatrix(world);
im3dShader->use();
// TODO: fixed size
glBindBuffer(GL_ARRAY_BUFFER, im3DVbo);
glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Im3DVertex),
vertices, GL_DYNAMIC_DRAW);
setAttribPointers(im3dattribDesc, 3);
num3DVertices = numVertices;
}
void
im3DRenderIndexed(PrimitiveType primType, void *indices, int32 numIndices)
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im3DIbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices*2,
indices, GL_DYNAMIC_DRAW);
setTexture(0, engine->imtexture);
flushCache();
glDrawElements(primTypeMap[primType], numIndices,
GL_UNSIGNED_SHORT, nil);
disableAttribPointers(im3dattribDesc, 3);
}
void
im3DEnd(void)
{
}
}
}
#endif

242
src/gl/gl3matfx.cpp Normal file
View File

@@ -0,0 +1,242 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include "../rwbase.h"
#include "../rwerror.h"
#include "../rwplg.h"
#include "../rwrender.h"
#include "../rwengine.h"
#include "../rwpipeline.h"
#include "../rwobjects.h"
#include "../rwanim.h"
#include "../rwplugins.h"
#ifdef RW_OPENGL
#include <GL/glew.h>
#endif
#include "rwgl3.h"
#include "rwgl3shader.h"
#include "rwgl3plg.h"
#include "rwgl3impl.h"
namespace rw {
namespace gl3 {
#ifdef RW_OPENGL
#define U(i) currentShader->uniformLocations[i]
static Shader *envShader;
static int32 u_texMatrix;
static int32 u_coefficient;
static void*
matfxOpen(void *o, int32, int32)
{
u_texMatrix = registerUniform("u_texMatrix");
u_coefficient = registerUniform("u_coefficient");
#include "shaders/matfx_gl3.inc"
matFXGlobals.pipelines[PLATFORM_GL3] = makeMatFXPipeline();
envShader = Shader::fromStrings(matfx_env_vert_src, matfx_env_frag_src);
return o;
}
static void*
matfxClose(void *o, int32, int32)
{
return o;
}
void
initMatFX(void)
{
Driver::registerPlugin(PLATFORM_GL3, 0, ID_MATFX,
matfxOpen, matfxClose);
}
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);
rw::SetRenderState(VERTEXALPHA, 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, &cam, &inv);
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);
rw::SetRenderState(VERTEXALPHA, 1);
rw::SetRenderState(SRCBLEND, BLENDONE);
rw::SetRenderState(DESTBLEND, BLENDONE);
flushCache();
glDrawElements(header->primType, inst->numIndex,
GL_UNSIGNED_SHORT, (void*)(uintptr)inst->offset);
rw::SetRenderState(SRCBLEND, BLENDSRCALPHA);
rw::SetRenderState(DESTBLEND, BLENDINVSRCALPHA);
}
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->attribDesc, header->numAttribs);
InstanceData *inst = header->inst;
int32 n = header->numMeshes;
// rw::SetRenderState(ALPHATESTFUNC, 1);
// rw::SetRenderState(ALPHATESTREF, 50);
int32 fx;
while(n--){
fx = MatFX::getEffects(inst->material);
switch(fx){
case MatFX::ENVMAP:
matfxEnvRender(header, inst);
break;
default:
matfxDefaultRender(header, inst);
}
inst++;
}
disableAttribPointers(header->attribDesc, header->numAttribs);
}
ObjPipeline*
makeMatFXPipeline(void)
{
ObjPipeline *pipe = new ObjPipeline(PLATFORM_GL3);
pipe->instanceCB = defaultInstanceCB;
pipe->uninstanceCB = defaultUninstanceCB;
pipe->renderCB = matfxRenderCB;
pipe->pluginID = ID_MATFX;
pipe->pluginData = 0;
return pipe;
}
#else
void initMatFX(void) { }
#endif
}
}

View File

@@ -5,9 +5,10 @@
#include "../rwbase.h"
#include "../rwerror.h"
#include "../rwplg.h"
#include "../rwrender.h"
#include "../rwengine.h"
#include "../rwpipeline.h"
#include "../rwobjects.h"
#include "../rwengine.h"
#ifdef RW_OPENGL
#include <GL/glew.h>
#include "rwgl3.h"
@@ -72,7 +73,6 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
Material *m;
RGBAf col;
GLfloat surfProps[4];
int id;
setWorldMatrix(atomic->getFrame()->getLTM());
lightingCB();

View File

@@ -201,6 +201,14 @@ Shader::use(void)
}
}
void
Shader::destroy(void)
{
glDeleteProgram(this->program);
rwFree(this->uniformLocations);
rwFree(this);
}
}
}

View File

@@ -6,10 +6,11 @@
#include "../rwbase.h"
#include "../rwerror.h"
#include "../rwplg.h"
#include "../rwrender.h"
#include "../rwengine.h"
#include "../rwpipeline.h"
#include "../rwobjects.h"
#include "../rwanim.h"
#include "../rwengine.h"
#include "../rwplugins.h"
#ifdef RW_OPENGL
#include <GL/glew.h>
@@ -27,214 +28,6 @@ namespace gl3 {
#define U(i) currentShader->uniformLocations[i]
// MatFX
static Shader *envShader;
static int32 u_texMatrix;
static int32 u_coefficient;
static void*
matfxOpen(void *o, int32, int32)
{
u_texMatrix = registerUniform("u_texMatrix");
u_coefficient = registerUniform("u_coefficient");
#include "shaders/matfx_gl3.inc"
matFXGlobals.pipelines[PLATFORM_GL3] = makeMatFXPipeline();
envShader = Shader::fromStrings(matfx_env_vert_src, matfx_env_frag_src);
return o;
}
static void*
matfxClose(void *o, int32, int32)
{
return o;
}
void
initMatFX(void)
{
Driver::registerPlugin(PLATFORM_GL3, 0, ID_MATFX,
matfxOpen, matfxClose);
}
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);
rw::SetRenderState(VERTEXALPHA, 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, &cam, &inv);
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);
rw::SetRenderState(VERTEXALPHA, 1);
rw::SetRenderState(SRCBLEND, BLENDONE);
rw::SetRenderState(DESTBLEND, BLENDONE);
flushCache();
glDrawElements(header->primType, inst->numIndex,
GL_UNSIGNED_SHORT, (void*)(uintptr)inst->offset);
rw::SetRenderState(SRCBLEND, BLENDSRCALPHA);
rw::SetRenderState(DESTBLEND, BLENDINVSRCALPHA);
}
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->attribDesc, header->numAttribs);
InstanceData *inst = header->inst;
int32 n = header->numMeshes;
// rw::SetRenderState(ALPHATESTFUNC, 1);
// rw::SetRenderState(ALPHATESTREF, 50);
int32 fx;
while(n--){
fx = MatFX::getEffects(inst->material);
switch(fx){
case MatFX::ENVMAP:
matfxEnvRender(header, inst);
break;
default:
matfxDefaultRender(header, inst);
}
inst++;
}
disableAttribPointers(header->attribDesc, header->numAttribs);
}
ObjPipeline*
makeMatFXPipeline(void)
{
ObjPipeline *pipe = new ObjPipeline(PLATFORM_GL3);
pipe->instanceCB = defaultInstanceCB;
pipe->uninstanceCB = defaultUninstanceCB;
pipe->renderCB = matfxRenderCB;
pipe->pluginID = ID_MATFX;
pipe->pluginData = 0;
return pipe;
}
// Skin
static Shader *skinShader;
static int32 u_boneMatrices;
@@ -242,7 +35,7 @@ static void*
skinOpen(void *o, int32, int32)
{
u_boneMatrices = registerUniform("u_boneMatrices");
#include "shaders/simple_gl3.inc"
#include "shaders/simple_fs_gl3.inc"
#include "shaders/skin_gl3.inc"
skinGlobals.pipelines[PLATFORM_GL3] = makeSkinPipeline();
skinShader = Shader::fromStrings(skin_vert_src, simple_frag_src);
@@ -448,7 +241,6 @@ skinRenderCB(Atomic *atomic, InstanceDataHeader *header)
Material *m;
RGBAf col;
GLfloat surfProps[4];
int id;
setWorldMatrix(atomic->getFrame()->getLTM());
lightingCB();
@@ -507,7 +299,6 @@ makeSkinPipeline(void)
#else
void initMatFX(void) { }
void initSkin(void) { }
#endif

View File

@@ -80,6 +80,22 @@ struct InstanceDataHeader : rw::InstanceDataHeader
#ifdef RW_GL3
struct Im3DVertex
{
V3d position;
uint8 r, g, b, a;
float32 u, v;
void setX(float32 x) { this->position.x = x; }
void setY(float32 y) { this->position.y = y; }
void setZ(float32 z) { this->position.z = z; }
void setColor(uint8 r, uint8 g, uint8 b, uint8 a) {
this->r = r; this->g = g; this->b = b; this->a = a; }
RGBA getColor(void) { return makeRGBA(this->r, this->g, this->b, this->a); }
void setU(float32 u) { this->u = u; }
void setV(float32 v) { this->v = v; }
};
struct Im2DVertex
{
float32 x, y, z, w;

View File

@@ -6,9 +6,15 @@ namespace gl3 {
extern Shader *simpleShader;
extern uint32 im2DVbo, im2DIbo;
void openIm2D(void);
void closeIm2D(void);
void im2DRenderIndexedPrimitive(PrimitiveType primType,
void *vertices, int32 numVertices, void *indices, int32 numIndices);
void openIm3D(void);
void closeIm3D(void);
void im3DTransform(void *vertices, int32 numVertices, Matrix *world);
void im3DRenderIndexed(PrimitiveType primType, void *indices, int32 numIndices);
void im3DEnd(void);
#endif
void rasterCreate(Raster *raster);

View File

@@ -35,6 +35,7 @@ public:
static Shader *fromFiles(const char *vs, const char *fs);
static Shader *fromStrings(const char *vsrc, const char *fsrc);
void use(void);
void destroy(void);
};
extern Shader *currentShader;

View File

@@ -1,20 +1,24 @@
all: im2d_gl3.inc simple_gl3.inc matfx_gl3.inc skin_gl3.inc
all: im2d_gl3.inc im3d_gl3.inc simple_vs_gl3.inc simple_fs_gl3.inc matfx_gl3.inc skin_gl3.inc
im2d_gl3.inc: im2d.frag im2d.vert
im2d_gl3.inc: im2d.vert
(echo 'const char *im2d_vert_src =';\
sed 's/..*/"&\\n"/' im2d.vert;\
echo ';';\
echo 'const char *im2d_frag_src =';\
sed 's/..*/"&\\n"/' im2d.frag;\
echo ';') >im2d_gl3.inc
simple_gl3.inc: simple.frag simple.vert
im3d_gl3.inc: im3d.vert
(echo 'const char *im3d_vert_src =';\
sed 's/..*/"&\\n"/' im3d.vert;\
echo ';') >im3d_gl3.inc
simple_vs_gl3.inc: simple.vert
(echo 'const char *simple_vert_src =';\
sed 's/..*/"&\\n"/' simple.vert;\
echo ';';\
echo 'const char *simple_frag_src =';\
echo ';') >simple_vs_gl3.inc
simple_fs_gl3.inc: simple.frag
(echo 'const char *simple_frag_src =';\
sed 's/..*/"&\\n"/' simple.frag;\
echo ';') >simple_gl3.inc
echo ';') >simple_fs_gl3.inc
matfx_gl3.inc: matfx_env.frag matfx_env.vert
(echo 'const char *matfx_env_vert_src =';\

View File

@@ -1,15 +0,0 @@
#version 330
uniform sampler2D tex;
in vec4 v_color;
in vec2 v_tex0;
out vec4 color;
void
main(void)
{
color = v_color*texture(tex, vec2(v_tex0.x, v_tex0.y));
}

View File

@@ -1,11 +1,15 @@
#version 330
//layout(std140) uniform Im2DState
//{
// int u_alphaTest;
// float u_alphaRef;
// mat4 u_xform;
//};
layout(std140) uniform State
{
int u_alphaTest;
float u_alphaRef;
int u_fogEnable;
float u_fogStart;
float u_fogEnd;
vec4 u_fogColor;
};
uniform vec4 u_xform;
@@ -15,6 +19,7 @@ layout(location = 3) in vec2 in_tex0;
out vec4 v_color;
out vec2 v_tex0;
out float v_fog;
void
main(void)
@@ -24,4 +29,5 @@ main(void)
gl_Position.xyz *= gl_Position.w;
v_color = in_color;
v_tex0 = in_tex0;
v_fog = clamp((gl_Position.z - u_fogEnd)/(u_fogStart - u_fogEnd), 0.0, 1.0);
}

View File

@@ -1,12 +1,16 @@
const char *im2d_vert_src =
"#version 330\n"
"//layout(std140) uniform Im2DState\n"
"//{\n"
"// int u_alphaTest;\n"
"// float u_alphaRef;\n"
"// mat4 u_xform;\n"
"//};\n"
"layout(std140) uniform State\n"
"{\n"
" int u_alphaTest;\n"
" float u_alphaRef;\n"
" int u_fogEnable;\n"
" float u_fogStart;\n"
" float u_fogEnd;\n"
" vec4 u_fogColor;\n"
"};\n"
"uniform vec4 u_xform;\n"
@@ -16,6 +20,7 @@ const char *im2d_vert_src =
"out vec4 v_color;\n"
"out vec2 v_tex0;\n"
"out float v_fog;\n"
"void\n"
"main(void)\n"
@@ -25,22 +30,6 @@ const char *im2d_vert_src =
" gl_Position.xyz *= gl_Position.w;\n"
" v_color = in_color;\n"
" v_tex0 = in_tex0;\n"
" v_fog = clamp((gl_Position.z - u_fogEnd)/(u_fogStart - u_fogEnd), 0.0, 1.0);\n"
"}\n"
;
const char *im2d_frag_src =
"#version 330\n"
"uniform sampler2D tex;\n"
"in vec4 v_color;\n"
"in vec2 v_tex0;\n"
"out vec4 color;\n"
"void\n"
"main(void)\n"
"{\n"
" color = v_color*texture(tex, vec2(v_tex0.x, v_tex0.y));\n"
"}\n"
;

42
src/gl/shaders/im3d.vert Normal file
View File

@@ -0,0 +1,42 @@
#version 330
layout(std140) uniform State
{
int u_alphaTest;
float u_alphaRef;
int u_fogEnable;
float u_fogStart;
float u_fogEnd;
vec4 u_fogColor;
};
layout(std140) uniform Scene
{
mat4 u_proj;
mat4 u_view;
};
layout(std140) uniform Object
{
mat4 u_world;
};
layout(location = 0) in vec3 in_pos;
layout(location = 2) in vec4 in_color;
layout(location = 3) in vec2 in_tex0;
out vec4 v_color;
out vec2 v_tex0;
out float v_fog;
void
main(void)
{
vec4 V = u_world * vec4(in_pos, 1.0);
vec4 cV = u_view * V;
gl_Position = u_proj * cV;
v_color = in_color;
v_tex0 = in_tex0;
v_fog = clamp((cV.z - u_fogEnd)/(u_fogStart - u_fogEnd), 0.0, 1.0);
}

View File

@@ -0,0 +1,44 @@
const char *im3d_vert_src =
"#version 330\n"
"layout(std140) uniform State\n"
"{\n"
" int u_alphaTest;\n"
" float u_alphaRef;\n"
" int u_fogEnable;\n"
" float u_fogStart;\n"
" float u_fogEnd;\n"
" vec4 u_fogColor;\n"
"};\n"
"layout(std140) uniform Scene\n"
"{\n"
" mat4 u_proj;\n"
" mat4 u_view;\n"
"};\n"
"layout(std140) uniform Object\n"
"{\n"
" mat4 u_world;\n"
"};\n"
"layout(location = 0) in vec3 in_pos;\n"
"layout(location = 2) in vec4 in_color;\n"
"layout(location = 3) in vec2 in_tex0;\n"
"out vec4 v_color;\n"
"out vec2 v_tex0;\n"
"out float v_fog;\n"
"void\n"
"main(void)\n"
"{\n"
" vec4 V = u_world * vec4(in_pos, 1.0);\n"
" vec4 cV = u_view * V; \n"
" gl_Position = u_proj * cV;\n"
" v_color = in_color;\n"
" v_tex0 = in_tex0;\n"
" v_fog = clamp((cV.z - u_fogEnd)/(u_fogStart - u_fogEnd), 0.0, 1.0);\n"
"}\n"
;

View File

@@ -0,0 +1,43 @@
const char *simple_frag_src =
"#version 330\n"
"layout(std140) uniform State\n"
"{\n"
" int u_alphaTest;\n"
" float u_alphaRef;\n"
" int u_fogEnable;\n"
" float u_fogStart;\n"
" float u_fogEnd;\n"
" vec4 u_fogColor;\n"
"};\n"
"uniform sampler2D tex;\n"
"in vec4 v_color;\n"
"in vec2 v_tex0;\n"
"in float v_fog;\n"
"out vec4 color;\n"
"void\n"
"main(void)\n"
"{\n"
" color = v_color*texture(tex, vec2(v_tex0.x, v_tex0.y));\n"
" if(u_fogEnable != 0)\n"
" color.rgb = mix(u_fogColor.rgb, color.rgb, v_fog);\n"
" switch(u_alphaTest){\n"
" default:\n"
" case 0: break;\n"
" case 1:\n"
" if(color.a < u_alphaRef)\n"
" discard;\n"
" break;\n"
" case 2:\n"
" if(color.a >= u_alphaRef)\n"
" discard;\n"
" break;\n"
" }\n"
"}\n"
;

View File

@@ -70,46 +70,3 @@ const char *simple_vert_src =
" v_fog = clamp((cV.z - u_fogEnd)/(u_fogStart - u_fogEnd), 0.0, 1.0);\n"
"}\n"
;
const char *simple_frag_src =
"#version 330\n"
"layout(std140) uniform State\n"
"{\n"
" int u_alphaTest;\n"
" float u_alphaRef;\n"
" int u_fogEnable;\n"
" float u_fogStart;\n"
" float u_fogEnd;\n"
" vec4 u_fogColor;\n"
"};\n"
"uniform sampler2D tex;\n"
"in vec4 v_color;\n"
"in vec2 v_tex0;\n"
"in float v_fog;\n"
"out vec4 color;\n"
"void\n"
"main(void)\n"
"{\n"
" color = v_color*texture(tex, vec2(v_tex0.x, v_tex0.y));\n"
" if(u_fogEnable != 0)\n"
" color.rgb = mix(u_fogColor.rgb, color.rgb, v_fog);\n"
" switch(u_alphaTest){\n"
" default:\n"
" case 0: break;\n"
" case 1:\n"
" if(color.a < u_alphaRef)\n"
" discard;\n"
" break;\n"
" case 2:\n"
" if(color.a >= u_alphaRef)\n"
" discard;\n"
" break;\n"
" }\n"
"}\n"
;