-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnow.cpp
184 lines (160 loc) · 4.67 KB
/
snow.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
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
#include "snow.h"
#include "gardenwidget.h"
#include "naivetree.h"
#include <QImage>
#include <QtCore/qmath.h>
#include <GL/glu.h>
const float G = 0.05;
void Particle::init() {
r = g = b = 255;
x = LENGTH * (float(qrand()) / RAND_MAX) - LENGTH / 2;
y = HEIGHT * (1.0 - 0.1 * float(qrand()) / RAND_MAX);
z = WIDTH * (float(qrand()) / RAND_MAX) - WIDTH / 2;
vy = 15 * (float(qrand())/ RAND_MAX);
vx = vz = 0;
ax = 0;
ay = G;
az = 0;
size = 30 + 15 * (float(qrand())/ RAND_MAX);
isdead = false;
}
Snow::Snow(QObject *parent) :
QObject(parent)
{
texture.load("data/snowball.bmp", "BMP");
snow_perframe = 0;
wind_x = wind_y = wind_z = 0;
}
Snow::~Snow() {
glDeleteTextures(1, &texture_id);
}
void Snow::init(Terrain *terrain, Camera *camera, QVector<NaiveTree*> *naiveforest) {
this->terrain = terrain;
this->camera = camera;
this->naiveforest = naiveforest;
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
texture = QGLWidget::convertToGLFormat(texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.width(), texture.height(),
0, GL_RGBA, GL_UNSIGNED_BYTE, texture.bits());
}
void Snow::render() {
glPushAttrib(GL_CURRENT_BIT);
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, texture_id);
float theta = camera->getTheta();
float phi = camera->getPhi();
float cosphi = qCos(phi);
float x00 = qSin(phi) * qSin(theta) - qCos(theta);
float x01 = -qSin(phi) * qSin(theta) - qCos(theta);
float x10 = qSin(phi) * qSin(theta) + qCos(theta);
float x11 = -qSin(phi) * qSin(theta) + qCos(theta);
float z00 = qSin(phi) * qCos(theta) + qSin(theta);
float z01 = -qSin(phi) * qCos(theta) + qSin(theta);
float z10 = qSin(phi) * qCos(theta) - qSin(theta);
float z11 = -qSin(phi) * qCos(theta) - qSin(theta);
int count = 0;
for (Particle &p : particles) {
if (p.isdead) {
if (count < snow_perframe) {
count++;
p.init();
}
continue;
}
glColor4ub(p.r, p.g, p.b, 255);
glNormal3f(qSin(theta), 0, qCos(theta));
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(p.x + p.size * x00, p.y - p.size * cosphi, p.z + p.size * z00);
glTexCoord2f(1, 0); glVertex3f(p.x + p.size * x10, p.y - p.size * cosphi, p.z + p.size * z10);
glTexCoord2f(1, 1); glVertex3f(p.x + p.size * x11, p.y + p.size * cosphi, p.z + p.size * z11);
glTexCoord2f(0, 1); glVertex3f(p.x + p.size * x01, p.y + p.size * cosphi, p.z + p.size * z01);
glEnd();
update_snow(p, count);
}
glPopMatrix();
glDisable(GL_BLEND);
glPopAttrib();
for (; count < snow_perframe; ++count)
particles.push_back(Particle());
}
void Snow::update_snow(Particle &p, int &count) {
p.x += p.vx;
p.y -= p.vy;
p.z += p.vz;
p.vx += p.ax + wind_x;
p.vy += p.ay + wind_y;
p.vz += p.az + wind_z;
if ((p.x <= -WIDTH / 2 || p.x >= WIDTH / 2) ||
(p.z <= -LENGTH / 2 || p.z >= LENGTH / 2) ||
p.y <= 0) {
if (count < snow_perframe) {
count++;
p.init();
}
else
p.isdead = true;
}
else if (p.y < terrain->snowed_height(p.x + WIDTH / 2, p.z + LENGTH / 2)) {
if (terrain->snow_hit(p)) {
if (count < snow_perframe) {
count++;
p.init();
}
else
p.isdead = true;
}
}
else {
for (NaiveTree *t : *naiveforest) {
if (t->hit(p, particles)) {
if (count < snow_perframe) {
count++;
p.init();
}
else
p.isdead = true;
}
}
}
}
int Snow::getSnow_perframe() const
{
return snow_perframe;
}
void Snow::setSnow_perframe(int value)
{
snow_perframe = value;
if (value == 0)
terrain->setSnowing(false);
else
terrain->setSnowing(true);
}
float Snow::getWind_x() const
{
return wind_x;
}
void Snow::setWind_x(float value)
{
wind_x = value;
}
float Snow::getWind_y() const
{
return wind_y;
}
void Snow::setWind_y(float value)
{
wind_y = value;
}
float Snow::getWind_z() const
{
return wind_z;
}
void Snow::setWind_z(float value)
{
wind_z = value;
}