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

About this blog

An eye into my 3D world

Entries in this blog


Spherical point sprites in OpenGL 3.3

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);
glBindBuffer (GL_ARRAY_BUFFER, vboID);
glBufferData (GL_ARRAY_BUFFER, sizeof(positions), &positions[0], GL_STATIC_DRAW);
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
glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glDrawArrays(GL_POINTS, 0, TOTAL);

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.



Sign in to follow this  
  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!