1
0
mirror of https://github.com/XProger/OpenLara.git synced 2025-08-11 15:45:05 +02:00

SW renderer fog

This commit is contained in:
XProger
2019-10-16 03:54:25 +03:00
parent 6bb66936b5
commit 582cbaf65d
2 changed files with 31 additions and 22 deletions

View File

@@ -24,6 +24,9 @@
#define CONV_COLOR(r,g,b) ((r << 16) | (g << 8) | b) #define CONV_COLOR(r,g,b) ((r << 16) | (g << 8) | b)
#endif #endif
#define SW_MAX_DIST (20.0f * 1024.0f)
#define SW_FOG_START (12.0f * 1024.0f)
namespace GAPI { namespace GAPI {
using namespace Core; using namespace Core;
@@ -466,9 +469,6 @@ namespace GAPI {
VertexSW *b = swVertices + indices[2]; VertexSW *b = swVertices + indices[2];
VertexSW *n = &_n; VertexSW *n = &_n;
if (t->w <= 0 || m->w <= 0 || b->w <= 0)
return;
if (checkBackface(t, m, b)) if (checkBackface(t, m, b))
return; return;
@@ -518,9 +518,6 @@ namespace GAPI {
VertexSW *n = &_n; VertexSW *n = &_n;
VertexSW *p = &_p; VertexSW *p = &_p;
if (t->w <= 0 || m->w <= 0 || o->w <= 0 || b->w <= 0)
return;
if (checkBackface(t, m, b)) if (checkBackface(t, m, b))
return; return;
@@ -570,7 +567,7 @@ namespace GAPI {
if (o->y != b->y) drawPart(*p, *o, *b, *b); if (o->y != b->y) drawPart(*p, *o, *b, *b);
} }
void applyLighting(VertexSW &result, const Vertex &vertex) { void applyLighting(VertexSW &result, const Vertex &vertex, float depth) {
vec3 coord = vec3(vertex.coord); vec3 coord = vec3(vertex.coord);
vec3 normal = vec3(vertex.normal).normal(); vec3 normal = vec3(vertex.normal).normal();
float lighting = 0.0f; float lighting = 0.0f;
@@ -582,8 +579,14 @@ namespace GAPI {
lighting += (max(0.0f, lum) * max(0.0f, 1.0f - att)) * light.intensity; lighting += (max(0.0f, lum) * max(0.0f, 1.0f - att)) * light.intensity;
} }
result.l = (255 - min(255, result.l + int32(lighting))) << 16; lighting += result.l;
// result.l = (255 - int32(lighting)) << 16;
depth -= SW_FOG_START;
if (depth > 0.0f) {
lighting *= clamp(1.0f - depth / (SW_MAX_DIST - SW_FOG_START), 0.0f, 1.0f);
}
result.l = (255 - min(255, int32(lighting))) << 16;
} }
bool transform(const Index *indices, const Vertex *vertices, int iStart, int iCount, int vStart) { bool transform(const Index *indices, const Vertex *vertices, int iStart, int iCount, int vStart) {
@@ -596,9 +599,9 @@ namespace GAPI {
swMatrix.viewport(0.0f, (float)Core::height, (float)Core::width, -(float)Core::height, 0.0f, 1.0f); swMatrix.viewport(0.0f, (float)Core::height, (float)Core::width, -(float)Core::height, 0.0f, 1.0f);
swMatrix = swMatrix * mViewProj * mModel; swMatrix = swMatrix * mViewProj * mModel;
const bool colored = vertices[vStart + indices[iStart]].color.w == 142;
int vIndex = 0; int vIndex = 0;
bool isTriangle = false; bool isTriangle = false;
bool colored = false;
for (int i = 0; i < iCount; i++) { for (int i = 0; i < iCount; i++) {
const Index index = indices[iStart + i]; const Index index = indices[iStart + i];
@@ -608,19 +611,27 @@ namespace GAPI {
if (vIndex == 1) { if (vIndex == 1) {
isTriangle = vertex.normal.w == 1; isTriangle = vertex.normal.w == 1;
} } else {
if (vIndex == 4) { // loader splits quads to two triangles with indices 012[02]3, we ignore [02] to make it quad again!
if (vertex.color.w == 142) { vIndex++;
colored = true; i++;
} continue;
}
if (!isTriangle && (vIndex == 4 || vIndex == 5)) { // loader splits quads to two triangles with indices 012[02]3, we ignore [02] to make it quad again!
continue;
} }
vec4 c; vec4 c;
c = swMatrix * vec4(vertex.coord.x, vertex.coord.y, vertex.coord.z, 1.0f); c = swMatrix * vec4(vertex.coord.x, vertex.coord.y, vertex.coord.z, 1.0f);
if (c.w < 0.0f || c.w > SW_MAX_DIST) { // skip primitive
if (isTriangle) {
i += 3 - vIndex;
} else {
i += 6 - vIndex;
}
vIndex = 0;
continue;
}
c.x /= c.w; c.x /= c.w;
c.y /= c.w; c.y /= c.w;
c.z /= c.w; c.z /= c.w;
@@ -643,12 +654,10 @@ namespace GAPI {
result.w = result.w << 16; result.w = result.w << 16;
result.l = ((vertex.light.x * ambient) >> 8); result.l = ((vertex.light.x * ambient) >> 8);
applyLighting(result, vertex); applyLighting(result, vertex, c.w);
swIndices.push(swVertices.push(result)); swIndices.push(swVertices.push(result));
ASSERT(!(isTriangle && vIndex > 3));
if (isTriangle && vIndex == 3) { if (isTriangle && vIndex == 3) {
swTriangles.push(swIndices.length - 3); swTriangles.push(swIndices.length - 3);
vIndex = 0; vIndex = 0;

View File

@@ -280,7 +280,7 @@ enum StringID {
const char *helpText = const char *helpText =
"Start - add second player or restore Lara@" "Start - add second player or restore Lara@"
"H - Show or hide this help@" "H - Show or hide this help@"
"ALT + ENTER - Fullscreen@" "ALT and ENTER - Fullscreen@"
"5 - Save Game@" "5 - Save Game@"
"9 - Load Game@" "9 - Load Game@"
"C - Look@" "C - Look@"