forked from skeeto/pixelcity
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sky.cpp
144 lines (109 loc) · 3.46 KB
/
Sky.cpp
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
/*-----------------------------------------------------------------------------
Sky.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
Did this need to be written as a class? It did not. There will never be
more than one sky in play, so the whole class structure here is superflous,
but harmless.
-----------------------------------------------------------------------------*/
#define SKYPOINTS 24
#ifdef WINDOWS
#include <windows.h>
#endif
#include <math.h>
#include <GL/gl.h>
#include "Camera.h"
#include "Macro.h"
#include "Math.h"
#include "Random.h"
#include "Render.h"
#include "Sky.h"
#include "Texture.h"
#include "glTypes.h"
#include "World.h"
static CSky* sky;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void SkyRender ()
{
if (sky && !RenderFlat ())
sky->Render ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void SkyClear ()
{
if(sky)
delete sky;
sky = NULL;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CSky::Render ()
{
GLvector angle, position;
if (!TextureReady ())
return;
glDepthMask (false);
glPushAttrib (GL_POLYGON_BIT | GL_FOG_BIT);
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
glDisable (GL_CULL_FACE);
glDisable (GL_FOG);
glPushMatrix ();
glLoadIdentity();
angle = CameraAngle ();
position = CameraPosition ();
glRotatef (angle.x, 1.0f, 0.0f, 0.0f);
glRotatef (angle.y, 0.0f, 1.0f, 0.0f);
glRotatef (angle.z, 0.0f, 0.0f, 1.0f);
glTranslatef (0.0f, -position.y / 100.0f, 0.0f);
glEnable (GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, TextureId (TEXTURE_SKY));
glCallList (m_list);
glPopMatrix ();
glPopAttrib ();
glDepthMask (true);
glEnable (GL_COLOR_MATERIAL);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CSky::CSky ()
{
GLvertex circle[SKYPOINTS];
GLvector pos;
float angle;
int i;
float size;
float rad;
float lum;
size = 10.0f;
for (i = 0; i < SKYPOINTS; i++) {
angle = (float)i / (float)(SKYPOINTS - 1);
angle *= 360;
angle *= DEGREES_TO_RADIANS;
circle[i].position.x = sinf (angle) * size;
circle[i].position.y = 0.1f;
circle[i].position.z = cosf (angle) * size;
circle[i].uv.x = ((float)i / (float)(SKYPOINTS - 1)) * 5.0f;
circle[i].uv.y = 0.5f;
rad = ((float)i / (SKYPOINTS - 1)) * 180.0f * DEGREES_TO_RADIANS;
lum = sinf (rad);
lum = (float)pow (lum, 5);
circle[i].color = glRgba (lum);
}
m_list = glGenLists(1);
glNewList (m_list, GL_COMPILE);
glColor3f (1, 1, 1);
glBegin (GL_QUAD_STRIP);
for (i = 0; i < SKYPOINTS; i++) {
glTexCoord2f (circle[i].uv.x, 0.0f);
glVertex3fv (&circle[i].position.x);
pos = circle[i].position;
pos.y = size / 3.5f;
glTexCoord2f (circle[i].uv.x, 1.0f);
glVertex3fv (&pos.x);
}
glEnd ();
glEndList();
sky = this;
}