当前位置: 动力学知识库 > 问答 > 编程问答 >

opengl - How to use CUDA interop for rendering spheres?

问题描述:

I have a large number of spheres. I do a cudamemcpy and transfer mass positions , charge , initial velocity and radius to the device. I calculate the interactions between all the sphere and compute the final positions. My problem is I want to render these spheres

GLuint vbo[2];

glGenBuffers( 2, vbo);

glBindBuffer( GL_ARRAY_BUFFER, vbo[0]);

unsigned int size = numBodies * 4 * sizeof( float);

float4* initialpos = (float4*)malloc(numBodies*4*sizeof(float));

for(unsigned int i = 0; i < numBodies; i++)

{

// Initialize initialpos which are the centers of the spheres

}

glBufferData( GL_ARRAY_BUFFER, size, initialpos, GL_DYNAMIC_DRAW);

free(initialpos);

glBindBuffer( GL_ARRAY_BUFFER, vbo[1]);

glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);

glBindBuffer( GL_ARRAY_BUFFER, 0);

// register vbo with CUDA

cudaGLRegisterBufferObject(vbo[0]);

cudaGLRegisterBufferObject(vbo[1]);

//inside the display function

float4* oldPos;

float4* newPos;

cudaGLMapBufferObject( (void**)&newPos, vbo[1-swap])

cudaGLMapBufferObject( (void**)&oldPos, vbo[swap]);

//Call a C function which calls LAUNCHES CUDA KERNEL with old and new Positions

// Unmap Buffer Objects

swap = 1 - swap;

I change newpos and oldPos in the kernel. Now to draw points I would

use a glDrawElements(GL_POINTS,.....);

My problem is how do I draw spheres with this data as each of the 4 bytes of the newpos array has the centre of a sphere basically something like instead of GL_POINTS something like "GL_SPHERES".

网友答案:

There is no such thing as "GL_SPHERES" in OpenGL that simply lets you draw a sphere. You might want to raytrace a sphere using point sprites and GLSL shaders. The general idea is described in:

Stefan Gumhold: Splatting Illuminated Ellipsoids with Depth Correction. VMV 2003.

By the way: This is currently the fastest way to render a large number of spherical particles and it will give you perfect spheres. The "particles" demo in the Nvidia GPU Computing SDK does something similar to render the particle spheres. However, the spheres may not overlap in the Nvidia demo, since it doesn't handle sphere-sphere intersections due to the uniform depth values!

分享给朋友:
您可能感兴趣的文章:
随机阅读: