Quake II RTX doxygen  1.0 dev
sky.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 1997-2001 Id Software, Inc.
3 Copyright (C) 2008 Andrey Nazarov
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #include "sw.h"
21 
22 //static float sky_rotate;
23 //static vec3_t sky_axis;
24 
25 static int r_skyframe;
26 static mface_t r_skyfaces[6];
27 static cplane_t r_skyplanes[6];
28 static mtexinfo_t r_skytexinfo[6];
29 static mvertex_t r_skyverts[8];
30 static medge_t r_skyedges[12];
31 static msurfedge_t r_skysurfedges[24];
32 
33 // 3dstudio environment map names
34 static const char r_skysidenames[6][3] = { "rt", "bk", "lf", "ft", "up", "dn" };
35 static const int r_skysideimage[6] = { 5, 2, 4, 1, 0, 3 };
36 
37 // I just copied this data from a box map...
38 static const int box_planes[12] = {
39  2, -128, 0, -128, 2, 128, 1, 128, 0, 128, 1, -128
40 };
41 static const int box_surfedges[24] = {
42  1, 2, 3, 4, 1, 5, 6, 7, 8, 9, 6, 10, 2, 7, 9, 11, 12, 3, 11, 8, 12, 10, 5, 4
43 };
44 static const int box_surfverts[24] = {
45  0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1
46 };
47 static const int box_edges[24] = {
48  1, 2, 2, 3, 3, 4, 4, 1, 1, 5, 5, 6, 6, 2, 7, 8, 8, 6, 5, 7, 8, 3, 7, 4
49 };
50 static const int box_flags[6] = {
51  0, 0, 1, 1, 1, 0
52 };
53 static const vec3_t box_axis[6][2] = {
54  { {0, -1, 0}, { -1, 0, 0} },
55  { {0, 1, 0}, {0, 0, -1} },
56  { {0, -1, 0}, {1, 0, 0} },
57  { {1, 0, 0}, {0, 0, -1} },
58  { {0, -1, 0}, {0, 0, -1} },
59  { { -1, 0, 0}, {0, 0, -1} }
60 };
61 static const vec3_t box_verts[8] = {
62  { -1, -1, -1},
63  { -1, 1, -1},
64  {1, 1, -1},
65  {1, -1, -1},
66  { -1, -1, 1},
67  { -1, 1, 1},
68  {1, -1, 1},
69  {1, 1, 1}
70 };
71 
72 /*
73 ================
74 R_InitSkyBox
75 ================
76 */
77 void R_InitSkyBox(void)
78 {
79  int i;
80 
81  for (i = 0; i < 6; i++) {
82  r_skyplanes[i].normal[box_planes[i * 2]] = 1;
83  r_skyplanes[i].dist = box_planes[i * 2 + 1];
84 
85  VectorCopy(box_axis[i][0], r_skytexinfo[i].axis[0]);
86  VectorCopy(box_axis[i][1], r_skytexinfo[i].axis[1]);
87 
88  r_skyfaces[i].plane = &r_skyplanes[i];
89  r_skyfaces[i].drawflags = box_flags[i] | DSURF_SKY;
90  r_skyfaces[i].numsurfedges = 4;
91  r_skyfaces[i].firstsurfedge = &r_skysurfedges[i * 4];
92  r_skyfaces[i].texinfo = &r_skytexinfo[i];
93  r_skyfaces[i].texturemins[0] = -128;
94  r_skyfaces[i].texturemins[1] = -128;
95  r_skyfaces[i].extents[0] = 256;
96  r_skyfaces[i].extents[1] = 256;
97  }
98 
99  for (i = 0; i < 24; i++) {
100  r_skysurfedges[i].edge = &r_skyedges[box_surfedges[i] - 1];
101  r_skysurfedges[i].vert = box_surfverts[i];
102  }
103 
104  for (i = 0; i < 12; i++) {
105  r_skyedges[i].v[0] = &r_skyverts[box_edges[i * 2 + 0] - 1];
106  r_skyedges[i].v[1] = &r_skyverts[box_edges[i * 2 + 1] - 1];
107  r_skyedges[i].cachededgeoffset = 0;
108  }
109 }
110 
111 /*
112 ================
113 R_EmitSkyBox
114 ================
115 */
116 void R_EmitSkyBox(void)
117 {
118  int i, j;
119  int oldkey;
120 
121  if (insubmodel)
122  return; // submodels should never have skies
123  if (r_skyframe == r_framecount)
124  return; // already set this frame
125 
127 
128  // set the eight fake vertexes
129  for (i = 0; i < 8; i++)
130  for (j = 0; j < 3; j++)
131  r_skyverts[i].point[j] = r_origin[j] + box_verts[i][j] * 128;
132 
133  // set the six fake planes
134  for (i = 0; i < 6; i++)
135  if (box_planes[i * 2 + 1] > 0)
136  r_skyplanes[i].dist = r_origin[box_planes[i * 2]] + 128;
137  else
138  r_skyplanes[i].dist = r_origin[box_planes[i * 2]] - 128;
139 
140  // fix texture offsets
141  for (i = 0; i < 6; i++) {
142  r_skytexinfo[i].offset[0] = -DotProduct(r_origin, r_skytexinfo[i].axis[0]);
143  r_skytexinfo[i].offset[1] = -DotProduct(r_origin, r_skytexinfo[i].axis[1]);
144  }
145 
146  // emit the six faces
147  oldkey = r_currentkey;
148  r_currentkey = 0x7ffffff0;
149  for (i = 0; i < 6; i++) {
150  R_RenderFace(&r_skyfaces[i], 15);
151  }
152  r_currentkey = oldkey; // bsp sorting order
153 }
154 
155 /*
156 ============
157 R_SetSky
158 ============
159 */
160 void R_SetSky(const char *name, float rotate, vec3_t axis)
161 {
162  int i;
163  char path[MAX_QPATH];
164 
165 // sky_rotate = rotate;
166 // VectorCopy(axis, sky_axis);
167 
168  for (i = 0; i < 6; i++) {
169  Q_concat(path, sizeof(path), "env/", name,
170  r_skysidenames[r_skysideimage[i]], ".tga", NULL);
171  FS_NormalizePath(path, path);
172  r_skytexinfo[i].image = IMG_Find(path, IT_SKY, IF_NONE);
173  }
174 }
175 
r_origin
vec3_t r_origin
Definition: main.c:52
box_edges
static const int box_edges[24]
Definition: sky.c:47
r_skyedges
static medge_t r_skyedges[12]
Definition: sky.c:30
box_surfverts
static const int box_surfverts[24]
Definition: sky.c:44
box_planes
static const int box_planes[12]
Definition: sky.c:38
r_framecount
int r_framecount
Definition: main.c:64
R_EmitSkyBox
void R_EmitSkyBox(void)
Definition: sky.c:116
R_RenderFace
void R_RenderFace(mface_t *fa, int clipflags)
Definition: raster.c:328
box_flags
static const int box_flags[6]
Definition: sky.c:50
r_skyplanes
static cplane_t r_skyplanes[6]
Definition: sky.c:27
sw.h
DSURF_SKY
#define DSURF_SKY
Definition: sw.h:43
box_axis
static const vec3_t box_axis[6][2]
Definition: sky.c:53
r_skyverts
static mvertex_t r_skyverts[8]
Definition: sky.c:29
r_skysurfedges
static msurfedge_t r_skysurfedges[24]
Definition: sky.c:31
box_verts
static const vec3_t box_verts[8]
Definition: sky.c:61
IMG_Find
image_t * IMG_Find(const char *name, imagetype_t type, imageflags_t flags)
Definition: images.c:1122
r_currentkey
int r_currentkey
Definition: edge.c:46
R_InitSkyBox
void R_InitSkyBox(void)
Definition: sky.c:77
r_skysideimage
static const int r_skysideimage[6]
Definition: sky.c:35
R_SetSky
void R_SetSky(const char *name, float rotate, vec3_t axis)
Definition: sky.c:160
r_skyframe
static int r_skyframe
Definition: sky.c:25
insubmodel
qboolean insubmodel
Definition: bsp.c:25
Q_concat
size_t Q_concat(char *dest, size_t size,...)
Definition: shared.c:758
box_surfedges
static const int box_surfedges[24]
Definition: sky.c:41
r_skytexinfo
static mtexinfo_t r_skytexinfo[6]
Definition: sky.c:28
r_skysidenames
static const char r_skysidenames[6][3]
Definition: sky.c:34
FS_NormalizePath
size_t FS_NormalizePath(char *out, const char *in)
Definition: files.c:331
r_skyfaces
static mface_t r_skyfaces[6]
Definition: sky.c:26