Initial sauer
[SauerbratenRemote.git] / src / engine / rendersky.cpp
blob3de606a5065f9bbf8e63dca241618721bad55a6a
1 #include "pch.h"
2 #include "engine.h"
4 Texture *sky[6] = { 0, 0, 0, 0, 0, 0 };
5 float spinsky = 0;
7 void loadsky(char *basename, float *spin)
9 spinsky = *spin;
11 static string lastsky = "";
12 if(strcmp(lastsky, basename)==0) return;
13 static const char *side[] = { "ft", "bk", "lf", "rt", "dn", "up" };
14 loopi(6)
16 s_sprintfd(name)("packages/%s_%s.jpg", basename, side[i]);
17 if((sky[i] = textureload(name, 3, true, false))==notexture)
19 strcpy(name+strlen(name)-3, "png");
20 if((sky[i] = textureload(name, 3, true, false))==notexture) conoutf("could not load sky texture packages/%s_%s", basename, side[i]);
23 s_strcpy(lastsky, basename);
26 COMMAND(loadsky, "sf");
28 void draw_envbox_face(float s0, float t0, int x0, int y0, int z0,
29 float s1, float t1, int x1, int y1, int z1,
30 float s2, float t2, int x2, int y2, int z2,
31 float s3, float t3, int x3, int y3, int z3,
32 GLuint texture)
34 glBindTexture(GL_TEXTURE_2D, texture);
35 glBegin(GL_QUADS);
36 glTexCoord2f(s3, t3); glVertex3i(x3, y3, z3);
37 glTexCoord2f(s2, t2); glVertex3i(x2, y2, z2);
38 glTexCoord2f(s1, t1); glVertex3i(x1, y1, z1);
39 glTexCoord2f(s0, t0); glVertex3i(x0, y0, z0);
40 glEnd();
41 xtraverts += 4;
44 void draw_envbox(int w, float zclip = 0.0f)
46 if(!sky[0]) fatal("no skybox");
48 float vclip = 1-zclip;
49 int z = int(ceil(2*w*(vclip-0.5f)));
51 glDepthMask(GL_FALSE);
53 draw_envbox_face(1.0f, vclip, -w, -w, z,
54 0.0f, vclip, w, -w, z,
55 0.0f, 0.0f, w, -w, -w,
56 1.0f, 0.0f, -w, -w, -w, sky[0]->gl);
58 draw_envbox_face(1.0f, vclip, +w, w, z,
59 0.0f, vclip, -w, w, z,
60 0.0f, 0.0f, -w, w, -w,
61 1.0f, 0.0f, +w, w, -w, sky[1]->gl);
63 draw_envbox_face(0.0f, 0.0f, -w, -w, -w,
64 1.0f, 0.0f, -w, w, -w,
65 1.0f, vclip, -w, w, z,
66 0.0f, vclip, -w, -w, z, sky[2]->gl);
68 draw_envbox_face(1.0f, vclip, +w, -w, z,
69 0.0f, vclip, +w, w, z,
70 0.0f, 0.0f, +w, w, -w,
71 1.0f, 0.0f, +w, -w, -w, sky[3]->gl);
73 if(!zclip)
74 draw_envbox_face(0.0f, 1.0f, -w, w, w,
75 0.0f, 0.0f, +w, w, w,
76 1.0f, 0.0f, +w, -w, w,
77 1.0f, 1.0f, -w, -w, w, sky[4]->gl);
79 draw_envbox_face(0.0f, 1.0f, +w, w, -w,
80 0.0f, 0.0f, -w, w, -w,
81 1.0f, 0.0f, -w, -w, -w,
82 1.0f, 1.0f, +w, -w, -w, sky[5]->gl);
84 glDepthMask(GL_TRUE);
87 VARP(sparklyfix, 0, 1, 1);
88 VAR(showsky, 0, 1, 1);
90 bool drawskylimits(bool explicitonly, float zreflect)
92 nocolorshader->set();
94 glDisable(GL_TEXTURE_2D);
95 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
96 bool rendered = rendersky(explicitonly, zreflect);
97 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
98 glEnable(GL_TEXTURE_2D);
100 defaultshader->set();
102 return rendered;
105 void drawskyoutline()
107 notextureshader->set();
109 glDisable(GL_TEXTURE_2D);
110 glDepthMask(GL_FALSE);
111 extern int wireframe;
112 if(!wireframe)
114 glEnable(GL_POLYGON_OFFSET_LINE);
115 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
117 glColor3f(0.5f, 0.0f, 0.5f);
118 rendersky(true);
119 if(!wireframe)
121 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
122 glDisable(GL_POLYGON_OFFSET_LINE);
124 glDepthMask(GL_TRUE);
125 glEnable(GL_TEXTURE_2D);
127 defaultshader->set();
130 void drawskybox(int farplane, bool limited, float zreflect)
132 if(limited && !zreflect)
134 if(!drawskylimits(false, 0) && !editmode && insideworld(camera1->o)) return;
137 bool fog = glIsEnabled(GL_FOG)==GL_TRUE;
138 if(fog) glDisable(GL_FOG);
140 glPushMatrix();
141 glLoadIdentity();
142 glRotatef(camera1->roll, 0, 0, 1);
143 glRotatef(camera1->pitch, -1, 0, 0);
144 glRotatef(camera1->yaw+spinsky*lastmillis/1000.0f, 0, 1, 0);
145 glRotatef(90, 1, 0, 0);
146 if(zreflect && camera1->o.z>=zreflect) glScalef(1, 1, -1);
147 glColor3f(1, 1, 1);
148 extern int ati_skybox_bug;
149 if(limited) glDepthFunc(editmode || !insideworld(camera1->o) || ati_skybox_bug || zreflect ? GL_ALWAYS : GL_GEQUAL);
150 draw_envbox(farplane/2, zreflect ? (zreflect+0.5f*(farplane-hdr.worldsize))/farplane : 0);
151 glPopMatrix();
153 if(limited)
155 glDepthFunc(GL_LESS);
156 if(!zreflect && editmode && showsky) drawskyoutline();
159 if(fog) glEnable(GL_FOG);
162 bool limitsky()
164 extern int ati_skybox_bug, explicitsky;
165 extern double skyarea;
166 return explicitsky || (!ati_skybox_bug && sparklyfix && skyarea / (double(hdr.worldsize)*double(hdr.worldsize)*6) < 0.9);