Debian Codegraph @

prboom2/src/gl_preprocess.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2000 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA.
26 *
27 * DESCRIPTION:
28 *
29 *---------------------------------------------------------------------
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #ifdef _WIN32
37 #define WIN32_LEAN_AND_MEAN
38 #include <windows.h>
39 #endif
40 #ifndef CALLBACK
41 #define CALLBACK
42 #endif
43
44 #include <assert.h>
45 #include <math.h>
46
47 #include "gl_opengl.h"
48
49 #include "z_zone.h"
50 #include "doomstat.h"
51 #include "gl_intern.h"
52 #include "gl_struct.h"
53 #include "p_maputl.h"
54 #include "r_main.h"
55 #include "am_map.h"
56 #include "lprintf.h"
57
58 static FILE *levelinfo;
59
60 static int gld_max_vertexes = 0;
61 static int gld_num_vertexes = 0;
62
63 static int triangulate_subsectors = 0;
64
65 // this is the list for all sectors to the loops
66 GLSector *sectorloops;
67
68 // this is the list for all subsectors to the loops
69 // uses by textured automap
70 GLMapSubsector *subsectorloops;
71
72 static void gld_AddGlobalVertexes(int count)
73 {
74 if ((gld_num_vertexes+count)>=gld_max_vertexes)
75 {
76 gld_max_vertexes+=count+1024;
77 flats_vbo = Z_Realloc(flats_vbo, gld_max_vertexes * sizeof(flats_vbo[0]));
78 }
79 }
80
81 static void gld_TurnOnSubsectorTriangulation(void)
82 {
83 triangulate_subsectors = 1;
84 }
85
86 static void gld_TurnOffSubsectorTriangulation(void)
87 {
88 triangulate_subsectors = 0;
89 }
90
91 static dboolean gld_TriangulateSubsector(subsector_t *ssec)
92 {
93 return !(ssec->sector->flags & SECTOR_IS_CLOSED) ||
94 triangulate_subsectors;
95 }
96
97 static int gld_SubsectorLoopIndex(subsector_t *ssec)
98 {
99 return ssec->sector->iSectorID;
100 }
101
102 static void gld_SetupSubsectorLoop(subsector_t *ssec, int iSubsectorID, int numedgepoints)
103 {
104 GLLoopDef **loop;
105 int *loopcount;
106
107 if (triangulate_subsectors)
108 {
109 loop = &subsectorloops[ iSubsectorID ].loops;
110 loopcount = &subsectorloops[ iSubsectorID ].loopcount;
111 }
112 else
113 {
114 int loop_index = gld_SubsectorLoopIndex(ssec);
115
116 loop = &sectorloops[ loop_index ].loops;
117 loopcount = &sectorloops[ loop_index ].loopcount;
118 }
119
120 (*loopcount)++;
121 (*loop) = Z_Realloc((*loop), sizeof(GLLoopDef)*(*loopcount));
122 ((*loop)[(*loopcount) - 1]).index = iSubsectorID;
123 ((*loop)[(*loopcount) - 1]).mode = GL_TRIANGLE_FAN;
124 ((*loop)[(*loopcount) - 1]).vertexcount = numedgepoints;
125 ((*loop)[(*loopcount) - 1]).vertexindex = gld_num_vertexes;
126 }
127
128 /*****************************
129 *
130 * FLATS
131 *
132 *****************************/
133
134 /* proff - 05/15/2000
135 * The idea and algorithm to compute the flats with nodes and subsectors is
136 * originaly from JHexen. I have redone it.
137 */
138
139 #define FIX2DBL(x) ((double)(x))
140 #define MAX_CC_SIDES 128
141
142 static dboolean gld_PointOnSide(vertex_t *p, divline_t *d)
143 {
144 // We'll return false if the point c is on the left side.
145 return ((FIX2DBL(d->y)-FIX2DBL(p->y))*FIX2DBL(d->dx)-(FIX2DBL(d->x)-FIX2DBL(p->x))*FIX2DBL(d->dy) >= 0);
146 }
147
148 // Lines start-end and fdiv must intersect.
149 static void gld_CalcIntersectionVertex(vertex_t *s, vertex_t *e, divline_t *d, vertex_t *i)
150 {
151 double ax = FIX2DBL(s->x), ay = FIX2DBL(s->y), bx = FIX2DBL(e->x), by = FIX2DBL(e->y);
152 double cx = FIX2DBL(d->x), cy = FIX2DBL(d->y), dx = cx+FIX2DBL(d->dx), dy = cy+FIX2DBL(d->dy);
153 double r = ((ay-cy)*(dx-cx)-(ax-cx)*(dy-cy)) / ((bx-ax)*(dy-cy)-(by-ay)*(dx-cx));
154 i->x = (fixed_t)((double)s->x + r*((double)e->x-(double)s->x));
155 i->y = (fixed_t)((double)s->y + r*((double)e->y-(double)s->y));
156 }
157
158 #undef FIX2DBL
159
160 // Returns a pointer to the list of points. It must be used.
161 //
162 static vertex_t *gld_FlatEdgeClipper(int *numpoints, vertex_t *points, int numclippers, divline_t *clippers)
163 {
164 unsigned char sidelist[MAX_CC_SIDES];
165 int i, k, num = *numpoints;
166
167 // We'll clip the polygon with each of the divlines. The left side of
168 // each divline is discarded.
169 for(i=0; i<numclippers; i++)
170 {
171 divline_t *curclip = &clippers[i];
172
173 // First we'll determine the side of each vertex. Points are allowed
174 // to be on the line.
175 for(k=0; k<num; k++)
176 sidelist[k] = gld_PointOnSide(&points[k], curclip);
177
178 for(k=0; k<num; k++)
179 {
180 int startIdx = k, endIdx = k+1;
181 // Check the end index.
182 if(endIdx == num) endIdx = 0; // Wrap-around.
183 // Clipping will happen when the ends are on different sides.
184 if(sidelist[startIdx] != sidelist[endIdx])
185 {
186 vertex_t newvert;
187
188 gld_CalcIntersectionVertex(&points[startIdx], &points[endIdx], curclip, &newvert);
189
190 // Add the new vertex. Also modify the sidelist.
191 points = (vertex_t*)Z_Realloc(points,(++num)*sizeof(vertex_t));
192 if(num >= MAX_CC_SIDES)
193 I_Error("gld_FlatEdgeClipper: Too many points in carver");
194
195 // Make room for the new vertex.
196 memmove(&points[endIdx+1], &points[endIdx],
197 (num - endIdx-1)*sizeof(vertex_t));
198 memcpy(&points[endIdx], &newvert, sizeof(newvert));
199
200 memmove(&sidelist[endIdx+1], &sidelist[endIdx], num-endIdx-1);
201 sidelist[endIdx] = 1;
202
203 // Skip over the new vertex.
204 k++;
205 }
206 }
207
208 // Now we must discard the points that are on the wrong side.
209 for(k=0; k<num; k++)
210 if(!sidelist[k])
211 {
212 memmove(&points[k], &points[k+1], (num - k-1)*sizeof(vertex_t));
213 memmove(&sidelist[k], &sidelist[k+1], num - k-1);
214 num--;
215 k--;
216 }
217 }
218 // Screen out consecutive identical points.
219 for(i=0; i<num; i++)
220 {
221 int previdx = i-1;
222 if(previdx < 0) previdx = num - 1;
223 if(points[i].x == points[previdx].x
224 && points[i].y == points[previdx].y)
225 {
226 // This point (i) must be removed.
227 memmove(&points[i], &points[i+1], sizeof(vertex_t)*(num-i-1));
228 num--;
229 i--;
230 }
231 }
232 *numpoints = num;
233 return points;
234 }
235
236 static void gld_FlatConvexCarver(int ssidx, int num, divline_t *list)
237 {
238 subsector_t *ssec=&subsectors[ssidx];
239 int numclippers = num+ssec->numlines;
240 divline_t *clippers;
241 int i, numedgepoints;
242 vertex_t *edgepoints;
243
244 clippers=(divline_t*)Z_Malloc(numclippers*sizeof(divline_t));
245 if (!clippers)
246 return;
247 for(i=0; i<num; i++)
248 {
249 clippers[i].x = list[num-i-1].x;
250 clippers[i].y = list[num-i-1].y;
251 clippers[i].dx = list[num-i-1].dx;
252 clippers[i].dy = list[num-i-1].dy;
253 }
254 for(i=num; i<numclippers; i++)
255 {
256 seg_t *seg = &segs[ssec->firstline+i-num];
257 clippers[i].x = seg->v1->x;
258 clippers[i].y = seg->v1->y;
259 clippers[i].dx = seg->v2->x-seg->v1->x;
260 clippers[i].dy = seg->v2->y-seg->v1->y;
261 }
262
263 // Setup the 'worldwide' polygon.
264 numedgepoints = 4;
265 edgepoints = (vertex_t*)Z_Malloc(numedgepoints*sizeof(vertex_t));
266
267 edgepoints[0].x = INT_MIN;
268 edgepoints[0].y = INT_MAX;
269
270 edgepoints[1].x = INT_MAX;
271 edgepoints[1].y = INT_MAX;
272
273 edgepoints[2].x = INT_MAX;
274 edgepoints[2].y = INT_MIN;
275
276 edgepoints[3].x = INT_MIN;
277 edgepoints[3].y = INT_MIN;
278
279 // Do some clipping, <snip> <snip>
280 edgepoints = gld_FlatEdgeClipper(&numedgepoints, edgepoints, numclippers, clippers);
281
282 if(!numedgepoints)
283 {
284 if (levelinfo) fprintf(levelinfo, "All carved away: subsector %lli - sector %i\n", (long long)(ssec-subsectors), ssec->sector->iSectorID);
285 }
286 else
287 {
288 if (numedgepoints >= 3)
289 {
290 gld_AddGlobalVertexes(numedgepoints);
291
292 if (flats_vbo)
293 {
294 gld_SetupSubsectorLoop(ssec, ssidx, numedgepoints);
295
296 for(i = 0; i < numedgepoints; i++)
297 {
298 flats_vbo[gld_num_vertexes].u = ( (float)edgepoints[i].x/(float)FRACUNIT)/64.0f;
299 flats_vbo[gld_num_vertexes].v = (-(float)edgepoints[i].y/(float)FRACUNIT)/64.0f;
300 flats_vbo[gld_num_vertexes].x = -(float)edgepoints[i].x/MAP_SCALE;
301 flats_vbo[gld_num_vertexes].y = 0.0f;
302 flats_vbo[gld_num_vertexes].z = (float)edgepoints[i].y/MAP_SCALE;
303 gld_num_vertexes++;
304 }
305 }
306 }
307 }
308 // We're done, free the edgepoints memory.
309 Z_Free(edgepoints);
310 Z_Free(clippers);
311 }
312
313 static void gld_CarveFlats(int bspnode, int numdivlines, divline_t *divlines)
314 {
315 node_t *nod;
316 divline_t *childlist, *dl;
317 int childlistsize = numdivlines+1;
318
319 // If this is a subsector we are dealing with, begin carving with the
320 // given list.
321 if (bspnode & NF_SUBSECTOR)
322 {
323 // We have arrived at a subsector. The divline list contains all
324 // the partition lines that carve out the subsector.
325 // special case for trivial maps (no nodes, single subsector)
326 int ssidx = (numnodes != 0) ? bspnode & (~NF_SUBSECTOR) : 0;
327
328 if (gld_TriangulateSubsector(&subsectors[ssidx]))
329 gld_FlatConvexCarver(ssidx, numdivlines, divlines);
330
331 return;
332 }
333
334 // Get a pointer to the node.
335 nod = nodes + bspnode;
336
337 // Allocate a new list for each child.
338 childlist = (divline_t*)Z_Malloc(childlistsize*sizeof(divline_t));
339
340 // Copy the previous lines.
341 if(divlines) memcpy(childlist,divlines,numdivlines*sizeof(divline_t));
342
343 dl = childlist + numdivlines;
344 dl->x = nod->x;
345 dl->y = nod->y;
346 // The right child gets the original line (LEFT side clipped).
347 dl->dx = nod->dx;
348 dl->dy = nod->dy;
349 gld_CarveFlats(nod->children[0],childlistsize,childlist);
350
351 // The left side. We must reverse the line, otherwise the wrong
352 // side would get clipped.
353 dl->dx = -nod->dx;
354 dl->dy = -nod->dy;
355 gld_CarveFlats(nod->children[1],childlistsize,childlist);
356
357 // We are finishing with this node, free the allocated list.
358 Z_Free(childlist);
359 }
360
361 static int currentsector; // the sector which is currently tesselated
362
363 // ntessBegin
364 //
365 // called when the tesselation of a new loop starts
366
367 static void CALLBACK ntessBegin( GLenum type )
368 {
369 #ifdef PRBOOM_DEBUG
370 if (levelinfo)
371 {
372 if (type==GL_TRIANGLES)
373 fprintf(levelinfo, "\t\tBegin: GL_TRIANGLES\n");
374 else
375 if (type==GL_TRIANGLE_FAN)
376 fprintf(levelinfo, "\t\tBegin: GL_TRIANGLE_FAN\n");
377 else
378 if (type==GL_TRIANGLE_STRIP)
379 fprintf(levelinfo, "\t\tBegin: GL_TRIANGLE_STRIP\n");
380 else
381 fprintf(levelinfo, "\t\tBegin: unknown\n");
382 }
383 #endif
384 // increase loopcount for currentsector
385 sectorloops[ currentsector ].loopcount++;
386 // reallocate to get space for another loop
387 sectorloops[ currentsector ].loops=Z_Realloc(sectorloops[currentsector].loops,sizeof(GLLoopDef)*sectorloops[currentsector].loopcount);
388 // set initial values for current loop
389 // currentloop is -> sectorloops[currentsector].loopcount-1
390 sectorloops[ currentsector ].loops[ sectorloops[currentsector].loopcount-1 ].index=-1;
391 sectorloops[ currentsector ].loops[ sectorloops[currentsector].loopcount-1 ].mode=type;
392 sectorloops[ currentsector ].loops[ sectorloops[currentsector].loopcount-1 ].vertexcount=0;
393 sectorloops[ currentsector ].loops[ sectorloops[currentsector].loopcount-1 ].vertexindex=gld_num_vertexes;
394 }
395
396 // ntessError
397 //
398 // called when the tesselation failes (DEBUG only)
399
400 static void CALLBACK ntessError(GLenum error)
401 {
402 #ifdef PRBOOM_DEBUG
403 const GLubyte *estring;
404 estring = gluErrorString(error);
405 fprintf(levelinfo, "\t\tTessellation Error: %s\n", estring);
406 #endif
407 }
408
409 // ntessCombine
410 //
411 // called when the two or more vertexes are on the same coordinate
412
413 static void CALLBACK ntessCombine( GLdouble coords[3], vertex_t *vert[4], GLfloat w[4], void **dataOut )
414 {
415 #ifdef PRBOOM_DEBUG
416 if (levelinfo)
417 {
418 fprintf(levelinfo, "\t\tVertexCombine Coords: x %10.5f, y %10.5f z %10.5f\n", coords[0], coords[1], coords[2]);
419 if (vert[0]) fprintf(levelinfo, "\t\tVertexCombine Vert1 : x %10i, y %10i p %p\n", vert[0]->x>>FRACBITS, vert[0]->y>>FRACBITS, vert[0]);
420 if (vert[1]) fprintf(levelinfo, "\t\tVertexCombine Vert2 : x %10i, y %10i p %p\n", vert[1]->x>>FRACBITS, vert[1]->y>>FRACBITS, vert[1]);
421 if (vert[2]) fprintf(levelinfo, "\t\tVertexCombine Vert3 : x %10i, y %10i p %p\n", vert[2]->x>>FRACBITS, vert[2]->y>>FRACBITS, vert[2]);
422 if (vert[3]) fprintf(levelinfo, "\t\tVertexCombine Vert4 : x %10i, y %10i p %p\n", vert[3]->x>>FRACBITS, vert[3]->y>>FRACBITS, vert[3]);
423 }
424 #endif
425 // just return the first vertex, because all vertexes are on the same coordinate
426 *dataOut = vert[0];
427 }
428
429 // ntessVertex
430 //
431 // called when a vertex is found
432
433 static void CALLBACK ntessVertex( vertex_t *vert )
434 {
435 #ifdef PRBOOM_DEBUG
436 if (levelinfo)
437 fprintf(levelinfo, "\t\tVertex : x %10i, y %10i\n", vert->x>>FRACBITS, vert->y>>FRACBITS);
438 #endif
439 // increase vertex count
440 sectorloops[ currentsector ].loops[ sectorloops[currentsector].loopcount-1 ].vertexcount++;
441
442 // increase vertex count
443 gld_AddGlobalVertexes(1);
444 // add the new vertex (vert is the second argument of gluTessVertex)
445 flats_vbo[gld_num_vertexes].u=( (float)vert->x/(float)FRACUNIT)/64.0f;
446 flats_vbo[gld_num_vertexes].v=(-(float)vert->y/(float)FRACUNIT)/64.0f;
447 flats_vbo[gld_num_vertexes].x=-(float)vert->x/MAP_SCALE;
448 flats_vbo[gld_num_vertexes].y=0.0f;
449 flats_vbo[gld_num_vertexes].z= (float)vert->y/MAP_SCALE;
450 gld_num_vertexes++;
451 }
452
453 // ntessEnd
454 //
455 // called when the tesselation of a the current loop ends (DEBUG only)
456
457 static void CALLBACK ntessEnd( void )
458 {
459 #ifdef PRBOOM_DEBUG
460 if (levelinfo)
461 fprintf(levelinfo, "\t\tEnd loopcount %i vertexcount %i\n", sectorloops[currentsector].loopcount, sectorloops[ currentsector ].loops[ sectorloops[currentsector].loopcount-1 ].vertexcount);
462 #endif
463 }
464
465 // gld_PrecalculateSector
466 //
467 // this calculates the loops for the sector "num"
468 //
469 // how does it work?
470 // first I have to credit Michael 'Kodak' Ryssen for the usage of the
471 // glu tesselation functions. the rest of this stuff is entirely done by me (proff).
472 // if there are any similarities, then they are implications of the algorithm.
473 //
474 // I'm starting with the first line of the current sector. I take it's ending vertex and
475 // add it to the tesselator. the current line is marked as used. then I'm searching for
476 // the next line which connects to the current line. if there is more than one line, I
477 // choose the one with the smallest angle to the current. if there is no next line, I
478 // start a new loop and take the first unused line in the sector. after all lines are
479 // processed, the polygon is tesselated.
480 //
481 // e6y
482 // The bug in algorithm of splitting of a sector into the closed contours was fixed.
483 // There is no more HOM at the starting area on MAP16 @ Eternal.wad
484 // I hope nothing was broken
485
486 static void gld_PrecalculateSector(int num)
487 {
488 int i;
489 dboolean *lineadded=NULL;
490 int linecount;
491 int currentline;
492 int oldline;
493 int currentloop;
494 int bestline;
495 int bestlinecount;
496 vertex_t *startvertex;
497 vertex_t *currentvertex = NULL; //e6y: fix use of uninitialized local variable below
498 angle_t lineangle;
499 angle_t angle;
500 angle_t bestangle;
501 GLUtesselator *tess;
502 double *v=NULL;
503 int maxvertexnum;
504 int vertexnum;
505
506 currentsector=num;
507 lineadded=Z_Malloc(sectors[num].linecount*sizeof(dboolean));
508 if (!lineadded)
509 {
510 if (levelinfo) fclose(levelinfo);
511 return;
512 }
513 // init tesselator
514 tess=gluNewTess();
515 if (!tess)
516 {
517 if (levelinfo) fclose(levelinfo);
518 Z_Free(lineadded);
519 return;
520 }
521 // set callbacks
522 gluTessCallback(tess, GLU_TESS_BEGIN, (void(CALLBACK*)())ntessBegin);
523 gluTessCallback(tess, GLU_TESS_VERTEX, (void(CALLBACK*)())ntessVertex);
524 gluTessCallback(tess, GLU_TESS_ERROR, (void(CALLBACK*)())ntessError);
525 gluTessCallback(tess, GLU_TESS_COMBINE, (void(CALLBACK*)())ntessCombine);
526 gluTessCallback(tess, GLU_TESS_END, (void(CALLBACK*)())ntessEnd);
527 if (levelinfo) fprintf(levelinfo, "sector %i, %i lines in sector\n", num, sectors[num].linecount);
528 // remove any line which has both sides in the same sector (i.e. Doom2 Map01 Sector 1)
529 for (i=0; i<sectors[num].linecount; i++)
530 {
531 lineadded[i]=false;
532 if (sectors[num].lines[i]->sidenum[0]!=NO_INDEX)
533 if (sectors[num].lines[i]->sidenum[1]!=NO_INDEX)
534 if (sides[sectors[num].lines[i]->sidenum[0]].sector
535 ==sides[sectors[num].lines[i]->sidenum[1]].sector)
536 {
537 lineadded[i]=true;
538 if (levelinfo) fprintf(levelinfo, "line %4i (iLineID %4i) has both sides in same sector (removed)\n", i, sectors[num].lines[i]->iLineID);
539 }
540 }
541 // e6y
542 // Remove any line which has a clone with the same vertexes and orientation
543 // (i.e. MM.WAD Map22 lines 1298 and 2397)
544 // There is no more HOM on Memento Mori MAP22 sector 299
545 for (i = 0; i < sectors[num].linecount - 1; i++)
546 {
547 int j;
548 for (j = i + 1; j < sectors[num].linecount; j++)
549 {
550 if (sectors[num].lines[i]->v1 == sectors[num].lines[j]->v1 &&
551 sectors[num].lines[i]->v2 == sectors[num].lines[j]->v2 &&
552 sectors[num].lines[i]->frontsector == sectors[num].lines[j]->frontsector &&
553 sectors[num].lines[i]->backsector == sectors[num].lines[j]->backsector &&
554 lineadded[i] == false && lineadded[j] == false)
555 {
556 lineadded[i] = true;
557 }
558 }
559 }
560
561 // initialize variables
562 linecount=sectors[num].linecount;
563 oldline=0;
564 currentline=0;
565 startvertex=sectors[num].lines[currentline]->v2;
566 currentloop=0;
567 vertexnum=0;
568 maxvertexnum=0;
569 // start tesselator
570 if (levelinfo) fprintf(levelinfo, "gluTessBeginPolygon\n");
571 gluTessBeginPolygon(tess, NULL);
572 if (levelinfo) fprintf(levelinfo, "\tgluTessBeginContour\n");
573 gluTessBeginContour(tess);
574 while (linecount)
575 {
576 // if there is no connected line, then start new loop
577 if ((oldline==currentline) || (startvertex==currentvertex))
578 {
579 currentline=-1;
580 for (i=0; i<sectors[num].linecount; i++)
581 if (!lineadded[i])
582 {
583 currentline=i;
584 currentloop++;
585 if ((sectors[num].lines[currentline]->sidenum[0]!=NO_INDEX) ? (sides[sectors[num].lines[currentline]->sidenum[0]].sector==&sectors[num]) : false)
586 startvertex=sectors[num].lines[currentline]->v1;
587 else
588 startvertex=sectors[num].lines[currentline]->v2;
589 if (levelinfo) fprintf(levelinfo, "\tNew Loop %3i\n", currentloop);
590 if (oldline!=0)
591 {
592 if (levelinfo) fprintf(levelinfo, "\tgluTessEndContour\n");
593 gluTessEndContour(tess);
594 // if (levelinfo) fprintf(levelinfo, "\tgluNextContour\n");
595 // gluNextContour(tess, GLU_CW);
596 if (levelinfo) fprintf(levelinfo, "\tgluTessBeginContour\n");
597 gluTessBeginContour(tess);
598 }
599 break;
600 }
601 }
602 if (currentline==-1)
603 break;
604 // add current line
605 lineadded[currentline]=true;
606 // check if currentsector is on the front side of the line ...
607 if ((sectors[num].lines[currentline]->sidenum[0]!=NO_INDEX) ? (sides[sectors[num].lines[currentline]->sidenum[0]].sector==&sectors[num]) : false)
608 {
609 // v2 is ending vertex
610 currentvertex=sectors[num].lines[currentline]->v2;
611 // calculate the angle of this line for use below
612 lineangle = R_PointToAngle2(sectors[num].lines[currentline]->v1->x,sectors[num].lines[currentline]->v1->y,sectors[num].lines[currentline]->v2->x,sectors[num].lines[currentline]->v2->y);
613 lineangle=(lineangle>>ANGLETOFINESHIFT)*360/8192;
614
615 //e6y: direction of a line shouldn't be changed
616 //if (lineangle>=180)
617 // lineangle=lineangle-360;
618
619 if (levelinfo) fprintf(levelinfo, "\t\tAdded Line %4i to Loop, iLineID %5i, Angle: %4i, flipped false\n", currentline, sectors[num].lines[currentline]->iLineID, lineangle);
620 }
621 else // ... or on the back side
622 {
623 // v1 is ending vertex
624 currentvertex=sectors[num].lines[currentline]->v1;
625 // calculate the angle of this line for use below
626 lineangle = R_PointToAngle2(sectors[num].lines[currentline]->v2->x,sectors[num].lines[currentline]->v2->y,sectors[num].lines[currentline]->v1->x,sectors[num].lines[currentline]->v1->y);
627 lineangle=(lineangle>>ANGLETOFINESHIFT)*360/8192;
628
629 //e6y: direction of a line shouldn't be changed
630 //if (lineangle>=180)
631 // lineangle=lineangle-360;
632
633 if (levelinfo) fprintf(levelinfo, "\t\tAdded Line %4i to Loop, iLineID %5i, Angle: %4i, flipped true\n", currentline, sectors[num].lines[currentline]->iLineID, lineangle);
634 }
635 if (vertexnum>=maxvertexnum)
636 {
637 maxvertexnum+=512;
638 v=Z_Realloc(v,maxvertexnum*3*sizeof(double));
639 }
640 // calculate coordinates for the glu tesselation functions
641 v[vertexnum*3+0]=-(double)currentvertex->x/(double)MAP_SCALE;
642 v[vertexnum*3+1]=0.0;
643 v[vertexnum*3+2]= (double)currentvertex->y/(double)MAP_SCALE;
644 // add the vertex to the tesselator, currentvertex is the pointer to the vertexlist of doom
645 // v[vertexnum] is the GLdouble array of the current vertex
646 if (levelinfo) fprintf(levelinfo, "\t\tgluTessVertex(%i, %i)\n",currentvertex->x>>FRACBITS,currentvertex->y>>FRACBITS);
647 gluTessVertex(tess, &v[vertexnum*3], currentvertex);
648 // increase vertexindex
649 vertexnum++;
650 // decrease linecount of current sector
651 linecount--;
652 // find the next line
653 oldline=currentline; // if this isn't changed at the end of the search, a new loop will start
654 bestline=-1; // set to start values
655 bestlinecount=0;
656 // set backsector if there is one
657 /*if (sectors[num].lines[currentline]->sidenum[1]!=NO_INDEX)
658 backsector=sides[sectors[num].lines[currentline]->sidenum[1]].sector;
659 else
660 backsector=NULL;*/
661 // search through all lines of the current sector
662 for (i=0; i<sectors[num].linecount; i++)
663 if (!lineadded[i]) // if the line isn't already added ...
664 // check if one of the vertexes is the same as the current vertex
665 if ((sectors[num].lines[i]->v1==currentvertex) || (sectors[num].lines[i]->v2==currentvertex))
666 {
667 // calculate the angle of this best line candidate
668 if ((sectors[num].lines[i]->sidenum[0]!=NO_INDEX) ? (sides[sectors[num].lines[i]->sidenum[0]].sector==&sectors[num]) : false)
669 angle = R_PointToAngle2(sectors[num].lines[i]->v1->x,sectors[num].lines[i]->v1->y,sectors[num].lines[i]->v2->x,sectors[num].lines[i]->v2->y);
670 else
671 angle = R_PointToAngle2(sectors[num].lines[i]->v2->x,sectors[num].lines[i]->v2->y,sectors[num].lines[i]->v1->x,sectors[num].lines[i]->v1->y);
672 angle=(angle>>ANGLETOFINESHIFT)*360/8192;
673
674 //e6y: direction of a line shouldn't be changed
675 //if (angle>=180)
676 // angle=angle-360;
677
678 // check if line is flipped ...
679 if ((sectors[num].lines[i]->sidenum[0]!=NO_INDEX) ? (sides[sectors[num].lines[i]->sidenum[0]].sector==&sectors[num]) : false)
680 {
681 // when the line is not flipped and startvertex is not the currentvertex then skip this line
682 if (sectors[num].lines[i]->v1!=currentvertex)
683 continue;
684 }
685 else
686 {
687 // when the line is flipped and endvertex is not the currentvertex then skip this line
688 if (sectors[num].lines[i]->v2!=currentvertex)
689 continue;
690 }
691 // set new best line candidate
692 if (bestline==-1) // if this is the first one ...
693 {
694 bestline=i;
695 bestangle=lineangle-angle;
696 bestlinecount++;
697 }
698 else
699 // check if the angle between the current line and this best line candidate is smaller then
700 // the angle of the last candidate
701 // e6y: for finding an angle between AB and BC vectors we should subtract
702 // (BC - BA) == (BC - (180 - AB)) == (angle-(180-lineangle))
703 if (D_abs((int) angle - (180 - (int) lineangle))<D_abs((int) bestangle))
704 {
705 bestline=i;
706 bestangle=angle-(180-lineangle);
707 bestlinecount++;
708 }
709 }
710 if (bestline!=-1) // if a line is found, make it the current line
711 {
712 currentline=bestline;
713 if (bestlinecount>1)
714 if (levelinfo) fprintf(levelinfo, "\t\tBestlinecount: %4i\n", bestlinecount);
715 }
716 }
717 // let the tesselator calculate the loops
718 if (levelinfo) fprintf(levelinfo, "\tgluTessEndContour\n");
719 gluTessEndContour(tess);
720 if (levelinfo) fprintf(levelinfo, "gluTessEndPolygon\n");
721 gluTessEndPolygon(tess);
722 // clean memory
723 gluDeleteTess(tess);
724 Z_Free(v);
725 Z_Free(lineadded);
726 }
727
728 /********************************************
729 * Name : gld_GetSubSectorVertices *
730 * created : 08/13/00 *
731 * modified : 09/18/00, adapted for PrBoom *
732 * author : figgi *
733 * what : prepares subsectorvertices *
734 * (glnodes only) *
735 ********************************************/
736
737 static void gld_GetSubSectorVertices(void)
738 {
739 int i, j;
740 int numedgepoints;
741
742 for(i = 0; i < numsubsectors; i++)
743 {
744 subsector_t* ssector = &subsectors[i];
745
746 if (!gld_TriangulateSubsector(ssector))
747 continue;
748
749 numedgepoints = ssector->numlines;
750
751 gld_AddGlobalVertexes(numedgepoints);
752
753 if (flats_vbo)
754 {
755 gld_SetupSubsectorLoop(ssector, i, numedgepoints);
756
757 for (j = 0; j < numedgepoints; j++)
758 {
759 flats_vbo[gld_num_vertexes].u =( (float)(segs[ssector->firstline + j].v1->x)/FRACUNIT)/64.0f;
760 flats_vbo[gld_num_vertexes].v =(-(float)(segs[ssector->firstline + j].v1->y)/FRACUNIT)/64.0f;
761 flats_vbo[gld_num_vertexes].x = -(float)(segs[ssector->firstline + j].v1->x)/MAP_SCALE;
762 flats_vbo[gld_num_vertexes].y = 0.0f;
763 flats_vbo[gld_num_vertexes].z = (float)(segs[ssector->firstline + j].v1->y)/MAP_SCALE;
764 gld_num_vertexes++;
765 }
766 }
767 }
768 }
769
770 // gld_PreprocessLevel
771 //
772 // this checks all sectors if they are closed and calls gld_PrecalculateSector to
773 // calculate the loops for every sector
774 // the idea to check for closed sectors is from DEU. check next commentary
775 /*
776 Note from RQ:
777 This is a very simple idea, but it works! The first test (above)
778 checks that all Sectors are closed. But if a closed set of LineDefs
779 is moved out of a Sector and has all its "external" SideDefs pointing
780 to that Sector instead of the new one, then we need a second test.
781 That's why I check if the SideDefs facing each other are bound to
782 the same Sector.
783
784 Other note from RQ:
785 Nowadays, what makes the power of a good editor is its automatic tests.
786 So, if you are writing another Doom editor, you will probably want
787 to do the same kind of tests in your program. Fine, but if you use
788 these ideas, don't forget to credit DEU... Just a reminder... :-)
789 */
790 // so I credited DEU
791
792 //
793 // e6y
794 // All sectors which are inside 64x64 square of map grid should be marked here.
795 // GL_CLAMP instead of GL_REPEAT should be used for them for avoiding seams
796 //
797
798 static void gld_MarkSectorsForClamp(void)
799 {
800 int i;
801
802 for (i = 0; i < numsectors; i++)
803 {
804 int loopnum; // current loop number
805 GLLoopDef *currentloop; // the current loop
806 GLfloat minu, maxu, minv, maxv;
807 dboolean fail;
808
809 minu = minv = 65535;
810 maxu = maxv = -65535;
811 fail = false;
812
813 for (loopnum = 0; !fail && loopnum < sectorloops[i].loopcount; loopnum++)
814 {
815 int vertexnum;
816 // set the current loop
817 currentloop = &sectorloops[i].loops[loopnum];
818 if (!currentloop)
819 continue;
820 for (vertexnum = currentloop->vertexindex; !fail && vertexnum<(currentloop->vertexindex+currentloop->vertexcount); vertexnum++)
821 {
822 vbo_xyz_uv_t *vbo;
823 vbo = &flats_vbo[vertexnum];
824
825 if (vbo->u < minu) minu = (float)floor(vbo->u);
826 if (vbo->v < minv) minv = (float)floor(vbo->v);
827 if (vbo->u > maxu) maxu = vbo->u;
828 if (vbo->v > maxv) maxv = vbo->v;
829
830 fail = (maxu - minu > 1.0f || maxv - minv > 1.0f);
831 }
832 }
833
834 if (!fail)
835 {
836 sectorloops[i].flags = SECTOR_CLAMPXY;
837
838 for (loopnum=0; loopnum<sectorloops[i].loopcount; loopnum++)
839 {
840 int vertexnum;
841 // set the current loop
842 currentloop = &sectorloops[i].loops[loopnum];
843 if (!currentloop)
844 continue;
845 for (vertexnum = currentloop->vertexindex; vertexnum < (currentloop->vertexindex+currentloop->vertexcount); vertexnum++)
846 {
847 flats_vbo[vertexnum].u = flats_vbo[vertexnum].u - minu;
848 flats_vbo[vertexnum].v = flats_vbo[vertexnum].v - minv;
849 }
850 }
851 }
852 }
853 }
854
855 static void gld_PreprocessSectors(void)
856 {
857 char *vertexcheck = NULL;
858 char *vertexcheck2 = NULL;
859 int v1num;
860 int v2num;
861 int i;
862 int j;
863
864 if (numsectors)
865 {
866 sectorloops=Z_Malloc(sizeof(GLSector)*numsectors);
867 if (!sectorloops)
868 I_Error("gld_PreprocessSectors: Not enough memory for array sectorloops");
869 memset(sectorloops, 0, sizeof(GLSector)*numsectors);
870 }
871
872 if (numsubsectors)
873 {
874 subsectorloops=Z_Malloc(sizeof(GLMapSubsector)*numsubsectors);
875 if (!subsectorloops)
876 I_Error("gld_PreprocessSectors: Not enough memory for array subsectorloops");
877 memset(subsectorloops, 0, sizeof(GLMapSubsector)*numsubsectors);
878 }
879
880 if (numsegs)
881 {
882 segrendered=Z_Calloc(numsegs, sizeof(byte));
883 if (!segrendered)
884 I_Error("gld_PreprocessSectors: Not enough memory for array segrendered");
885 }
886
887 if (numlines)
888 {
889 linerendered[0]=Z_Calloc(numlines, sizeof(*linerendered[0]));
890 linerendered[1]=Z_Calloc(numlines, sizeof(*linerendered[1]));
891 if (!linerendered[0] || !linerendered[1])
892 I_Error("gld_PreprocessSectors: Not enough memory for array linerendered");
893 }
894
895 flats_vbo = NULL;
896 gld_max_vertexes=0;
897 gld_num_vertexes=0;
898 if (numvertexes)
899 {
900 gld_AddGlobalVertexes(numvertexes*2);
901 }
902
903 if (numvertexes)
904 {
905 vertexcheck=Z_Malloc(numvertexes*sizeof(vertexcheck[0]));
906 vertexcheck2=Z_Malloc(numvertexes*sizeof(vertexcheck2[0]));
907 if (!vertexcheck || !vertexcheck2)
908 {
909 if (levelinfo) fclose(levelinfo);
910 I_Error("gld_PreprocessSectors: Not enough memory for array vertexcheck");
911 return;
912 }
913 }
914
915 for (i=0; i<numsectors; i++)
916 {
917 memset(vertexcheck,0,numvertexes*sizeof(vertexcheck[0]));
918 memset(vertexcheck2,0,numvertexes*sizeof(vertexcheck2[0]));
919 for (j=0; j<sectors[i].linecount; j++)
920 {
921 line_t *l = sectors[i].lines[j];
922 v1num = l->v1 - vertexes;
923 v2num = l->v2 - vertexes;
924
925 // e6y: for correct handling of missing textures.
926 // We do not need to apply some algos for isolated lines.
927 vertexcheck2[v1num]++;
928 vertexcheck2[v2num]++;
929
930 if (l->frontsector == l->backsector)
931 continue;
932
933 if (l->frontsector == &sectors[i])
934 {
935 vertexcheck[v1num]|=1;
936 vertexcheck[v2num]|=2;
937 }
938 else
939 {
940 vertexcheck[v1num]|=2;
941 vertexcheck[v2num]|=1;
942 }
943 }
944 if (sectors[i].linecount<3)
945 {
946 #ifdef PRBOOM_DEBUG
947 lprintf(LO_ERROR, "sector %i is not closed! %i lines in sector\n", i, sectors[i].linecount);
948 #endif
949 if (levelinfo) fprintf(levelinfo, "sector %i is not closed! %i lines in sector\n", i, sectors[i].linecount);
950 sectors[i].flags &= ~SECTOR_IS_CLOSED;
951 }
952 else
953 {
954 sectors[i].flags |= SECTOR_IS_CLOSED;
955 for (j=0; j<numvertexes; j++)
956 {
957 if ((vertexcheck[j]==1) || (vertexcheck[j]==2))
958 {
959 #ifdef PRBOOM_DEBUG
960 lprintf(LO_ERROR, "sector %i is not closed at vertex %i ! %i lines in sector\n", i, j, sectors[i].linecount);
961 #endif
962 if (levelinfo) fprintf(levelinfo, "sector %i is not closed at vertex %i ! %i lines in sector\n", i, j, sectors[i].linecount);
963 sectors[i].flags &= ~SECTOR_IS_CLOSED;
964 }
965 }
966 }
967
968 // e6y: marking all the isolated lines
969 for (j=0; j<sectors[i].linecount; j++)
970 {
971 v1num=((intptr_t)sectors[i].lines[j]->v1-(intptr_t)vertexes)/sizeof(vertex_t);
972 v2num=((intptr_t)sectors[i].lines[j]->v2-(intptr_t)vertexes)/sizeof(vertex_t);
973 if (vertexcheck2[v1num] < 2 && vertexcheck2[v2num] < 2)
974 {
975 sectors[i].lines[j]->r_flags |= RF_ISOLATED;
976 }
977 }
978
979 // figgi -- adapted for glnodes
980 if (sectors[i].flags & SECTOR_IS_CLOSED)
981 gld_PrecalculateSector(i);
982 }
983 Z_Free(vertexcheck);
984 Z_Free(vertexcheck2);
985
986 // figgi -- adapted for glnodes
987 if (numnodes)
988 {
989 if (!use_gl_nodes)
990 gld_CarveFlats(numnodes-1, 0, 0);
991 else
992 gld_GetSubSectorVertices();
993 }
994
995 gld_ProcessTexturedMap();
996
997 if (levelinfo) fclose(levelinfo);
998
999 //e6y: for seamless rendering
1000 gld_MarkSectorsForClamp();
1001 }
1002
1003 static void gld_PreprocessSegs(void)
1004 {
1005 int i;
1006
1007 gl_segs=Z_Malloc(numsegs*sizeof(GLSeg));
1008 for (i=0; i<numsegs; i++)
1009 {
1010 gl_segs[i].x1=-(float)segs[i].v1->x/(float)MAP_SCALE;
1011 gl_segs[i].z1= (float)segs[i].v1->y/(float)MAP_SCALE;
1012 gl_segs[i].x2=-(float)segs[i].v2->x/(float)MAP_SCALE;
1013 gl_segs[i].z2= (float)segs[i].v2->y/(float)MAP_SCALE;
1014 }
1015
1016 gl_lines=Z_Malloc(numlines*sizeof(GLSeg));
1017 for (i=0; i<numlines; i++)
1018 {
1019 gl_lines[i].x1=-(float)lines[i].v1->x/(float)MAP_SCALE;
1020 gl_lines[i].z1= (float)lines[i].v1->y/(float)MAP_SCALE;
1021 gl_lines[i].x2=-(float)lines[i].v2->x/(float)MAP_SCALE;
1022 gl_lines[i].z2= (float)lines[i].v2->y/(float)MAP_SCALE;
1023 }
1024 }
1025
1026 void gld_PreprocessLevel(void)
1027 {
1028 // e6y: speedup of level reloading
1029 // Do not preprocess GL data twice for same level
1030 if (!gl_preprocessed)
1031 {
1032 int i;
1033 static int numsectors_prev = 0;
1034 static int numsubsectors_prev = 0;
1035
1036 Z_Free(gl_segs);
1037 Z_Free(gl_lines);
1038
1039 Z_Free(flats_vbo);
1040 flats_vbo = NULL;
1041
1042 Z_Free(segrendered);
1043 Z_Free(linerendered[0]);
1044 Z_Free(linerendered[1]);
1045
1046 for (i = 0; i < numsectors_prev; i++)
1047 {
1048 Z_Free(sectorloops[i].loops);
1049 }
1050 Z_Free(sectorloops);
1051 for (i = 0; i < numsubsectors_prev; i++)
1052 {
1053 Z_Free(subsectorloops[i].loops);
1054 }
1055 Z_Free(subsectorloops);
1056
1057 gld_Precache();
1058 gld_PreprocessSectors();
1059 gld_PreprocessFakeSectors();
1060 gld_PreprocessSegs();
1061
1062 numsectors_prev = numsectors;
1063 numsubsectors_prev = numsubsectors;
1064 }
1065 else
1066 {
1067 gld_PreprocessFakeSectors();
1068
1069 memset(segrendered, 0, numsegs*sizeof(segrendered[0]));
1070 memset(linerendered[0], 0, numlines*sizeof(linerendered[0][0]));
1071 memset(linerendered[1], 0, numlines*sizeof(linerendered[1][0]));
1072 }
1073
1074 rendermarker = 0;
1075
1076 gld_ResetLastTexture();
1077 gld_ResetTexturedAutomap();
1078
1079 gld_FreeDrawInfo();
1080
1081 if (!gl_preprocessed)
1082 {
1083 if (gl_ext_arb_vertex_buffer_object)
1084 {
1085 if (flats_vbo_id)
1086 {
1087 // delete VBO when already exists
1088 GLEXT_glDeleteBuffersARB(1, &flats_vbo_id);
1089 }
1090 // generate a new VBO and get the associated ID
1091 GLEXT_glGenBuffersARB(1, &flats_vbo_id);
1092 // bind VBO in order to use
1093 GLEXT_glBindBufferARB(GL_ARRAY_BUFFER, flats_vbo_id);
1094 // upload data to VBO
1095 GLEXT_glBufferDataARB(GL_ARRAY_BUFFER,
1096 gld_num_vertexes * sizeof(flats_vbo[0]),
1097 flats_vbo, GL_STATIC_DRAW_ARB);
1098
1099 Z_Free(flats_vbo);
1100 flats_vbo = NULL;
1101 }
1102 }
1103
1104 //e6y
1105 gld_InitVertexData();
1106
1107 gl_preprocessed = true;
1108 }
1109
1110 /*****************************
1111 *
1112 * AUTOMAP
1113 *
1114 *****************************/
1115
1116 void gld_ProcessTexturedMap(void)
1117 {
1118 if (subsectorloops && subsectorloops[0].loops == NULL)
1119 {
1120 gld_TurnOnSubsectorTriangulation();
1121
1122 if (!use_gl_nodes)
1123 gld_CarveFlats(numnodes-1, 0, 0);
1124 else
1125 gld_GetSubSectorVertices();
1126
1127 gld_TurnOffSubsectorTriangulation();
1128 }
1129 }