-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStarfield.cpp
More file actions
221 lines (190 loc) · 6.25 KB
/
Starfield.cpp
File metadata and controls
221 lines (190 loc) · 6.25 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <apollo/Graphics/glu.h>
#include <SDL_image.h>
#include <apollo/Apollo.h>
#include <apollo/Graphics/Starfield.h>
#include <string>
#include <vector>
#include <TinyXML/tinyxml.h>
#include <apollo/Utilities/ResourceManager.h>
#include <apollo/Utilities/Matrix2x3.h>
namespace Graphics
{
namespace Matrices
{
void SetProjectionMatrix ( const matrix2x3& m );
void SetViewMatrix ( const matrix2x3& m );
void SetModelMatrix ( const matrix2x3& m );
const matrix2x3& CurrentMatrix ();
const matrix2x3& ProjectionMatrix ();
const matrix2x3& ViewMatrix ();
const matrix2x3& ModelMatrix ();
}
namespace StarfieldBuilding
{
std::vector<std::pair<SDL_Surface*, float> > starTypes;
void InitStars ()
{
if (starTypes.empty())
{
// load the XML config file
SDL_RWops* rwops = ResourceManager::OpenFile("Stars/Starfield.xml");
assert(rwops);
size_t len;
char* ptr = (char*)ResourceManager::ReadFull(&len, rwops, 1);
assert(ptr);
char* fileData = (char*)malloc(len + 1);
memcpy(fileData, ptr, len);
fileData[len] = '\0';
TiXmlDocument xmlDoc;
xmlDoc.Parse(fileData);
assert(!xmlDoc.Error());
free((void*)fileData);
TiXmlElement* root = xmlDoc.RootElement();
assert(root);
TiXmlElement* child = root->FirstChildElement("star");
do
{
const char* starName = child->GetText();
assert(starName);
double starFrequency = 0.2f;
child->Attribute("frequency", &starFrequency);
SDL_RWops* rwops = ResourceManager::OpenFile(std::string("Stars/") + starName + ".png");
assert(rwops);
SDL_Surface* surface = IMG_Load_RW(rwops, 1);
assert(surface);
starTypes.push_back(std::make_pair(surface, (float)starFrequency));
} while (child = child->NextSiblingElement());
}
}
static SDL_Surface* surfaceTexture = NULL;
void CreateStarfieldTexture ()
{
void* base = calloc(4, STARFIELD_WIDTH * STARFIELD_HEIGHT);
surfaceTexture = SDL_CreateRGBSurfaceFrom(base, STARFIELD_WIDTH, STARFIELD_HEIGHT, 32, STARFIELD_WIDTH * 4, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
uint8_t* components = (uint8_t*)surfaceTexture->pixels;
for (int i = 0; i < STARFIELD_WIDTH * STARFIELD_HEIGHT; i++)
{
components[(i*4)+3] = 0x00;
}
}
void UploadStarfieldTexture ()
{
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, STARFIELD_WIDTH, STARFIELD_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, surfaceTexture->pixels);
SDL_FreeSurface(surfaceTexture);
surfaceTexture = NULL;
}
void SpatterStars ( SDL_Surface* source, float freq );
void SpatterAllStars ( )
{
for (std::vector<std::pair<SDL_Surface*, float> >::iterator iter = starTypes.begin(); iter != starTypes.end(); iter++)
{
SpatterStars(iter->first, iter->second);
}
}
static const float starfieldSparseness = 16.0f;
void SpatterStars ( SDL_Surface* source, float freq )
{
int count = (freq / starfieldSparseness) * (STARFIELD_WIDTH);
count += (rand() % 3 - 1);
if (count < 0) count = 0;
int w_border = source->w / 2;
int h_border = source->h / 2;
int min_x = w_border;
int max_x = STARFIELD_WIDTH - w_border;
int min_y = h_border;
int max_y = STARFIELD_HEIGHT - h_border;
int x_w = max_x - min_x;
int y_w = max_y - min_y;
assert(w_border >= 0);
assert(h_border >= 0);
assert(min_x >= 0);
assert(max_x >= 0);
assert(min_y >= 0);
assert(max_y >= 0);
assert(x_w > 0);
assert(y_w > 0);
for (int i = 0; i < count; i++)
{
int x, y;
x = (rand() % x_w) + min_x;
y = (rand() % y_w) + min_y;
assert(x >= 0);
assert(y >= 0);
assert(x < STARFIELD_WIDTH);
assert(y < STARFIELD_HEIGHT);
SDL_Rect dstRect;
dstRect.x = (x - w_border);
dstRect.y = (y - h_border);
dstRect.w = w_border * 2;
dstRect.h = h_border * 2;
//glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, x - w_border, y - w_border, source->w, source->h, SDL_BYTEORDER == SDL_BIG_ENDIAN ? GL_RGBA : GL_ABGR_EXT, GL_UNSIGNED_BYTE, source->pixels);
SDL_BlitSurface(source, NULL, surfaceTexture, &dstRect);
}
}
}
using namespace StarfieldBuilding;
Starfield::Starfield ()
{
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
InitStars();
CreateStarfieldTexture();
SpatterAllStars();
UploadStarfieldTexture();
}
Starfield::~Starfield ()
{
glDeleteTextures(1, &texID);
}
vec2 Starfield::Dimensions ( float depth )
{
(void)depth;
return vec2(STARFIELD_WIDTH, STARFIELD_HEIGHT);
}
const static float starfieldScale = 1.98f;
const static float starfieldSpeed = 0.0003f;
void Starfield::Draw ( float depth, vec2 centre )
{
glBindTexture(GL_TEXTURE_2D, texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
matrix2x3 oldProj = Matrices::ProjectionMatrix();
Matrices::SetProjectionMatrix(matrix2x3());
Matrices::SetViewMatrix(matrix2x3());
Matrices::SetModelMatrix(matrix2x3());
vec2 coordinates[4];
coordinates[0] = vec2(-1.0f, -1.0f);
coordinates[1] = vec2(1.0f, -1.0f);
coordinates[2] = vec2(1.0f, 1.0f );
coordinates[3] = vec2(-1.0f, 1.0f );
float depthTransform = depth + 1.0f;
vec2 textureCoordinates[4];
textureCoordinates[0] = (-centre*starfieldSpeed*depthTransform + vec2(-1.0, -1.0)) * starfieldScale;
textureCoordinates[1] = (-centre*starfieldSpeed*depthTransform + vec2( 1.0, -1.0)) * starfieldScale;
textureCoordinates[2] = (-centre*starfieldSpeed*depthTransform + vec2( 1.0, 1.0)) * starfieldScale;
textureCoordinates[3] = (-centre*starfieldSpeed*depthTransform + vec2(-1.0, 1.0)) * starfieldScale;
glVertexPointer(2, GL_FLOAT, 0, coordinates);
glTexCoordPointer(2, GL_FLOAT, 0, textureCoordinates);
glDrawArrays(GL_QUADS, 0, 4);
Matrices::SetProjectionMatrix(oldProj);
/*(void)depth;
Matrices::SetViewMatrix(matrix2x3::Translate(centre));
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texID);
const matrix2x3& mvpMatrix = Matrices::CurrentMatrix();
matrix2x3 mvpInverse = mvpMatrix.Inverse();
vec2 coordinates[4];
coordinates[0] = mvpInverse * vec2(-1.0f, -1.0f);
coordinates[1] = mvpInverse * vec2(1.0f, -1.0f);
coordinates[2] = mvpInverse * vec2(1.0f, 1.0f );
coordinates[3] = mvpInverse * vec2(-1.0f, 1.0f );
vec2 textureReferences[4];
for (int i = 0; i < 4; i++)
{
textureReferences[i] = (coordinates[i] - centre) / starfieldScale;
}
glVertexPointer(2, GL_FLOAT, 0, coordinates);
glTexCoordPointer(2, GL_FLOAT, 0, textureReferences);
glDrawArrays(GL_QUADS, 0, 4);*/
}
}