Jump to content
  • Advertisement
Sign in to follow this  
myrkul

OpenGL Particle simulation [OpenGl, Cuda, Shaders]

This topic is 2572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,
I try to create particle simulation (I'm beginner with OpenGl and Cuda). In my simulation I use code from Cuda Sdk (Particles).
OK, let me explain my problem: I render 1k particles and move them. Everything works fine until particle are near the center of coordinate system...
The problem is that they grow(each particle has const radius so its unexpected situation - picture below)... and I really don't know why.

Can u help me?

picwu.jpg
Bigger size

My code:

function void init()

GLuint pointsVBO;
float3 *pointsVBO_device; // device pointer for VBO
struct cudaGraphicsResource *pointsVBO_Resource;
const float radius = 0.2;

void init(void)
{
GLenum err = glewInit();
if (GLEW_OK != err) exit(0);

initializePositionsAndVelocities();

void **v_device_addr;
cudaGetSymbolAddress ((void **)&v_device_addr, "v_device");
cudaMemcpy (v_device_addr, velocities, sizeof(float3) * N, cudaMemcpyHostToDevice );

glGenBuffers(1,&pointsVBO);
glBindBuffer(GL_ARRAY_BUFFER, pointsVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STREAM_DRAW);

cudaGraphicsGLRegisterBuffer(&pointsVBO_Resource, pointsVBO, cudaGraphicsMapFlagsNone );

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClearDepth(1.0f);
glDisable(GL_DEPTH_TEST);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
}


function compileProgram()

GLuint 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);

glLinkProgram(program);

// 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;
}

return program;
}


function renderScene()

void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_POINT_SPRITE_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);

GLuint program = compileProgram(vertexShader, spherePixelShader);
float fov = 60;

glUseProgram(program);
glUniform1f(glGetUniformLocation(program, "pointScale"),
parameter.height / tanf(fov*0.5f*(float)M_PI/180.0f));
glUniform1f(glGetUniformLocation(program, "pointRadius"), radius);

glColor3f(1, 1, 0);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, pointsVBO);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_POINTS, 0, N);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glDisableClientState(GL_VERTEX_ARRAY);

glUseProgram(0);
glDisable(GL_POINT_SPRITE_ARB);
glutSwapBuffers();
}


shader

#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);

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * vec4(gl_Vertex.xyz, 1.0);

gl_FrontColor = gl_Color;
}
);

// pixel shader for rendering points as shaded spheres
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);
float mag = dot(N.xy, N.xy);
if (mag > 1.0) discard; // kill pixels outside circle
N.z = sqrt(1.0-mag);

// calculate lighting
float diffuse = max(0.0, dot(lightDir, N));

gl_FragColor = gl_Color * diffuse;
}
);

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!