DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
water.cpp
1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3
4#include "water.hpp"
5
6#include <libpkt.h>
7
8#include "boxvu0.hpp"
9#include "dataalloc.hpp"
10#include "mglib.hpp"
11#include "texture.hpp"
12
13int SetTEX0(u_int *packet, u_long tex0, u_long tex1);
14#ifndef PORT
18static void pretest(float matrix[4][4], float *translation) {
19 register float *matrix_data = &matrix[0][0];
20 register float *offset = translation;
21
22 asm {
23 lqc2 vf10, 0(matrix_data)
24 lqc2 vf11, 16(matrix_data)
25 lqc2 vf12, 32(matrix_data)
26 lqc2 vf13, 48(matrix_data)
27 lqc2 vf14, 0(offset)
28 }
29}
30
34static void Trans_AddCell(float *output, float *position) {
35 register float *destination = output;
36 register float *source = position;
37
38 asm {
39 lqc2 vf16, 0(source)
40 vmulax ACC, vf10, vf16
41 vmadday ACC, vf11, vf16
42 vmaddaz ACC, vf12, vf16
43 vmaddw vf17, vf13, vf16
44 vadd.xz vf16, vf16, vf14
45 sqc2 vf17, 0(destination)
46 sqc2 vf16, 0(source)
47 }
48}
49#endif
50void CWater::SetParam(float speed, float damping_rate, float scale, float shift) {
51 wave_speed = speed;
52 damping = damping_rate;
53 height_scale = scale;
54 distortion = shift;
55}
56
57void CWater::SetColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha) {
58 color[0] = red;
59 color[1] = green;
60 color[2] = blue;
61 color[3] = alpha;
62}
63
64int CWater::CreateVUData(unsigned int *output, RenderInfo *info) {
65 float *above;
66 int word;
67 int i;
68 int k;
69 float row_f;
70 float *here;
71 sceVu0FVECTOR *out;
72 sceVu0FVECTOR *uv;
73 CTexture *texture;
74 int j;
75 float *below;
76 float *cell_above;
77 float *cell;
78 int count;
79 u_long128 *top;
80 int first;
81 u_long128 *bottom;
82 u_long128 *uv_top;
83 u_long128 *uv_bottom;
84 int vertex_count;
85 int qwc;
86 int unpack_word;
87 u_int nloop;
88 u_int *tag;
89 u_long128 *xyz;
90 u_long128 *rgbaq;
91 u_long128 *st;
92 word = 0;
93 sceGsTex0 tex0;
94 float rgba[4];
95
96 rgba[0] = color[0];
97 rgba[1] = color[1];
98 rgba[2] = color[2];
99 rgba[3] = color[3];
100 u_int end[4] = {0x11000000, 0, 0, 0};
101 float uv_base[4] = {320.0f, SCREEN_QUARTER_HEIGHT_F, 0.0f, 0.0f};
102 u_int first_kick[4] = {0, 0, 0, 0x14000000};
103 u_int kick[4] = {0, 0, 0, 0x17000000};
104 u_int unpack[4] = {0, 0, 0, 0x6C008000};
105 sceVu0FVECTOR row_step;
106 sceVu0FVECTOR column_step;
107 sceVu0FMATRIX local_to_world;
108 sceVu0FMATRIX local_to_screen;
109 sceVu0FVECTOR vertices[64][64];
110 sceVu0FVECTOR uvs[64][64];
111 sceVu0FVECTOR position;
112
113 visual.vu_data = output;
114 frame.GetLWMatrix(local_to_world);
115 sceVu0MulMatrix(local_to_screen, info->view_screen, local_to_world);
116
117 for (i = 0; i < 3; i++) {
118 row_step[i] = (vertex[1][i] - vertex[0][i]) / (float) (rows - 1);
119 column_step[i] = (vertex[2][i] - vertex[0][i]) / (float) (columns - 1);
120 }
121
122 row_step[3] = column_step[3] = 0.0f;
123 row_step[1] = column_step[1] = 0.0f;
124 pretest(local_to_screen, column_step);
125
126 first = 0;
127 position[3] = 1.0f;
128
129 for (i = 0, row_f = 0.0f; i < rows; i++, row_f += 1.0f) {
130 position[0] = vertex[0][0] + row_f * row_step[0];
131 position[1] = vertex[0][1] + row_f * row_step[1];
132 position[2] = vertex[0][2] + row_f * row_step[2];
133 here = &height[i * columns];
134 above = here - columns;
135 below = here + columns; // Unused here, but part of the retail source.
136
137 if (i == 0) {
138 above = here;
139 }
140
141 out = vertices[i];
142 uv = uvs[i];
143
144 for (j = 0; j < columns; j++) {
145 Trans_AddCell(*out++, position);
146 position[1] = *here * height_scale;
147 (*uv)[0] = uv_base[0] + distortion * (*above - *here);
148 (*uv)[1] = uv_base[1] + distortion * (here[0] - here[1]);
149 uv++;
150 here++;
151 above++;
152 }
153 }
154
155 texture = TexManager.GetTexture(TexManager.GetTextureHandle("work", -1));
156 tex0 = *(sceGsTex0 *) &texture->tex0;
157 tex0.bits.tcc = 0;
158
159 if (tags_built != 0) {
160 word += 16;
161 } else {
162 word += SetTEX0(output, *(u_long *) &tex0, 0);
163 }
164
165 // Each row pair becomes triangle strips of at most 27 columns; j counts the
166 // columns still to be emitted.
167 for (i = 0, row_f = 0.0f; i < rows - 1; i++, row_f += 1.0f) {
168 j = columns;
169 cell = &height[i * columns];
170 below = cell + columns;
171 cell_above = cell - columns;
172
173 if (i == 0) {
174 cell_above = cell;
175 }
176
177 top = (u_long128 *) vertices[i];
178 bottom = (u_long128 *) vertices[i + 1];
179 uv_top = (u_long128 *) uvs[i];
180 uv_bottom = (u_long128 *) uvs[i + 1];
181
182 while (j > 0) {
183 count = 27;
184
185 if (j < 27) {
186 count = j;
187 }
188
189 vertex_count = count * 2;
190 qwc = 0;
191
192 if (tags_built == 0) {
193 *(u_long128 *) &output[word] = *(u_long128 *) unpack;
194 }
195
196 unpack_word = word + 3;
197
198 if (tags_built != 0) {
199 word += 8;
200 qwc++;
201 } else {
202 nloop = vertex_count | 0x8000;
203 tag = &output[word + 4];
204 tag[0] = nloop;
205 tag[1] = 0x309E4000;
206 tag[2] = 0x413;
207 tag[3] = 0;
208 word += 8;
209 qwc++;
210 }
211
212 if (tags_built == 0) {
213 output[word] = vertex_count;
214 }
215
216 word += 4;
217 qwc++;
218 xyz = (u_long128 *) &output[word];
219 rgbaq = xyz + vertex_count;
220 st = rgbaq + vertex_count;
221
222 for (k = 0; k < count; k++) {
223 *xyz++ = *top++;
224 *xyz++ = *bottom++;
225 *rgbaq++ = *(u_long128 *) rgba;
226 *rgbaq++ = *(u_long128 *) rgba;
227 *st++ = *uv_top++;
228 *st++ = *uv_bottom++;
229 }
230
231 // Consecutive chunks share a column so the strip stays joined.
232 cell--;
233 below--;
234 cell_above--;
235 top--;
236 bottom--;
237 uv_top--;
238 uv_bottom--;
239 word += count * 24;
240 qwc += count * 6;
241
242 if (tags_built != 0) {
243 word += 4;
244 } else {
245 output[unpack_word] |= qwc << 16;
246
247 if (first == 0) {
248 *(u_long128 *) &output[word] = *(u_long128 *) first_kick;
249 first = 1;
250 } else {
251 *(u_long128 *) &output[word] = *(u_long128 *) kick;
252 }
253
254 word += 4;
255 }
256
257 j -= 27;
258 }
259
260 if (tags_built != 0) {
261 word += 4;
262 } else {
263 *(u_long128 *) &output[word] = *(u_long128 *) end;
264 word += 4;
265 }
266 }
267
268 visual.vu_size = word >> 2;
269 return visual.vu_size;
270}
271
272extern "C" int DrawVu1__6CWaterFP10RenderInfoP13sceVif1PacketP1(CWater *water, RenderInfo *info, sceVif1Packet *draw_packet, void *parent_info) {
273 if (water->CheckClip() != 0) {
274 return 0;
275 }
276
277 sceVif1PkCnt(draw_packet, 0);
278 sceVif1PkOpenDirectCode(draw_packet, 0);
279 sceVif1PkOpenGifTag(draw_packet, *(u_long128 *) &GiftagAD);
280 sceGsTest test = mgPixelTest;
281 test.bits.ate = 0;
282 test.bits.date = 0;
283 sceVif1PkAddGsAD(draw_packet, SCE_GS_TEST_1, *(u_long *) &test);
284 sceVif1PkCloseGifTag(draw_packet);
285 sceVif1PkCloseDirectCode(draw_packet);
286
287 sceVu0FMATRIX local_to_world;
288 water->frame.GetLWMatrix(local_to_world);
289 u_int *vu_packet = water->packet[!DBuffID + 1];
290 water->CreateVUData(vu_packet, &mgRenderInfo);
291 info->fog_enabled = false;
292 int size = water->visual.DrawVu1(draw_packet, local_to_world, info, (VU1_PROGRAM) 15, NULL, 0, 0);
293
294 sceVif1PkCnt(draw_packet, 0);
295 sceVif1PkOpenDirectCode(draw_packet, 0);
296 sceVif1PkOpenGifTag(draw_packet, *(u_long128 *) &GiftagAD);
297 sceVif1PkAddGsAD(draw_packet, SCE_GS_TEST_1, *(u_long *) &mgPixelTest);
298 sceVif1PkCloseGifTag(draw_packet);
299 sceVif1PkCloseDirectCode(draw_packet);
300 return size;
301}
302
304 sceVu0FVECTOR box[2];
305 sceVu0FVECTOR corner[4];
306
307 frame.GetWorldPosition(corner[0], vertex[0]);
308 frame.GetWorldPosition(corner[1], vertex[1]);
309 frame.GetWorldPosition(corner[2], vertex[2]);
310 frame.GetWorldPosition(corner[3], vertex[3]);
311 VectorMaxMin(box[0], box[1], corner[0], corner[1], corner[2], corner[3]);
312 return MGClipBox((CBoxVu0 *) box);
313}
314
315void CWater::Hamon() {
316 int i;
317 int j;
318 float *source;
319 float *target;
320
321 // The two buffers hold this step and the last; the older one is written.
322 if (height == height_a) {
323 source = height_a;
324 target = height_b;
325 } else {
326 target = height_a;
327 source = height_b;
328 }
329
330 height = target;
331
332 float speed = wave_speed * wave_speed;
333 float centre = 2.0f * (1.0f - 2.0f * speed);
334 float loss = damping;
335
336 for (i = 1; i < rows - 1; i++) {
337 for (j = 1; j < columns - 1; j++) {
338 float *cell = &source[j + i * columns];
339 float *out = &target[j + i * columns];
340 float around = *(cell - columns) + (cell[-1] + cell[1] + *(cell + columns));
341 around *= speed;
342 float centre_term = centre * *cell;
343 float previous = *out;
344 *out = around + (centre_term - previous) - loss * (*cell - previous);
345 }
346 }
347}
348
349void CWater::SetVertex(float *corner0, float *corner1, float *corner2, float *corner3) {
350 sceVu0CopyVector(vertex[0], corner0);
351 sceVu0CopyVector(vertex[1], corner1);
352 sceVu0CopyVector(vertex[2], corner2);
353 sceVu0CopyVector(vertex[3], corner3);
354}
355
356void CWater::Shake(int row, int column, float height_change) {
357 row %= rows;
358 column %= columns;
359
360 // The edge cells hold the surface still, so a ripple starts inside them.
361 if (row <= 0) {
362 row = 1;
363 }
364
365 if (column <= 0) {
366 column = 1;
367 }
368
369 if (row > rows - 2) {
370 row = rows - 2;
371 }
372
373 if (column > columns - 2) {
374 column = columns - 2;
375 }
376
377 float *cell = &height[column];
378 cell[row * columns] += height_change;
379}
380
381void CWater::SetSize(int row_count, int column_count, CDataAlloc2<1> *arena) {
382 if (arena == NULL) {
383 arena = &WaterData;
384 }
385
386 int quads = ((row_count * column_count) >> 2) + 1;
387 height_a = (float *) arena->Alloc(quads);
388 height_b = (float *) arena->Alloc(quads);
389 rows = row_count;
390 columns = column_count;
391
392 for (int i = 0; i < rows * columns; i++) {
393 height_b[i] = 0.0f;
394 height_a[i] = 0.0f;
395 }
396
398
399 RenderInfo info;
400 tags_built = 0;
401 arena->Align64();
402 packet[1] = (u_int *) (arena->base + arena->used * 16);
403 int packet_quads = CreateVUData(packet[1], &info);
404 arena->Alloc(packet_quads);
405 packet[2] = (u_int *) arena->Alloc64(packet_quads);
406 CreateVUData(packet[2], &info);
407 tags_built = 1;
408}
409
410CWater::CWater() {
411 rows = 0;
412 columns = 0;
413 packet[2] = NULL;
414 packet[1] = NULL;
415 packet[0] = NULL;
416 color[0] = 0x80;
417 color[1] = 0x80;
418 color[2] = 0x80;
419 color[3] = 0x80;
420 wave_speed = 0.1f;
421 damping = 0.015f;
422 height_scale = 0.0f;
423 distortion = 0.0f;
424}
u_char * base
Definition dataalloc.hpp:24
u_long tex0
Definition texture.hpp:57
virtual int DrawVu1(u_int *packet, float(*matrix)[4], RenderInfo *info, VU1_PROGRAM program, u_long128 *draw_state, int unknown1, int unknown2)
float wave_speed
Definition op_c.cpp:146
float height_scale
Definition op_c.cpp:148
float damping
Definition op_c.cpp:147
float * height
Definition op_c.cpp:139
float * height_b
Definition op_c.cpp:141
u_int * packet[3]
Definition op_c.cpp:143
int CreateVUData(unsigned int *output, RenderInfo *info)
Definition water.cpp:64
float * height_a
Definition op_c.cpp:140
sceVu0FVECTOR vertex[4]
Definition op_c.cpp:142
s32 tags_built
Definition op_c.cpp:150
u8 color[4]
Definition op_c.cpp:145
int CheckClip()
Definition water.cpp:303
s32 columns
Definition op_c.cpp:138
s32 rows
Definition op_c.cpp:137
float distortion
Definition op_c.cpp:149
CFrameVu1 frame
Definition op_c.cpp:151
CVisualPolyVu1 visual
Definition op_c.cpp:144
sceVu0FMATRIX view_screen