From cfc056a336706ef4bbf8a9bace4525db06cb6f3b Mon Sep 17 00:00:00 2001 From: XProger Date: Sun, 23 Apr 2017 04:39:26 +0300 Subject: [PATCH] #23 ambient occlusion blob --- src/controller.h | 9 +++---- src/core.h | 4 +++- src/lara.h | 2 +- src/mesh.h | 53 +++++++++++++++++++++++++++++------------ src/shaders/shader.glsl | 15 ++++++------ 5 files changed, 55 insertions(+), 28 deletions(-) diff --git a/src/controller.h b/src/controller.h index 6414a5c..857f3c9 100644 --- a/src/controller.h +++ b/src/controller.h @@ -543,10 +543,11 @@ struct Controller { game->setShader(Core::pass, Shader::FLASH, false, false); Core::active.shader->setParam(uViewProj, m); Core::active.shader->setParam(uBasis, b); - Core::active.shader->setParam(uMaterial, vec4(0.0f, 0.0f, 0.0f, 0.5f)); + float alpha = lerp(0.7f, 0.90f, clamp((pos.y - this->pos.y) / 1024.0f, 0.0f, 1.0f) ); + Core::active.shader->setParam(uMaterial, vec4(vec3(0.5f * (1.0f - alpha)), alpha)); Core::active.shader->setParam(uAmbient, vec3(0.0f)); - Core::setBlending(bmAlpha); + Core::setBlending(bmMultiply); mesh->renderShadowBlob(); Core::setBlending(bmNone); @@ -594,10 +595,10 @@ struct Controller { frameIndex = Core::stats.frame; // blob shadow // TODO: fake AO - if (!Core::settings.shadows && Core::pass == Core::passCompose && TR::castShadow(entity.type)) { + if (Core::pass == Core::passCompose && TR::castShadow(entity.type)) { TR::Level::FloorInfo info; level->getFloorInfo(entity.room, entity.x, entity.y, entity.z, info); - renderShadow(mesh, vec3(float(entity.x), info.floor - 16.0f, float(entity.z)), box.center(), box.size() * 0.8f, angle.y); + renderShadow(mesh, vec3(float(entity.x), info.floor - 16.0f, float(entity.z)), box.center(), box.size(), angle.y); } } }; diff --git a/src/core.h b/src/core.h index 2443408..7aa5a27 100644 --- a/src/core.h +++ b/src/core.h @@ -490,8 +490,10 @@ namespace Core { Sound::init(); - for (int i = 0; i < MAX_LIGHTS; i++) + for (int i = 0; i < MAX_LIGHTS; i++) { + lightPos[i] = vec3(0.0); lightColor[i] = vec4(0, 0, 0, 1); + } eye = 0.0f; uint32 data = 0x00000000; diff --git a/src/lara.h b/src/lara.h index a04ae17..3316370 100644 --- a/src/lara.h +++ b/src/lara.h @@ -407,7 +407,7 @@ struct Lara : Character { if (level->extra.braid > -1) braid = new Braid(this, vec3(-4.0f, 24.0f, -48.0f)); -reset(15, vec3(70067, -256, 29104), -0.68f); // level 2 (pool) +//reset(15, vec3(70067, -256, 29104), -0.68f); // level 2 (pool) #ifdef _DEBUG //reset(14, vec3(40448, 3584, 60928), PI * 0.5f, true); // gym (pool) diff --git a/src/mesh.h b/src/mesh.h index 4853691..806350c 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -246,9 +246,9 @@ struct MeshBuilder { // shadow blob mesh (8 triangles, 8 vertices) shadowBlob.vStart = vCount; shadowBlob.iStart = iCount; - shadowBlob.iCount = 8 * 3; + shadowBlob.iCount = 8 * 3 * 3; iCount += shadowBlob.iCount; - vCount += 8; + vCount += 8 * 2 + 1; // shadow box (for stencil shadow volumes with degenerate triangles) shadowBox.vStart = vCount; @@ -370,23 +370,46 @@ struct MeshBuilder { aCount += level.spriteSequencesCount; // build shadow blob - for (int i = 0; i < 8; i++) { - Vertex &v = vertices[vCount + i]; - v.normal = { 0, -1, 0, 1 }; - v.color = { 255, 255, 255, 0 }; - v.texCoord = { 32688, 32688, 0, 0 }; + for (int i = 0; i < 9; i++) { + Vertex &v0 = vertices[vCount + i * 2 + 0]; + v0.normal = { 0, -1, 0, 1 }; + v0.texCoord = { 32688, 32688, 0, 0 }; + v0.color = { 0, 0, 0, 0 }; + + if (i == 8) { + v0.coord = { 0, 0, 0, 0 }; + break; + } float a = i * (PI / 4.0f) + (PI / 8.0f); - short c = short(cosf(a) * 512.0f); - short s = short(sinf(a) * 512.0f); - v.coord = { c, 0, s, 0 }; + float c = cosf(a); + float s = sinf(a); + short c0 = short(c * 256.0f); + short s0 = short(s * 256.0f); + short c1 = short(c * 512.0f); + short s1 = short(s * 512.0f); + v0.coord = { c0, 0, s0, 0 }; - int idx = iCount + i * 3; - indices[idx + 0] = i; - indices[idx + 1] = 0; - indices[idx + 2] = (i + 1) % 8; + Vertex &v1 = vertices[vCount + i * 2 + 1]; + v1 = v0; + v1.coord = { c1, 0, s1, 0 }; + v1.color = { 255, 255, 255, 0 }; + + int idx = iCount + i * 3 * 3; + int j = ((i + 1) % 8) * 2; + indices[idx++] = i * 2; + indices[idx++] = 8 * 2; + indices[idx++] = j; + + indices[idx++] = i * 2 + 1; + indices[idx++] = i * 2; + indices[idx++] = j; + + indices[idx++] = i * 2 + 1; + indices[idx++] = j; + indices[idx++] = j + 1; } - vCount += 8; + vCount += 8 * 2 + 1; iCount += shadowBlob.iCount; aCount++; diff --git a/src/shaders/shader.glsl b/src/shaders/shader.glsl index f724889..7ba2b5a 100644 --- a/src/shaders/shader.glsl +++ b/src/shaders/shader.glsl @@ -10,6 +10,8 @@ varying vec4 vTexCoord; // xy - atlas coords, zw - caustics coords varying vec4 vViewVec; // xyz - dir * dist, w - coord.y uniform vec3 uViewPos; + varying vec4 vDiffuse; + #ifndef TYPE_FLASH #ifdef PASS_COMPOSE varying vec4 vNormal; // xyz - normal dir, w - fog factor @@ -24,7 +26,6 @@ varying vec4 vTexCoord; // xy - atlas coords, zw - caustics coords uniform vec4 uLightColor[4]; // xyz - color, w - radius * intensity #endif - varying vec4 vDiffuse; varying vec4 vLight; // 4 lights intensity #if defined(OPT_WATER) && defined(UNDERWATER) @@ -132,7 +133,7 @@ varying vec4 vTexCoord; // xy - atlas coords, zw - caustics coords } void _diffuse() { - #if !defined(PASS_SHADOW) && !defined(TYPE_FLASH) + #if !defined(PASS_SHADOW) vDiffuse = vec4(aColor.xyz * (uMaterial.x * 2.0), uMaterial.w); #ifdef UNDERWATER @@ -142,6 +143,10 @@ varying vec4 vTexCoord; // xy - atlas coords, zw - caustics coords #ifdef TYPE_MIRROR vDiffuse.xyz *= vec3(0.3, 0.3, 2.0); // blue color dodge for crystal #endif + + #ifdef TYPE_FLASH + vDiffuse.xyz += uMaterial.w; + #endif #endif } @@ -388,11 +393,7 @@ varying vec4 vTexCoord; // xy - atlas coords, zw - caustics coords #endif #ifndef PASS_SHADOW - #ifdef TYPE_FLASH - color *= uMaterial.xxxw; - #else - color *= vDiffuse; - #endif + color *= vDiffuse; #endif #ifdef PASS_SHADOW