-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStarfield.cpp
159 lines (127 loc) · 3.93 KB
/
Starfield.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//==============================================================================
//
// File: Starfield.cpp
//
// Purpose: Generates and draws a random starfield for the background.
//
// Modified: 04/07/2009 Allen Smith. Creation Date.
//
//==============================================================================
#include "Starfield.h"
#include <cstdlib>
//========== Starfield =========================================================
//
// Purpose: Constructor.
//
//==============================================================================
Starfield::Starfield()
{
displayListTag = 0;
starCount = 0;
}
//========== ~Starfield ========================================================
//
// Purpose: Destructor.
//
//==============================================================================
Starfield::~Starfield()
{
DestroyDisplayList();
}
//========== SetStarCount ======================================================
//
// Purpose: Regenerates a starfield with the specified number of stars.
//
//==============================================================================
void Starfield::SetStarCount(unsigned starCountIn)
{
this->starCount = starCountIn;
GenerateRandomStarfield();
OptimizeDrawing();
}//end SetStarCount
//========== Draw ==============================================================
//
// Purpose: Paints the starfield onscreen.
//
//==============================================================================
void Starfield::Draw()
{
unsigned int counter = 0;
Star currentStar;
if(this->displayListTag == 0)
{
// Immediate-mode drawing
glBegin(GL_QUADS);
{
for(counter = 0; counter < this->starCount; counter++)
{
currentStar = this->stars.at(counter);
glColor3f(currentStar.whiteness, currentStar.whiteness, currentStar.whiteness);
glVertex2f(currentStar.location.x , currentStar.location.y );
glVertex2f(currentStar.location.x + currentStar.size, currentStar.location.y );
glVertex2f(currentStar.location.x + currentStar.size, currentStar.location.y + currentStar.size);
glVertex2f(currentStar.location.x , currentStar.location.y + currentStar.size);
}
}
glEnd();
}
else
{
// Optimize drawing
glCallList(this->displayListTag);
}
}//end Draw
//========== GenerateRandomStarfield ===========================================
//
// Purpose: Creates a pretty starfield for the background
//
//==============================================================================
void Starfield::GenerateRandomStarfield()
{
unsigned counter = 0;
Star currentStar;
// Get rid of the old stars
this->stars.clear();
// Make new ones
for(counter = 0; counter < this->starCount; counter++)
{
currentStar.location.x = rand() % FRAMEWIDTH;
currentStar.location.y = rand() % FRAMEHEIGHT;
currentStar.whiteness = (float) rand() / RAND_MAX;
currentStar.size = (float) rand() / RAND_MAX * 0.75 + 0.25;
this->stars.push_back(currentStar);
}
}//end GenerateRandomStarfield
//========== OptimizeDrawing ===================================================
//
// Purpose: Stores the drawing into a display list, so that it happens very
// quickly next time!
//
//==============================================================================
void Starfield::OptimizeDrawing()
{
GLuint newList = 0;
// clear the current display list.
DestroyDisplayList();
// generate a new one
newList = glGenLists(1);
glNewList(newList, GL_COMPILE);
{
Draw();
}
glEndList();
this->displayListTag = newList;
}//end OptimizeDrawing
//========== DestroyDisplayList ================================================
//
// Purpose: Eliminates the fast optimized display list.
//
//==============================================================================
void Starfield::DestroyDisplayList()
{
if(this->displayListTag != 0)
{
glDeleteLists(this->displayListTag, 1);
this->displayListTag = 0;
}
}//end DestroyDisplayList