View Full Version : Particle system help

01-09-2018, 07:25 AM
I have been going through the web trying to find the method to make a texture based instance particle system. I can make a window with a camera pan, but would like to have a functional particle system.

01-09-2018, 10:02 PM
I have been devoting several videos on my opengl tutorial youtube channel to just that purpose! My favorite programming challenge / hobby is playing with particle systems!


I implement particle systems two main ways: 1) Drawing Textures on Quads, and 2) Drawing Textures on Point Sprites.

For the #2 method, here's the video showing how to enable point sprites:


and here's the video showing the first particle system:


... and this is one using the quads method...


I am just learning too, and if you have any requests for videos, please let me know, anything that I can research and make a tutorial video for, would be great! Thanks,

Let me know what you think,


01-10-2018, 06:04 AM
I Have checked out your tutorials they seem good gonna subscribe. one thing I would like to know is the whole shader.txt and vertex.txt in notepad at the moment i am working with a camera that is stuck on a fixed angle i can post the code if needed. Have to have something done before friday.

01-11-2018, 06:22 AM

#include "Scene.h"
#include <iostream>
#include <SDL/SDL.h>


_cameraAngleX = 0.3f, _cameraAngleY = 1.5f;

// Set up the viewing matrix
// This represents the camera's orientation and position
_viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0,0,-3.5f) );

// Set up a projection matrix
_projMatrix = glm:erspective(45.0f, 1.0f, 0.1f, 100.0f);

// Set up your scene here

//skybox setup
_skysphere = new GameObject();

Material *skysphereTexture = new Material();
skysphereTexture->LoadShaders("VertShader.txt", "FragShader.txt");
skysphereTexture->SetDiffuseColour(glm::vec3(1.0, 1.0, 1.0));

Mesh *skyMesh = new Mesh();



// Position of the light, in world-space
_lightPosition = glm::vec3(10,10,0);

// Create a game object
// This needs a material and a mesh
_model = new GameObject();
_fountain = new GameObject();

// Create the material for the game object
Material *modelMaterial = new Material();
Material *fountainMaterial = new Material();

// Shaders are now in files
fountainMaterial->LoadShaders("VertShader.txt", "FragShader.txt");

// You can set some simple material properties, these values are passed to the shader
// This colour modulates the texture colour

modelMaterial->SetDiffuseColour( glm::vec3(1.0f,1.0f,1.0f) );
fountainMaterial->SetDiffuseColour( glm::vec3(1.0f,1.0f,1.0f) );
// The material currently supports one texture
// This is multiplied by all the light components (ambient, diffuse, specular)
// Note that the diffuse colour set with the line above will be multiplied by the texture colour
// If you want just the texture colour, use modelMaterial->SetDiffuseColour( glm::vec3(1,1,1) );


// Need to tell the material the light's position
// If you change the light's position you need to call this again

// Tell the game object to use this material

// The mesh is the geometry for the object
Mesh *modelMesh = new Mesh();
Mesh *fountainMesh = new Mesh();

// Load from OBJ file. This must have triangulated geometry

// Tell the game object to use this mesh
_fountain->SetPosition(0, -3, 0);

// You should neatly clean everything up here

void Scene::Update( float deltaTs )
// Update the game object (this is currently hard-coded to rotate)
_model->Update( deltaTs );

// This updates the camera's position and orientation
_viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, -11.0f)); // Provides offset away from player object
_viewMatrix = glm::rotate(_viewMatrix, _cameraAngleX, glm::vec3(1, 0, 0)); // Allows player to rotate camera using player object as pivot
_viewMatrix = glm::rotate(_viewMatrix, _cameraAngleY, glm::vec3(0, 1, 0));


void Scene::Draw()
// Draw that model, giving it the camera's position and projection
_skysphere->Draw(_viewMatrix, _projMatrix);
_fountain->Draw(_viewMatrix, _projMatrix);

##version 430 core vert shader.txt
// Per-vertex inputs
layout(location = 0) in vec4 vPosition;
layout(location = 1) in vec3 vNormalIn;
layout(location = 2) in vec2 vTexCoordIn;

// Uniform data inputs are the same for all vertices
uniform mat4 modelMat;
uniform mat4 invModelMat;
uniform mat4 viewMat;
uniform mat4 projMat;

uniform vec4 worldSpaceLightPos = {1,0.0,1,1};

// These per-vertex outputs must correspond to the per-fragment inputs in the fragment shader
out vec3 vNormalV;
out vec3 eyeSpaceLightPosV;
out vec3 eyeSpaceVertPosV;
out vec2 texCoord;

void main()
// Perform vertex transformations
gl_Position = projMat * viewMat * modelMat * vPosition;

// Vector from eye to vertex position, in eye-space
eyeSpaceVertPosV = vec3(viewMat * modelMat * vPosition);
// Vector from vertex position to light position, in eye-space
eyeSpaceLightPosV = vec3(viewMat * worldSpaceLightPos);

// Vertex normal, in eye-space
vNormalV = mat3(viewMat * modelMat) * vNormalIn;

// Pass through the texture coordinate
texCoord = vTexCoordIn;

#version 430 core frag shader

// Input per-fragment data
// These must correspond to the varying outputs from the vertex shader
in vec3 vNormalV;
in vec3 eyeSpaceLightPosV;
in vec3 eyeSpaceVertPosV;
in vec2 texCoord;

// Input uniform data - these have values that will be the same for every fragment
uniform vec3 lightColour = {0.8,0.8,0.8};
uniform vec3 emissiveColour = {0,0,0};
uniform vec3 ambientColour = {0.1f,0.1f,0.2f};
uniform vec3 diffuseColour = {1.0f,1.0f,1.0f};
uniform vec3 specularColour = {1.0f,1.0f,1.0f};
uniform float shininess = 50.0f;
uniform float alpha = 1.0f;

// The output of the fragment shader is the fragment's colour
out vec4 fragColour;

// This is another input to allow us to access a texture
uniform sampler2D tex1;

void main()
// Calculate the direction from the sample position to the light
vec3 lightDir = normalize( eyeSpaceLightPosV - eyeSpaceVertPosV );
// Ensure the normal is a unit vector
vec3 normal = normalize( vNormalV );
// Direction from eye to sample point
vec3 eyeDir = normalize( -eyeSpaceVertPosV );
// Vector half way between one to eye and one to light
vec3 halfVec = normalize( eyeDir + lightDir );

// Retrieve colour from texture
vec3 texColour = vec3(texture(tex1,vec2(texCoord.x,1-texCoord.y)));
// Calculate diffuse lighting
vec3 diffuse = diffuseColour * lightColour * max( dot( normal, lightDir ), 0);

// Put specular lighting code here!
vec3 specular = vec3(0);

// Final colour uses the texture colour for all components
// If you want a separate texture for specular you will need to change this
fragColour = vec4( emissiveColour + texColour*(ambientColour + diffuse + specular), alpha);

Particle bit

#include "Particle.h"
#include "glew.h"
#include <iostream>
#include <SDL/SDL.h>
#include <GLM/glm.hpp>
#include <GLM/gtc/matrix_transform.hpp>





#include <string>
#include <GLM/glm.hpp>
#include "glew.h"
#include <GLM\matrix.hpp>
#include <GLM/gtc/matrix_transform.hpp>

class Particle



01-11-2018, 06:38 AM

01-11-2018, 09:23 AM
Be sure to use the [ code ] [ / code ] tags when posting your code. Especially when posting large amounts of code.