使用OpenGL制作动画,使用CUDA计算

使用OpenGL制作动画,使用CUDA计算,opengl,cuda,interop,Opengl,Cuda,Interop,我写了一个CUDA随机行走模拟。也就是说,我有两个向量X和Y,其中包含我模拟的所有粒子的位置。在每个时间步中,我根据我的计算更新它们的位置 /* Calculate velocity magnitude */ CURAND_CALL(curandGenerateNormal(gen, dev_v, Nrand,0,1.0)); /* Generate direction on device */ CURAND_CALL(curandGenerateUniform(gen, dev_alpha,

我写了一个CUDA随机行走模拟。也就是说,我有两个向量X和Y,其中包含我模拟的所有粒子的位置。在每个时间步中,我根据我的计算更新它们的位置

/* Calculate velocity magnitude */
CURAND_CALL(curandGenerateNormal(gen, dev_v, Nrand,0,1.0));
/* Generate direction on device */
CURAND_CALL(curandGenerateUniform(gen, dev_alpha, Nrand));
/* X_t+1 = X_t + V */
scosaxpy<<<blocksPerGrid,threadsPerBlock>>>(Nrand,dev_alpha,dev_v,dev_x);
ssinaxpy<<<blocksPerGrid,threadsPerBlock>>>(Nrand,dev_alpha,dev_v,dev_y);
现在是我没有背景的部分。用OpenGL可视化。我查阅了一些示例(NVIDIA还提供了一些CUDA和OpenGL之间交互的示例),但大多数示例要么过于复杂,要么无法处理由CUDA进行计算的情况

我的问题是:有谁能告诉我用以下方式可视化输出的步骤吗

for(t=0;t<T;t++){
   /* calculate velocity magnitude and direction */
   /* update position */
   /* eventually copy memory to host */

   /* Update the position of my particles in a 2D plot */
}

用于(t=0;t在OpenGL中有很多方法可以做到这一点。它们的复杂性和性能各不相同。由于您使用CUDA,我相信您希望可视化运行得非常快。这比只渲染这些点而不考虑性能要复杂一些。如果您以前没有OpenGL经验,我建议使用最简单的方法htforward解决方案,即一次绘制一个点。请看本教程(这只是一个示例。您可以尝试任何具有工作代码的教程):

试着运行它并查看一个三角形。现在找到display()函数,并查看负责绘制的代码放在glBegin(GL_TRIANGLES)和glEnd()之间

你需要修改它,因为你不想画三角形。这里是你应该做的。在CUDA步骤之后,我希望你有一个3D点阵列

for(t=0;t<T;t++){
   /* calculate velocity magnitude and direction */
   /* update position */
   /* eventually copy memory to host */
}

/* Now your points are stored in an array. Let's say float particles[T][3]; */
/* Update the position of my particles in a 2D plot */
display();

用于(t=0;t您需要可视化输出吗?为什么写入文本文件并检查输出是否正确还不够?我也在将数据写入文件,但为了更好地理解,我想可视化结果。我也不明白为什么这篇文章被否决。请您解释一下?我没有否决您的问题,但那些没有这样做的人可能不喜欢你让他们做你的工作:)有很多OpenGL教程,所以你在完成这项工作时没有“真正的”障碍。。。
for(t=0;t<T;t++){
   /* calculate velocity magnitude and direction */
   /* update position */
   /* eventually copy memory to host */
}

/* Now your points are stored in an array. Let's say float particles[T][3]; */
/* Update the position of my particles in a 2D plot */
display();
glBegin(GL_POINTS);
    for(int t=0;i<T;t++)
    {
        glColor3f(1.0f,1.0f,1.0f);  //In case you want to give them unique colors in future     
        glVertex3f( particles[t][0], particles[t][1], particles[t][2]);
    }                               
glEnd();