about summary refs log tree commit diff
path: root/src/decal.zig
blob: 76338805cd691c6d28f4dce029c118c6b17bb2f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Decal construction and drawing
// Copyright (C) 2002  David Rosen
// Copyright (C) 2003  Steven Fuller
// Copyright (C) 2023  Nguyễn Gia Phong
//
// This file is part of Black Shades.
//
// Black Shades is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Black Shades is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Black Shades.  If not, see <https://www.gnu.org/licenses/>.

const Child = std.meta.Child;
const Model = model.Model;
const XYZ = geom.XYZ;
const assert = std.debug.assert;
const c = @import("cimport.zig");
const geom = @import("geom.zig");
const model = @import("model.zig");
const norm = geom.norm;
const segCrossModelTrans = model.segCrossModelTrans;
const segCrossTrigon = geom.segCrossTrigon;
const splat = geom.splat;
const std = @import("std");

const ones: @Vector(3, f32) = @splat(1.0);
const max_len = 120;

const Kind = enum(c_int) { bullet_hole, crater, blood_pool };

const Decals = extern struct {
    // GLuint is always 32-bit.
    hole_textures: [2]u32,
    blood_textures: [11]u32,
    len: u32,
    kind: [max_len]Kind,
    points: [max_len * 8]XYZ,
    numpoints: [max_len]u32,
    texcoordsx: [max_len * 8]f32,
    texcoordsy: [max_len * 8]f32,
    alive: [max_len]f32,
};

fn cross(u: @Vector(3, f32), v: @Vector(3, f32)) @Vector(3, f32) {
    return .{
        u[1] * v[2] - u[2] * v[1],
        u[2] * v[0] - u[0] * v[2],
        u[0] * v[1] - u[1] * v[0],
    };
}

fn normalize(vector: anytype, unit: Child(@TypeOf(vector))) @TypeOf(vector) {
    const d = norm(vector) / unit;
    if (d == 0) return vector;
    return vector / @as(@TypeOf(vector), @splat(d));
}

fn pointTouchModel(p: @Vector(3, f32), n: @Vector(3, f32),
                   m: *const Model, move: XYZ, rot: f32) bool {
    var temp: XYZ = undefined;
    return segCrossModelTrans(@bitCast(p + n), @bitCast(p - n),
                              m, move, rot, &temp) > -1;
}

export fn addDecal(d: *Decals, kind: Kind, location: XYZ,
                   size: f32, normal: XYZ, poly: c_int,
                   m: *const Model, move: XYZ, rot: f32) void {
    if (d.len >= max_len) return;
    const n: @Vector(3, f32) = @bitCast(normal);
    const abs_n = @fabs(n);
    var major: u2 = 0;
    if (abs_n[1] > abs_n[major])
        major = 1;
    if (abs_n[2] > abs_n[major])
        major = 2;

    const r: @Vector(3, f32) = if (@reduce(.And, abs_n != ones))
        cross(switch (major) {
            0 => .{ 1.0, 0.0, 0.0 },
            1 => .{ 0.0, 1.0, 0.0 },
            2 => .{ 0.0, 0.0, 1.0 },
            else => unreachable,
        }, n)
    else if (major == 0 and normal.x > 0 or major == 1)
        .{ 0.0, 0.0, -1.0 }
    else if (major == 0)
        .{ 0.0, 0.0, 1.0 }
    else
        .{ normal.z, 0.0, 0.0 };
    const up = normalize(cross(n, r), size / 3.0);
    const right = normalize(r, size / 3.0);
    const loc: @Vector(3, f32) = @bitCast(location);
    const face = m.faces[@intCast(poly)];
    const n_eps = n * splat(3, @as(f32, 0.02));
    const n_eps2 = n * splat(3, @as(f32, 0.04));

    d.kind[d.len] = kind;
    d.numpoints[d.len] = 0;
    d.alive[d.len] = 0;
    for ([_]f32{ -1, 1, 1, -1 }, [_]f32{ -1, -1, 1, 1 }) |x, y| {
        var p = loc + right * splat(3, x) + up * splat(3, y);
        var i = d.len * 8 + d.numpoints[d.len];
        var temp: XYZ = undefined;
        if (move.x == 0 and move.y == 0 and move.z == 0 and rot == 0
            or segCrossTrigon(@bitCast(p + n_eps2), @bitCast(p - n_eps2),
                              &face[0].position, &face[1].position,
                              &face[2].position, &normal, &temp)) {
            d.texcoordsx[i] = x * 0.5 + 0.5;
            d.texcoordsy[i] = y * 0.5 + 0.5;
            d.points[i] = @bitCast(p + n_eps);
            d.numpoints[d.len] += 1;
            continue;
        }

        const count_inc = @max(0.01, @min(1.0 / size, 0.2));
        var good: bool = false;
        var count = 1.0 - count_inc;
        while (!good and count > -1.0) : (count -= count_inc) {
            d.texcoordsx[i] = x * 0.5 + 0.5;
            d.texcoordsy[i] = y * count * 0.5 + 0.5;
            p = loc + right * splat(3, x) + up * splat(3, y * count);
            good = pointTouchModel(p, n_eps2, m, move, rot);
        }
        if (good) {
            d.points[i] = @bitCast(p + n_eps);
            d.numpoints[d.len] += 1;
            i += 1;
        }

        good = false;
        count = 1.0 - count_inc;
        while (!good and count > -1.0) : (count -= count_inc) {
            d.texcoordsx[i] = x * count * 0.5 + 0.5;
            d.texcoordsy[i] = y * 0.5 + 0.5;
            p = loc + right * splat(3, x * count) + up * splat(3, y);
            good = pointTouchModel(p, n_eps2, m, move, rot);
	}
        if (good) {
            d.points[i] = @bitCast(p + n_eps);
            d.numpoints[d.len] += 1;
            continue;
        }

        var count2 = 1.0 - count_inc;
        while (!good and count2 > -1.0) : (count2 -= count_inc) {
            count = 1.0 - count_inc;
            while (!good and count > -1.0) : (count -= count_inc) {
                d.texcoordsx[i] = x * count2 * 0.5 + 0.5;
                d.texcoordsy[i] = y * count * 0.5 + 0.5;
                p = loc + right * splat(3, x * count2)
                        + up * splat(3, y * count);
                good = pointTouchModel(p, n_eps2, m, move, rot);
            }
        }
        if (good) {
            d.points[i] = @bitCast(p + n_eps);
            d.numpoints[d.len] += 1;
        }
    }
    d.len += 1;
}

