I am working on a particle simulation where we have a lot of spheres which can have different radii. Using this tutorial http://mmmovania.blogspot.de/2011/01/point-sprites-as-spheres-in-opengl33.html (see also code below) I was able to create a sphere from a point, but they all have the size from glPointSize(). Is it possible to extend this with a radius?
#version 330
out vec4 vFragColor;
uniform vec3 Color;
uniform vec3 lightDir;
void main(void)
{
// calculate normal from texture coordinates
vec3 N;
N.xy = gl_PointCoord* 2.0 - vec2(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));
vFragColor = vec4(Color,1) * diffuse;
}
Maybe of interest: I am using #version 120
for my shaders.
Or, is there a better way to do this?
So, combining the two answers I now have:
void
Draw::setGeometry(float* geometry, float* velocity, float* radius, GLuint size){
this->_size = size;
glGenBuffers(1, &_vertexData);
glBindBuffer(GL_ARRAY_BUFFER, _vertexData);
glBufferData(GL_ARRAY_BUFFER, _size * 3 * sizeof(float), geometry, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &_velocityData);
glBindBuffer(GL_ARRAY_BUFFER, _velocityData);
glBufferData(GL_ARRAY_BUFFER, size * 3 * sizeof(float), velocity, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &_radiusData);
glBindBuffer(GL_ARRAY_BUFFER, _radiusData);
glBufferData(GL_ARRAY_BUFFER, size * sizeof(float), radius, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
And the drawing method:
void
Draw::paint(GLenum mode){
glBindBuffer(GL_ARRAY_BUFFER, _vertexData);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(mode, 0, _size * 3 * sizeof(float));
glDisableVertexAttribArray(0);
}
And the Vertex Shader:
#version 130
attribute vec3 position;
attribute vec3 velocity;
attribute float size;
uniform mat4 MVP;
uniform mat4 MV;
void main(){
gl_PointSize = size;
gl_Position = MVP * vec4(position, 1.0);
}
The problem is that when gl_PointSize = 15
is set, they are drawn well. When I make it equal with size, then it looks like this: http://picpaste.com/Big_particles-daIBnJCG.png
Works better now. Particles still getting bigger as they move along.