Skip to content

Commit

Permalink
Clarified shader code for particles
Browse files Browse the repository at this point in the history
  • Loading branch information
hsm4 committed Dec 11, 2023
1 parent 4cff6bc commit 85e20d8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 44 deletions.
53 changes: 27 additions & 26 deletions modules/sl/source/gl/SLGLEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
enum SLGLBufferType
{
BT_float = GL_FLOAT, //!< float vertex attributes
BT_int = GL_INT, //!< int vertex attributes
BT_int = GL_INT, //!< int vertex attributes
BT_ubyte = GL_UNSIGNED_BYTE, //!< vertex index type (0-2^8)
BT_ushort = GL_UNSIGNED_SHORT, //!< vertex index type (0-2^16)
BT_uint = GL_UNSIGNED_INT //!< vertex index type (0-2^32)
Expand Down Expand Up @@ -55,33 +55,34 @@ enum SLGLAttributeType
SLVVfloat Jw; //!< 2D Vector of per vertex joint weights (opt.) layout (location = 7)
*/

AT_position = 0, //!< Vertex position as a 2, 3 or 4 component vectors
AT_normal, //!< Vertex normal as a 3 component vector
AT_uv1, //!< Vertex 1st texture coordinate as 2 component vector
AT_uv2, //!< Vertex 2nd texture coordinate as 2 component vector
AT_color, //!< Vertex color as 3 or 4 component vector
AT_tangent, //!< Vertex tangent as a 4 component vector (see SLMesh)
AT_jointIndex, //!< Vertex joint id for vertex skinning
AT_jointWeight, //!< Vertex joint weight for vertex skinning
AT_position = 0, //!< Vertex position as a 2, 3 or 4 component vectors
AT_normal, //!< Vertex normal as a 3 component vector
AT_uv1, //!< Vertex 1st texture coordinate as 2 component vector
AT_uv2, //!< Vertex 2nd texture coordinate as 2 component vector
AT_color, //!< Vertex color as 3 or 4 component vector
AT_tangent, //!< Vertex tangent as a 4 component vector (see SLMesh)
AT_jointIndex, //!< Vertex joint id for vertex skinning
AT_jointWeight, //!< Vertex joint weight for vertex skinning

AT_custom0, //!< Custom vertex attribute 0
AT_custom1, //!< Custom vertex attribute 1
AT_custom2, //!< Custom vertex attribute 2
AT_custom3, //!< Custom vertex attribute 3
AT_custom4, //!< Custom vertex attribute 4
AT_custom5, //!< Custom vertex attribute 5
AT_custom6, //!< Custom vertex attribute 6
AT_custom7, //!< Custom vertex attribute 7
AT_custom8, //!< Custom vertex attribute 8
AT_custom9, //!< Custom vertex attribute 9
AT_custom0, //!< Custom vertex attribute 0
AT_custom1, //!< Custom vertex attribute 1
AT_custom2, //!< Custom vertex attribute 2
AT_custom3, //!< Custom vertex attribute 3
AT_custom4, //!< Custom vertex attribute 4
AT_custom5, //!< Custom vertex attribute 5
AT_custom6, //!< Custom vertex attribute 6
AT_custom7, //!< Custom vertex attribute 7
AT_custom8, //!< Custom vertex attribute 8
AT_custom9, //!< Custom vertex attribute 9

AT_velocity = 1, //!< Vertex velocity 3 component vectors
AT_startTime = 2, //!< Vertex start time float
AT_initialVelocity = 3, //!< Vertex initial velocity 3 component vectors
AT_rotation = 4, //!< Vertex rotation float
AT_angularVelo = 5, //!< Vertex angulare velocity for rotation float
AT_texNum = 6, //!< Vertex texture number int
AT_initialPosition = 7 //!< Vertex initial position 3 component vectors
AT_velocity = 1, //!< Vertex velocity 3 component vectors
AT_startTime = 2, //!< Vertex start time float
AT_initialVelocity = 3, //!< Vertex initial velocity 3 component vectors
AT_rotation = 4, //!< Vertex rotation float
AT_angularVelo = 5, //!< Vertex angular velocity for rotation float
AT_texNum = 6, //!< Vertex texture number int
AT_initialPosition = 7, //!< Vertex initial position 3 component vectors
AT_instancePosition = 8 //!< Vertex instance position for instanced particle drawing
};
//-----------------------------------------------------------------------------
//! Enumeration for buffer usage types also supported by OpenGL ES
Expand Down
33 changes: 17 additions & 16 deletions modules/sl/source/gl/SLGLProgramGenerated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const string vertInput_PS_a_texNum = R"(
layout (location = 6) in uint a_texNum; // Particle rotation attribute)";
const string vertInput_PS_a_initP = R"(
layout (location = 7) in vec3 a_initialPosition;// Particle initial position attribute)";
const string vertInput_PS_a_positionP = R"(
layout (location = 8) in vec3 a_positionP; // Particle position attribute)";
const string vertInput_PS_a_InstPos = R"(
layout (location = 8) in vec3 a_instancePos; // Particle instance triangle vertex position attribute)";
const string vertInput_a_uv0 = R"(
layout (location = 2) in vec2 a_uv0; // Vertex tex.coord. 1 for diffuse color)";
const string vertInput_a_uv1 = R"(
Expand All @@ -62,7 +62,7 @@ uniform mat4 u_mMatrix; // Model matrix (object to world tra
uniform mat4 u_vMatrix; // View matrix (world to camera transform)
uniform mat4 u_pMatrix; // Projection matrix (camera to normalize device coords.))";
const string vertInput_u_matrix_vOmv = R"(
uniform mat4 u_vOmvMatrix; // view or modelview matrix)";
uniform mat4 u_vOmvMatrix; // view or modelview matrix)";
//-----------------------------------------------------------------------------
const string vertInput_u_skinning = R"(
Expand Down Expand Up @@ -167,9 +167,9 @@ const string vertOutput_PS_struct_End = R"(
} vert; )";