export fn drawDecals(d: *const Decals) void {
    c.glAlphaFunc(c.GL_GREATER, 0.01);
    c.glDepthFunc(c.GL_LEQUAL);
    c.glEnable(c.GL_BLEND);
    c.glEnable(c.GL_CULL_FACE);
    c.glEnable(c.GL_TEXTURE_2D);
    c.glEnable(c.GL_LIGHTING);
    c.glDepthMask(0);
    c.glAlphaFunc(c.GL_GREATER, 0.01);
    c.glTexParameterf(c.GL_TEXTURE_2D, c.GL_TEXTURE_WRAP_S, c.GL_CLAMP);
    c.glTexParameterf(c.GL_TEXTURE_2D, c.GL_TEXTURE_WRAP_T, c.GL_CLAMP);
    c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA);
    c.glEnable(c.GL_POLYGON_OFFSET_FILL);
    for (0..d.len) |i| {
        switch (d.kind[i]) {
            .bullet_hole, .crater => |k| {
                c.glColor4f(1.0, 1.0, 1.0, 1.0 - d.alive[i] / 10.0);
                c.glBindTexture(c.GL_TEXTURE_2D, d.hole_textures[switch (k) {
                    .bullet_hole => 0,
                    .crater => 1,
                    // https://github.com/ziglang/zig/issues/12863
                    else => unreachable,  // TODO: remove
                }]);
            },
            .blood_pool => {
                const alpha = if (d.alive[i] < 2.0)
                    @mod(d.alive[i], 0.2) + 0.8
                else
                    (20.0 - d.alive[i]) / 18.0;
                c.glColor4f(1.0, 1.0, 1.0, alpha);
                const j: usize = @intFromFloat(d.alive[i] * 5.0);
                c.glBindTexture(c.GL_TEXTURE_2D, d.blood_textures[@min(j, 10)]);
            },
        }

        c.glPushMatrix();
        c.glBegin(c.GL_TRIANGLE_FAN);
        for (0..d.numpoints[i]) |j| {
            c.glTexCoord2f(d.texcoordsx[i * 8 + j], d.texcoordsy[i * 8 + j]);
            c.glVertex3f(d.points[i * 8 + j].x,
                d.points[i * 8 + j].y, d.points[i * 8 + j].z);
        }
        c.glEnd();
        c.glPopMatrix();
    }
    c.glDepthMask(1);
    c.glDisable(c.GL_TEXTURE_2D);
    c.glEnable(c.GL_CULL_FACE);
    c.glDisable(c.GL_POLYGON_OFFSET_FILL);
    c.glDepthFunc(c.GL_LEQUAL);
}

export fn updateDecals(d: *Decals) void {
    for (0..d.len) |i| {
        d.alive[i] += c.multiplier;
        if (d.alive[i] < @as(f32, switch (d.kind[i]) {
            .bullet_hole, .crater => 10.0,
            .blood_pool => 20.0,
        })) continue;

        d.len -= 1;
        const last = d.len;
        d.numpoints[i] = d.numpoints[last];
        d.alive[i] = d.alive[last];
        d.kind[i] = d.kind[last];
        for (0..d.numpoints[i]) |j| {
            d.points[i * 8 + j] = d.points[last * 8 + j];
            d.texcoordsx[i * 8 + j] = d.texcoordsx[last * 8 + j];
            d.texcoordsy[i * 8 + j] = d.texcoordsy[last * 8 + j];
        }
    }
}

export fn destroyDecals(d: *const Decals) void {
    c.glDeleteTextures(2, &d.hole_textures);
    c.glDeleteTextures(11, &d.blood_textures);
}