Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualic0de

Posted 22 October 2012 - 09:57 PM

So I tried simulating my particles in the vertex shader. I wrote a simple shader that moves point sprite particles based on a velocity vector passed via the colour buffer. Also passed through the alpha component of the colour buffer is the time of the particle's creation. I passed a simple gravity vector as velocity (0, -1.0, 0) and I ran into a problem. The particles fall alright but they start off far lower than they are supposed to. I'm thinking this might have to do with the fact that I'm passing data through the color buffer.

Vertex Shader:
uniform float Time;
uniform float scr_h;


void main()
{
	 vec4 newpos = gl_Vertex;

	 newpos.xyz += gl_Color.xyz*(Time - gl_Color.w); // move the sprite based on velocity

	 vec3 particle = vec3(gl_ModelViewMatrix * newpos); // scale point sprite for perspective
	 gl_PointSize = scr_h / sqrt(dot(particle, particle));
	
	 gl_Position = gl_ModelViewProjectionMatrix * newpos;
}

C++ part:
glUniform1f(ptime, float(SDL_GetTicks()/1000.0));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glUniform1f(pr_scrh, float(screen->h));
glBindBuffer(GL_ARRAY_BUFFER, Vbuffer);
glBufferData(GL_ARRAY_BUFFER, particle::getNumParticles() * 3 * sizeof(float), particle::get(), GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, Tbuffer);
glBufferData(GL_ARRAY_BUFFER, particle::getNumParticles() * 4 * sizeof(float), particle::getVelocityTime(), GL_STATIC_DRAW);
glColorPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_POINTS, 0,  particle::getNumParticles() * 3);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

Anything obvious wrong with my code or even my approach?

P.S. don't bother telling me if anything I'm using is deprecated because this is targeted for OpenGL 2.1

#1ic0de

Posted 22 October 2012 - 09:56 PM

So I tried simulating my particles in the vertex shader. I wrote a simple shader that moves point sprite particles based on a velocity vector passed via the colour buffer. Also passed through the alpha component of the colour buffer is the time of the particle's creation. I passed a simple gravity vector as velocity (0, -1.0, 0) and I ran into a problem. The particles fall alright but they start off far lower than they are supposed to. I'm thinking this might have to do with the fact that I'm passing data through the color buffer.

Vertex Shader:
uniform float Time;
uniform float scr_h;


void main()
{
	 vec4 newpos = gl_Vertex;

	 newpos.xyz += gl_Color.xyz*(Time - gl_Color.w); // move the sprite based on velocity

	 vec3 particle = vec3(gl_ModelViewMatrix * newpos); // scale point sprite for perspective
	 gl_PointSize = scr_h / sqrt(dot(particle, particle));
     
	 gl_Position = gl_ModelViewProjectionMatrix * newpos;
}

C++ part:
glUniform1f(ptime, float(SDL_GetTicks()/1000.0));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glUniform1f(pr_scrh, float(screen->h));
glBindBuffer(GL_ARRAY_BUFFER, Vbuffer);
glBufferData(GL_ARRAY_BUFFER, particle::getNumParticles() * 3 * sizeof(float), particle::get(), GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, Tbuffer);
glBufferData(GL_ARRAY_BUFFER, particle::getNumParticles() * 4 * sizeof(float), particle::getVelocityTime(), GL_STATIC_DRAW);
glColorPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_POINTS, 0,  particle::getNumParticles() * 3);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

Anything obvious

P.S. don't bother telling me if anything I'm using is deprecated because this is targeted for OpenGL 2.1

PARTNERS