const string vertOutput_PS_instanced_transparency = R"(
out float transparency; // transparency of a particle )";
out float transparency; // transparency of a particle )";
const string fragInput_PS_instanced_transparency = R"(
in float transparency; // transparency of a particle )";
in float transparency; // transparency of a particle )";

const string fragMain_PS_instanced_v_doColorOverLT = R"(
vec4 color = vec4(colorByAge(v_age/u_tTL), 1.0); // Particle color)";
Expand Down Expand Up @@ -203,7 +203,7 @@ out float tf_rotation; // To transform feedback)";
const string vertOutput_PS_tf_r_angularVelo = R"(
out float tf_angularVelo; // To transform feedback)";
const string vertOutput_PS_tf_texNum = R"(
flat out uint tf_texNum; // To transform feedback)";
flat out uint tf_texNum; // To transform feedback)";
const string vertOutput_PS_tf_initP = R"(
out vec3 tf_initialPosition; // To transform feedback)";

Expand Down Expand Up @@ -350,20 +350,20 @@ const string vertMain_PS_v_texNum = R"(
const string vertMain_PS_v_a = R"(
float age = u_time - a_startTime; // Get the age of the particle)";
const string vertMain_PS_v_tC = R"(
v_texCoord = 0.5 * (a_positionP.xy + vec2(1.0));)";
v_texCoord = 0.5 * (a_instancePos.xy + vec2(1.0));)";
const string vertMain_PS_v_age = R"(
v_age = age;)";
const string vertMain_PS_v_tC_flipbook = R"(
int actCI = int(mod(float(a_texNum), float(u_col)));
float actC = float(actCI);
float actR = floor(float(int(a_texNum) - actCI) / float(u_col));
vec2 p = 0.5 * (a_positionP.xy + vec2(1.0));
vec2 p = 0.5 * (a_instancePos.xy + vec2(1.0));
v_texCoord = vec2((actC + p.x)/float(u_col), 1.0 - (actR - p.y)/float(u_row));
)";

const string vertMain_PS_instanced_position = R"(
vec3 position = a_positionP;
vec3 position = a_instancePos;
)";
const string vertMain_PS_instanced_v_s = R"(
float size = age / u_tTL;)";
Expand Down Expand Up @@ -410,7 +410,7 @@ const string vertMain_PS_instanced_EndAll = R"(
}
)";
const string vertMain_PS_instanced_EndAll_VertBillboard = R"(
//gl_Position = vec4(a_position + a_positionP, 1);
//gl_Position = vec4(a_position + a_instancePos, 1);
gl_Position = u_pMatrix * u_vYawPMatrix * vec4(a_position + position, 1.0);
}
)";
Expand Down Expand Up @@ -535,9 +535,9 @@ uniform int u_row; // Number of row of flipbook texture)";
const string geomOutput_PS_v_pC = R"(
out vec4 v_particleColor; // The resulting color per vertex)";
const string geomOutput_PS_v_tC = R"(
out vec2 v_texCoord; // Texture coordinate at vertex)";
out vec2 v_texCoord; // Texture coordinate at vertex)";
const string vertOutput_PS_v_tC = R"(
out vec2 v_texCoord; // Texture coordinate at vertex)";
out vec2 v_texCoord; // Texture coordinate at vertex)";
//-----------------------------------------------------------------------------
const string geomMain_PS_v_s = R"(
float scale = u_scale;)";
Expand Down Expand Up @@ -2192,10 +2192,11 @@ void SLGLProgramGenerated::buildPerPixParticleInstanced(SLMaterial* mat)
vertCode += shaderHeader();

// Vertex shader inputs
vertCode += vertInput_PS_a_positionP;
vertCode += vertInput_PS_a_p; //position
vertCode += vertInput_PS_a_st; // start time
if (rot) vertCode += vertInput_PS_a_r; //rotation as float
vertCode += vertInput_PS_a_InstPos; // instance position
vertCode += vertInput_PS_a_p; // position
vertCode += vertInput_PS_a_st; // start time
if (rot) vertCode += vertInput_PS_a_r; // rotation as float

if (FlBoTex)
{
vertCode += vertInput_PS_a_texNum; // per particle texture number
Expand Down
4 changes: 2 additions & 2 deletions modules/sl/source/mesh/SLParticleSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,13 @@ void SLParticleSystem::generate()
I32.push_back(0);

_renderVao1.deleteGL();
_renderVao1.setAttrib(AT_custom0, AT_custom0, &P);
_renderVao1.setAttrib(AT_custom0, AT_instancePosition, &P);
_renderVao1.setIndices(&I32);
_renderVao1.setExternalVBO(_vao1.vbo(), 2);
_renderVao1.generate((SLuint)P.size());

_renderVao2.deleteGL();
_renderVao2.setAttrib(AT_custom0, AT_custom0, &P);
_renderVao2.setAttrib(AT_custom0, AT_instancePosition, &P);
_renderVao2.setIndices(&I32);
_renderVao2.setExternalVBO(_vao2.vbo(), 2);
_renderVao2.generate((SLuint)P.size());
Expand Down

0 comments on commit 85e20d8

Please sign in to comment.