-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
executable file
·181 lines (148 loc) · 4.49 KB
/
main.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
#include <GL/glew.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> /* for cos(), sin(), and sqrt() */
#include <GL/glut.h>
#include "PManager.h"
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "uistate.h"
int windowWidth = 800, windowHeight = 600;
GLuint partbuf = 0;
const float dt = 16.0f;
// Peter's mystical ui controller for arcball transformation and stuff
static UIState *ui;
void setupEmitters(void)
{
// Create a fountain
PGroup* bounce_group = ParticleManager::get()->newGroup("gravity");
Emitter *e = ParticleManager::get()->newEmitter();
e->setLocation(glm::vec3(0, 0, 0))
->setParticleLocationF(new locationF(0.05f))
->setParticleVelocityF(new coneVelocityF(8.f, 1.f, glm::vec3(0, 1, 0), 0.85f))
->setParticleLifetimeF(new lifetimeNormalF(3.0, 0.1))
->setOutputRate(5000)
->setOutputGroup("gravity");
ParticleManager::get()->addEmitter(e);
bounce_group->addAction(new ConstForceF(6, glm::vec3(0, -1, 0)));
}
void showMessage(GLfloat x, GLfloat y, GLfloat z, char *message)
{
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(x, y, z);
glScalef(.02, .02, .02);
while (*message) {
glutStrokeCharacter(GLUT_STROKE_ROMAN, *message);
message++;
}
glPopMatrix();
}
void displayParticleCount(void)
{
char buffer [50];
static int cnt = 0;
cnt++;
static int np = ParticleManager::get()->numParticles();
if (!(cnt % 100)) np = ParticleManager::get()->numParticles();
sprintf (buffer, "particles: %d", np);
showMessage(-15, 15, 0, buffer);
}
void redraw(void)
{
// Update first
ParticleManager::get()->startUpdate(dt * 0.001f);
ParticleManager::get()->update();
// Now render
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Apply the camera transformation.
ui->ApplyViewingTransformation();
displayParticleCount();
ParticleManager::get()->render(dt * 0.001f);
glutSwapBuffers();
}
void reshape(int width, int height)
{
windowWidth = width;
windowHeight = height;
if( width <= 0 || height <= 0 ) return;
ui->WindowX() = width;
ui->WindowY() = height;
ui->Aspect() = float( width ) / height;
ui->SetupViewport();
ui->SetupViewingFrustum();
}
void mouse(int button, int state, int x, int y)
{
// Just pass it on to the ui controller.
ui->MouseFunction(button, state, x, y);
}
void motion(int x, int y)
{
// Just pass it on to the ui controller.
ui->MotionFunction(x, y);
}
void keyboard(GLubyte key, GLint x, GLint y)
{
// Quit on ESC
if (key == 27)
exit(0);
if (key == 'r')
ParticleManager::get()->startRecording();
if (key == 's')
{
const double *times = ParticleManager::get()->stopRecording();
std::cout << "Recorded times:\n"
<< "creation(us): " << times[0] << '\n'
<< "update(us): " << times[1] << '\n'
<< "render(us): " << times[2] << '\n';
}
}
void idle()
{
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutCreateWindow("kiss_particle demo");
glutDisplayFunc(redraw);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
GLenum err = glewInit();
if (err != GLEW_OK)
{
std::cerr << "Unable to initialize GLEW: " << glewGetErrorString(err) << '\n';
exit(1);
}
setupEmitters();
glEnable(GL_DEPTH_TEST);
ui = new UIState;
ui->Trans() = glm::vec3(0, 0, 0);
ui->Radius() = 80;
ui->Near() = .1;
ui->Far() = 1000;
ui->CTrans() = glm::vec3(0, 0, -40);
glGenBuffers(1, &partbuf);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
void renderParticles(const std::vector<particleData> &data)
{
glBindBuffer(GL_ARRAY_BUFFER, partbuf);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(particleData), &data.front(), GL_STREAM_DRAW);
glMatrixMode(GL_MODELVIEW);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 7*sizeof(float), 0);
glColorPointer(4, GL_FLOAT, 7*sizeof(float), (void *)(3 * sizeof(float)));
glDrawArrays(GL_POINTS, 0, data.size());
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}