Creating an exploding/sanding effect in OpenGL/GLSL

31 Views Asked by At

I'm currently working on a program that renders a model and creates an exploding and sanding effect like the exact one described in this article: https://jiadongchen.com/2020/07/using-the-geometry-shader-to-implement-model-explosion-effect/

I'm identified the steps as:

  1. Reduce the 3 points of the triangle into a single point
  2. Move points according to physics equation

I'm stuck on part 1. Here is what I have so far in my geometry shader file:

#version 330 core
// Input vertex data, different for all executions of this shader.
layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;


in VS_OUT {
    vec2 UV;
} gs_in[];


out vec2 TexCoords;

uniform float current_time;
uniform int boom;

vec3 GetNormal()
{
    vec3 a = vec3(gl_in[0].gl_Position) - vec3(gl_in[1].gl_Position);
    vec3 b = vec3(gl_in[2].gl_Position) - vec3(gl_in[1].gl_Position);
    return normalize(cross(a, b));
}

vec4 explode(vec4 position, vec3 normal, int boom)
{
    vec3 direction;
    float vb;
    float magnitude = 1.0;
    if (boom ==1){
        vb=(abs(sin(current_time/3.0))/ 2.0) * magnitude;
        direction = normal * vb; 
    }
    else {
        direction = vec3(0.0,0.0,0.0);
    }
    return position + vec4(direction, 0.0);
}

void main() {    
    vec3 normal = GetNormal();

    if (boom == 1) {
    gl_Position = (gl_in[0].gl_Position + gl_in[1].gl_Position + gl_in[2].gl_Position) / 3.0;
        TexCoords = gs_in[0].UV;
        EmitVertex();
    } else {
    gl_Position = explode(gl_in[0].gl_Position, normal,boom);
        TexCoords = gs_in[0].UV;
        EmitVertex();
        gl_Position = explode(gl_in[1].gl_Position, normal,boom);
        TexCoords = gs_in[1].UV;
        EmitVertex();
        gl_Position = explode(gl_in[2].gl_Position, normal,boom);
        TexCoords = gs_in[2].UV;
        EmitVertex();
    }
    EndPrimitive();
}

When boom = 1 (user sets this), I want the triangle vertices to turn into a single point. However, nothing shows up when this happens. The triangles show up fine though.

However when I change this line layout (triangle_strip, max_vertices = 3) out; to layout (points, max_vertices=1) out;, it renders the model as singular points. However, I need it to render first as the entire model and then explode when the user specifies. How can I achieve this?

0

There are 0 best solutions below