-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.c
More file actions
369 lines (290 loc) · 10.7 KB
/
Copy pathrender.c
File metadata and controls
369 lines (290 loc) · 10.7 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "3d.h"
#include "gfx.h"
#include "grafico.h"
#include "render.h"
#include "mapa.h"
#define MAX_VERTS_POR_POLIGONO 16
#define FAR_CLIP 500.0
int facesRendered;
extern int _debug;
extern int _lightON;
extern int _showRendering, _showMap2D, _showBBox;
extern float tempo_de_jogo;
int render_clip_near_face(
ponto_t *in[MAX_VERTS_POR_POLIGONO],
int in_count,
ponto_t out[MAX_VERTS_POR_POLIGONO * 2]
) {
int out_count = 0;
for (int i = 0; i < in_count; i++) {
ponto_t *cur = in[i];
ponto_t *prev = in[(i - 1 + in_count) % in_count];
float d_cur = cur->rot.z - NEAR_Z;
float d_prev = prev->rot.z - NEAR_Z;
int inside_cur = d_cur >= 0.0f;
int inside_prev = d_prev >= 0.0f;
if (inside_cur) {
if (!inside_prev) {
// Interseção entrada — calcula ponto novo
float t = d_prev / (d_prev - d_cur);
ponto_t p;
// Interpolação linear para posição
p.rot.x = prev->rot.x + t * (cur->rot.x - prev->rot.x);
p.rot.y = prev->rot.y + t * (cur->rot.y - prev->rot.y);
p.rot.z = NEAR_Z;
// Correção de perspectiva (1/z)
float inv_z1 = 1.0f / prev->rot.z;
float inv_z2 = 1.0f / cur->rot.z;
float u1_corr = prev->tex.x * inv_z1;
float v1_corr = prev->tex.y * inv_z1;
float u2_corr = cur->tex.x * inv_z2;
float v2_corr = cur->tex.y * inv_z2;
float inv_z = inv_z1 + t * (inv_z2 - inv_z1);
float u_corr = u1_corr + t * (u2_corr - u1_corr);
float v_corr = v1_corr + t * (v2_corr - v1_corr);
p.tex.x = u_corr / inv_z;
p.tex.y = v_corr / inv_z;
out[out_count++] = p;
}
// Ponto atual é visível — copia direto
out[out_count++] = *cur;
}
else if (inside_prev) {
// Interseção saída — calcula ponto novo
float t = d_prev / (d_prev - d_cur);
ponto_t p;
p.rot.x = prev->rot.x + t * (cur->rot.x - prev->rot.x);
p.rot.y = prev->rot.y + t * (cur->rot.y - prev->rot.y);
p.rot.z = NEAR_Z;
float inv_z1 = 1.0f / prev->rot.z;
float inv_z2 = 1.0f / cur->rot.z;
float u1_corr = prev->tex.x * inv_z1;
float v1_corr = prev->tex.y * inv_z1;
float u2_corr = cur->tex.x * inv_z2;
float v2_corr = cur->tex.y * inv_z2;
float inv_z = inv_z1 + t * (inv_z2 - inv_z1);
float u_corr = u1_corr + t * (u2_corr - u1_corr);
float v_corr = v1_corr + t * (v2_corr - v1_corr);
p.tex.x = u_corr / inv_z;
p.tex.y = v_corr / inv_z;
out[out_count++] = p;
}
}
return out_count;
}
void render_desenha_entidade(camera_t *cam, entidade_t *ent)
{
obj3d_t *obj = ent->obj;
ponto_t *verts[MAX_VERTS_POR_POLIGONO];
ponto_t clipped[MAX_VERTS_POR_POLIGONO * 2];
ponto_t *clipped_ptrs[MAX_VERTS_POR_POLIGONO * 2];
vetor3d_t viewDir;
triangulo_t *tri;
skinvert_t *svxt1, *svxt2, *svxt3;
int i, j, clipped_count;
// int objFacesRendered = 0;
entidade_projecao3D(cam, ent);
for (i=0, tri = obj->tris; i<obj->numtris; i++, tri++) {
// Pega os vértices do triângulo
verts[0] = &obj->verts[tri->v[0]];
verts[1] = &obj->verts[tri->v[1]];
verts[2] = &obj->verts[tri->v[2]];
// Backface culling
// if (tri->normal.z < 0) {
// // continue;
// }
// Calcula o centro da face (em espaço da câmera)
viewDir.x = (verts[0]->rot.x + verts[1]->rot.x + verts[2]->rot.x) / 3.0f;
viewDir.y = (verts[0]->rot.y + verts[1]->rot.y + verts[2]->rot.y) / 3.0f;
viewDir.z = (verts[0]->rot.z + verts[1]->rot.z + verts[2]->rot.z) / 3.0f;
vetor_normalize(&viewDir); // opcional, mas bom para estabilidade
// Dot product entre normal da face e viewDir
if (vetor_dot_product(tri->normal, viewDir) >= 0) {
continue; // Culling: está de costas
}
// coordenadas de textura
svxt1 = &obj->skinmap[tri->v[0]];
svxt2 = &obj->skinmap[tri->v[1]];
svxt3 = &obj->skinmap[tri->v[2]];
verts[0]->tex.x = svxt1->s;
verts[0]->tex.y = svxt1->t;
verts[1]->tex.x = svxt2->s;
verts[1]->tex.y = svxt2->t;
verts[2]->tex.x = svxt3->s;
verts[2]->tex.y = svxt3->t;
if (!tri->isFront) {
if (svxt1->onseam) verts[0]->tex.x += 0.5;
if (svxt2->onseam) verts[1]->tex.x += 0.5;
if (svxt3->onseam) verts[2]->tex.x += 0.5;
}
// Faz o clipping contra o plano NEAR
clipped_count = render_clip_near_face(verts, 3, clipped);
if (clipped_count < 3) continue;
// Projeta os vértices válidos
for (j=0; j < clipped_count; j++) {
grafico_projecao3D(&clipped[j]);
clipped_ptrs[j] = &clipped[j];
}
grafico_desenha_poligono(clipped_ptrs, clipped_count, &obj->texture, NULL,0,0);
// objFacesRendered++;
}
if (_showBBox && !ent->id) {
// vetor3d_t bboxmin = {20,20,20};
// vetor3d_t bboxmax = {30,30,30};
vetor3d_t bboxmin = ent->obj->frameinfo[ent->numFrameSel].bboxmin;
vetor3d_t bboxmax = ent->obj->frameinfo[ent->numFrameSel].bboxmax;
// vetor3d_t bboxmin = { (float)bbMin[0]-128, (float)bbMin[1]-128, (float)bbMin[2] };
// vetor3d_t bboxmax = { (float)bbMax[0]-128, (float)bbMax[1]-128, (float)bbMax[2] };
grafico_desenha_cubo(cam, ent->posicao, bboxmin, bboxmax, 100,230,100);
}
// printf(" obj[%s]fr[%d de %d]", obj->nome, objFacesRendered, obj->numtris);
}
int render_desenhaFace(face_t *face, mapa_t *mapa)
{
ponto_t *verts[MAX_VERTS_POR_POLIGONO];
ponto_t clipped[MAX_VERTS_POR_POLIGONO * 2];
ponto_t *clipped_ptrs[MAX_VERTS_POR_POLIGONO * 2];
edge_t *edge;
int *ledge, vxtNum, s, t;
vetor3d_t *vBase;
face->drawn = 1;
if (face->numedges > MAX_VERTS_POR_POLIGONO) {
//printf("face[%d] > numEgdes %d muito grande! ", i, face->numedges);
return 1;
}
if (face->texinfo->miptex == mapa->numTextureTrigger)
return 2;
ledge = (int *)face->firstledge;
for (int v=0; v < face->numedges; v++, ledge++) {
if (*ledge < 0) {
edge = (edge_t *)&mapa->edges[-*ledge];
vxtNum = edge->v[1];
} else {
edge = (edge_t *)&mapa->edges[*ledge];
vxtNum = edge->v[0];
}
verts[v] = &mapa->verts[vxtNum];
vBase = &mapa->base[vxtNum];
float s = vetor_dot_product(*vBase, face->texinfo->vetorS) + face->texinfo->distS;
float t = vetor_dot_product(*vBase, face->texinfo->vetorT) + face->texinfo->distT;
// Coordenadas da textura (normalizadas 0–1)
verts[v]->tex.x = s / face->texture->width;
verts[v]->tex.y = t / face->texture->height;
// Coordenadas do lightmap (em escala de texel)
verts[v]->tex_luz.x = (s - face->light_mins_s) / face->light_width;//(s / 16.0f) - face->light_mins_s;
verts[v]->tex_luz.y = (t - face->light_mins_t) / face->light_height;//(t / 16.0f) - face->light_mins_t;
}
//dbg
// if (i != 100) continue;
// Faz o clipping contra o plano NEAR
int clipped_count = render_clip_near_face(verts, face->numedges, clipped);
if (clipped_count < 3) return 3;
// Projeta os vértices válidos
for (int v = 0; v < clipped_count; v++) {
grafico_projecao3D(&clipped[v]);
clipped_ptrs[v] = &clipped[v];
}
if (_debug)
printf("\nFace{lW:%d-lH:%d}{minsS:%d-minsT:%d}[S:%.1f,%.1f,%.1f+%.1f--T:%.1f,%.1f,%.1f+%.1f]\n",
face->light_width, face->light_height, face->light_mins_s, face->light_mins_t,
face->texinfo->vetorS.x, face->texinfo->vetorS.y, face->texinfo->vetorS.z, face->texinfo->distS,
face->texinfo->vetorT.x, face->texinfo->vetorT.y, face->texinfo->vetorT.z, face->texinfo->distT
);
if (strncmp(face->texture->name, "sky", 3) == 0) {
// Use função especial para céu
grafico_desenha_poligono_sky(clipped_ptrs, clipped_count,
face->texture, tempo_de_jogo);
} else {
grafico_desenha_poligono(clipped_ptrs, clipped_count, face->texture,
_lightON ? face->light : NULL, face->light_width, face->light_height);
}
return 0;
}
void render_desenha_node(node_t *node, mapa_t *mapa, camera_t *cam, char *vis)
{
if (node->contents < 0) {
// É umm leaf
leaf_t *leaf = (leaf_t *)node;
// Verifica se o leaf está visível
if (!(vis[leaf->key >> 3] & (1 << (leaf->key & 7))))
return;
// Render LEAF
int j;
face_t *face, **mark;
for (j=0, mark = leaf->firstmarksurface; j < leaf->nummarksurfaces; j++, mark++) {
face = *mark;
if (!face->drawn && // ignore dups
((face->plano->ON && !face->side) || (!face->plano->ON && face->side)) && // backface culling
(!_debug || _debug == face->id)) { // debug
if (!render_desenhaFace(face, mapa)) {
facesRendered++;
if (_showRendering) gfx_flush();
}
}
}
return;
}
// Nó interno
plano_t *plano = node->plane;
// Distância da câmera ao plano do nó
float dist = plano->normal.x * cam->pos.x +
plano->normal.y * cam->pos.y +
plano->normal.z * cam->pos.z - plano->dist;
int frente = (dist >= 0) ? 0 : 1;
int tras = 1 - frente;
// travessia da arvore BSP INVERTIDA! Para usar o zBuffer e evitar pixels
// Depois desenha o lado mais próximo
render_desenha_node(node->children[frente], mapa, cam, vis);
// Primeiro desenha o lado mais distante (painters algo)
render_desenha_node(node->children[tras], mapa, cam, vis);
}
void render_desenha_mapa(mapa_t *mapa, camera_t *cam)
{
int i, bspON = 1;
leaf_t *leafCAM;
face_t *face;
byte *vis;
mapa_projecao3D(cam, mapa);
leafCAM = mapa_discoverLeaf(&cam->pos, mapa);
printf("L:%d ", leafCAM->visofs);
vis = mapa_leafVIS(leafCAM, mapa);
for (i=0, face = mapa->faces; i < mapa->numfaces; i++, face++)
face->drawn = 0;
facesRendered = 0;
if (bspON) {
// Iniciar a recursao pela arvore BSP
render_desenha_node(mapa->nodes, mapa, cam, vis);
} else {
int j;
face_t **mark;
leaf_t *leaf = &mapa->leafs[1];
for (i=0; i < mapa->numleafs - 1; i++, leaf++) {
if (!(vis[i >> 3] & (1 << (i & 7)))) {
// LEAF nao visivel
continue;
}
// Render LEAF
for (j=0, mark = leaf->firstmarksurface; j < leaf->nummarksurfaces; j++, mark++) {
face = *mark;
if (!face->drawn && // ignore dups
((face->plano->ON && !face->side) || (!face->plano->ON && face->side)) && // backface culling
(!_debug || _debug == face->id)) { // debug
if (!render_desenhaFace(face, mapa)) {
facesRendered++;
}
}
}
}
}
printf(" facesRender[%d de %d]", facesRendered, mapa->numfaces);
if (!_showRendering && _showMap2D)
mostraMapa2D(mapa, cam, vis);
// face = mapa->faces;
// for (int i=0; i < mapa->numfaces; i++, face++) {
// render_desenhaFace(face, mapa, paleta);
// }
}