mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-25 22:28:17 +08:00
Apply consistent code formatting across the repo. Add clang-format and pre-commit hooks.
This commit is contained in:
@@ -11,66 +11,65 @@
|
||||
|
||||
// OpenGL Graphics includes
|
||||
#define HELPERGL_EXTERN_GL_FUNC_IMPLEMENTATION
|
||||
#include <helper_gl.h>
|
||||
|
||||
#include "particleSystem.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <assert.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cuda_runtime.h>
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_functions.h>
|
||||
#include <helper_gl.h>
|
||||
#include <math.h>
|
||||
#include <memory.h>
|
||||
|
||||
#include "particleSystem.cuh"
|
||||
#include "particles_kernel.cuh"
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#include <helper_functions.h>
|
||||
#include <helper_cuda.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <memory.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
|
||||
#ifndef CUDART_PI_F
|
||||
#define CUDART_PI_F 3.141592654f
|
||||
#define CUDART_PI_F 3.141592654f
|
||||
#endif
|
||||
|
||||
ParticleSystem::ParticleSystem(uint numParticles, uint3 gridSize, bool bUseOpenGL) :
|
||||
m_bInitialized(false),
|
||||
m_bUseOpenGL(bUseOpenGL),
|
||||
m_numParticles(numParticles),
|
||||
m_hPos(0),
|
||||
m_hVel(0),
|
||||
m_dPos(0),
|
||||
m_dVel(0),
|
||||
m_gridSize(gridSize),
|
||||
m_timer(NULL),
|
||||
m_solverIterations(1)
|
||||
ParticleSystem::ParticleSystem(uint numParticles, uint3 gridSize, bool bUseOpenGL)
|
||||
: m_bInitialized(false)
|
||||
, m_bUseOpenGL(bUseOpenGL)
|
||||
, m_numParticles(numParticles)
|
||||
, m_hPos(0)
|
||||
, m_hVel(0)
|
||||
, m_dPos(0)
|
||||
, m_dVel(0)
|
||||
, m_gridSize(gridSize)
|
||||
, m_timer(NULL)
|
||||
, m_solverIterations(1)
|
||||
{
|
||||
m_numGridCells = m_gridSize.x*m_gridSize.y*m_gridSize.z;
|
||||
m_numGridCells = m_gridSize.x * m_gridSize.y * m_gridSize.z;
|
||||
// float3 worldSize = make_float3(2.0f, 2.0f, 2.0f);
|
||||
|
||||
m_gridSortBits = 18; // increase this for larger grids
|
||||
m_gridSortBits = 18; // increase this for larger grids
|
||||
|
||||
// set simulation parameters
|
||||
m_params.gridSize = m_gridSize;
|
||||
m_params.numCells = m_numGridCells;
|
||||
m_params.gridSize = m_gridSize;
|
||||
m_params.numCells = m_numGridCells;
|
||||
m_params.numBodies = m_numParticles;
|
||||
|
||||
m_params.particleRadius = 1.0f / 64.0f;
|
||||
m_params.colliderPos = make_float3(-1.2f, -0.8f, 0.8f);
|
||||
m_params.colliderPos = make_float3(-1.2f, -0.8f, 0.8f);
|
||||
m_params.colliderRadius = 0.2f;
|
||||
|
||||
m_params.worldOrigin = make_float3(-1.0f, -1.0f, -1.0f);
|
||||
// m_params.cellSize = make_float3(worldSize.x / m_gridSize.x, worldSize.y / m_gridSize.y, worldSize.z / m_gridSize.z);
|
||||
float cellSize = m_params.particleRadius * 2.0f; // cell size equal to particle diameter
|
||||
// m_params.cellSize = make_float3(worldSize.x / m_gridSize.x, worldSize.y / m_gridSize.y, worldSize.z /
|
||||
// m_gridSize.z);
|
||||
float cellSize = m_params.particleRadius * 2.0f; // cell size equal to particle diameter
|
||||
m_params.cellSize = make_float3(cellSize, cellSize, cellSize);
|
||||
|
||||
m_params.spring = 0.5f;
|
||||
m_params.damping = 0.02f;
|
||||
m_params.shear = 0.1f;
|
||||
m_params.attraction = 0.0f;
|
||||
m_params.spring = 0.5f;
|
||||
m_params.damping = 0.02f;
|
||||
m_params.shear = 0.1f;
|
||||
m_params.attraction = 0.0f;
|
||||
m_params.boundaryDamping = -0.5f;
|
||||
|
||||
m_params.gravity = make_float3(0.0f, -0.0003f, 0.0f);
|
||||
m_params.gravity = make_float3(0.0f, -0.0003f, 0.0f);
|
||||
m_params.globalDamping = 1.0f;
|
||||
|
||||
_initialize(numParticles);
|
||||
@@ -82,8 +81,7 @@ ParticleSystem::~ParticleSystem()
|
||||
m_numParticles = 0;
|
||||
}
|
||||
|
||||
uint
|
||||
ParticleSystem::createVBO(uint size)
|
||||
uint ParticleSystem::createVBO(uint size)
|
||||
{
|
||||
GLuint vbo;
|
||||
glGenBuffers(1, &vbo);
|
||||
@@ -93,63 +91,84 @@ ParticleSystem::createVBO(uint size)
|
||||
return vbo;
|
||||
}
|
||||
|
||||
inline float lerp(float a, float b, float t)
|
||||
{
|
||||
return a + t*(b-a);
|
||||
}
|
||||
inline float lerp(float a, float b, float t) { return a + t * (b - a); }
|
||||
|
||||
// create a color ramp
|
||||
void colorRamp(float t, float *r)
|
||||
{
|
||||
const int ncolors = 7;
|
||||
float c[ncolors][3] =
|
||||
{
|
||||
{ 1.0, 0.0, 0.0, },
|
||||
{ 1.0, 0.5, 0.0, },
|
||||
{ 1.0, 1.0, 0.0, },
|
||||
{ 0.0, 1.0, 0.0, },
|
||||
{ 0.0, 1.0, 1.0, },
|
||||
{ 0.0, 0.0, 1.0, },
|
||||
{ 1.0, 0.0, 1.0, },
|
||||
const int ncolors = 7;
|
||||
float c[ncolors][3] = {
|
||||
{
|
||||
1.0,
|
||||
0.0,
|
||||
0.0,
|
||||
},
|
||||
{
|
||||
1.0,
|
||||
0.5,
|
||||
0.0,
|
||||
},
|
||||
{
|
||||
1.0,
|
||||
1.0,
|
||||
0.0,
|
||||
},
|
||||
{
|
||||
0.0,
|
||||
1.0,
|
||||
0.0,
|
||||
},
|
||||
{
|
||||
0.0,
|
||||
1.0,
|
||||
1.0,
|
||||
},
|
||||
{
|
||||
0.0,
|
||||
0.0,
|
||||
1.0,
|
||||
},
|
||||
{
|
||||
1.0,
|
||||
0.0,
|
||||
1.0,
|
||||
},
|
||||
};
|
||||
t = t * (ncolors-1);
|
||||
int i = (int) t;
|
||||
t = t * (ncolors - 1);
|
||||
int i = (int)t;
|
||||
float u = t - floorf(t);
|
||||
r[0] = lerp(c[i][0], c[i+1][0], u);
|
||||
r[1] = lerp(c[i][1], c[i+1][1], u);
|
||||
r[2] = lerp(c[i][2], c[i+1][2], u);
|
||||
r[0] = lerp(c[i][0], c[i + 1][0], u);
|
||||
r[1] = lerp(c[i][1], c[i + 1][1], u);
|
||||
r[2] = lerp(c[i][2], c[i + 1][2], u);
|
||||
}
|
||||
|
||||
void
|
||||
ParticleSystem::_initialize(int numParticles)
|
||||
void ParticleSystem::_initialize(int numParticles)
|
||||
{
|
||||
assert(!m_bInitialized);
|
||||
|
||||
m_numParticles = numParticles;
|
||||
|
||||
// allocate host storage
|
||||
m_hPos = new float[m_numParticles*4];
|
||||
m_hVel = new float[m_numParticles*4];
|
||||
memset(m_hPos, 0, m_numParticles*4*sizeof(float));
|
||||
memset(m_hVel, 0, m_numParticles*4*sizeof(float));
|
||||
m_hPos = new float[m_numParticles * 4];
|
||||
m_hVel = new float[m_numParticles * 4];
|
||||
memset(m_hPos, 0, m_numParticles * 4 * sizeof(float));
|
||||
memset(m_hVel, 0, m_numParticles * 4 * sizeof(float));
|
||||
|
||||
m_hCellStart = new uint[m_numGridCells];
|
||||
memset(m_hCellStart, 0, m_numGridCells*sizeof(uint));
|
||||
memset(m_hCellStart, 0, m_numGridCells * sizeof(uint));
|
||||
|
||||
m_hCellEnd = new uint[m_numGridCells];
|
||||
memset(m_hCellEnd, 0, m_numGridCells*sizeof(uint));
|
||||
memset(m_hCellEnd, 0, m_numGridCells * sizeof(uint));
|
||||
|
||||
// allocate GPU data
|
||||
unsigned int memSize = sizeof(float) * 4 * m_numParticles;
|
||||
|
||||
if (m_bUseOpenGL)
|
||||
{
|
||||
if (m_bUseOpenGL) {
|
||||
m_posVbo = createVBO(memSize);
|
||||
registerGLBufferObject(m_posVbo, &m_cuda_posvbo_resource);
|
||||
}
|
||||
else
|
||||
{
|
||||
checkCudaErrors(cudaMalloc((void **)&m_cudaPosVBO, memSize)) ;
|
||||
else {
|
||||
checkCudaErrors(cudaMalloc((void **)&m_cudaPosVBO, memSize));
|
||||
}
|
||||
|
||||
allocateArray((void **)&m_dVel, memSize);
|
||||
@@ -157,41 +176,38 @@ ParticleSystem::_initialize(int numParticles)
|
||||
allocateArray((void **)&m_dSortedPos, memSize);
|
||||
allocateArray((void **)&m_dSortedVel, memSize);
|
||||
|
||||
allocateArray((void **)&m_dGridParticleHash, m_numParticles*sizeof(uint));
|
||||
allocateArray((void **)&m_dGridParticleIndex, m_numParticles*sizeof(uint));
|
||||
allocateArray((void **)&m_dGridParticleHash, m_numParticles * sizeof(uint));
|
||||
allocateArray((void **)&m_dGridParticleIndex, m_numParticles * sizeof(uint));
|
||||
|
||||
allocateArray((void **)&m_dCellStart, m_numGridCells*sizeof(uint));
|
||||
allocateArray((void **)&m_dCellEnd, m_numGridCells*sizeof(uint));
|
||||
allocateArray((void **)&m_dCellStart, m_numGridCells * sizeof(uint));
|
||||
allocateArray((void **)&m_dCellEnd, m_numGridCells * sizeof(uint));
|
||||
|
||||
if (m_bUseOpenGL)
|
||||
{
|
||||
m_colorVBO = createVBO(m_numParticles*4*sizeof(float));
|
||||
if (m_bUseOpenGL) {
|
||||
m_colorVBO = createVBO(m_numParticles * 4 * sizeof(float));
|
||||
registerGLBufferObject(m_colorVBO, &m_cuda_colorvbo_resource);
|
||||
|
||||
// fill color buffer
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_colorVBO);
|
||||
float *data = (float *) glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
||||
float *ptr = data;
|
||||
float *data = (float *)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
||||
float *ptr = data;
|
||||
|
||||
for (uint i=0; i<m_numParticles; i++)
|
||||
{
|
||||
float t = i / (float) m_numParticles;
|
||||
for (uint i = 0; i < m_numParticles; i++) {
|
||||
float t = i / (float)m_numParticles;
|
||||
#if 0
|
||||
*ptr++ = rand() / (float) RAND_MAX;
|
||||
*ptr++ = rand() / (float) RAND_MAX;
|
||||
*ptr++ = rand() / (float) RAND_MAX;
|
||||
#else
|
||||
colorRamp(t, ptr);
|
||||
ptr+=3;
|
||||
ptr += 3;
|
||||
#endif
|
||||
*ptr++ = 1.0f;
|
||||
}
|
||||
|
||||
glUnmapBuffer(GL_ARRAY_BUFFER);
|
||||
}
|
||||
else
|
||||
{
|
||||
checkCudaErrors(cudaMalloc((void **)&m_cudaColorVBO, sizeof(float)*numParticles*4));
|
||||
else {
|
||||
checkCudaErrors(cudaMalloc((void **)&m_cudaColorVBO, sizeof(float) * numParticles * 4));
|
||||
}
|
||||
|
||||
sdkCreateTimer(&m_timer);
|
||||
@@ -201,15 +217,14 @@ ParticleSystem::_initialize(int numParticles)
|
||||
m_bInitialized = true;
|
||||
}
|
||||
|
||||
void
|
||||
ParticleSystem::_finalize()
|
||||
void ParticleSystem::_finalize()
|
||||
{
|
||||
assert(m_bInitialized);
|
||||
|
||||
delete [] m_hPos;
|
||||
delete [] m_hVel;
|
||||
delete [] m_hCellStart;
|
||||
delete [] m_hCellEnd;
|
||||
delete[] m_hPos;
|
||||
delete[] m_hVel;
|
||||
delete[] m_hCellStart;
|
||||
delete[] m_hCellEnd;
|
||||
|
||||
freeArray(m_dVel);
|
||||
freeArray(m_dSortedPos);
|
||||
@@ -220,106 +235,86 @@ ParticleSystem::_finalize()
|
||||
freeArray(m_dCellStart);
|
||||
freeArray(m_dCellEnd);
|
||||
|
||||
if (m_bUseOpenGL)
|
||||
{
|
||||
if (m_bUseOpenGL) {
|
||||
unregisterGLBufferObject(m_cuda_colorvbo_resource);
|
||||
unregisterGLBufferObject(m_cuda_posvbo_resource);
|
||||
glDeleteBuffers(1, (const GLuint *)&m_posVbo);
|
||||
glDeleteBuffers(1, (const GLuint *)&m_colorVBO);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
checkCudaErrors(cudaFree(m_cudaPosVBO));
|
||||
checkCudaErrors(cudaFree(m_cudaColorVBO));
|
||||
}
|
||||
}
|
||||
|
||||
// step the simulation
|
||||
void
|
||||
ParticleSystem::update(float deltaTime)
|
||||
void ParticleSystem::update(float deltaTime)
|
||||
{
|
||||
assert(m_bInitialized);
|
||||
|
||||
float *dPos;
|
||||
|
||||
if (m_bUseOpenGL)
|
||||
{
|
||||
dPos = (float *) mapGLBufferObject(&m_cuda_posvbo_resource);
|
||||
if (m_bUseOpenGL) {
|
||||
dPos = (float *)mapGLBufferObject(&m_cuda_posvbo_resource);
|
||||
}
|
||||
else
|
||||
{
|
||||
dPos = (float *) m_cudaPosVBO;
|
||||
else {
|
||||
dPos = (float *)m_cudaPosVBO;
|
||||
}
|
||||
|
||||
// update constants
|
||||
setParameters(&m_params);
|
||||
|
||||
// integrate
|
||||
integrateSystem(
|
||||
dPos,
|
||||
m_dVel,
|
||||
deltaTime,
|
||||
m_numParticles);
|
||||
integrateSystem(dPos, m_dVel, deltaTime, m_numParticles);
|
||||
|
||||
// calculate grid hash
|
||||
calcHash(
|
||||
m_dGridParticleHash,
|
||||
m_dGridParticleIndex,
|
||||
dPos,
|
||||
m_numParticles);
|
||||
calcHash(m_dGridParticleHash, m_dGridParticleIndex, dPos, m_numParticles);
|
||||
|
||||
// sort particles based on hash
|
||||
sortParticles(m_dGridParticleHash, m_dGridParticleIndex, m_numParticles);
|
||||
|
||||
// reorder particle arrays into sorted order and
|
||||
// find start and end of each cell
|
||||
reorderDataAndFindCellStart(
|
||||
m_dCellStart,
|
||||
m_dCellEnd,
|
||||
m_dSortedPos,
|
||||
m_dSortedVel,
|
||||
m_dGridParticleHash,
|
||||
m_dGridParticleIndex,
|
||||
dPos,
|
||||
m_dVel,
|
||||
m_numParticles,
|
||||
m_numGridCells);
|
||||
reorderDataAndFindCellStart(m_dCellStart,
|
||||
m_dCellEnd,
|
||||
m_dSortedPos,
|
||||
m_dSortedVel,
|
||||
m_dGridParticleHash,
|
||||
m_dGridParticleIndex,
|
||||
dPos,
|
||||
m_dVel,
|
||||
m_numParticles,
|
||||
m_numGridCells);
|
||||
|
||||
// process collisions
|
||||
collide(
|
||||
m_dVel,
|
||||
m_dSortedPos,
|
||||
m_dSortedVel,
|
||||
m_dGridParticleIndex,
|
||||
m_dCellStart,
|
||||
m_dCellEnd,
|
||||
m_numParticles,
|
||||
m_numGridCells);
|
||||
collide(m_dVel,
|
||||
m_dSortedPos,
|
||||
m_dSortedVel,
|
||||
m_dGridParticleIndex,
|
||||
m_dCellStart,
|
||||
m_dCellEnd,
|
||||
m_numParticles,
|
||||
m_numGridCells);
|
||||
|
||||
// note: do unmap at end here to avoid unnecessary graphics/CUDA context switch
|
||||
if (m_bUseOpenGL)
|
||||
{
|
||||
if (m_bUseOpenGL) {
|
||||
unmapGLBufferObject(m_cuda_posvbo_resource);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ParticleSystem::dumpGrid()
|
||||
void ParticleSystem::dumpGrid()
|
||||
{
|
||||
// dump grid information
|
||||
copyArrayFromDevice(m_hCellStart, m_dCellStart, 0, sizeof(uint)*m_numGridCells);
|
||||
copyArrayFromDevice(m_hCellEnd, m_dCellEnd, 0, sizeof(uint)*m_numGridCells);
|
||||
copyArrayFromDevice(m_hCellStart, m_dCellStart, 0, sizeof(uint) * m_numGridCells);
|
||||
copyArrayFromDevice(m_hCellEnd, m_dCellEnd, 0, sizeof(uint) * m_numGridCells);
|
||||
uint maxCellSize = 0;
|
||||
|
||||
for (uint i=0; i<m_numGridCells; i++)
|
||||
{
|
||||
if (m_hCellStart[i] != 0xffffffff)
|
||||
{
|
||||
for (uint i = 0; i < m_numGridCells; i++) {
|
||||
if (m_hCellStart[i] != 0xffffffff) {
|
||||
uint cellSize = m_hCellEnd[i] - m_hCellStart[i];
|
||||
|
||||
// printf("cell: %d, %d particles\n", i, cellSize);
|
||||
if (cellSize > maxCellSize)
|
||||
{
|
||||
if (cellSize > maxCellSize) {
|
||||
maxCellSize = cellSize;
|
||||
}
|
||||
}
|
||||
@@ -328,186 +323,166 @@ ParticleSystem::dumpGrid()
|
||||
printf("maximum particles per cell = %d\n", maxCellSize);
|
||||
}
|
||||
|
||||
void
|
||||
ParticleSystem::dumpParticles(uint start, uint count)
|
||||
void ParticleSystem::dumpParticles(uint start, uint count)
|
||||
{
|
||||
// debug
|
||||
copyArrayFromDevice(m_hPos, 0, &m_cuda_posvbo_resource, sizeof(float)*4*count);
|
||||
copyArrayFromDevice(m_hVel, m_dVel, 0, sizeof(float)*4*count);
|
||||
copyArrayFromDevice(m_hPos, 0, &m_cuda_posvbo_resource, sizeof(float) * 4 * count);
|
||||
copyArrayFromDevice(m_hVel, m_dVel, 0, sizeof(float) * 4 * count);
|
||||
|
||||
for (uint i=start; i<start+count; i++)
|
||||
{
|
||||
for (uint i = start; i < start + count; i++) {
|
||||
// printf("%d: ", i);
|
||||
printf("pos: (%.4f, %.4f, %.4f, %.4f)\n", m_hPos[i*4+0], m_hPos[i*4+1], m_hPos[i*4+2], m_hPos[i*4+3]);
|
||||
printf("vel: (%.4f, %.4f, %.4f, %.4f)\n", m_hVel[i*4+0], m_hVel[i*4+1], m_hVel[i*4+2], m_hVel[i*4+3]);
|
||||
printf("pos: (%.4f, %.4f, %.4f, %.4f)\n",
|
||||
m_hPos[i * 4 + 0],
|
||||
m_hPos[i * 4 + 1],
|
||||
m_hPos[i * 4 + 2],
|
||||
m_hPos[i * 4 + 3]);
|
||||
printf("vel: (%.4f, %.4f, %.4f, %.4f)\n",
|
||||
m_hVel[i * 4 + 0],
|
||||
m_hVel[i * 4 + 1],
|
||||
m_hVel[i * 4 + 2],
|
||||
m_hVel[i * 4 + 3]);
|
||||
}
|
||||
}
|
||||
|
||||
float *
|
||||
ParticleSystem::getArray(ParticleArray array)
|
||||
float *ParticleSystem::getArray(ParticleArray array)
|
||||
{
|
||||
assert(m_bInitialized);
|
||||
|
||||
float *hdata = 0;
|
||||
float *ddata = 0;
|
||||
float *hdata = 0;
|
||||
float *ddata = 0;
|
||||
struct cudaGraphicsResource *cuda_vbo_resource = 0;
|
||||
|
||||
switch (array)
|
||||
{
|
||||
default:
|
||||
case POSITION:
|
||||
hdata = m_hPos;
|
||||
ddata = m_dPos;
|
||||
cuda_vbo_resource = m_cuda_posvbo_resource;
|
||||
break;
|
||||
switch (array) {
|
||||
default:
|
||||
case POSITION:
|
||||
hdata = m_hPos;
|
||||
ddata = m_dPos;
|
||||
cuda_vbo_resource = m_cuda_posvbo_resource;
|
||||
break;
|
||||
|
||||
case VELOCITY:
|
||||
hdata = m_hVel;
|
||||
ddata = m_dVel;
|
||||
break;
|
||||
case VELOCITY:
|
||||
hdata = m_hVel;
|
||||
ddata = m_dVel;
|
||||
break;
|
||||
}
|
||||
|
||||
copyArrayFromDevice(hdata, ddata, &cuda_vbo_resource, m_numParticles*4*sizeof(float));
|
||||
copyArrayFromDevice(hdata, ddata, &cuda_vbo_resource, m_numParticles * 4 * sizeof(float));
|
||||
return hdata;
|
||||
}
|
||||
|
||||
void
|
||||
ParticleSystem::setArray(ParticleArray array, const float *data, int start, int count)
|
||||
void ParticleSystem::setArray(ParticleArray array, const float *data, int start, int count)
|
||||
{
|
||||
assert(m_bInitialized);
|
||||
|
||||
switch (array)
|
||||
{
|
||||
default:
|
||||
case POSITION:
|
||||
{
|
||||
if (m_bUseOpenGL)
|
||||
{
|
||||
unregisterGLBufferObject(m_cuda_posvbo_resource);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_posVbo);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, start*4*sizeof(float), count*4*sizeof(float), data);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
registerGLBufferObject(m_posVbo, &m_cuda_posvbo_resource);
|
||||
}
|
||||
else
|
||||
{
|
||||
copyArrayToDevice(m_cudaPosVBO, data, start*4*sizeof(float), count*4*sizeof(float));
|
||||
}
|
||||
}
|
||||
break;
|
||||
switch (array) {
|
||||
default:
|
||||
case POSITION: {
|
||||
if (m_bUseOpenGL) {
|
||||
unregisterGLBufferObject(m_cuda_posvbo_resource);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_posVbo);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, start * 4 * sizeof(float), count * 4 * sizeof(float), data);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
registerGLBufferObject(m_posVbo, &m_cuda_posvbo_resource);
|
||||
}
|
||||
else {
|
||||
copyArrayToDevice(m_cudaPosVBO, data, start * 4 * sizeof(float), count * 4 * sizeof(float));
|
||||
}
|
||||
} break;
|
||||
|
||||
case VELOCITY:
|
||||
copyArrayToDevice(m_dVel, data, start*4*sizeof(float), count*4*sizeof(float));
|
||||
break;
|
||||
case VELOCITY:
|
||||
copyArrayToDevice(m_dVel, data, start * 4 * sizeof(float), count * 4 * sizeof(float));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
inline float frand()
|
||||
{
|
||||
return rand() / (float) RAND_MAX;
|
||||
}
|
||||
inline float frand() { return rand() / (float)RAND_MAX; }
|
||||
|
||||
void
|
||||
ParticleSystem::initGrid(uint *size, float spacing, float jitter, uint numParticles)
|
||||
void ParticleSystem::initGrid(uint *size, float spacing, float jitter, uint numParticles)
|
||||
{
|
||||
srand(1973);
|
||||
|
||||
for (uint z=0; z<size[2]; z++)
|
||||
{
|
||||
for (uint y=0; y<size[1]; y++)
|
||||
{
|
||||
for (uint x=0; x<size[0]; x++)
|
||||
{
|
||||
uint i = (z*size[1]*size[0]) + (y*size[0]) + x;
|
||||
for (uint z = 0; z < size[2]; z++) {
|
||||
for (uint y = 0; y < size[1]; y++) {
|
||||
for (uint x = 0; x < size[0]; x++) {
|
||||
uint i = (z * size[1] * size[0]) + (y * size[0]) + x;
|
||||
|
||||
if (i < numParticles)
|
||||
{
|
||||
m_hPos[i*4] = (spacing * x) + m_params.particleRadius - 1.0f + (frand()*2.0f-1.0f)*jitter;
|
||||
m_hPos[i*4+1] = (spacing * y) + m_params.particleRadius - 1.0f + (frand()*2.0f-1.0f)*jitter;
|
||||
m_hPos[i*4+2] = (spacing * z) + m_params.particleRadius - 1.0f + (frand()*2.0f-1.0f)*jitter;
|
||||
m_hPos[i*4+3] = 1.0f;
|
||||
if (i < numParticles) {
|
||||
m_hPos[i * 4] = (spacing * x) + m_params.particleRadius - 1.0f + (frand() * 2.0f - 1.0f) * jitter;
|
||||
m_hPos[i * 4 + 1] =
|
||||
(spacing * y) + m_params.particleRadius - 1.0f + (frand() * 2.0f - 1.0f) * jitter;
|
||||
m_hPos[i * 4 + 2] =
|
||||
(spacing * z) + m_params.particleRadius - 1.0f + (frand() * 2.0f - 1.0f) * jitter;
|
||||
m_hPos[i * 4 + 3] = 1.0f;
|
||||
|
||||
m_hVel[i*4] = 0.0f;
|
||||
m_hVel[i*4+1] = 0.0f;
|
||||
m_hVel[i*4+2] = 0.0f;
|
||||
m_hVel[i*4+3] = 0.0f;
|
||||
m_hVel[i * 4] = 0.0f;
|
||||
m_hVel[i * 4 + 1] = 0.0f;
|
||||
m_hVel[i * 4 + 2] = 0.0f;
|
||||
m_hVel[i * 4 + 3] = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ParticleSystem::reset(ParticleConfig config)
|
||||
void ParticleSystem::reset(ParticleConfig config)
|
||||
{
|
||||
switch (config)
|
||||
{
|
||||
default:
|
||||
case CONFIG_RANDOM:
|
||||
{
|
||||
int p = 0, v = 0;
|
||||
switch (config) {
|
||||
default:
|
||||
case CONFIG_RANDOM: {
|
||||
int p = 0, v = 0;
|
||||
|
||||
for (uint i=0; i < m_numParticles; i++)
|
||||
{
|
||||
float point[3];
|
||||
point[0] = frand();
|
||||
point[1] = frand();
|
||||
point[2] = frand();
|
||||
m_hPos[p++] = 2 * (point[0] - 0.5f);
|
||||
m_hPos[p++] = 2 * (point[1] - 0.5f);
|
||||
m_hPos[p++] = 2 * (point[2] - 0.5f);
|
||||
m_hPos[p++] = 1.0f; // radius
|
||||
m_hVel[v++] = 0.0f;
|
||||
m_hVel[v++] = 0.0f;
|
||||
m_hVel[v++] = 0.0f;
|
||||
m_hVel[v++] = 0.0f;
|
||||
}
|
||||
}
|
||||
break;
|
||||
for (uint i = 0; i < m_numParticles; i++) {
|
||||
float point[3];
|
||||
point[0] = frand();
|
||||
point[1] = frand();
|
||||
point[2] = frand();
|
||||
m_hPos[p++] = 2 * (point[0] - 0.5f);
|
||||
m_hPos[p++] = 2 * (point[1] - 0.5f);
|
||||
m_hPos[p++] = 2 * (point[2] - 0.5f);
|
||||
m_hPos[p++] = 1.0f; // radius
|
||||
m_hVel[v++] = 0.0f;
|
||||
m_hVel[v++] = 0.0f;
|
||||
m_hVel[v++] = 0.0f;
|
||||
m_hVel[v++] = 0.0f;
|
||||
}
|
||||
} break;
|
||||
|
||||
case CONFIG_GRID:
|
||||
{
|
||||
float jitter = m_params.particleRadius*0.01f;
|
||||
uint s = (int) ceilf(powf((float) m_numParticles, 1.0f / 3.0f));
|
||||
uint gridSize[3];
|
||||
gridSize[0] = gridSize[1] = gridSize[2] = s;
|
||||
initGrid(gridSize, m_params.particleRadius*2.0f, jitter, m_numParticles);
|
||||
}
|
||||
break;
|
||||
case CONFIG_GRID: {
|
||||
float jitter = m_params.particleRadius * 0.01f;
|
||||
uint s = (int)ceilf(powf((float)m_numParticles, 1.0f / 3.0f));
|
||||
uint gridSize[3];
|
||||
gridSize[0] = gridSize[1] = gridSize[2] = s;
|
||||
initGrid(gridSize, m_params.particleRadius * 2.0f, jitter, m_numParticles);
|
||||
} break;
|
||||
}
|
||||
|
||||
setArray(POSITION, m_hPos, 0, m_numParticles);
|
||||
setArray(VELOCITY, m_hVel, 0, m_numParticles);
|
||||
}
|
||||
|
||||
void
|
||||
ParticleSystem::addSphere(int start, float *pos, float *vel, int r, float spacing)
|
||||
void ParticleSystem::addSphere(int start, float *pos, float *vel, int r, float spacing)
|
||||
{
|
||||
uint index = start;
|
||||
|
||||
for (int z=-r; z<=r; z++)
|
||||
{
|
||||
for (int y=-r; y<=r; y++)
|
||||
{
|
||||
for (int x=-r; x<=r; x++)
|
||||
{
|
||||
float dx = x*spacing;
|
||||
float dy = y*spacing;
|
||||
float dz = z*spacing;
|
||||
float l = sqrtf(dx*dx + dy*dy + dz*dz);
|
||||
float jitter = m_params.particleRadius*0.01f;
|
||||
for (int z = -r; z <= r; z++) {
|
||||
for (int y = -r; y <= r; y++) {
|
||||
for (int x = -r; x <= r; x++) {
|
||||
float dx = x * spacing;
|
||||
float dy = y * spacing;
|
||||
float dz = z * spacing;
|
||||
float l = sqrtf(dx * dx + dy * dy + dz * dz);
|
||||
float jitter = m_params.particleRadius * 0.01f;
|
||||
|
||||
if ((l <= m_params.particleRadius*2.0f*r) && (index < m_numParticles))
|
||||
{
|
||||
m_hPos[index*4] = pos[0] + dx + (frand()*2.0f-1.0f)*jitter;
|
||||
m_hPos[index*4+1] = pos[1] + dy + (frand()*2.0f-1.0f)*jitter;
|
||||
m_hPos[index*4+2] = pos[2] + dz + (frand()*2.0f-1.0f)*jitter;
|
||||
m_hPos[index*4+3] = pos[3];
|
||||
if ((l <= m_params.particleRadius * 2.0f * r) && (index < m_numParticles)) {
|
||||
m_hPos[index * 4] = pos[0] + dx + (frand() * 2.0f - 1.0f) * jitter;
|
||||
m_hPos[index * 4 + 1] = pos[1] + dy + (frand() * 2.0f - 1.0f) * jitter;
|
||||
m_hPos[index * 4 + 2] = pos[2] + dz + (frand() * 2.0f - 1.0f) * jitter;
|
||||
m_hPos[index * 4 + 3] = pos[3];
|
||||
|
||||
m_hVel[index*4] = vel[0];
|
||||
m_hVel[index*4+1] = vel[1];
|
||||
m_hVel[index*4+2] = vel[2];
|
||||
m_hVel[index*4+3] = vel[3];
|
||||
m_hVel[index * 4] = vel[0];
|
||||
m_hVel[index * 4 + 1] = vel[1];
|
||||
m_hVel[index * 4 + 2] = vel[2];
|
||||
m_hVel[index * 4 + 3] = vel[3];
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,43 +25,47 @@
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
extern "C" {
|
||||
void cudaInit(int argc, char **argv);
|
||||
extern "C"
|
||||
{
|
||||
void cudaInit(int argc, char **argv);
|
||||
|
||||
void allocateArray(void **devPtr, int size);
|
||||
void freeArray(void *devPtr);
|
||||
void allocateArray(void **devPtr, int size);
|
||||
void freeArray(void *devPtr);
|
||||
|
||||
void threadSync();
|
||||
void threadSync();
|
||||
|
||||
void copyArrayFromDevice(void *host, const void *device,
|
||||
struct cudaGraphicsResource **cuda_vbo_resource,
|
||||
int size);
|
||||
void copyArrayToDevice(void *device, const void *host, int offset, int size);
|
||||
void registerGLBufferObject(uint vbo,
|
||||
struct cudaGraphicsResource **cuda_vbo_resource);
|
||||
void unregisterGLBufferObject(struct cudaGraphicsResource *cuda_vbo_resource);
|
||||
void *mapGLBufferObject(struct cudaGraphicsResource **cuda_vbo_resource);
|
||||
void unmapGLBufferObject(struct cudaGraphicsResource *cuda_vbo_resource);
|
||||
void copyArrayFromDevice(void *host, const void *device, struct cudaGraphicsResource **cuda_vbo_resource, int size);
|
||||
void copyArrayToDevice(void *device, const void *host, int offset, int size);
|
||||
void registerGLBufferObject(uint vbo, struct cudaGraphicsResource **cuda_vbo_resource);
|
||||
void unregisterGLBufferObject(struct cudaGraphicsResource *cuda_vbo_resource);
|
||||
void *mapGLBufferObject(struct cudaGraphicsResource **cuda_vbo_resource);
|
||||
void unmapGLBufferObject(struct cudaGraphicsResource *cuda_vbo_resource);
|
||||
|
||||
void setParameters(SimParams *hostParams);
|
||||
void setParameters(SimParams *hostParams);
|
||||
|
||||
void integrateSystem(float *pos, float *vel, float deltaTime,
|
||||
uint numParticles);
|
||||
void integrateSystem(float *pos, float *vel, float deltaTime, uint numParticles);
|
||||
|
||||
void calcHash(uint *gridParticleHash, uint *gridParticleIndex, float *pos,
|
||||
int numParticles);
|
||||
void calcHash(uint *gridParticleHash, uint *gridParticleIndex, float *pos, int numParticles);
|
||||
|
||||
void reorderDataAndFindCellStart(uint *cellStart, uint *cellEnd,
|
||||
float *sortedPos, float *sortedVel,
|
||||
uint *gridParticleHash,
|
||||
uint *gridParticleIndex, float *oldPos,
|
||||
float *oldVel, uint numParticles,
|
||||
uint numCells);
|
||||
void reorderDataAndFindCellStart(uint *cellStart,
|
||||
uint *cellEnd,
|
||||
float *sortedPos,
|
||||
float *sortedVel,
|
||||
uint *gridParticleHash,
|
||||
uint *gridParticleIndex,
|
||||
float *oldPos,
|
||||
float *oldVel,
|
||||
uint numParticles,
|
||||
uint numCells);
|
||||
|
||||
void collide(float *newVel, float *sortedPos, float *sortedVel,
|
||||
uint *gridParticleIndex, uint *cellStart, uint *cellEnd,
|
||||
uint numParticles, uint numCells);
|
||||
void collide(float *newVel,
|
||||
float *sortedPos,
|
||||
float *sortedVel,
|
||||
uint *gridParticleIndex,
|
||||
uint *cellStart,
|
||||
uint *cellEnd,
|
||||
uint numParticles,
|
||||
uint numCells);
|
||||
|
||||
void sortParticles(uint *dGridParticleHash, uint *dGridParticleIndex,
|
||||
uint numParticles);
|
||||
void sortParticles(uint *dGridParticleHash, uint *dGridParticleIndex, uint numParticles);
|
||||
}
|
||||
|
||||
@@ -29,118 +29,118 @@
|
||||
#define __PARTICLESYSTEM_H__
|
||||
|
||||
#define DEBUG_GRID 0
|
||||
#define DO_TIMING 0
|
||||
#define DO_TIMING 0
|
||||
|
||||
#include <helper_functions.h>
|
||||
|
||||
#include "particles_kernel.cuh"
|
||||
#include "vector_functions.h"
|
||||
|
||||
// Particle system class
|
||||
class ParticleSystem {
|
||||
public:
|
||||
ParticleSystem(uint numParticles, uint3 gridSize, bool bUseOpenGL);
|
||||
~ParticleSystem();
|
||||
class ParticleSystem
|
||||
{
|
||||
public:
|
||||
ParticleSystem(uint numParticles, uint3 gridSize, bool bUseOpenGL);
|
||||
~ParticleSystem();
|
||||
|
||||
enum ParticleConfig { CONFIG_RANDOM, CONFIG_GRID, _NUM_CONFIGS };
|
||||
enum ParticleConfig { CONFIG_RANDOM, CONFIG_GRID, _NUM_CONFIGS };
|
||||
|
||||
enum ParticleArray {
|
||||
POSITION,
|
||||
VELOCITY,
|
||||
};
|
||||
enum ParticleArray {
|
||||
POSITION,
|
||||
VELOCITY,
|
||||
};
|
||||
|
||||
void update(float deltaTime);
|
||||
void reset(ParticleConfig config);
|
||||
void update(float deltaTime);
|
||||
void reset(ParticleConfig config);
|
||||
|
||||
float *getArray(ParticleArray array);
|
||||
void setArray(ParticleArray array, const float *data, int start, int count);
|
||||
float *getArray(ParticleArray array);
|
||||
void setArray(ParticleArray array, const float *data, int start, int count);
|
||||
|
||||
int getNumParticles() const { return m_numParticles; }
|
||||
int getNumParticles() const { return m_numParticles; }
|
||||
|
||||
unsigned int getCurrentReadBuffer() const { return m_posVbo; }
|
||||
unsigned int getColorBuffer() const { return m_colorVBO; }
|
||||
unsigned int getCurrentReadBuffer() const { return m_posVbo; }
|
||||
unsigned int getColorBuffer() const { return m_colorVBO; }
|
||||
|
||||
void *getCudaPosVBO() const { return (void *)m_cudaPosVBO; }
|
||||
void *getCudaColorVBO() const { return (void *)m_cudaColorVBO; }
|
||||
void *getCudaPosVBO() const { return (void *)m_cudaPosVBO; }
|
||||
void *getCudaColorVBO() const { return (void *)m_cudaColorVBO; }
|
||||
|
||||
void dumpGrid();
|
||||
void dumpParticles(uint start, uint count);
|
||||
void dumpGrid();
|
||||
void dumpParticles(uint start, uint count);
|
||||
|
||||
void setIterations(int i) { m_solverIterations = i; }
|
||||
void setIterations(int i) { m_solverIterations = i; }
|
||||
|
||||
void setDamping(float x) { m_params.globalDamping = x; }
|
||||
void setGravity(float x) { m_params.gravity = make_float3(0.0f, x, 0.0f); }
|
||||
void setDamping(float x) { m_params.globalDamping = x; }
|
||||
void setGravity(float x) { m_params.gravity = make_float3(0.0f, x, 0.0f); }
|
||||
|
||||
void setCollideSpring(float x) { m_params.spring = x; }
|
||||
void setCollideDamping(float x) { m_params.damping = x; }
|
||||
void setCollideShear(float x) { m_params.shear = x; }
|
||||
void setCollideAttraction(float x) { m_params.attraction = x; }
|
||||
void setCollideSpring(float x) { m_params.spring = x; }
|
||||
void setCollideDamping(float x) { m_params.damping = x; }
|
||||
void setCollideShear(float x) { m_params.shear = x; }
|
||||
void setCollideAttraction(float x) { m_params.attraction = x; }
|
||||
|
||||
void setColliderPos(float3 x) { m_params.colliderPos = x; }
|
||||
void setColliderPos(float3 x) { m_params.colliderPos = x; }
|
||||
|
||||
float getParticleRadius() { return m_params.particleRadius; }
|
||||
float3 getColliderPos() { return m_params.colliderPos; }
|
||||
float getColliderRadius() { return m_params.colliderRadius; }
|
||||
uint3 getGridSize() { return m_params.gridSize; }
|
||||
float3 getWorldOrigin() { return m_params.worldOrigin; }
|
||||
float3 getCellSize() { return m_params.cellSize; }
|
||||
float getParticleRadius() { return m_params.particleRadius; }
|
||||
float3 getColliderPos() { return m_params.colliderPos; }
|
||||
float getColliderRadius() { return m_params.colliderRadius; }
|
||||
uint3 getGridSize() { return m_params.gridSize; }
|
||||
float3 getWorldOrigin() { return m_params.worldOrigin; }
|
||||
float3 getCellSize() { return m_params.cellSize; }
|
||||
|
||||
void addSphere(int index, float *pos, float *vel, int r, float spacing);
|
||||
void addSphere(int index, float *pos, float *vel, int r, float spacing);
|
||||
|
||||
protected: // methods
|
||||
ParticleSystem() {}
|
||||
uint createVBO(uint size);
|
||||
protected: // methods
|
||||
ParticleSystem() {}
|
||||
uint createVBO(uint size);
|
||||
|
||||
void _initialize(int numParticles);
|
||||
void _finalize();
|
||||
void _initialize(int numParticles);
|
||||
void _finalize();
|
||||
|
||||
void initGrid(uint *size, float spacing, float jitter, uint numParticles);
|
||||
void initGrid(uint *size, float spacing, float jitter, uint numParticles);
|
||||
|
||||
protected: // data
|
||||
bool m_bInitialized, m_bUseOpenGL;
|
||||
uint m_numParticles;
|
||||
protected: // data
|
||||
bool m_bInitialized, m_bUseOpenGL;
|
||||
uint m_numParticles;
|
||||
|
||||
// CPU data
|
||||
float *m_hPos; // particle positions
|
||||
float *m_hVel; // particle velocities
|
||||
// CPU data
|
||||
float *m_hPos; // particle positions
|
||||
float *m_hVel; // particle velocities
|
||||
|
||||
uint *m_hParticleHash;
|
||||
uint *m_hCellStart;
|
||||
uint *m_hCellEnd;
|
||||
uint *m_hParticleHash;
|
||||
uint *m_hCellStart;
|
||||
uint *m_hCellEnd;
|
||||
|
||||
// GPU data
|
||||
float *m_dPos;
|
||||
float *m_dVel;
|
||||
// GPU data
|
||||
float *m_dPos;
|
||||
float *m_dVel;
|
||||
|
||||
float *m_dSortedPos;
|
||||
float *m_dSortedVel;
|
||||
float *m_dSortedPos;
|
||||
float *m_dSortedVel;
|
||||
|
||||
// grid data for sorting method
|
||||
uint *m_dGridParticleHash; // grid hash value for each particle
|
||||
uint *m_dGridParticleIndex; // particle index for each particle
|
||||
uint *m_dCellStart; // index of start of each cell in sorted list
|
||||
uint *m_dCellEnd; // index of end of cell
|
||||
// grid data for sorting method
|
||||
uint *m_dGridParticleHash; // grid hash value for each particle
|
||||
uint *m_dGridParticleIndex; // particle index for each particle
|
||||
uint *m_dCellStart; // index of start of each cell in sorted list
|
||||
uint *m_dCellEnd; // index of end of cell
|
||||
|
||||
uint m_gridSortBits;
|
||||
uint m_gridSortBits;
|
||||
|
||||
uint m_posVbo; // vertex buffer object for particle positions
|
||||
uint m_colorVBO; // vertex buffer object for colors
|
||||
uint m_posVbo; // vertex buffer object for particle positions
|
||||
uint m_colorVBO; // vertex buffer object for colors
|
||||
|
||||
float *m_cudaPosVBO; // these are the CUDA deviceMem Pos
|
||||
float *m_cudaColorVBO; // these are the CUDA deviceMem Color
|
||||
float *m_cudaPosVBO; // these are the CUDA deviceMem Pos
|
||||
float *m_cudaColorVBO; // these are the CUDA deviceMem Color
|
||||
|
||||
struct cudaGraphicsResource
|
||||
*m_cuda_posvbo_resource; // handles OpenGL-CUDA exchange
|
||||
struct cudaGraphicsResource
|
||||
*m_cuda_colorvbo_resource; // handles OpenGL-CUDA exchange
|
||||
struct cudaGraphicsResource *m_cuda_posvbo_resource; // handles OpenGL-CUDA exchange
|
||||
struct cudaGraphicsResource *m_cuda_colorvbo_resource; // handles OpenGL-CUDA exchange
|
||||
|
||||
// params
|
||||
SimParams m_params;
|
||||
uint3 m_gridSize;
|
||||
uint m_numGridCells;
|
||||
// params
|
||||
SimParams m_params;
|
||||
uint3 m_gridSize;
|
||||
uint m_numGridCells;
|
||||
|
||||
StopWatchInterface *m_timer;
|
||||
StopWatchInterface *m_timer;
|
||||
|
||||
uint m_solverIterations;
|
||||
uint m_solverIterations;
|
||||
};
|
||||
|
||||
#endif // __PARTICLESYSTEM_H__
|
||||
#endif // __PARTICLESYSTEM_H__
|
||||
|
||||
@@ -35,189 +35,184 @@
|
||||
#include <GL/freeglut.h>
|
||||
#endif
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cuda_gl_interop.h>
|
||||
#include <cuda_runtime.h>
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_functions.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
#include <cuda_gl_interop.h>
|
||||
|
||||
#include <helper_cuda.h>
|
||||
|
||||
#include <helper_functions.h>
|
||||
#include "particles_kernel_impl.cuh"
|
||||
#include "thrust/device_ptr.h"
|
||||
#include "thrust/for_each.h"
|
||||
#include "thrust/iterator/zip_iterator.h"
|
||||
#include "thrust/sort.h"
|
||||
|
||||
#include "particles_kernel_impl.cuh"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
void cudaInit(int argc, char **argv)
|
||||
{
|
||||
int devID;
|
||||
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
devID = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
if (devID < 0)
|
||||
void cudaInit(int argc, char **argv)
|
||||
{
|
||||
printf("No CUDA Capable devices found, exiting...\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
int devID;
|
||||
|
||||
void allocateArray(void **devPtr, size_t size)
|
||||
{
|
||||
checkCudaErrors(cudaMalloc(devPtr, size));
|
||||
}
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
devID = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
void freeArray(void *devPtr) { checkCudaErrors(cudaFree(devPtr)); }
|
||||
|
||||
void threadSync() { checkCudaErrors(cudaDeviceSynchronize()); }
|
||||
|
||||
void copyArrayToDevice(void *device, const void *host, int offset, int size)
|
||||
{
|
||||
checkCudaErrors(
|
||||
cudaMemcpy((char *)device + offset, host, size, cudaMemcpyHostToDevice));
|
||||
}
|
||||
|
||||
void registerGLBufferObject(uint vbo,
|
||||
struct cudaGraphicsResource **cuda_vbo_resource)
|
||||
{
|
||||
checkCudaErrors(cudaGraphicsGLRegisterBuffer(cuda_vbo_resource, vbo,
|
||||
cudaGraphicsMapFlagsNone));
|
||||
}
|
||||
|
||||
void unregisterGLBufferObject(struct cudaGraphicsResource *cuda_vbo_resource)
|
||||
{
|
||||
checkCudaErrors(cudaGraphicsUnregisterResource(cuda_vbo_resource));
|
||||
}
|
||||
|
||||
void *mapGLBufferObject(struct cudaGraphicsResource **cuda_vbo_resource)
|
||||
{
|
||||
void *ptr;
|
||||
checkCudaErrors(cudaGraphicsMapResources(1, cuda_vbo_resource, 0));
|
||||
size_t num_bytes;
|
||||
checkCudaErrors(cudaGraphicsResourceGetMappedPointer(
|
||||
(void **)&ptr, &num_bytes, *cuda_vbo_resource));
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void unmapGLBufferObject(struct cudaGraphicsResource *cuda_vbo_resource)
|
||||
{
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0));
|
||||
}
|
||||
|
||||
void copyArrayFromDevice(void *host, const void *device,
|
||||
struct cudaGraphicsResource **cuda_vbo_resource,
|
||||
int size)
|
||||
{
|
||||
if (cuda_vbo_resource)
|
||||
{
|
||||
device = mapGLBufferObject(cuda_vbo_resource);
|
||||
if (devID < 0) {
|
||||
printf("No CUDA Capable devices found, exiting...\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaMemcpy(host, device, size, cudaMemcpyDeviceToHost));
|
||||
void allocateArray(void **devPtr, size_t size) { checkCudaErrors(cudaMalloc(devPtr, size)); }
|
||||
|
||||
if (cuda_vbo_resource)
|
||||
void freeArray(void *devPtr) { checkCudaErrors(cudaFree(devPtr)); }
|
||||
|
||||
void threadSync() { checkCudaErrors(cudaDeviceSynchronize()); }
|
||||
|
||||
void copyArrayToDevice(void *device, const void *host, int offset, int size)
|
||||
{
|
||||
unmapGLBufferObject(*cuda_vbo_resource);
|
||||
checkCudaErrors(cudaMemcpy((char *)device + offset, host, size, cudaMemcpyHostToDevice));
|
||||
}
|
||||
}
|
||||
|
||||
void setParameters(SimParams *hostParams)
|
||||
{
|
||||
// copy parameters to constant memory
|
||||
checkCudaErrors(cudaMemcpyToSymbol(cudaParams, hostParams, sizeof(SimParams)));
|
||||
}
|
||||
void registerGLBufferObject(uint vbo, struct cudaGraphicsResource **cuda_vbo_resource)
|
||||
{
|
||||
checkCudaErrors(cudaGraphicsGLRegisterBuffer(cuda_vbo_resource, vbo, cudaGraphicsMapFlagsNone));
|
||||
}
|
||||
|
||||
// Round a / b to nearest higher integer value
|
||||
uint iDivUp(uint a, uint b) { return (a % b != 0) ? (a / b + 1) : (a / b); }
|
||||
void unregisterGLBufferObject(struct cudaGraphicsResource *cuda_vbo_resource)
|
||||
{
|
||||
checkCudaErrors(cudaGraphicsUnregisterResource(cuda_vbo_resource));
|
||||
}
|
||||
|
||||
// compute grid and thread block size for a given number of elements
|
||||
void computeGridSize(uint n, uint blockSize, uint &numBlocks,
|
||||
uint &numThreads)
|
||||
{
|
||||
numThreads = min(blockSize, n);
|
||||
numBlocks = iDivUp(n, numThreads);
|
||||
}
|
||||
void *mapGLBufferObject(struct cudaGraphicsResource **cuda_vbo_resource)
|
||||
{
|
||||
void *ptr;
|
||||
checkCudaErrors(cudaGraphicsMapResources(1, cuda_vbo_resource, 0));
|
||||
size_t num_bytes;
|
||||
checkCudaErrors(cudaGraphicsResourceGetMappedPointer((void **)&ptr, &num_bytes, *cuda_vbo_resource));
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void integrateSystem(float *pos, float *vel, float deltaTime,
|
||||
uint numParticles)
|
||||
{
|
||||
thrust::device_ptr<float4> d_pos4((float4 *)pos);
|
||||
thrust::device_ptr<float4> d_vel4((float4 *)vel);
|
||||
void unmapGLBufferObject(struct cudaGraphicsResource *cuda_vbo_resource)
|
||||
{
|
||||
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0));
|
||||
}
|
||||
|
||||
thrust::for_each(
|
||||
thrust::make_zip_iterator(thrust::make_tuple(d_pos4, d_vel4)),
|
||||
thrust::make_zip_iterator(
|
||||
thrust::make_tuple(d_pos4 + numParticles, d_vel4 + numParticles)),
|
||||
integrate_functor(deltaTime));
|
||||
}
|
||||
void copyArrayFromDevice(void *host, const void *device, struct cudaGraphicsResource **cuda_vbo_resource, int size)
|
||||
{
|
||||
if (cuda_vbo_resource) {
|
||||
device = mapGLBufferObject(cuda_vbo_resource);
|
||||
}
|
||||
|
||||
void calcHash(uint *gridParticleHash, uint *gridParticleIndex, float *pos,
|
||||
int numParticles)
|
||||
{
|
||||
uint numThreads, numBlocks;
|
||||
computeGridSize(numParticles, 256, numBlocks, numThreads);
|
||||
checkCudaErrors(cudaMemcpy(host, device, size, cudaMemcpyDeviceToHost));
|
||||
|
||||
// execute the kernel
|
||||
calcHashD<<<numBlocks, numThreads>>>(gridParticleHash, gridParticleIndex,
|
||||
(float4 *)pos, numParticles);
|
||||
if (cuda_vbo_resource) {
|
||||
unmapGLBufferObject(*cuda_vbo_resource);
|
||||
}
|
||||
}
|
||||
|
||||
// check if kernel invocation generated an error
|
||||
getLastCudaError("Kernel execution failed");
|
||||
}
|
||||
void setParameters(SimParams *hostParams)
|
||||
{
|
||||
// copy parameters to constant memory
|
||||
checkCudaErrors(cudaMemcpyToSymbol(cudaParams, hostParams, sizeof(SimParams)));
|
||||
}
|
||||
|
||||
void reorderDataAndFindCellStart(uint *cellStart, uint *cellEnd,
|
||||
float *sortedPos, float *sortedVel,
|
||||
uint *gridParticleHash,
|
||||
uint *gridParticleIndex, float *oldPos,
|
||||
float *oldVel, uint numParticles,
|
||||
uint numCells)
|
||||
{
|
||||
uint numThreads, numBlocks;
|
||||
computeGridSize(numParticles, 256, numBlocks, numThreads);
|
||||
// Round a / b to nearest higher integer value
|
||||
uint iDivUp(uint a, uint b) { return (a % b != 0) ? (a / b + 1) : (a / b); }
|
||||
|
||||
// set all cells to empty
|
||||
checkCudaErrors(cudaMemset(cellStart, 0xffffffff, numCells * sizeof(uint)));
|
||||
// compute grid and thread block size for a given number of elements
|
||||
void computeGridSize(uint n, uint blockSize, uint &numBlocks, uint &numThreads)
|
||||
{
|
||||
numThreads = min(blockSize, n);
|
||||
numBlocks = iDivUp(n, numThreads);
|
||||
}
|
||||
|
||||
uint smemSize = sizeof(uint) * (numThreads + 1);
|
||||
reorderDataAndFindCellStartD<<<numBlocks, numThreads, smemSize>>>(
|
||||
cellStart, cellEnd, (float4 *)sortedPos, (float4 *)sortedVel,
|
||||
gridParticleHash, gridParticleIndex, (float4 *)oldPos, (float4 *)oldVel,
|
||||
numParticles);
|
||||
getLastCudaError("Kernel execution failed: reorderDataAndFindCellStartD");
|
||||
}
|
||||
void integrateSystem(float *pos, float *vel, float deltaTime, uint numParticles)
|
||||
{
|
||||
thrust::device_ptr<float4> d_pos4((float4 *)pos);
|
||||
thrust::device_ptr<float4> d_vel4((float4 *)vel);
|
||||
|
||||
void collide(float *newVel, float *sortedPos, float *sortedVel,
|
||||
uint *gridParticleIndex, uint *cellStart, uint *cellEnd,
|
||||
uint numParticles, uint numCells)
|
||||
{
|
||||
// thread per particle
|
||||
uint numThreads, numBlocks;
|
||||
computeGridSize(numParticles, 64, numBlocks, numThreads);
|
||||
thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(d_pos4, d_vel4)),
|
||||
thrust::make_zip_iterator(thrust::make_tuple(d_pos4 + numParticles, d_vel4 + numParticles)),
|
||||
integrate_functor(deltaTime));
|
||||
}
|
||||
|
||||
// execute the kernel
|
||||
collideD<<<numBlocks, numThreads>>>((float4 *)newVel, (float4 *)sortedPos,
|
||||
(float4 *)sortedVel, gridParticleIndex,
|
||||
cellStart, cellEnd, numParticles);
|
||||
void calcHash(uint *gridParticleHash, uint *gridParticleIndex, float *pos, int numParticles)
|
||||
{
|
||||
uint numThreads, numBlocks;
|
||||
computeGridSize(numParticles, 256, numBlocks, numThreads);
|
||||
|
||||
// check if kernel invocation generated an error
|
||||
getLastCudaError("Kernel execution failed");
|
||||
}
|
||||
// execute the kernel
|
||||
calcHashD<<<numBlocks, numThreads>>>(gridParticleHash, gridParticleIndex, (float4 *)pos, numParticles);
|
||||
|
||||
void sortParticles(uint *dGridParticleHash, uint *dGridParticleIndex,
|
||||
uint numParticles)
|
||||
{
|
||||
thrust::sort_by_key(
|
||||
thrust::device_ptr<uint>(dGridParticleHash),
|
||||
thrust::device_ptr<uint>(dGridParticleHash + numParticles),
|
||||
thrust::device_ptr<uint>(dGridParticleIndex));
|
||||
}
|
||||
// check if kernel invocation generated an error
|
||||
getLastCudaError("Kernel execution failed");
|
||||
}
|
||||
|
||||
void reorderDataAndFindCellStart(uint *cellStart,
|
||||
uint *cellEnd,
|
||||
float *sortedPos,
|
||||
float *sortedVel,
|
||||
uint *gridParticleHash,
|
||||
uint *gridParticleIndex,
|
||||
float *oldPos,
|
||||
float *oldVel,
|
||||
uint numParticles,
|
||||
uint numCells)
|
||||
{
|
||||
uint numThreads, numBlocks;
|
||||
computeGridSize(numParticles, 256, numBlocks, numThreads);
|
||||
|
||||
// set all cells to empty
|
||||
checkCudaErrors(cudaMemset(cellStart, 0xffffffff, numCells * sizeof(uint)));
|
||||
|
||||
uint smemSize = sizeof(uint) * (numThreads + 1);
|
||||
reorderDataAndFindCellStartD<<<numBlocks, numThreads, smemSize>>>(cellStart,
|
||||
cellEnd,
|
||||
(float4 *)sortedPos,
|
||||
(float4 *)sortedVel,
|
||||
gridParticleHash,
|
||||
gridParticleIndex,
|
||||
(float4 *)oldPos,
|
||||
(float4 *)oldVel,
|
||||
numParticles);
|
||||
getLastCudaError("Kernel execution failed: reorderDataAndFindCellStartD");
|
||||
}
|
||||
|
||||
void collide(float *newVel,
|
||||
float *sortedPos,
|
||||
float *sortedVel,
|
||||
uint *gridParticleIndex,
|
||||
uint *cellStart,
|
||||
uint *cellEnd,
|
||||
uint numParticles,
|
||||
uint numCells)
|
||||
{
|
||||
// thread per particle
|
||||
uint numThreads, numBlocks;
|
||||
computeGridSize(numParticles, 64, numBlocks, numThreads);
|
||||
|
||||
// execute the kernel
|
||||
collideD<<<numBlocks, numThreads>>>((float4 *)newVel,
|
||||
(float4 *)sortedPos,
|
||||
(float4 *)sortedVel,
|
||||
gridParticleIndex,
|
||||
cellStart,
|
||||
cellEnd,
|
||||
numParticles);
|
||||
|
||||
// check if kernel invocation generated an error
|
||||
getLastCudaError("Kernel execution failed");
|
||||
}
|
||||
|
||||
void sortParticles(uint *dGridParticleHash, uint *dGridParticleIndex, uint numParticles)
|
||||
{
|
||||
thrust::sort_by_key(thrust::device_ptr<uint>(dGridParticleHash),
|
||||
thrust::device_ptr<uint>(dGridParticleHash + numParticles),
|
||||
thrust::device_ptr<uint>(dGridParticleIndex));
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -32,27 +32,28 @@
|
||||
typedef unsigned int uint;
|
||||
|
||||
// simulation parameters
|
||||
struct SimParams {
|
||||
float3 colliderPos;
|
||||
float colliderRadius;
|
||||
struct SimParams
|
||||
{
|
||||
float3 colliderPos;
|
||||
float colliderRadius;
|
||||
|
||||
float3 gravity;
|
||||
float globalDamping;
|
||||
float particleRadius;
|
||||
float3 gravity;
|
||||
float globalDamping;
|
||||
float particleRadius;
|
||||
|
||||
uint3 gridSize;
|
||||
uint numCells;
|
||||
float3 worldOrigin;
|
||||
float3 cellSize;
|
||||
uint3 gridSize;
|
||||
uint numCells;
|
||||
float3 worldOrigin;
|
||||
float3 cellSize;
|
||||
|
||||
uint numBodies;
|
||||
uint maxParticlesPerCell;
|
||||
uint numBodies;
|
||||
uint maxParticlesPerCell;
|
||||
|
||||
float spring;
|
||||
float damping;
|
||||
float shear;
|
||||
float attraction;
|
||||
float boundaryDamping;
|
||||
float spring;
|
||||
float damping;
|
||||
float shear;
|
||||
float attraction;
|
||||
float boundaryDamping;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -32,9 +32,14 @@
|
||||
#ifndef _PARTICLES_KERNEL_H_
|
||||
#define _PARTICLES_KERNEL_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <cooperative_groups.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "thrust/device_ptr.h"
|
||||
#include "thrust/for_each.h"
|
||||
#include "thrust/iterator/zip_iterator.h"
|
||||
#include "thrust/sort.h"
|
||||
|
||||
namespace cg = cooperative_groups;
|
||||
#include "helper_math.h"
|
||||
@@ -46,298 +51,289 @@ __constant__ SimParams cudaParams;
|
||||
|
||||
struct integrate_functor
|
||||
{
|
||||
float deltaTime;
|
||||
float deltaTime;
|
||||
|
||||
__host__ __device__ integrate_functor(float delta_time)
|
||||
: deltaTime(delta_time) {}
|
||||
__host__ __device__ integrate_functor(float delta_time)
|
||||
: deltaTime(delta_time)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename Tuple>
|
||||
__device__ void operator()(Tuple t)
|
||||
{
|
||||
volatile float4 posData = thrust::get<0>(t);
|
||||
volatile float4 velData = thrust::get<1>(t);
|
||||
float3 pos = make_float3(posData.x, posData.y, posData.z);
|
||||
float3 vel = make_float3(velData.x, velData.y, velData.z);
|
||||
template <typename Tuple> __device__ void operator()(Tuple t)
|
||||
{
|
||||
volatile float4 posData = thrust::get<0>(t);
|
||||
volatile float4 velData = thrust::get<1>(t);
|
||||
float3 pos = make_float3(posData.x, posData.y, posData.z);
|
||||
float3 vel = make_float3(velData.x, velData.y, velData.z);
|
||||
|
||||
vel += cudaParams.gravity * deltaTime;
|
||||
vel *= cudaParams.globalDamping;
|
||||
vel += cudaParams.gravity * deltaTime;
|
||||
vel *= cudaParams.globalDamping;
|
||||
|
||||
// new position = old position + velocity * deltaTime
|
||||
pos += vel * deltaTime;
|
||||
// new position = old position + velocity * deltaTime
|
||||
pos += vel * deltaTime;
|
||||
|
||||
// set this to zero to disable collisions with cube sides
|
||||
#if 1
|
||||
|
||||
if (pos.x > 1.0f - cudaParams.particleRadius)
|
||||
{
|
||||
pos.x = 1.0f - cudaParams.particleRadius;
|
||||
vel.x *= cudaParams.boundaryDamping;
|
||||
}
|
||||
if (pos.x > 1.0f - cudaParams.particleRadius) {
|
||||
pos.x = 1.0f - cudaParams.particleRadius;
|
||||
vel.x *= cudaParams.boundaryDamping;
|
||||
}
|
||||
|
||||
if (pos.x < -1.0f + cudaParams.particleRadius)
|
||||
{
|
||||
pos.x = -1.0f + cudaParams.particleRadius;
|
||||
vel.x *= cudaParams.boundaryDamping;
|
||||
}
|
||||
if (pos.x < -1.0f + cudaParams.particleRadius) {
|
||||
pos.x = -1.0f + cudaParams.particleRadius;
|
||||
vel.x *= cudaParams.boundaryDamping;
|
||||
}
|
||||
|
||||
if (pos.y > 1.0f - cudaParams.particleRadius)
|
||||
{
|
||||
pos.y = 1.0f - cudaParams.particleRadius;
|
||||
vel.y *= cudaParams.boundaryDamping;
|
||||
}
|
||||
if (pos.y > 1.0f - cudaParams.particleRadius) {
|
||||
pos.y = 1.0f - cudaParams.particleRadius;
|
||||
vel.y *= cudaParams.boundaryDamping;
|
||||
}
|
||||
|
||||
if (pos.z > 1.0f - cudaParams.particleRadius)
|
||||
{
|
||||
pos.z = 1.0f - cudaParams.particleRadius;
|
||||
vel.z *= cudaParams.boundaryDamping;
|
||||
}
|
||||
if (pos.z > 1.0f - cudaParams.particleRadius) {
|
||||
pos.z = 1.0f - cudaParams.particleRadius;
|
||||
vel.z *= cudaParams.boundaryDamping;
|
||||
}
|
||||
|
||||
if (pos.z < -1.0f + cudaParams.particleRadius)
|
||||
{
|
||||
pos.z = -1.0f + cudaParams.particleRadius;
|
||||
vel.z *= cudaParams.boundaryDamping;
|
||||
}
|
||||
if (pos.z < -1.0f + cudaParams.particleRadius) {
|
||||
pos.z = -1.0f + cudaParams.particleRadius;
|
||||
vel.z *= cudaParams.boundaryDamping;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
if (pos.y < -1.0f + cudaParams.particleRadius)
|
||||
{
|
||||
pos.y = -1.0f + cudaParams.particleRadius;
|
||||
vel.y *= cudaParams.boundaryDamping;
|
||||
}
|
||||
if (pos.y < -1.0f + cudaParams.particleRadius) {
|
||||
pos.y = -1.0f + cudaParams.particleRadius;
|
||||
vel.y *= cudaParams.boundaryDamping;
|
||||
}
|
||||
|
||||
// store new position and velocity
|
||||
thrust::get<0>(t) = make_float4(pos, posData.w);
|
||||
thrust::get<1>(t) = make_float4(vel, velData.w);
|
||||
}
|
||||
// store new position and velocity
|
||||
thrust::get<0>(t) = make_float4(pos, posData.w);
|
||||
thrust::get<1>(t) = make_float4(vel, velData.w);
|
||||
}
|
||||
};
|
||||
|
||||
// calculate position in uniform grid
|
||||
__device__ int3 calcGridPos(float3 p)
|
||||
{
|
||||
int3 gridPos;
|
||||
gridPos.x = floorf((p.x - cudaParams.worldOrigin.x) / cudaParams.cellSize.x);
|
||||
gridPos.y = floorf((p.y - cudaParams.worldOrigin.y) / cudaParams.cellSize.y);
|
||||
gridPos.z = floorf((p.z - cudaParams.worldOrigin.z) / cudaParams.cellSize.z);
|
||||
return gridPos;
|
||||
int3 gridPos;
|
||||
gridPos.x = floorf((p.x - cudaParams.worldOrigin.x) / cudaParams.cellSize.x);
|
||||
gridPos.y = floorf((p.y - cudaParams.worldOrigin.y) / cudaParams.cellSize.y);
|
||||
gridPos.z = floorf((p.z - cudaParams.worldOrigin.z) / cudaParams.cellSize.z);
|
||||
return gridPos;
|
||||
}
|
||||
|
||||
// calculate address in grid from position (clamping to edges)
|
||||
__device__ uint calcGridHash(int3 gridPos)
|
||||
{
|
||||
gridPos.x = gridPos.x &
|
||||
(cudaParams.gridSize.x - 1); // wrap grid, assumes size is power of 2
|
||||
gridPos.y = gridPos.y & (cudaParams.gridSize.y - 1);
|
||||
gridPos.z = gridPos.z & (cudaParams.gridSize.z - 1);
|
||||
return __umul24(__umul24(gridPos.z, cudaParams.gridSize.y), cudaParams.gridSize.x) +
|
||||
__umul24(gridPos.y, cudaParams.gridSize.x) + gridPos.x;
|
||||
gridPos.x = gridPos.x & (cudaParams.gridSize.x - 1); // wrap grid, assumes size is power of 2
|
||||
gridPos.y = gridPos.y & (cudaParams.gridSize.y - 1);
|
||||
gridPos.z = gridPos.z & (cudaParams.gridSize.z - 1);
|
||||
return __umul24(__umul24(gridPos.z, cudaParams.gridSize.y), cudaParams.gridSize.x)
|
||||
+ __umul24(gridPos.y, cudaParams.gridSize.x) + gridPos.x;
|
||||
}
|
||||
|
||||
// calculate grid hash value for each particle
|
||||
__global__ void calcHashD(uint *gridParticleHash, // output
|
||||
uint *gridParticleIndex, // output
|
||||
float4 *pos, // input: positions
|
||||
uint numParticles)
|
||||
__global__ void calcHashD(uint *gridParticleHash, // output
|
||||
uint *gridParticleIndex, // output
|
||||
float4 *pos, // input: positions
|
||||
uint numParticles)
|
||||
{
|
||||
uint index = __umul24(blockIdx.x, blockDim.x) + threadIdx.x;
|
||||
uint index = __umul24(blockIdx.x, blockDim.x) + threadIdx.x;
|
||||
|
||||
if (index >= numParticles)
|
||||
return;
|
||||
if (index >= numParticles)
|
||||
return;
|
||||
|
||||
volatile float4 p = pos[index];
|
||||
volatile float4 p = pos[index];
|
||||
|
||||
// get address in grid
|
||||
int3 gridPos = calcGridPos(make_float3(p.x, p.y, p.z));
|
||||
uint hash = calcGridHash(gridPos);
|
||||
// get address in grid
|
||||
int3 gridPos = calcGridPos(make_float3(p.x, p.y, p.z));
|
||||
uint hash = calcGridHash(gridPos);
|
||||
|
||||
// store grid hash and particle index
|
||||
gridParticleHash[index] = hash;
|
||||
gridParticleIndex[index] = index;
|
||||
// store grid hash and particle index
|
||||
gridParticleHash[index] = hash;
|
||||
gridParticleIndex[index] = index;
|
||||
}
|
||||
|
||||
// rearrange particle data into sorted order, and find the start of each cell
|
||||
// in the sorted hash array
|
||||
__global__ void reorderDataAndFindCellStartD(
|
||||
uint *cellStart, // output: cell start index
|
||||
uint *cellEnd, // output: cell end index
|
||||
float4 *sortedPos, // output: sorted positions
|
||||
float4 *sortedVel, // output: sorted velocities
|
||||
uint *gridParticleHash, // input: sorted grid hashes
|
||||
uint *gridParticleIndex, // input: sorted particle indices
|
||||
float4 *oldPos, // input: sorted position array
|
||||
float4 *oldVel, // input: sorted velocity array
|
||||
uint numParticles)
|
||||
__global__ void reorderDataAndFindCellStartD(uint *cellStart, // output: cell start index
|
||||
uint *cellEnd, // output: cell end index
|
||||
float4 *sortedPos, // output: sorted positions
|
||||
float4 *sortedVel, // output: sorted velocities
|
||||
uint *gridParticleHash, // input: sorted grid hashes
|
||||
uint *gridParticleIndex, // input: sorted particle indices
|
||||
float4 *oldPos, // input: sorted position array
|
||||
float4 *oldVel, // input: sorted velocity array
|
||||
uint numParticles)
|
||||
{
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
extern __shared__ uint sharedHash[]; // blockSize + 1 elements
|
||||
uint index = __umul24(blockIdx.x, blockDim.x) + threadIdx.x;
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
extern __shared__ uint sharedHash[]; // blockSize + 1 elements
|
||||
uint index = __umul24(blockIdx.x, blockDim.x) + threadIdx.x;
|
||||
|
||||
uint hash;
|
||||
uint hash;
|
||||
|
||||
// handle case when no. of particles not multiple of block size
|
||||
if (index < numParticles)
|
||||
{
|
||||
hash = gridParticleHash[index];
|
||||
// handle case when no. of particles not multiple of block size
|
||||
if (index < numParticles) {
|
||||
hash = gridParticleHash[index];
|
||||
|
||||
// Load hash data into shared memory so that we can look
|
||||
// at neighboring particle's hash value without loading
|
||||
// two hash values per thread
|
||||
sharedHash[threadIdx.x + 1] = hash;
|
||||
// Load hash data into shared memory so that we can look
|
||||
// at neighboring particle's hash value without loading
|
||||
// two hash values per thread
|
||||
sharedHash[threadIdx.x + 1] = hash;
|
||||
|
||||
if (index > 0 && threadIdx.x == 0)
|
||||
{
|
||||
// first thread in block must load neighbor particle hash
|
||||
sharedHash[0] = gridParticleHash[index - 1];
|
||||
}
|
||||
}
|
||||
|
||||
cg::sync(cta);
|
||||
|
||||
if (index < numParticles)
|
||||
{
|
||||
// If this particle has a different cell index to the previous
|
||||
// particle then it must be the first particle in the cell,
|
||||
// so store the index of this particle in the cell.
|
||||
// As it isn't the first particle, it must also be the cell end of
|
||||
// the previous particle's cell
|
||||
|
||||
if (index == 0 || hash != sharedHash[threadIdx.x])
|
||||
{
|
||||
cellStart[hash] = index;
|
||||
|
||||
if (index > 0)
|
||||
cellEnd[sharedHash[threadIdx.x]] = index;
|
||||
if (index > 0 && threadIdx.x == 0) {
|
||||
// first thread in block must load neighbor particle hash
|
||||
sharedHash[0] = gridParticleHash[index - 1];
|
||||
}
|
||||
}
|
||||
|
||||
if (index == numParticles - 1)
|
||||
{
|
||||
cellEnd[hash] = index + 1;
|
||||
cg::sync(cta);
|
||||
|
||||
if (index < numParticles) {
|
||||
// If this particle has a different cell index to the previous
|
||||
// particle then it must be the first particle in the cell,
|
||||
// so store the index of this particle in the cell.
|
||||
// As it isn't the first particle, it must also be the cell end of
|
||||
// the previous particle's cell
|
||||
|
||||
if (index == 0 || hash != sharedHash[threadIdx.x]) {
|
||||
cellStart[hash] = index;
|
||||
|
||||
if (index > 0)
|
||||
cellEnd[sharedHash[threadIdx.x]] = index;
|
||||
}
|
||||
|
||||
if (index == numParticles - 1) {
|
||||
cellEnd[hash] = index + 1;
|
||||
}
|
||||
|
||||
// Now use the sorted index to reorder the pos and vel data
|
||||
uint sortedIndex = gridParticleIndex[index];
|
||||
float4 pos = oldPos[sortedIndex];
|
||||
float4 vel = oldVel[sortedIndex];
|
||||
|
||||
sortedPos[index] = pos;
|
||||
sortedVel[index] = vel;
|
||||
}
|
||||
|
||||
// Now use the sorted index to reorder the pos and vel data
|
||||
uint sortedIndex = gridParticleIndex[index];
|
||||
float4 pos = oldPos[sortedIndex];
|
||||
float4 vel = oldVel[sortedIndex];
|
||||
|
||||
sortedPos[index] = pos;
|
||||
sortedVel[index] = vel;
|
||||
}
|
||||
}
|
||||
|
||||
// collide two spheres using DEM method
|
||||
__device__ float3 collideSpheres(float3 posA, float3 posB, float3 velA,
|
||||
float3 velB, float radiusA, float radiusB,
|
||||
float attraction)
|
||||
__device__ float3
|
||||
collideSpheres(float3 posA, float3 posB, float3 velA, float3 velB, float radiusA, float radiusB, float attraction)
|
||||
{
|
||||
// calculate relative position
|
||||
float3 relPos = posB - posA;
|
||||
// calculate relative position
|
||||
float3 relPos = posB - posA;
|
||||
|
||||
float dist = length(relPos);
|
||||
float collideDist = radiusA + radiusB;
|
||||
float dist = length(relPos);
|
||||
float collideDist = radiusA + radiusB;
|
||||
|
||||
float3 force = make_float3(0.0f);
|
||||
float3 force = make_float3(0.0f);
|
||||
|
||||
if (dist < collideDist)
|
||||
{
|
||||
float3 norm = relPos / dist;
|
||||
if (dist < collideDist) {
|
||||
float3 norm = relPos / dist;
|
||||
|
||||
// relative velocity
|
||||
float3 relVel = velB - velA;
|
||||
// relative velocity
|
||||
float3 relVel = velB - velA;
|
||||
|
||||
// relative tangential velocity
|
||||
float3 tanVel = relVel - (dot(relVel, norm) * norm);
|
||||
// relative tangential velocity
|
||||
float3 tanVel = relVel - (dot(relVel, norm) * norm);
|
||||
|
||||
// spring force
|
||||
force = -cudaParams.spring * (collideDist - dist) * norm;
|
||||
// dashpot (damping) force
|
||||
force += cudaParams.damping * relVel;
|
||||
// tangential shear force
|
||||
force += cudaParams.shear * tanVel;
|
||||
// attraction
|
||||
force += attraction * relPos;
|
||||
}
|
||||
// spring force
|
||||
force = -cudaParams.spring * (collideDist - dist) * norm;
|
||||
// dashpot (damping) force
|
||||
force += cudaParams.damping * relVel;
|
||||
// tangential shear force
|
||||
force += cudaParams.shear * tanVel;
|
||||
// attraction
|
||||
force += attraction * relPos;
|
||||
}
|
||||
|
||||
return force;
|
||||
return force;
|
||||
}
|
||||
|
||||
// collide a particle against all other particles in a given cell
|
||||
__device__ float3 collideCell(int3 gridPos, uint index, float3 pos, float3 vel,
|
||||
float4 *oldPos, float4 *oldVel, uint *cellStart,
|
||||
uint *cellEnd)
|
||||
__device__ float3 collideCell(int3 gridPos,
|
||||
uint index,
|
||||
float3 pos,
|
||||
float3 vel,
|
||||
float4 *oldPos,
|
||||
float4 *oldVel,
|
||||
uint *cellStart,
|
||||
uint *cellEnd)
|
||||
{
|
||||
uint gridHash = calcGridHash(gridPos);
|
||||
uint gridHash = calcGridHash(gridPos);
|
||||
|
||||
// get start of bucket for this cell
|
||||
uint startIndex = cellStart[gridHash];
|
||||
// get start of bucket for this cell
|
||||
uint startIndex = cellStart[gridHash];
|
||||
|
||||
float3 force = make_float3(0.0f);
|
||||
float3 force = make_float3(0.0f);
|
||||
|
||||
if (startIndex != 0xffffffff) // cell is not empty
|
||||
{
|
||||
// iterate over particles in this cell
|
||||
uint endIndex = cellEnd[gridHash];
|
||||
|
||||
for (uint j = startIndex; j < endIndex; j++)
|
||||
if (startIndex != 0xffffffff) // cell is not empty
|
||||
{
|
||||
if (j != index) // check not colliding with self
|
||||
{
|
||||
float3 pos2 = make_float3(oldPos[j]);
|
||||
float3 vel2 = make_float3(oldVel[j]);
|
||||
// iterate over particles in this cell
|
||||
uint endIndex = cellEnd[gridHash];
|
||||
|
||||
// collide two spheres
|
||||
force += collideSpheres(pos, pos2, vel, vel2, cudaParams.particleRadius,
|
||||
cudaParams.particleRadius, cudaParams.attraction);
|
||||
}
|
||||
for (uint j = startIndex; j < endIndex; j++) {
|
||||
if (j != index) // check not colliding with self
|
||||
{
|
||||
float3 pos2 = make_float3(oldPos[j]);
|
||||
float3 vel2 = make_float3(oldVel[j]);
|
||||
|
||||
// collide two spheres
|
||||
force += collideSpheres(
|
||||
pos, pos2, vel, vel2, cudaParams.particleRadius, cudaParams.particleRadius, cudaParams.attraction);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return force;
|
||||
return force;
|
||||
}
|
||||
|
||||
__global__ void collideD(
|
||||
float4 *newVel, // output: new velocity
|
||||
float4 *oldPos, // input: sorted positions
|
||||
float4 *oldVel, // input: sorted velocities
|
||||
uint *gridParticleIndex, // input: sorted particle indices
|
||||
uint *cellStart, uint *cellEnd, uint numParticles)
|
||||
__global__ void collideD(float4 *newVel, // output: new velocity
|
||||
float4 *oldPos, // input: sorted positions
|
||||
float4 *oldVel, // input: sorted velocities
|
||||
uint *gridParticleIndex, // input: sorted particle indices
|
||||
uint *cellStart,
|
||||
uint *cellEnd,
|
||||
uint numParticles)
|
||||
{
|
||||
uint index = __mul24(blockIdx.x, blockDim.x) + threadIdx.x;
|
||||
uint index = __mul24(blockIdx.x, blockDim.x) + threadIdx.x;
|
||||
|
||||
if (index >= numParticles)
|
||||
return;
|
||||
if (index >= numParticles)
|
||||
return;
|
||||
|
||||
// read particle data from sorted arrays
|
||||
float3 pos = make_float3(oldPos[index]);
|
||||
float3 vel = make_float3(oldVel[index]);
|
||||
// read particle data from sorted arrays
|
||||
float3 pos = make_float3(oldPos[index]);
|
||||
float3 vel = make_float3(oldVel[index]);
|
||||
|
||||
// get address in grid
|
||||
int3 gridPos = calcGridPos(pos);
|
||||
// get address in grid
|
||||
int3 gridPos = calcGridPos(pos);
|
||||
|
||||
// examine neighbouring cells
|
||||
float3 force = make_float3(0.0f);
|
||||
// examine neighbouring cells
|
||||
float3 force = make_float3(0.0f);
|
||||
|
||||
for (int z = -1; z <= 1; z++)
|
||||
{
|
||||
for (int y = -1; y <= 1; y++)
|
||||
{
|
||||
for (int x = -1; x <= 1; x++)
|
||||
{
|
||||
int3 neighbourPos = gridPos + make_int3(x, y, z);
|
||||
force += collideCell(neighbourPos, index, pos, vel, oldPos, oldVel,
|
||||
cellStart, cellEnd);
|
||||
}
|
||||
for (int z = -1; z <= 1; z++) {
|
||||
for (int y = -1; y <= 1; y++) {
|
||||
for (int x = -1; x <= 1; x++) {
|
||||
int3 neighbourPos = gridPos + make_int3(x, y, z);
|
||||
force += collideCell(neighbourPos, index, pos, vel, oldPos, oldVel, cellStart, cellEnd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// collide with cursor sphere
|
||||
force += collideSpheres(pos, cudaParams.colliderPos, vel,
|
||||
make_float3(0.0f, 0.0f, 0.0f), cudaParams.particleRadius,
|
||||
cudaParams.colliderRadius, 0.0f);
|
||||
// collide with cursor sphere
|
||||
force += collideSpheres(pos,
|
||||
cudaParams.colliderPos,
|
||||
vel,
|
||||
make_float3(0.0f, 0.0f, 0.0f),
|
||||
cudaParams.particleRadius,
|
||||
cudaParams.colliderRadius,
|
||||
0.0f);
|
||||
|
||||
// write new velocity back to original unsorted location
|
||||
uint originalIndex = gridParticleIndex[index];
|
||||
newVel[originalIndex] = make_float4(vel + force, 0.0f);
|
||||
// write new velocity back to original unsorted location
|
||||
uint originalIndex = gridParticleIndex[index];
|
||||
newVel[originalIndex] = make_float4(vel + force, 0.0f);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -25,8 +25,8 @@
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// OpenGL Graphics includes
|
||||
@@ -41,128 +41,134 @@
|
||||
#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_colorVBO(0) {
|
||||
_initGL();
|
||||
: m_pos(0)
|
||||
, m_numParticles(0)
|
||||
, m_pointSize(1.0f)
|
||||
, m_particleRadius(0.125f * 0.5f)
|
||||
, m_program(0)
|
||||
, m_vbo(0)
|
||||
, m_colorVBO(0)
|
||||
{
|
||||
_initGL();
|
||||
}
|
||||
|
||||
ParticleRenderer::~ParticleRenderer() { m_pos = 0; }
|
||||
|
||||
void ParticleRenderer::setPositions(float *pos, int numParticles) {
|
||||
m_pos = pos;
|
||||
m_numParticles = numParticles;
|
||||
void ParticleRenderer::setPositions(float *pos, int numParticles)
|
||||
{
|
||||
m_pos = pos;
|
||||
m_numParticles = numParticles;
|
||||
}
|
||||
|
||||
void ParticleRenderer::setVertexBuffer(unsigned int vbo, int numParticles) {
|
||||
m_vbo = vbo;
|
||||
m_numParticles = numParticles;
|
||||
void ParticleRenderer::setVertexBuffer(unsigned int vbo, int numParticles)
|
||||
{
|
||||
m_vbo = vbo;
|
||||
m_numParticles = numParticles;
|
||||
}
|
||||
|
||||
void ParticleRenderer::_drawPoints() {
|
||||
if (!m_vbo) {
|
||||
glBegin(GL_POINTS);
|
||||
{
|
||||
int k = 0;
|
||||
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;
|
||||
}
|
||||
for (int i = 0; i < m_numParticles; ++i) {
|
||||
glVertex3fv(&m_pos[k]);
|
||||
k += 4;
|
||||
}
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
glEnd();
|
||||
} else {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glVertexPointer(4, GL_FLOAT, 0, 0);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
else {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glVertexPointer(4, GL_FLOAT, 0, 0);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
|
||||
if (m_colorVBO) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_colorVBO);
|
||||
glColorPointer(4, GL_FLOAT, 0, 0);
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
if (m_colorVBO) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_colorVBO);
|
||||
glColorPointer(4, GL_FLOAT, 0, 0);
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
}
|
||||
|
||||
glDrawArrays(GL_POINTS, 0, m_numParticles);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
glDisableClientState(GL_COLOR_ARRAY);
|
||||
}
|
||||
|
||||
glDrawArrays(GL_POINTS, 0, m_numParticles);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
glDisableClientState(GL_COLOR_ARRAY);
|
||||
}
|
||||
}
|
||||
|
||||
void ParticleRenderer::display(DisplayMode mode /* = PARTICLE_POINTS */) {
|
||||
switch (mode) {
|
||||
void ParticleRenderer::display(DisplayMode mode /* = PARTICLE_POINTS */)
|
||||
{
|
||||
switch (mode) {
|
||||
case PARTICLE_POINTS:
|
||||
glColor3f(1, 1, 1);
|
||||
glPointSize(m_pointSize);
|
||||
_drawPoints();
|
||||
break;
|
||||
glColor3f(1, 1, 1);
|
||||
glPointSize(m_pointSize);
|
||||
_drawPoints();
|
||||
break;
|
||||
|
||||
default:
|
||||
case PARTICLE_SPHERES:
|
||||
glEnable(GL_POINT_SPRITE_ARB);
|
||||
glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
|
||||
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
|
||||
glDepthMask(GL_TRUE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_POINT_SPRITE_ARB);
|
||||
glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
|
||||
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
|
||||
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);
|
||||
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);
|
||||
|
||||
glColor3f(1, 1, 1);
|
||||
_drawPoints();
|
||||
glColor3f(1, 1, 1);
|
||||
_drawPoints();
|
||||
|
||||
glUseProgram(0);
|
||||
glDisable(GL_POINT_SPRITE_ARB);
|
||||
break;
|
||||
}
|
||||
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);
|
||||
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);
|
||||
glShaderSource(vertexShader, 1, &vsource, 0);
|
||||
glShaderSource(fragmentShader, 1, &fsource, 0);
|
||||
|
||||
glCompileShader(vertexShader);
|
||||
glCompileShader(fragmentShader);
|
||||
glCompileShader(vertexShader);
|
||||
glCompileShader(fragmentShader);
|
||||
|
||||
GLuint program = glCreateProgram();
|
||||
GLuint program = glCreateProgram();
|
||||
|
||||
glAttachShader(program, vertexShader);
|
||||
glAttachShader(program, fragmentShader);
|
||||
glAttachShader(program, vertexShader);
|
||||
glAttachShader(program, fragmentShader);
|
||||
|
||||
glLinkProgram(program);
|
||||
glLinkProgram(program);
|
||||
|
||||
// check if program linked
|
||||
GLint success = 0;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
||||
// check if program linked
|
||||
GLint success = 0;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
||||
|
||||
if (!success) {
|
||||
char temp[256];
|
||||
glGetProgramInfoLog(program, 256, 0, temp);
|
||||
printf("Failed to link program:\n%s\n", temp);
|
||||
glDeleteProgram(program);
|
||||
program = 0;
|
||||
}
|
||||
if (!success) {
|
||||
char temp[256];
|
||||
glGetProgramInfoLog(program, 256, 0, temp);
|
||||
printf("Failed to link program:\n%s\n", temp);
|
||||
glDeleteProgram(program);
|
||||
program = 0;
|
||||
}
|
||||
|
||||
return program;
|
||||
return program;
|
||||
}
|
||||
|
||||
void ParticleRenderer::_initGL() {
|
||||
m_program = _compileProgram(vertexShader, spherePixelShader);
|
||||
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);
|
||||
glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE);
|
||||
glClampColorARB(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -28,46 +28,48 @@
|
||||
#ifndef __RENDER_PARTICLES__
|
||||
#define __RENDER_PARTICLES__
|
||||
|
||||
class ParticleRenderer {
|
||||
public:
|
||||
ParticleRenderer();
|
||||
~ParticleRenderer();
|
||||
class ParticleRenderer
|
||||
{
|
||||
public:
|
||||
ParticleRenderer();
|
||||
~ParticleRenderer();
|
||||
|
||||
void setPositions(float *pos, int numParticles);
|
||||
void setVertexBuffer(unsigned int vbo, int numParticles);
|
||||
void setColorBuffer(unsigned int vbo) { m_colorVBO = vbo; }
|
||||
void setPositions(float *pos, int numParticles);
|
||||
void setVertexBuffer(unsigned int vbo, int numParticles);
|
||||
void setColorBuffer(unsigned int vbo) { m_colorVBO = vbo; }
|
||||
|
||||
enum DisplayMode { PARTICLE_POINTS, PARTICLE_SPHERES, PARTICLE_NUM_MODES };
|
||||
enum DisplayMode { PARTICLE_POINTS, PARTICLE_SPHERES, PARTICLE_NUM_MODES };
|
||||
|
||||
void display(DisplayMode mode = PARTICLE_POINTS);
|
||||
void displayGrid();
|
||||
void display(DisplayMode mode = PARTICLE_POINTS);
|
||||
void displayGrid();
|
||||
|
||||
void setPointSize(float size) { m_pointSize = size; }
|
||||
void setParticleRadius(float r) { m_particleRadius = r; }
|
||||
void setFOV(float fov) { m_fov = fov; }
|
||||
void setWindowSize(int w, int h) {
|
||||
m_window_w = w;
|
||||
m_window_h = h;
|
||||
}
|
||||
void setPointSize(float size) { m_pointSize = size; }
|
||||
void setParticleRadius(float r) { m_particleRadius = r; }
|
||||
void setFOV(float fov) { m_fov = fov; }
|
||||
void setWindowSize(int w, int h)
|
||||
{
|
||||
m_window_w = w;
|
||||
m_window_h = h;
|
||||
}
|
||||
|
||||
protected: // methods
|
||||
void _initGL();
|
||||
void _drawPoints();
|
||||
GLuint _compileProgram(const char *vsource, const char *fsource);
|
||||
protected: // methods
|
||||
void _initGL();
|
||||
void _drawPoints();
|
||||
GLuint _compileProgram(const char *vsource, const char *fsource);
|
||||
|
||||
protected: // data
|
||||
float *m_pos;
|
||||
int m_numParticles;
|
||||
protected: // data
|
||||
float *m_pos;
|
||||
int m_numParticles;
|
||||
|
||||
float m_pointSize;
|
||||
float m_particleRadius;
|
||||
float m_fov;
|
||||
int m_window_w, m_window_h;
|
||||
float m_pointSize;
|
||||
float m_particleRadius;
|
||||
float m_fov;
|
||||
int m_window_w, m_window_h;
|
||||
|
||||
GLuint m_program;
|
||||
GLuint m_program;
|
||||
|
||||
GLuint m_vbo;
|
||||
GLuint m_colorVBO;
|
||||
GLuint m_vbo;
|
||||
GLuint m_colorVBO;
|
||||
};
|
||||
|
||||
#endif //__ RENDER_PARTICLES__
|
||||
#endif //__ RENDER_PARTICLES__
|
||||
|
||||
@@ -28,41 +28,38 @@
|
||||
#define STRINGIFY(A) #A
|
||||
|
||||
// vertex shader
|
||||
const char *vertexShader = STRINGIFY(
|
||||
uniform float pointRadius; // point size in world space
|
||||
uniform float pointScale; // scale to calculate size in pixels
|
||||
uniform float densityScale;
|
||||
uniform float densityOffset;
|
||||
void main()
|
||||
{
|
||||
// calculate window-space point size
|
||||
vec3 posEye = vec3(gl_ModelViewMatrix * vec4(gl_Vertex.xyz, 1.0));
|
||||
float dist = length(posEye);
|
||||
gl_PointSize = pointRadius * (pointScale / dist);
|
||||
const char *vertexShader = STRINGIFY(uniform float pointRadius; // point size in world space
|
||||
uniform float pointScale; // scale to calculate size in pixels
|
||||
uniform float densityScale;
|
||||
uniform float densityOffset;
|
||||
void main() {
|
||||
// calculate window-space point size
|
||||
vec3 posEye = vec3(gl_ModelViewMatrix * vec4(gl_Vertex.xyz, 1.0));
|
||||
float dist = length(posEye);
|
||||
gl_PointSize = pointRadius * (pointScale / dist);
|
||||
|
||||
gl_TexCoord[0] = gl_MultiTexCoord0;
|
||||
gl_Position = gl_ModelViewProjectionMatrix * vec4(gl_Vertex.xyz, 1.0);
|
||||
gl_TexCoord[0] = gl_MultiTexCoord0;
|
||||
gl_Position = gl_ModelViewProjectionMatrix * vec4(gl_Vertex.xyz, 1.0);
|
||||
|
||||
gl_FrontColor = gl_Color;
|
||||
});
|
||||
gl_FrontColor = gl_Color;
|
||||
});
|
||||
|
||||
// pixel shader for rendering points as shaded spheres
|
||||
const char *spherePixelShader = STRINGIFY(
|
||||
void main()
|
||||
{
|
||||
const char *spherePixelShader = STRINGIFY(void main() {
|
||||
const vec3 lightDir = vec3(0.577, 0.577, 0.577);
|
||||
|
||||
// calculate normal from texture coordinates
|
||||
vec3 N;
|
||||
N.xy = gl_TexCoord[0].xy*vec2(2.0, -2.0) + vec2(-1.0, 1.0);
|
||||
N.xy = gl_TexCoord[0].xy * vec2(2.0, -2.0) + vec2(-1.0, 1.0);
|
||||
float mag = dot(N.xy, N.xy);
|
||||
|
||||
if (mag > 1.0) discard; // kill pixels outside circle
|
||||
if (mag > 1.0)
|
||||
discard; // kill pixels outside circle
|
||||
|
||||
N.z = sqrt(1.0-mag);
|
||||
N.z = sqrt(1.0 - mag);
|
||||
|
||||
// calculate lighting
|
||||
float diffuse = max(0.0, dot(lightDir, N));
|
||||
|
||||
gl_FragColor = gl_Color * diffuse;
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user