-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_particles.cpp
237 lines (194 loc) · 6.6 KB
/
render_particles.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* GPU Smoldyn: Smoldyn algorithm ported to the GPU using CUDA 2.2
* Writtern By Lorenzo Dematté, 2010-2011
*
* This file is part of GPU Smoldyn
*
* GPU Smoldyn is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GPU Smoldyn is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*
* Based on algorithm and source code of Smoldyn, written by Steve Andrews, 2003.
*
* Portions taken by code examples in NVIDIA Whitepapers, GPU Gems 2 and 3,
* Copyright 1993-2009 NVIDIA Corporation, Addison-Wesley and the original authors.
*
*/
#include <GL/glew.h>
#include <math.h>
#include <assert.h>
#include <stdio.h>
#include "render_particles.h"
#include "shaders.h"
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
ParticleRenderer::ParticleRenderer()
: m_pos(0),
m_numParticles(0),
m_pointSize(1.0f),
m_particleRadius(0.125f * 0.5f),
m_program(0),
m_vbo(0),
m_typesVBO(0)
{
_initGL();
}
ParticleRenderer::~ParticleRenderer()
{
m_pos = 0;
}
void ParticleRenderer::setPositions(float *pos, int numParticles)
{
m_pos = pos;
m_numParticles = numParticles;
}
void ParticleRenderer::setBufferObjects(unsigned int posVBO, unsigned int typesVBO, int numParticles)
{
m_vbo = posVBO;
m_typesVBO = typesVBO;
m_numParticles = numParticles;
}
void ParticleRenderer::_drawPoints()
{
if (!m_vbo)
{
glBegin(GL_POINTS);
{
int k = 0;
for (int i = 0; i < m_numParticles; ++i)
{
glVertex3fv(&m_pos[k]);
k += 4;
}
}
glEnd();
}
else
{
glBindBufferARB(GL_ARRAY_BUFFER_ARB, m_vbo);
glVertexPointer(4, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
if (m_typesVBO)
{
//glBindBufferARB(GL_ARRAY_BUFFER_ARB, m_colorVBO);
//glColorPointer(4, GL_FLOAT, 0, 0);
//glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, m_typesVBO);
//void* mem = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY_ARB);
//GLenum err = glGetError();
//glUnmapBuffer(GL_ARRAY_BUFFER);
//bind to TEX1, not TEX0!!
//glClientActiveTextureARB(GL_TEXTURE1_ARB);
//glTexCoordPointer(1, GL_INT, 0, 0);
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);
int loc = glGetAttribLocation(m_program, "type");
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, 1, GL_INT, GL_FALSE, 0, 0);
}
glDrawArrays(GL_POINTS, 0, m_numParticles);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableVertexAttribArray(glGetAttribLocation(m_program, "type"));
//glDisableClientState(GL_COLOR_ARRAY);
//glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//glClientActiveTextureARB(GL_TEXTURE0_ARB);
}
}
void ParticleRenderer::display(DisplayMode mode /* = PARTICLE_POINTS */)
{
switch (mode)
{
case PARTICLE_POINTS:
glColor3f(1, 1, 1);
glPointSize(m_pointSize);
_drawPoints();
break;
default:
case PARTICLE_SPHERES:
glEnable(GL_POINT_SPRITE_ARB);
glClientActiveTextureARB(GL_TEXTURE0_ARB);
glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glUseProgram(m_program);
glUniform1f( glGetUniformLocation(m_program, "pointScale"), m_window_h / tanf(m_fov*0.5f*(float)M_PI/180.0f) );
glUniform1f( glGetUniformLocation(m_program, "pointRadius"), m_particleRadius );
float colors[] = { 0, 1, 0, 1,
1, 0, 0, 1,
0, 0, 1, 1,
1, 1, 0, 1,
0, 1, 1, 1,
1, 0, 1, 1,
0, 1, 0, 1,
1, 0, 0, 1,
0, 0, 1, 1,
1, 1, 0, 1,
0, 1, 1, 1,
1, 0, 1, 1,
0, 1, 0, 1,
1, 0, 0, 1,
0, 0, 1, 1,
1, 1, 0, 1,
0, 1, 1, 1,
1, 0, 1, 1,
0, 1, 0, 1,
1, 0, 0, 1,
0, 0, 1, 1,
1, 1, 0, 1,
0, 1, 1, 1,
1, 0, 1, 1,
1, 1, 1, 0
};
glUniform4fv(glGetUniformLocation(m_program, "colors"), 25, colors);
glColor3f(1, 1, 1);
_drawPoints();
glUseProgram(0);
glDisable(GL_POINT_SPRITE_ARB);
break;
}
}
GLuint
ParticleRenderer::_compileProgram(const char *vsource, const char *fsource)
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertexShader, 1, &vsource, 0);
glShaderSource(fragmentShader, 1, &fsource, 0);
glCompileShader(vertexShader);
glCompileShader(fragmentShader);
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
//glBindAttribLocation(program, 1, "type");
glLinkProgram(program);
// check if program linked
GLint success = 0;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
char temp[1024];
glGetProgramInfoLog(program, 1024, 0, temp);
printf("Failed to link program:\n%s\n", temp);
glDeleteProgram(program);
program = 0;
}
return program;
}
void ParticleRenderer::_initGL()
{
m_program = _compileProgram(vertexShader, spherePixelShader);
#if !defined(__APPLE__) && !defined(MACOSX)
glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE);
glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE);
#endif
}