1 #include "particle_buffer.h"
  2 
  3 void particleBuffer_init( ParticleBuffer* particleBuffer, int maxParticles )
  4 {
  5 	if( quadric == NULL )
  6 	{
  7 		quadric = gluNewQuadric();;
  8 	}
  9 
 10 	particleBuffer->particles = (Particle*)_aligned_malloc( maxParticles * sizeof( Particle ), 16 );
 11 
 12 	particleBuffer->maxParticles = maxParticles;
 13 	particleBuffer->curParticles = 0;
 14 }
 15 
 16 
 17 void particleBuffer_copy( ParticleBuffer* particleBuffer, ParticleBuffer* particleBuffer_out, bool new_pb )
 18 {
 19 	if( new_pb )
 20 	{
 21 		*particleBuffer_out = *particleBuffer;
 22 		particleBuffer_out->particles = (Particle*)_aligned_malloc( sizeof( Particle ) * particleBuffer->maxParticles, 16 );
 23 		particleBuffer_out->constraints = particleBuffer->constraints;//(Particle*)_aligned_malloc( sizeof( Particle ) * particleBuffer->maxParticles, 16 );
 24 	}
 25 
 26 	memcpy( particleBuffer_out->particles,
 27 			particleBuffer->particles,
 28 			particleBuffer->maxParticles * sizeof( Particle ) );
 29 }
 30 
 31 
 32 
 33 
 34 ParticleBuffer* s_pParticleBuffer;
 35 float s_fTimeStep;
 36 
 37 void particles_force_kernel( int thread_index, struct thread_group* thread_group )
 38 {	
 39 	int curParticles    = s_pParticleBuffer->curParticles;
 40 	Particle* particles = s_pParticleBuffer->particles;
 41 
 42 	int lowerbounds = thread_index * total_particles / thread_group->thread_no;
 43 	int upperbounds = lowerbounds + total_particles / thread_group->thread_no;
 44 	
 45 	for( int i = lowerbounds; i < upperbounds; ++i )
 46 	{
 47 		solve_forces_mp( &particles[i], s_pParticleBuffer, s_fTimeStep );
 48 	}
 49 }
 50 
 51 void particles_position_kernel( int thread_index, struct thread_group* thread_group )
 52 {	
 53 	int curParticles    = s_pParticleBuffer->curParticles;
 54 	Particle* particles = s_pParticleBuffer->particles;
 55 
 56 	int lowerbounds = thread_index * total_particles / thread_group->thread_no;
 57 	int upperbounds = lowerbounds + total_particles / thread_group->thread_no;
 58 	
 59 	for( int i = lowerbounds; i < upperbounds; ++i )
 60 	{
 61 		solve_position( &particles[i],
 62 			s_pParticleBuffer->constraints,
 63 			s_pParticleBuffer->constraints_dim,
 64 			s_fTimeStep );
 65 	}
 66 }
 67 
 68 
 69 
 70 
 71 void particleBuffer_solve( ParticleBuffer* particleBuffer, float timeStep )
 72 {
 73 #if defined( SINGLE_PASS )
 74 	int curParticles   = particleBuffer->curParticles;
 75 	Particle* particle = particleBuffer->particles;
 76 	
 77 	for( int i = 0; i < curParticles ; ++i )
 78 	{
 79 		particle_solve( particle++, particleBuffer, timeStep );		
 80 	}
 81 
 82 #elif defined( MULTI_PASS )
 83 	
 84 	int curParticles   = particleBuffer->curParticles;
 85 	Particle* particles = particleBuffer->particles;
 86 	
 87 	//-----------------------------------------------------------------
 88 	// Solve the forces acting on the particle
 89 	//-----------------------------------------------------------------
 90 	for( int i = 0; i < curParticles ; ++i )
 91 	{
 92 		solve_forces_mp( &particles[i], particleBuffer, timeStep );
 93 	}
 94 
 95 	
 96 	//-----------------------------------------------------------------
 97 	// Solve the position of the particles
 98 	//-----------------------------------------------------------------
 99 	for( int i = 0; i < curParticles ; ++i )
100 	{
101 		solve_position( &particles[i], particleBuffer->constraints, particleBuffer->constraints_dim, timeStep );
102 	}
103 
104 #elif defined( MULTI_THREAD )
105 	s_pParticleBuffer = particleBuffer;
106 	s_fTimeStep = timeStep;
107 
108 	//-----------------------------------------------------------------
109 	// Solve the forces acting on the particle
110 	//-----------------------------------------------------------------
111 	thread_group.thread_run = particles_force_kernel;
112 	Thread_Group_execute( &thread_group );
113 	Thread_Group_wait( &thread_group );
114 	
115 	//-----------------------------------------------------------------
116 	// Solve the position of the particles
117 	//-----------------------------------------------------------------
118 	thread_group.thread_run = particles_position_kernel;
119 	Thread_Group_execute( &thread_group );
120 	Thread_Group_wait( &thread_group );
121 #endif
122 }
123 
124 
125 
126 void particleBuffer_draw( ParticleBuffer* particleBuffer )
127 {
128 	int curParticles = particleBuffer->curParticles;
129 	Particle* particle = particleBuffer->particles;
130 #if defined( FACING_PARTICLES )
131 	glDisable( GL_LIGHTING );
132 
133 	Vector3 left_vec;
134 	Vector3 look;
135 	Vector3 up;
136 
137 	vector3_sub( &g_camera.target, &g_camera.position, &look );
138 	vector3_crs( &g_camera.up, &look, &left_vec );
139 	vector3_crs( &left_vec, &look, &up );
140 
141 	vector3_normalize( &left_vec );
142 	vector3_mul( &left_vec, PARTICLE_RADIUS * 2 );
143 
144 	vector3_normalize( &up );
145 	vector3_mul( &up, PARTICLE_RADIUS * 2 );
146 
147 	Vector3 ptn1, ptn2, ptn3, ptn4;
148 
149 	vector3_add( &left_vec, &up, &ptn1 );
150 
151 	vector3_mul( &left_vec, -1.0f );
152 	vector3_mul( &up, -1.0f );
153 
154 	vector3_add( &left_vec, &ptn1, &ptn2 );
155 	vector3_add( &left_vec, &ptn2, &ptn2 );
156 
157 	vector3_add( &up, &ptn2, &ptn3 );
158 	vector3_add( &up, &ptn3, &ptn3 );
159 
160 	vector3_add( &up, &ptn1, &ptn4 );
161 	vector3_add( &up, &ptn4, &ptn4 );
162 
163 
164 	glEnable (GL_BLEND); glBlendFunc (GL_ONE, GL_ZERO);
165 	glDepthFunc( GL_ALWAYS );
166 	glEnable( GL_TEXTURE_2D );
167 	glDisable( GL_LIGHTING );
168 	glBindTexture(GL_TEXTURE_2D, point_texture);	
169 	for( int i = 0; i < curParticles ; ++i )
170 	{
171 		glPushMatrix();
172 		glTranslatef( particle[i].position.x_com,
173 			particle[i].position.y_com,
174 			particle[i].position.z_com );
175 
176 		glBegin( GL_QUADS );
177 			glTexCoord2f(1.0f, 1.0f);glColor4f( 1.0f, 1.0f, 1.0f, 0.7f );glVertex3fv( ptn1.data );
178 			glTexCoord2f(0.0f, 1.0f);glColor4f( 1.0f, 1.0f, 1.0f, 0.7f );glVertex3fv( ptn2.data );
179 			glTexCoord2f(0.0f, 0.0f);glColor4f( 1.0f, 1.0f, 1.0f, 0.7f );glVertex3fv( ptn3.data );
180 			glTexCoord2f(1.0f, 0.0f);glColor4f( 1.0f, 1.0f, 1.0f, 0.7f );glVertex3fv( ptn4.data );
181 		glEnd();
182 		glPopMatrix();
183 	}
184 	glDisable( GL_TEXTURE_2D );
185 	glDisable( GL_TEXTURE_2D );
186 	glDepthFunc( GL_LEQUAL );
187 #elif defined( MESH_PARTICLES )
188 	for( int i = 0; i < curParticles ; ++i )
189 	{
190 		particle_draw( particle + i );
191 	}
192 #endif
193 
194 	glEnable( GL_LIGHTING );
195 
196 	glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE);
197 	glColor4f( 0.0f, 0.0f, 1.0f, 0.5f );
198 
199 	glMatrixMode( GL_MODELVIEW );
200 	glPushMatrix();
201 	glTranslatef( force_point.x_com, 
202 		force_point.y_com, 
203 		force_point.z_com );
204 	gluSphere( quadric, FB_MAX_DISTANCE, 5, 5 );
205 	glPopMatrix();
206 
207 	if( particleBuffer->constraints )
208 	{
209 		Constraint* constraint = particleBuffer->constraints;
210 
211 		for( int i = 0; i < particleBuffer->constraints_dim; ++i )
212 		{
213 			constraint[i].constraintDraw( constraint + i );
214 		}
215 	}
216 }
217 
218 
219 
220 void particle_cube( int rect_x, int rect_y, int rect_z, float radius, float mass, ParticleBuffer* particleBuffer )
221 {
222 	particleBuffer->curParticles = particleBuffer->maxParticles;
223 
224 	for( int x = 0; x < rect_x; ++x )
225 	{
226 		for( int y = 0; y < rect_y; ++y )
227 		{
228 			for( int z = 0; z < rect_z; ++z )
229 			{
230 				Particle *p = &(particleBuffer->particles[ x + y * rect_x + z * rect_x * rect_y ]);
231 				VECTOR_SET( p->position,
232 					x * radius * 2 - ( rect_x - 1 ) * radius,
233 					y * radius * 2 + HEIGHT_OFFSET,
234 					z * radius * 2 - ( rect_z - 1 ) * radius );				
235 				VECTOR_SET( p->velocity,
236 					0.00f,
237 					0.00f,
238 					0.00f );
239 			}
240 		}
241 	}
242 }


syntax highlighted by Code2HTML, v. 0.9.1