mirror of
https://github.com/aap/librw.git
synced 2025-12-19 17:09:51 +00:00
implemented im2d for d3d, fun little software T&L renderer
This commit is contained in:
@@ -70,7 +70,6 @@ static UniformObject uniformObject;
|
||||
int32 u_matColor;
|
||||
int32 u_surfaceProps;
|
||||
|
||||
|
||||
Shader *simpleShader;
|
||||
|
||||
static bool32 stateDirty = 1;
|
||||
@@ -304,6 +303,17 @@ setActiveTexture(int32 n)
|
||||
void
|
||||
setTexture(int32 n, Texture *tex)
|
||||
{
|
||||
// TODO: support mipmaps
|
||||
static GLint filternomip[] = {
|
||||
0, GL_NEAREST, GL_LINEAR,
|
||||
GL_NEAREST, GL_LINEAR,
|
||||
GL_NEAREST, GL_LINEAR
|
||||
};
|
||||
|
||||
static GLint wrap[] = {
|
||||
0, GL_REPEAT, GL_MIRRORED_REPEAT,
|
||||
GL_CLAMP, GL_CLAMP_TO_BORDER
|
||||
};
|
||||
bool32 alpha;
|
||||
setActiveTexture(GL_TEXTURE0+n);
|
||||
if(tex == nil || tex->raster->platform != PLATFORM_GL3 ||
|
||||
@@ -315,6 +325,13 @@ setTexture(int32 n, Texture *tex)
|
||||
nativeRasterOffset);
|
||||
glBindTexture(GL_TEXTURE_2D, natras->texid);
|
||||
alpha = natras->hasAlpha;
|
||||
if(tex->filterAddressing != natras->filterAddressing){
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filternomip[tex->getFilter()]);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filternomip[tex->getFilter()]);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap[tex->getAddressU()]);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap[tex->getAddressV()]);
|
||||
natras->filterAddressing = tex->filterAddressing;
|
||||
}
|
||||
}
|
||||
|
||||
if(n == 0){
|
||||
@@ -518,25 +535,6 @@ initOpenGL(void)
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
byte whitepixel[4] = {0xFF, 0xFF, 0xFF, 0xFF};
|
||||
glGenTextures(1, &whitetex);
|
||||
glBindTexture(GL_TEXTURE_2D, whitetex);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1,
|
||||
0, GL_RGBA, GL_UNSIGNED_BYTE, &whitepixel);
|
||||
|
||||
im2DInit();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
finalizeOpenGL(void)
|
||||
{
|
||||
#include "shaders/simple_gl3.inc"
|
||||
simpleShader = Shader::fromStrings(simple_vert_src, simple_frag_src);
|
||||
|
||||
glGenBuffers(1, &ubo_state);
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, ubo_state);
|
||||
glBindBufferBase(GL_UNIFORM_BUFFER, gl3::findBlock("State"), ubo_state);
|
||||
@@ -558,6 +556,25 @@ finalizeOpenGL(void)
|
||||
GL_DYNAMIC_DRAW);
|
||||
glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
||||
|
||||
byte whitepixel[4] = {0xFF, 0xFF, 0xFF, 0xFF};
|
||||
glGenTextures(1, &whitetex);
|
||||
glBindTexture(GL_TEXTURE_2D, whitetex);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1,
|
||||
0, GL_RGBA, GL_UNSIGNED_BYTE, &whitepixel);
|
||||
|
||||
#include "shaders/simple_gl3.inc"
|
||||
simpleShader = Shader::fromStrings(simple_vert_src, simple_frag_src);
|
||||
|
||||
openIm2D();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
finalizeOpenGL(void)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,19 +16,20 @@
|
||||
namespace rw {
|
||||
namespace gl3 {
|
||||
|
||||
uint32 im2DVbo, im2DIbo;
|
||||
static uint32 im2DVbo, im2DIbo;
|
||||
static int32 u_xform;
|
||||
|
||||
#define STARTINDICES 1024
|
||||
#define STARTVERTICES 1024
|
||||
#define STARTINDICES 10000
|
||||
#define STARTVERTICES 10000
|
||||
|
||||
static Shader *im2dShader;
|
||||
static AttribDesc attribDesc[3] = {
|
||||
{ ATTRIB_POS, GL_FLOAT, GL_FALSE, 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, r) },
|
||||
sizeof(Im2DVertex), offsetof(Im2DVertex, u) },
|
||||
};
|
||||
|
||||
static int primTypeMap[] = {
|
||||
@@ -42,8 +43,10 @@ static int primTypeMap[] = {
|
||||
};
|
||||
|
||||
void
|
||||
im2DInit(void)
|
||||
openIm2D(void)
|
||||
{
|
||||
u_xform = registerUniform("u_xform");
|
||||
|
||||
#include "shaders/im2d_gl3.inc"
|
||||
im2dShader = Shader::fromStrings(im2d_vert_src, im2d_frag_src);
|
||||
|
||||
@@ -65,6 +68,11 @@ 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);
|
||||
@@ -73,9 +81,15 @@ im2DRenderIndexedPrimitive(PrimitiveType primType,
|
||||
glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Im2DVertex),
|
||||
vertices, GL_DYNAMIC_DRAW);
|
||||
|
||||
setAttribPointers(attribDesc, 3);
|
||||
im2dShader->use();
|
||||
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();
|
||||
|
||||
@@ -23,9 +23,27 @@ int32 nativeRasterOffset;
|
||||
void
|
||||
rasterCreate(Raster *raster)
|
||||
{
|
||||
switch(raster->type){
|
||||
case Raster::CAMERA:
|
||||
// TODO: set/check width, height, depth, format?
|
||||
raster->flags |= Raster::DONTALLOCATE;
|
||||
break;
|
||||
case Raster::ZBUFFER:
|
||||
// TODO: set/check width, height, depth, format?
|
||||
raster->flags |= Raster::DONTALLOCATE;
|
||||
break;
|
||||
case Raster::TEXTURE:
|
||||
// continue below
|
||||
break;
|
||||
default:
|
||||
assert(0 && "unsupported format");
|
||||
}
|
||||
|
||||
if(raster->flags & Raster::DONTALLOCATE)
|
||||
return;
|
||||
|
||||
assert(raster->type == Raster::TEXTURE);
|
||||
|
||||
#ifdef RW_OPENGL
|
||||
Gl3Raster *natras = PLUGINOFFSET(Gl3Raster, raster, nativeRasterOffset);
|
||||
switch(raster->format & 0xF00){
|
||||
@@ -54,11 +72,10 @@ rasterCreate(Raster *raster)
|
||||
|
||||
glGenTextures(1, &natras->texid);
|
||||
glBindTexture(GL_TEXTURE_2D, natras->texid);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, natras->internalFormat,
|
||||
raster->width, raster->height,
|
||||
0, natras->format, natras->type, nil);
|
||||
natras->filterAddressing = ~0;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
#endif
|
||||
|
||||
@@ -78,11 +78,23 @@ struct InstanceDataHeader : rw::InstanceDataHeader
|
||||
InstanceData *inst;
|
||||
};
|
||||
|
||||
#ifdef RW_GL3
|
||||
|
||||
struct Im2DVertex
|
||||
{
|
||||
float32 x, y, z;
|
||||
float32 x, y, z, w;
|
||||
uint8 r, g, b, a;
|
||||
float32 u, v;
|
||||
|
||||
void setScreenX(float32 x) { this->x = x; }
|
||||
void setScreenY(float32 y) { this->y = y; }
|
||||
void setScreenZ(float32 z) { this->z = z; }
|
||||
void setCameraZ(float32 z) { this->w = z; }
|
||||
void setRecipCameraZ(float32 recipz) { }
|
||||
void setColor(uint8 r, uint8 g, uint8 b, uint8 a) {
|
||||
this->r = r; this->g = g; this->b = b; this->a = a; }
|
||||
void setU(float32 u) { this->u = u; }
|
||||
void setV(float32 v) { this->v = v; }
|
||||
};
|
||||
|
||||
void setAttribPointers(AttribDesc *attribDescs, int32 numAttribs);
|
||||
@@ -105,6 +117,8 @@ void setTexture(int32 n, Texture *tex);
|
||||
|
||||
void flushCache(void);
|
||||
|
||||
#endif
|
||||
|
||||
class ObjPipeline : public rw::ObjPipeline
|
||||
{
|
||||
public:
|
||||
@@ -136,6 +150,8 @@ struct Gl3Raster
|
||||
uint32 texid;
|
||||
|
||||
bool32 hasAlpha;
|
||||
// cached filtermode and addressing
|
||||
uint32 filterAddressing;
|
||||
};
|
||||
|
||||
void registerNativeRaster(void);
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace gl3 {
|
||||
|
||||
extern Shader *simpleShader;
|
||||
extern uint32 im2DVbo, im2DIbo;
|
||||
void im2DInit(void);
|
||||
void openIm2D(void);
|
||||
void im2DRenderIndexedPrimitive(PrimitiveType primType,
|
||||
void *vertices, int32 numVertices, void *indices, int32 numIndices);
|
||||
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
#version 330
|
||||
|
||||
layout(std140) uniform Im2DState
|
||||
{
|
||||
int u_alphaTest;
|
||||
float u_alphaRef;
|
||||
mat4 u_xform;
|
||||
};
|
||||
//layout(std140) uniform Im2DState
|
||||
//{
|
||||
// int u_alphaTest;
|
||||
// float u_alphaRef;
|
||||
// mat4 u_xform;
|
||||
//};
|
||||
|
||||
layout(location = 0) in vec3 in_pos;
|
||||
uniform vec4 u_xform;
|
||||
|
||||
layout(location = 0) in vec4 in_pos;
|
||||
layout(location = 2) in vec4 in_color;
|
||||
layout(location = 3) in vec2 in_tex0;
|
||||
|
||||
@@ -17,7 +19,9 @@ out vec2 v_tex0;
|
||||
void
|
||||
main(void)
|
||||
{
|
||||
gl_Position = vec4(in_pos, 1.0);
|
||||
gl_Position = in_pos;
|
||||
gl_Position.xy = gl_Position.xy * u_xform.xy + u_xform.zw;
|
||||
gl_Position.xyz *= gl_Position.w;
|
||||
v_color = in_color;
|
||||
v_tex0 = in_tex0;
|
||||
}
|
||||
|
||||
@@ -1,14 +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 Im2DState\n"
|
||||
"//{\n"
|
||||
"// int u_alphaTest;\n"
|
||||
"// float u_alphaRef;\n"
|
||||
"// mat4 u_xform;\n"
|
||||
"//};\n"
|
||||
|
||||
"layout(location = 0) in vec3 in_pos;\n"
|
||||
"uniform vec4 u_xform;\n"
|
||||
|
||||
"layout(location = 0) in vec4 in_pos;\n"
|
||||
"layout(location = 2) in vec4 in_color;\n"
|
||||
"layout(location = 3) in vec2 in_tex0;\n"
|
||||
|
||||
@@ -18,7 +20,9 @@ const char *im2d_vert_src =
|
||||
"void\n"
|
||||
"main(void)\n"
|
||||
"{\n"
|
||||
" gl_Position = vec4(in_pos, 1.0);\n"
|
||||
" gl_Position = in_pos;\n"
|
||||
" gl_Position.xy = gl_Position.xy * u_xform.xy + u_xform.zw;\n"
|
||||
" gl_Position.xyz *= gl_Position.w;\n"
|
||||
" v_color = in_color;\n"
|
||||
" v_tex0 = in_tex0;\n"
|
||||
"}\n"
|
||||
|
||||
Reference in New Issue
Block a user