Jump to content
  • Advertisement
Sign in to follow this  
  • entry
    1
  • comments
    0
  • views
    6877

Spherical point sprites in OpenGL 3.3

Sign in to follow this  
mobeen

4091 views

It often helps to render particle sprites as sphere. One it helps to reduces the polygonization which would otherwise be needed and second it too simple to achieve. So without any delay lets see how to put it up in OpenGL3.3.
Geometry setup:
We store positions of 9 vertices in the global scope as follows.

const int TOTAL= 9;
GLfloat positions[3*TOTAL]={-1,0,-1, 0,0,-1, 1,0,-1,
-1,0, 0, 0,0, 0, 1,0, 0,
-1,0, 1, 0,0, 1, 1,0,1};


Next, we setup a vertex array object (VAO) and a vertex buffer object (VBO) for holding the point positions.

//Create vao and vbo stuff
glGenVertexArrays(1, &vaoID);
glGenBuffers (1, &vboID);
glBindVertexArray(vaoID);
glBindBuffer (GL_ARRAY_BUFFER, vboID);
glBufferData (GL_ARRAY_BUFFER, sizeof(positions), &positions[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(shader["vVertex"]);
glVertexAttribPointer (shader["vVertex"], 3, GL_FLOAT, GL_FALSE,stride,0);glBindVertexArray(0);


[size="2"]The shader is handled by using a custom GLSLShader class. Once the vao setup is complete, we proceed with the rendering.

//draw the points
shader.Use();
glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glBindVertexArray(vaoID);
glDrawArrays(GL_POINTS, 0, TOTAL);
glBindVertexArray(0);
shader.UnUse();


Shaders
The vertex shader is a simple passthrough shader computing the clip space position of the points.

#version 330
in vec3 vVertex;
uniform mat4 MVP;
void main()
{
gl_Position = MVP*vec4(vVertex,1);
}

The fragment shader calculates the normal from the texture coordinates and then calculates the diffuse contribution. The texture coordinates in case of point sprites are available in the gl_PointCoord variable.

#version 330
out vec4 vFragColor;
uniform vec3 Color;
uniform vec3 lightDir;
void main()
{
// 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;
}

That's it. Running the code gives the following output.
particlesAsSpheres.png


Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 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!