]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/extra/bsp/qbsp3/glfile.c
Merge branch 'NateEag-master-patch-12920' into 'master'
[xonotic/netradiant.git] / tools / quake2 / extra / bsp / qbsp3 / glfile.c
1 /*
2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
4
5 This file is part of Quake 2 Tools source code.
6
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 ===========================================================================
21 */
22
23 #include "qbsp.h"
24
25 int             c_glfaces;
26
27 int PortalVisibleSides (portal_t *p)
28 {
29         int             fcon, bcon;
30
31         if (!p->onnode)
32                 return 0;               // outside
33
34         fcon = p->nodes[0]->contents;
35         bcon = p->nodes[1]->contents;
36
37         // same contents never create a face
38         if (fcon == bcon)
39                 return 0;
40
41         // FIXME: is this correct now?
42         if (!fcon)
43                 return 1;
44         if (!bcon)
45                 return 2;
46         return 0;
47 }
48
49 void OutputWinding (winding_t *w, FILE *glview)
50 {
51         static  int     level = 128;
52         vec_t           light;
53         int                     i;
54
55         fprintf (glview, "%i\n", w->numpoints);
56         level+=28;
57         light = (level&255)/255.0;
58         for (i=0 ; i<w->numpoints ; i++)
59         {
60                 fprintf (glview, "%6.3f %6.3f %6.3f %6.3f %6.3f %6.3f\n",
61                         w->p[i][0],
62                         w->p[i][1],
63                         w->p[i][2],
64                         light,
65                         light,
66                         light);
67         }
68         fprintf (glview, "\n");
69 }
70
71 /*
72 =============
73 OutputPortal
74 =============
75 */
76 void OutputPortal (portal_t *p, FILE *glview)
77 {
78         winding_t       *w;
79         int             sides;
80
81         sides = PortalVisibleSides (p);
82         if (!sides)
83                 return;
84
85         c_glfaces++;
86
87         w = p->winding;
88
89         if (sides == 2)         // back side
90                 w = ReverseWinding (w);
91
92         OutputWinding (w, glview);
93
94         if (sides == 2)
95                 FreeWinding(w);
96 }
97
98 /*
99 =============
100 WriteGLView_r
101 =============
102 */
103 void WriteGLView_r (node_t *node, FILE *glview)
104 {
105         portal_t        *p, *nextp;
106
107         if (node->planenum != PLANENUM_LEAF)
108         {
109                 WriteGLView_r (node->children[0], glview);
110                 WriteGLView_r (node->children[1], glview);
111                 return;
112         }
113
114         // write all the portals
115         for (p=node->portals ; p ; p=nextp)
116         {
117                 if (p->nodes[0] == node)
118                 {
119                         OutputPortal (p, glview);
120                         nextp = p->next[0];
121                 }
122                 else
123                         nextp = p->next[1];
124         }
125 }
126
127 /*
128 =============
129 WriteGLView
130 =============
131 */
132 void WriteGLView (tree_t *tree, char *source)
133 {
134         char    name[1024];
135         FILE    *glview;
136
137         c_glfaces = 0;
138         sprintf (name, "%s%s.gl",outbase, source);
139         printf ("Writing %s\n", name);
140
141         glview = fopen (name, "w");
142         if (!glview)
143                 Error ("Couldn't open %s", name);
144         WriteGLView_r (tree->headnode, glview);
145         fclose (glview);
146
147         printf ("%5i c_glfaces\n", c_glfaces);
148 }
149