2016-06-27 21:59:35 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include "../rwbase.h"
|
|
|
|
#include "../rwerror.h"
|
|
|
|
#include "../rwplg.h"
|
2017-08-29 10:12:56 +02:00
|
|
|
#include "../rwrender.h"
|
|
|
|
#include "../rwengine.h"
|
2016-06-27 21:59:35 +02:00
|
|
|
#include "../rwpipeline.h"
|
|
|
|
#include "../rwobjects.h"
|
2016-07-11 20:27:21 +02:00
|
|
|
#include "../rwanim.h"
|
2016-06-27 21:59:35 +02:00
|
|
|
#include "../rwplugins.h"
|
|
|
|
#ifdef RW_OPENGL
|
|
|
|
#include <GL/glew.h>
|
|
|
|
#endif
|
|
|
|
#include "rwgl3.h"
|
|
|
|
#include "rwgl3shader.h"
|
2016-06-29 12:53:02 +02:00
|
|
|
#include "rwgl3plg.h"
|
|
|
|
|
|
|
|
#include "rwgl3impl.h"
|
2016-06-27 21:59:35 +02:00
|
|
|
|
|
|
|
namespace rw {
|
|
|
|
namespace gl3 {
|
|
|
|
|
|
|
|
#ifdef RW_OPENGL
|
|
|
|
|
2017-08-26 20:08:23 +02:00
|
|
|
#define U(i) currentShader->uniformLocations[i]
|
|
|
|
|
|
|
|
static Shader *skinShader;
|
|
|
|
static int32 u_boneMatrices;
|
2016-07-11 20:27:21 +02:00
|
|
|
|
2016-06-27 21:59:35 +02:00
|
|
|
static void*
|
|
|
|
skinOpen(void *o, int32, int32)
|
|
|
|
{
|
2017-08-26 20:08:23 +02:00
|
|
|
u_boneMatrices = registerUniform("u_boneMatrices");
|
2017-08-29 10:12:56 +02:00
|
|
|
#include "shaders/simple_fs_gl3.inc"
|
2016-07-19 18:40:10 +02:00
|
|
|
#include "shaders/skin_gl3.inc"
|
2016-06-27 21:59:35 +02:00
|
|
|
skinGlobals.pipelines[PLATFORM_GL3] = makeSkinPipeline();
|
2016-07-19 18:40:10 +02:00
|
|
|
skinShader = Shader::fromStrings(skin_vert_src, simple_frag_src);
|
2016-06-27 21:59:35 +02:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void*
|
|
|
|
skinClose(void *o, int32, int32)
|
|
|
|
{
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
initSkin(void)
|
|
|
|
{
|
|
|
|
Driver::registerPlugin(PLATFORM_GL3, 0, ID_SKIN,
|
|
|
|
skinOpen, skinClose);
|
2016-07-11 20:27:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ATTRIB_WEIGHTS = ATTRIB_TEXCOORDS7+1,
|
|
|
|
ATTRIB_INDICES
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
skinInstanceCB(Geometry *geo, InstanceDataHeader *header)
|
|
|
|
{
|
|
|
|
AttribDesc attribs[14], *a;
|
|
|
|
uint32 stride;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create attribute descriptions
|
|
|
|
//
|
|
|
|
a = attribs;
|
|
|
|
stride = 0;
|
|
|
|
|
|
|
|
// Positions
|
|
|
|
a->index = ATTRIB_POS;
|
|
|
|
a->size = 3;
|
|
|
|
a->type = GL_FLOAT;
|
|
|
|
a->normalized = GL_FALSE;
|
|
|
|
a->offset = stride;
|
|
|
|
stride += 12;
|
|
|
|
a++;
|
|
|
|
|
|
|
|
// Normals
|
|
|
|
// TODO: compress
|
2017-03-16 11:42:59 +01:00
|
|
|
bool hasNormals = !!(geo->flags & Geometry::NORMALS);
|
2016-07-11 20:27:21 +02:00
|
|
|
if(hasNormals){
|
|
|
|
a->index = ATTRIB_NORMAL;
|
|
|
|
a->size = 3;
|
|
|
|
a->type = GL_FLOAT;
|
|
|
|
a->normalized = GL_FALSE;
|
|
|
|
a->offset = stride;
|
|
|
|
stride += 12;
|
|
|
|
a++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prelighting
|
2017-03-16 11:42:59 +01:00
|
|
|
bool isPrelit = !!(geo->flags & Geometry::PRELIT);
|
2016-07-11 20:27:21 +02:00
|
|
|
if(isPrelit){
|
|
|
|
a->index = ATTRIB_COLOR;
|
|
|
|
a->size = 4;
|
|
|
|
a->type = GL_UNSIGNED_BYTE;
|
|
|
|
a->normalized = GL_TRUE;
|
|
|
|
a->offset = stride;
|
|
|
|
stride += 4;
|
|
|
|
a++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Texture coordinates
|
|
|
|
for(int32 n = 0; n < geo->numTexCoordSets; n++){
|
|
|
|
a->index = ATTRIB_TEXCOORDS0+n;
|
|
|
|
a->size = 2;
|
|
|
|
a->type = GL_FLOAT;
|
|
|
|
a->normalized = GL_FALSE;
|
|
|
|
a->offset = stride;
|
|
|
|
stride += 8;
|
|
|
|
a++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weights
|
|
|
|
a->index = ATTRIB_WEIGHTS;
|
|
|
|
a->size = 4;
|
|
|
|
a->type = GL_FLOAT;
|
|
|
|
a->normalized = GL_FALSE;
|
|
|
|
a->offset = stride;
|
|
|
|
stride += 16;
|
|
|
|
a++;
|
|
|
|
|
|
|
|
// Indices
|
|
|
|
a->index = ATTRIB_INDICES;
|
|
|
|
a->size = 4;
|
|
|
|
a->type = GL_UNSIGNED_BYTE;
|
|
|
|
a->normalized = GL_FALSE;
|
|
|
|
a->offset = stride;
|
|
|
|
stride += 4;
|
|
|
|
a++;
|
|
|
|
|
|
|
|
header->numAttribs = a - attribs;
|
|
|
|
for(a = attribs; a != &attribs[header->numAttribs]; a++)
|
|
|
|
a->stride = stride;
|
2017-08-25 14:06:53 +02:00
|
|
|
header->attribDesc = rwNewT(AttribDesc, header->numAttribs, MEMDUR_EVENT | ID_GEOMETRY);
|
2016-07-11 20:27:21 +02:00
|
|
|
memcpy(header->attribDesc, attribs,
|
|
|
|
header->numAttribs*sizeof(AttribDesc));
|
|
|
|
|
|
|
|
//
|
|
|
|
// Allocate and fill vertex buffer
|
|
|
|
//
|
|
|
|
Skin *skin = Skin::get(geo);
|
2017-08-25 14:06:53 +02:00
|
|
|
uint8 *verts = rwNewT(uint8, header->totalNumVertex*stride, MEMDUR_EVENT | ID_GEOMETRY);
|
2016-07-11 20:27:21 +02:00
|
|
|
header->vertexBuffer = verts;
|
|
|
|
|
|
|
|
// Positions
|
|
|
|
for(a = attribs; a->index != ATTRIB_POS; a++)
|
|
|
|
;
|
|
|
|
instV3d(VERT_FLOAT3, verts + a->offset,
|
|
|
|
geo->morphTargets[0].vertices,
|
|
|
|
header->totalNumVertex, a->stride);
|
|
|
|
|
|
|
|
// Normals
|
|
|
|
if(hasNormals){
|
|
|
|
for(a = attribs; a->index != ATTRIB_NORMAL; a++)
|
|
|
|
;
|
|
|
|
instV3d(VERT_FLOAT3, verts + a->offset,
|
|
|
|
geo->morphTargets[0].normals,
|
|
|
|
header->totalNumVertex, a->stride);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prelighting
|
|
|
|
if(isPrelit){
|
|
|
|
for(a = attribs; a->index != ATTRIB_COLOR; a++)
|
|
|
|
;
|
|
|
|
instColor(VERT_RGBA, verts + a->offset,
|
|
|
|
geo->colors,
|
|
|
|
header->totalNumVertex, a->stride);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Texture coordinates
|
|
|
|
for(int32 n = 0; n < geo->numTexCoordSets; n++){
|
|
|
|
for(a = attribs; a->index != ATTRIB_TEXCOORDS0+n; a++)
|
|
|
|
;
|
2017-08-05 01:44:37 +02:00
|
|
|
instTexCoords(VERT_FLOAT2, verts + a->offset,
|
2016-07-11 20:27:21 +02:00
|
|
|
geo->texCoords[n],
|
|
|
|
header->totalNumVertex, a->stride);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weights
|
|
|
|
for(a = attribs; a->index != ATTRIB_WEIGHTS; a++)
|
|
|
|
;
|
2017-08-05 01:44:37 +02:00
|
|
|
float *w = skin->weights;
|
2016-07-11 20:27:21 +02:00
|
|
|
instV4d(VERT_FLOAT4, verts + a->offset,
|
2017-08-05 01:44:37 +02:00
|
|
|
(V4d*)w,
|
2016-07-11 20:27:21 +02:00
|
|
|
header->totalNumVertex, a->stride);
|
|
|
|
|
|
|
|
// Indices
|
|
|
|
for(a = attribs; a->index != ATTRIB_INDICES; a++)
|
|
|
|
;
|
|
|
|
// not really colors of course but what the heck
|
|
|
|
instColor(VERT_RGBA, verts + a->offset,
|
2017-08-05 01:44:37 +02:00
|
|
|
(RGBA*)skin->indices,
|
2016-07-11 20:27:21 +02:00
|
|
|
header->totalNumVertex, a->stride);
|
|
|
|
|
|
|
|
glGenBuffers(1, &header->vbo);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, header->vbo);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, header->totalNumVertex*stride,
|
|
|
|
header->vertexBuffer, GL_STATIC_DRAW);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
skinUninstanceCB(Geometry *geo, InstanceDataHeader *header)
|
|
|
|
{
|
|
|
|
assert(0 && "can't uninstance");
|
|
|
|
}
|
|
|
|
|
|
|
|
static float skinMatrices[64*16];
|
|
|
|
|
|
|
|
void
|
|
|
|
updateSkinMatrices(Atomic *a)
|
|
|
|
{
|
|
|
|
Skin *skin = Skin::get(a->geometry);
|
|
|
|
HAnimHierarchy *hier = Skin::getHierarchy(a);
|
|
|
|
Matrix *invMats = (Matrix*)skin->inverseMatrices;
|
|
|
|
|
|
|
|
float *m;
|
|
|
|
m = (float*)skinMatrices;
|
|
|
|
for(int i = 0; i < hier->numNodes; i++){
|
2017-08-04 19:54:03 +02:00
|
|
|
invMats[i].flags = 0;
|
|
|
|
Matrix::mult((Matrix*)m, &invMats[i], &hier->matrices[i]);
|
2016-07-11 20:27:21 +02:00
|
|
|
m[3] = 0.0f;
|
|
|
|
m[7] = 0.0f;
|
|
|
|
m[11] = 0.0f;
|
|
|
|
m[15] = 1.0f;
|
|
|
|
m += 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
skinRenderCB(Atomic *atomic, InstanceDataHeader *header)
|
|
|
|
{
|
|
|
|
Material *m;
|
|
|
|
RGBAf col;
|
|
|
|
GLfloat surfProps[4];
|
|
|
|
|
|
|
|
setWorldMatrix(atomic->getFrame()->getLTM());
|
|
|
|
lightingCB();
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, header->vbo);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, header->ibo);
|
2016-08-21 18:20:27 +02:00
|
|
|
setAttribPointers(header->attribDesc, header->numAttribs);
|
2016-07-11 20:27:21 +02:00
|
|
|
|
|
|
|
InstanceData *inst = header->inst;
|
|
|
|
int32 n = header->numMeshes;
|
|
|
|
|
2017-03-16 01:10:01 +01:00
|
|
|
// rw::setRenderState(ALPHATESTFUNC, 1);
|
|
|
|
// rw::setRenderState(ALPHATESTREF, 50);
|
2016-07-11 20:27:21 +02:00
|
|
|
|
|
|
|
skinShader->use();
|
|
|
|
|
|
|
|
updateSkinMatrices(atomic);
|
2017-08-26 20:08:23 +02:00
|
|
|
glUniformMatrix4fv(U(u_boneMatrices), 64, GL_FALSE,
|
2016-07-11 20:27:21 +02:00
|
|
|
(GLfloat*)skinMatrices);
|
|
|
|
|
|
|
|
while(n--){
|
|
|
|
m = inst->material;
|
|
|
|
|
|
|
|
convColor(&col, &m->color);
|
2017-08-26 20:08:23 +02:00
|
|
|
glUniform4fv(U(u_matColor), 1, (GLfloat*)&col);
|
2016-07-11 20:27:21 +02:00
|
|
|
|
|
|
|
surfProps[0] = m->surfaceProps.ambient;
|
|
|
|
surfProps[1] = m->surfaceProps.specular;
|
|
|
|
surfProps[2] = m->surfaceProps.diffuse;
|
|
|
|
surfProps[3] = 0.0f;
|
2017-08-26 20:08:23 +02:00
|
|
|
glUniform4fv(U(u_surfaceProps), 1, surfProps);
|
2016-07-11 20:27:21 +02:00
|
|
|
|
|
|
|
setTexture(0, m->texture);
|
|
|
|
|
2017-07-27 15:05:30 +02:00
|
|
|
rw::SetRenderState(VERTEXALPHA, inst->vertexAlpha || m->color.alpha != 0xFF);
|
2016-07-11 20:27:21 +02:00
|
|
|
|
|
|
|
flushCache();
|
|
|
|
glDrawElements(header->primType, inst->numIndex,
|
|
|
|
GL_UNSIGNED_SHORT, (void*)(uintptr)inst->offset);
|
|
|
|
inst++;
|
|
|
|
}
|
2016-08-21 18:20:27 +02:00
|
|
|
disableAttribPointers(header->attribDesc, header->numAttribs);
|
2016-06-27 21:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjPipeline*
|
|
|
|
makeSkinPipeline(void)
|
|
|
|
{
|
|
|
|
ObjPipeline *pipe = new ObjPipeline(PLATFORM_GL3);
|
2016-07-11 20:27:21 +02:00
|
|
|
pipe->instanceCB = skinInstanceCB;
|
|
|
|
pipe->uninstanceCB = skinUninstanceCB;
|
|
|
|
pipe->renderCB = skinRenderCB;
|
2016-06-27 21:59:35 +02:00
|
|
|
pipe->pluginID = ID_SKIN;
|
|
|
|
pipe->pluginData = 1;
|
|
|
|
return pipe;
|
|
|
|
}
|
|
|
|
|
2016-07-05 11:36:43 +02:00
|
|
|
#else
|
|
|
|
|
|
|
|
void initSkin(void) { }
|
|
|
|
|
2016-06-27 21:59:35 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 14:05:45 +02:00
|
|
|
|