revised initialization again and restructured code for that

This commit is contained in:
aap
2016-06-27 21:59:35 +02:00
parent 454e7b8ac6
commit d3cff5c06c
37 changed files with 1549 additions and 913 deletions

70
src/gl/gl3.cpp Normal file
View File

@@ -0,0 +1,70 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include "../rwbase.h"
#include "../rwerror.h"
#include "../rwplg.h"
#include "../rwpipeline.h"
#include "../rwobjects.h"
#include "../rwengine.h"
#include "../rwplugins.h"
#ifdef RW_OPENGL
#include <GL/glew.h>
#endif
#include "rwgl3.h"
#include "rwgl3shader.h"
#include "rwgl3impl.h"
namespace rw {
namespace gl3 {
// TODO: make some of these things platform-independent
void*
driverOpen(void *o, int32, int32)
{
printf("gl3 open\n");
#ifdef RW_OPENGL
driver[PLATFORM_GL3]->defaultPipeline = makeDefaultPipeline();
#endif
driver[PLATFORM_GL3]->rasterNativeOffset = nativeRasterOffset;
driver[PLATFORM_GL3]->rasterCreate = rasterCreate;
driver[PLATFORM_GL3]->rasterLock = rasterLock;
driver[PLATFORM_GL3]->rasterUnlock = rasterUnlock;
driver[PLATFORM_GL3]->rasterNumLevels = rasterNumLevels;
driver[PLATFORM_GL3]->rasterFromImage = rasterFromImage;
initializeRender();
return o;
}
void*
driverClose(void *o, int32, int32)
{
return o;
}
void
initializePlatform(void)
{
Driver::registerPlugin(PLATFORM_GL3, 0, PLATFORM_GL3,
driverOpen, driverClose);
registerNativeRaster();
#ifdef RW_OPENGL
// uniforms need to be registered before any shaders are created
registerBlock("Scene");
registerBlock("Object");
registerBlock("State");
registerUniform("u_matColor");
registerUniform("u_surfaceProps");
#endif
}
}
}

View File

@@ -21,23 +21,6 @@ namespace gl3 {
// TODO: make some of these things platform-independent
void
initializePlatform(void)
{
registerNativeRaster();
#ifdef RW_OPENGL
driver[PLATFORM_GL3].defaultPipeline = makeDefaultPipeline();
// uniforms need to be registered before any shaders are created
registerBlock("Scene");
registerBlock("Object");
registerUniform("u_matColor");
registerUniform("u_surfaceProps");
#endif
initializeRender();
}
#ifdef RW_OPENGL
static void
@@ -251,30 +234,6 @@ makeDefaultPipeline(void)
return pipe;
}
ObjPipeline*
makeSkinPipeline(void)
{
ObjPipeline *pipe = new ObjPipeline(PLATFORM_GL3);
pipe->instanceCB = defaultInstanceCB;
pipe->uninstanceCB = defaultUninstanceCB;
pipe->renderCB = defaultRenderCB;
pipe->pluginID = ID_SKIN;
pipe->pluginData = 1;
return pipe;
}
ObjPipeline*
makeMatFXPipeline(void)
{
ObjPipeline *pipe = new ObjPipeline(PLATFORM_GL3);
pipe->instanceCB = defaultInstanceCB;
pipe->uninstanceCB = defaultUninstanceCB;
pipe->renderCB = defaultRenderCB;
pipe->pluginID = ID_MATFX;
pipe->pluginData = 0;
return pipe;
}
#endif
}

95
src/gl/gl3plugins.cpp Normal file
View File

@@ -0,0 +1,95 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include "../rwbase.h"
#include "../rwerror.h"
#include "../rwplg.h"
#include "../rwpipeline.h"
#include "../rwobjects.h"
#include "../rwengine.h"
#include "../rwplugins.h"
#ifdef RW_OPENGL
#include <GL/glew.h>
#endif
#include "rwgl3.h"
#include "rwgl3shader.h"
namespace rw {
namespace gl3 {
#ifdef RW_OPENGL
// MatFX
static void*
matfxOpen(void *o, int32, int32)
{
matFXGlobals.pipelines[PLATFORM_GL3] = makeMatFXPipeline();
return o;
}
static void*
matfxClose(void *o, int32, int32)
{
return o;
}
void
initMatFX(void)
{
Driver::registerPlugin(PLATFORM_GL3, 0, ID_MATFX,
matfxOpen, matfxClose);
}
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 void*
skinOpen(void *o, int32, int32)
{
skinGlobals.pipelines[PLATFORM_GL3] = makeSkinPipeline();
return o;
}
static void*
skinClose(void *o, int32, int32)
{
return o;
}
void
initSkin(void)
{
Driver::registerPlugin(PLATFORM_GL3, 0, ID_SKIN,
skinOpen, skinClose);
}
ObjPipeline*
makeSkinPipeline(void)
{
ObjPipeline *pipe = new ObjPipeline(PLATFORM_GL3);
pipe->instanceCB = defaultInstanceCB;
pipe->uninstanceCB = defaultUninstanceCB;
pipe->renderCB = defaultRenderCB;
pipe->pluginID = ID_SKIN;
pipe->pluginData = 1;
return pipe;
}
#endif
}
}

