1
0
mirror of https://github.com/XProger/OpenLara.git synced 2025-04-20 19:11:50 +02:00

resolved #6 billboard sprites for items, affecting from room & entity intensity, #10 add simple and buggy caustics

This commit is contained in:
XProger 2016-09-04 14:43:23 +03:00
parent 705e6b2c0d
commit 6a26a4eadd
4 changed files with 133 additions and 71 deletions

Binary file not shown.

View File

@ -16,7 +16,7 @@ const char SHADER[] =
;
struct Level {
enum { shStatic, shSprite, shMAX };
enum { shStatic, shCaustics, shSprite, shMAX };
TR::Level level;
Shader *shaders[shMAX];
@ -31,6 +31,7 @@ struct Level {
MeshRange geometry;
MeshRange sprites;
} *roomRanges;
MeshRange *spriteRanges;
Camera camera;
@ -42,8 +43,10 @@ struct Level {
} *meshInfo;
Level(Stream &stream) : level{stream}, time(0.0f) {
shaders[shStatic] = new Shader(SHADER);
shaders[shSprite] = new Shader(SHADER, "#define SPRITE\n");
shaders[shStatic] = new Shader(SHADER);
shaders[shCaustics] = new Shader(SHADER, "#define CAUSTICS\n");
shaders[shSprite] = new Shader(SHADER, "#define SPRITE\n");
initAtlas();
initMesh();
@ -60,7 +63,7 @@ struct Level {
camera.fov = 75.0f;
camera.znear = 0.1f * 2048.0f;
camera.zfar = 1000.0f * 2048.0f;
camera.offset = vec3(0, 0, 768);
camera.offset = vec3(0, 0, 1024);
camera.deltaPos = vec3(0.0f, 768.0f, 0.0f);
camera.deltaAngle = vec3(0.0f, PI, 0.0f);
camera.angle = vec3(0.0f);
@ -73,6 +76,7 @@ struct Level {
delete mesh;
delete[] roomRanges;
delete[] meshInfo;
delete[] spriteRanges;
delete lara;
}
@ -112,6 +116,47 @@ struct Level {
delete[] data;
}
void addSprite(Index *indices, Vertex *vertices, int &iCount, int &vCount, int vStart, int16 x, int16 y, int16 z, const TR::SpriteTexture &sprite, uint8 intensity) {
int vIndex = vCount - vStart;
indices[iCount + 0] = vIndex + 0;
indices[iCount + 1] = vIndex + 1;
indices[iCount + 2] = vIndex + 2;
indices[iCount + 3] = vIndex + 0;
indices[iCount + 4] = vIndex + 2;
indices[iCount + 5] = vIndex + 3;
iCount += 6;
Vertex *quad = &vertices[vCount];
quad[0].coord = quad[1].coord = quad[2].coord = quad[3].coord = { x, y, z };
int tx = (sprite.tile % 4) * 256;
int ty = (sprite.tile / 4) * 256;
int16 u0 = ((tx + sprite.u) << 5) + 16;
int16 v0 = ((ty + sprite.v) << 5) + 16;
int16 u1 = u0 + (sprite.w >> 3);
int16 v1 = v0 + (sprite.h >> 3);
quad[0].texCoord = { u0, v0 };
quad[1].texCoord = { u1, v0 };
quad[2].texCoord = { u1, v1 };
quad[3].texCoord = { u0, v1 };
quad[0].normal = { sprite.r, sprite.t, 0, 0 };
quad[1].normal = { sprite.l, sprite.t, 0, 0 };
quad[2].normal = { sprite.l, sprite.b, 0, 0 };
quad[3].normal = { sprite.r, sprite.b, 0, 0 };
quad[0].color = quad[1].color = quad[2].color = quad[3].color = { intensity, intensity, intensity, 255 };
vCount += 4;
}
void initMesh() {
// TODO: sort by texture attribute (t.attribute == 2 ? bmAdd : bmAlpha)
@ -119,7 +164,7 @@ struct Level {
int iCount = 0, vCount = 0;
// get rooms mesh info
// get size of mesh for rooms (geometry & sprites)
for (int i = 0; i < level.roomsCount; i++) {
TR::Room::Data &d = level.rooms[i].data;
RoomRange &r = roomRanges[i];
@ -168,13 +213,25 @@ struct Level {
OFFSET(ptr->ctCount * sizeof(TR::Triangle) + sizeof(TR::Mesh));
ptr = (TR::Mesh*)(((int)ptr + 3) & -4);
}
meshInfo = new MeshInfo[mCount];
// get size of mesh for sprite sequences
spriteRanges = new MeshRange[level.spriteSequencesCount];
for (int i = 0; i < level.spriteSequencesCount; i++) {
// TODO: sequences not only first frame
spriteRanges[i].vStart = vCount;
spriteRanges[i].iStart = iCount;
spriteRanges[i].iCount = 6;
iCount += 6;
vCount += 4;
}
// make meshes buffer (single vertex buffer object for all geometry & sprites on level)
Index *indices = new Index[iCount];
Vertex *vertices = new Vertex[vCount];
iCount = vCount = 0;
// build rooms
for (int i = 0; i < level.roomsCount; i++) {
TR::Room::Data &d = level.rooms[i].data;
@ -246,53 +303,15 @@ struct Level {
TR::Room::Info &info = level.rooms[i].info;
vStart = vCount;
for (int j = 0; j < d.sCount; j++) {
auto &f = d.sprites[j];
int vIndex = vCount - vStart;
indices[iCount + 0] = vIndex + 0;
indices[iCount + 1] = vIndex + 1;
indices[iCount + 2] = vIndex + 2;
indices[iCount + 3] = vIndex + 0;
indices[iCount + 4] = vIndex + 2;
indices[iCount + 5] = vIndex + 3;
iCount += 6;
TR::Room::Data::Sprite &f = d.sprites[j];
TR::Room::Data::Vertex &v = d.vertices[f.vertex];
TR::SpriteTexture &sprite = level.spriteTextures[f.texture];
Vertex *quad = &vertices[vCount];
quad[0].coord = quad[1].coord = quad[2].coord = quad[3].coord = { v.vertex.x, v.vertex.y, v.vertex.z };
int tx = (sprite.tile % 4) * 256;
int ty = (sprite.tile / 4) * 256;
int16 u0 = ((tx + sprite.u) << 5) + 16;
int16 v0 = ((ty + sprite.v) << 5) + 16;
int16 u1 = u0 + (sprite.w >> 3);
int16 v1 = v0 + (sprite.h >> 3);
quad[0].texCoord = { u0, v0 };
quad[1].texCoord = { u1, v0 };
quad[2].texCoord = { u1, v1 };
quad[3].texCoord = { u0, v1 };
quad[0].normal = { sprite.r, sprite.t, 0, 0 };
quad[1].normal = { sprite.l, sprite.t, 0, 0 };
quad[2].normal = { sprite.l, sprite.b, 0, 0 };
quad[3].normal = { sprite.r, sprite.b, 0, 0 };
uint8 a = 255 - (v.lighting >> 5);
quad[0].color = quad[1].color = quad[2].color = quad[3].color = { a, a, a, 255 };
vCount += 4;
uint8 intensity = 255 - (v.lighting >> 5);
addSprite(indices, vertices, iCount, vCount, vStart, v.vertex.x, v.vertex.y, v.vertex.z, sprite, intensity);
}
}
// objects geometry
// build objects geometry
mCount = 0;
ptr = (TR::Mesh*)level.meshData;
while ( ((int)ptr - (int)level.meshData) < level.meshDataSize * sizeof(uint16) ) {
@ -474,6 +493,12 @@ struct Level {
info.iCount = iCount - info.iStart;
}
// build sprite sequences
for (int i = 0; i < level.spriteSequencesCount; i++) {
TR::SpriteTexture &sprite = level.spriteTextures[level.spriteSequences[i].sStart];
addSprite(indices, vertices, iCount, vCount, vCount, 0, -16, 0, sprite, 255);
}
mesh = new Mesh(indices, iCount, vertices, vCount);
delete[] indices;
delete[] vertices;
@ -486,6 +511,16 @@ struct Level {
return NULL;
}
Shader *setRoomShader(const TR::Room &room, float intensity) {
if (room.flags & TR::ROOM_FLAG_WATER) {
Core::color = vec4(0.6f * intensity, 0.9f * intensity, 0.9f * intensity, 1.0f);
return shaders[shCaustics];
} else {
Core::color = vec4(intensity, intensity, intensity, 1.0f);
return shaders[shStatic];
}
}
void renderRoom(int index) {
TR::Room &room = level.rooms[index];
@ -496,23 +531,26 @@ struct Level {
mat4 m = Core::mModel;
Core::mModel.translate(offset);
Core::color = vec4(1.0f);
Core::ambient = vec3(1.0f);
Core::lightColor = vec4(0.0f, 0.0f, 0.0f, 1.0f);
Shader *sh = shaders[shStatic];
Shader *sh = setRoomShader(room, 1.0f);
sh->bind();
sh->setParam(uModel, Core::mModel);
sh->setParam(uColor, Core::color);
sh->setParam(uAmbient, Core::ambient);
sh->setParam(uLightColor, Core::lightColor);
// render room geometry
mesh->render(roomRanges[index].geometry);
// render room sprites
if (roomRanges[index].sprites.iCount) {
sh = shaders[shSprite];
sh->bind();
sh->setParam(uModel, Core::mModel);
sh->setParam(uColor, Core::color);
mesh->render(roomRanges[index].sprites);
}
@ -742,7 +780,9 @@ struct Level {
void renderEntity(const TR::Entity &entity) {
// if (!(entity.flags & ENTITY_FLAG_VISIBLE))
// return;
if (!(level.rooms[entity.room].flags & TR::ROOM_FLAG_VISIBLE)) // check for room visibility
TR::Room &room = level.rooms[entity.room];
if (!(room.flags & TR::ROOM_FLAG_VISIBLE)) // check for room visibility
return;
mat4 m = Core::mModel;
@ -751,23 +791,36 @@ struct Level {
float c = (entity.intensity > -1) ? (1.0f - entity.intensity / (float)0x1FFF) : 1.0f;
float l = 1.0f;
Core::color = vec4(c, c, c, 1.0);
// set shader
setRoomShader(room, c)->bind();
Core::active.shader->setParam(uColor, Core::color);
// get light parameters for entity
getLight(vec3(entity.x, entity.y, entity.z), entity.room);
// render entity models (TODO: remapping or consider model and entity id's)
bool isModel = false;
for (int i = 0; i < level.modelsCount; i++)
if (entity.id == level.models[i].id) {
isModel = true;
renderModel(level.models[i], vec3(0, entity.rotation / 16384.0f * PI * 0.5f, 0));
break;
}
/*
for (int i = 0; i < level.spriteSequencesCount; i++)
if (entity.id == level.spriteSequences[i].id) {
renderSprite(level.spriteTextures[level.spriteSequences[i].sStart]);
break;
}
*/
// if entity is billboard
if (!isModel) {
Core::color = vec4(c, c, c, 1.0f);
shaders[shSprite]->bind();
Core::active.shader->setParam(uModel, Core::mModel);
Core::active.shader->setParam(uColor, Core::color);
for (int i = 0; i < level.spriteSequencesCount; i++)
if (entity.id == level.spriteSequences[i].id) {
mesh->render(spriteRanges[i]);
break;
}
}
Core::mModel = m;
}
@ -825,7 +878,8 @@ struct Level {
shaders[i]->bind();
shaders[i]->setParam(uViewProj, Core::mViewProj);
shaders[i]->setParam(uViewInv, Core::mViewInv);
shaders[i]->setParam(uViewPos, Core::viewPos);
shaders[i]->setParam(uViewPos, Core::viewPos);
shaders[i]->setParam(uParam, vec4(time, sinf(time * 1024.0f), 0, 0));
}
glEnable(GL_DEPTH_TEST);
@ -840,9 +894,6 @@ struct Level {
renderRoom(getCameraRoomIndex());
renderRoom(lara->getEntity().room);
//for (int i = 0; i < level.roomsCount; i++)
// renderRoom(i);
shaders[shStatic]->bind();
for (int i = 0; i < level.entitiesCount; i++)
renderEntity(level.entities[i]);

View File

@ -2,10 +2,10 @@ R"====(
#ifndef SPRITE
varying vec4 vNormal;
varying vec3 vLightVec;
varying vec3 vViewVec;
#endif
varying vec2 vTexCoord;
varying vec4 vColor;
varying vec3 vViewVec;
#ifdef VERTEX
uniform mat4 uViewProj;
@ -16,6 +16,10 @@ varying vec3 vViewVec;
#else
uniform mat4 uViewInv;
#endif
#ifdef CAUSTICS
uniform vec4 uParam;
#endif
attribute vec3 aCoord;
attribute vec2 aTexCoord;
@ -26,6 +30,12 @@ varying vec3 vViewVec;
vec4 coord = uModel * vec4(aCoord, 1.0);
vTexCoord = aTexCoord;
vColor = aColor;
#ifdef CAUSTICS
float sum = coord.x + coord.y + coord.z;
vColor.xyz *= abs(sin(sum / 512.0 + uParam.x)) * 0.75 + 0.25;
#endif
#ifndef SPRITE
vViewVec = uViewPos - coord.xyz;
vLightVec = uLightPos - coord.xyz;
@ -36,11 +46,12 @@ varying vec3 vViewVec;
gl_Position = uViewProj * coord;
}
#else
uniform sampler2D sDiffuse;
uniform sampler2D sDiffuse;
uniform vec4 uColor;
#ifndef SPRITE
uniform vec4 uColor;
uniform vec3 uAmbient;
uniform vec4 uLightColor;
uniform vec3 uAmbient;
uniform vec4 uLightColor;
#endif
void main() {
@ -48,8 +59,8 @@ varying vec3 vViewVec;
if (color.w < 0.9)
discard;
color *= vColor;
color *= uColor;
#ifndef SPRITE
color *= uColor;
color.xyz = pow(abs(color.xyz), vec3(2.2));
float lum = dot(normalize(vNormal.xyz), normalize(vLightVec));
float att = max(0.0, 1.0 - dot(vLightVec, vLightVec) / uLightColor.w);

View File

@ -5,11 +5,11 @@
enum AttribType { aCoord, aTexCoord, aNormal, aColor, aMAX };
enum SamplerType { sDiffuse, sMAX };
enum UniformType { uViewProj, uViewInv, uModel, uColor, uAmbient, uViewPos, uLightPos, uLightColor, uMAX };
enum UniformType { uViewProj, uViewInv, uModel, uColor, uAmbient, uViewPos, uLightPos, uLightColor, uParam, uMAX };
const char *AttribName[aMAX] = { "aCoord", "aTexCoord", "aNormal", "aColor" };
const char *SamplerName[sMAX] = { "sDiffuse" };
const char *UniformName[uMAX] = { "uViewProj", "uViewInv", "uModel", "uColor", "uAmbient", "uViewPos", "uLightPos", "uLightColor" };
const char *UniformName[uMAX] = { "uViewProj", "uViewInv", "uModel", "uColor", "uAmbient", "uViewPos", "uLightPos", "uLightColor", "uParam" };
struct Shader {
GLuint ID;