View File

@@ -21,7 +21,7 @@ namespace gl3 {
int32 nativeRasterOffset;
static void
void
rasterCreate(Raster *raster)
{
Gl3Raster *natras = PLUGINOFFSET(Gl3Raster, raster, nativeRasterOffset);
@@ -42,27 +42,27 @@ rasterCreate(Raster *raster)
#endif
}
static uint8*
uint8*
rasterLock(Raster*, int32 level)
{
printf("locking\n");
return nil;
}
static void
void
rasterUnlock(Raster*, int32)
{
printf("unlocking\n");
}
static int32
int32
rasterNumLevels(Raster*)
{
printf("numlevels\n");
return 0;
}
static void
void
rasterFromImage(Raster *raster, Image *image)
{
int32 format;
@@ -112,26 +112,8 @@ copyNativeRaster(void *dst, void *, int32 offset, int32)
return dst;
}
static void*
nativeOpen(void*, int32, int32)
{
driver[PLATFORM_GL3].rasterNativeOffset = nativeRasterOffset;
driver[PLATFORM_GL3].rasterCreate = rasterCreate;
driver[PLATFORM_GL3].rasterLock = rasterLock;
driver[PLATFORM_GL3].rasterUnlock = rasterUnlock;
driver[PLATFORM_GL3].rasterNumLevels = rasterNumLevels;
driver[PLATFORM_GL3].rasterFromImage = rasterFromImage;
}
static void*
nativeClose(void*, int32, int32)
{
printf("gl3 native close\n");
}
void registerNativeRaster(void)
{
Engine::registerPlugin(0, ID_RASTERGL3, nativeOpen, nativeClose);
nativeRasterOffset = Raster::registerPlugin(sizeof(Gl3Raster),
ID_RASTERGL3,
createNativeRaster,

View File

@@ -17,6 +17,12 @@
namespace rw {
namespace gl3 {
struct UniformState
{
int alphaFunc;
float32 alphaRef;
};
struct UniformScene
{
float32 proj[16];
@@ -47,8 +53,9 @@ struct UniformObject
};
GLuint vao;
GLuint ubo_scene, ubo_object;
GLuint ubo_state, ubo_scene, ubo_object;
GLuint whitetex;
UniformState uniformState;
UniformScene uniformScene;
UniformObject uniformObject;
@@ -113,7 +120,7 @@ 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);
@@ -125,6 +132,14 @@ initializeRender(void)
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &ubo_state);
glBindBuffer(GL_UNIFORM_BUFFER, ubo_state);
glBindBufferBase(GL_UNIFORM_BUFFER, gl3::findBlock("State"), ubo_state);
glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformState), &uniformState,
GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glGenBuffers(1, &ubo_scene);
glBindBuffer(GL_UNIFORM_BUFFER, ubo_scene);
glBindBufferBase(GL_UNIFORM_BUFFER, gl3::findBlock("Scene"), ubo_scene);
@@ -163,6 +178,7 @@ setAttribPointers(InstanceDataHeader *header)
}
}
static bool32 stateDirty = 1;
static bool32 sceneDirty = 1;
static bool32 objectDirty = 1;
@@ -262,6 +278,21 @@ setVertexAlpha(bool32 alpha)
vertexAlpha = alpha;
}
void
setAlphaTestFunc(int32 f)
{
uniformState.alphaFunc = f;
stateDirty = 1;
}
void
setAlphaRef(float32 f)
{
uniformState.alphaRef = f;
stateDirty = 1;
}
#define U(s) currentShader->uniformLocations[findUniform(s)]
void
flushCache(void)
@@ -278,6 +309,12 @@ flushCache(void)
&uniformScene);
sceneDirty = 0;
}
if(stateDirty){
glBindBuffer(GL_UNIFORM_BUFFER, ubo_state);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(UniformState),
&uniformState);
stateDirty = 0;
}
}
void
@@ -305,6 +342,56 @@ 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)
{
@@ -322,11 +409,12 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
InstanceData *inst = header->inst;
int32 n = header->numMeshes;
setAlphaTestFunc(1);
setAlphaRef(0.2);
while(n--){
m = inst->material;
#define U(s) currentShader->uniformLocations[findUniform(s)]
convColor(&col, &m->color);
glUniform4fv(U("u_matColor"), 1, (GLfloat*)&col);

View File

@@ -60,6 +60,10 @@ struct InstanceDataHeader : rw::InstanceDataHeader
void setAttribPointers(InstanceDataHeader *header);
// Render state
void setAlphaTestFunc(int32 f);
void setAlphaRef(float32 f);
// per Scene
void setProjectionMatrix(float32*);
void setViewMatrix(float32*);
@@ -88,6 +92,8 @@ void defaultInstanceCB(Geometry *geo, InstanceDataHeader *header);
void defaultUninstanceCB(Geometry *geo, InstanceDataHeader *header);
void defaultRenderCB(Atomic *atomic, InstanceDataHeader *header);
void matfxRenderCB(Atomic *atomic, InstanceDataHeader *header);
ObjPipeline *makeDefaultPipeline(void);
ObjPipeline *makeSkinPipeline(void);
ObjPipeline *makeMatFXPipeline(void);

11
src/gl/rwgl3impl.h Normal file
View File

@@ -0,0 +1,11 @@
namespace rw {
namespace gl3 {
void rasterCreate(Raster *raster);
uint8 *rasterLock(Raster*, int32 level);
void rasterUnlock(Raster*, int32);
int32 rasterNumLevels(Raster*);
void rasterFromImage(Raster *raster, Image *image);
}
}

13
src/gl/rwgl3plg.h Normal file
View File

@@ -0,0 +1,13 @@
namespace rw {
namespace gl3 {
void matfxRenderCB(Atomic *atomic, InstanceDataHeader *header);
ObjPipeline *makeSkinPipeline(void);
ObjPipeline *makeMatFXPipeline(void);
void initMatFX(void);
void initSkin(void);
}
}

View File

@@ -60,12 +60,16 @@ ObjPipeline *makeDefaultPipeline(void);
// Skin plugin
void initSkin(void);
Stream *readNativeSkin(Stream *stream, int32, void *object, int32 offset);
Stream *writeNativeSkin(Stream *stream, int32 len, void *object, int32 offset);
int32 getSizeNativeSkin(void *object, int32 offset);
ObjPipeline *makeSkinPipeline(void);
// MatFX plugin
void initMatFX(void);
ObjPipeline *makeMatFXPipeline(void);
// Raster

View File

@@ -21,10 +21,25 @@
namespace rw {
namespace wdgl {
void*
driverOpen(void *o, int32, int32)
{
printf("wdgl open\n");
driver[PLATFORM_WDGL]->defaultPipeline = makeDefaultPipeline();
return o;
}
void*
driverClose(void *o, int32, int32)
{
return o;
}
void
initializePlatform(void)
{
driver[PLATFORM_WDGL].defaultPipeline = makeDefaultPipeline();
Driver::registerPlugin(PLATFORM_WDGL, 0, PLATFORM_WDGL,
driverOpen, driverClose);
}
@@ -659,6 +674,28 @@ skinUninstanceCB(Geometry *geo)
skin->findUsedBones(geo->numVertices);
}
// Skin
static void*
skinOpen(void *o, int32, int32)
{
skinGlobals.pipelines[PLATFORM_WDGL] = makeSkinPipeline();
return o;
}
static void*
skinClose(void *o, int32, int32)
{
return o;
}
void
initSkin(void)
{
Driver::registerPlugin(PLATFORM_WDGL, 0, ID_SKIN,
skinOpen, skinClose);
}
ObjPipeline*
makeSkinPipeline(void)
{
@@ -671,6 +708,28 @@ makeSkinPipeline(void)
return pipe;
}
// MatFX
static void*
matfxOpen(void *o, int32, int32)
{
matFXGlobals.pipelines[PLATFORM_WDGL] = makeMatFXPipeline();
return o;
}
static void*
matfxClose(void *o, int32, int32)
{
return o;
}
void
initMatFX(void)
{
Driver::registerPlugin(PLATFORM_WDGL, 0, ID_MATFX,
matfxOpen, matfxClose);
}
ObjPipeline*
makeMatFXPipeline(void)
{