Advertisement

Simple hello world Geometry Shader / OpenGL

Started by May 01, 2020 07:15 PM
6 comments, last by taby 4 years, 9 months ago

Does anyone have a simple code to use a geometry shader that spits out one triangle?

I'm moving the Marching Cubes code off of the CPU and onto the GPU. Again, the source is free.

Red book (9th) and blue book (7th) and their online repositories

https://github.com/openglredbook/examples

http://www.openglsuperbible.com/example-code/

and

https://learnopengl.com/Advanced-OpenGL/Geometry-Shader

Advertisement

Good references. Thanks!

I've decided that I'm going to do this in a compute shader. There will be 8 input textures and 15 output textures.

Edit: Code not important.

Advertisement

I decided that the compute shader is not really the way to go in terms of implementing Marching Cubes.

I now know that you need to pass along in out variables from the vertex shader to the geometry shader to the fragment shader. So, a simple Hello World example is:

Vertex shader:

#version 430 core


// Per-vertex inputs
layout (location = 0) in vec4 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec3 colour;

uniform mat4 mv_matrix;
uniform mat4 proj_matrix;

// Inputs from vertex shader
out VS_OUT
{
    vec3 N;
    vec3 L;
    vec3 V;
    vec3 vertex_colour;
} vs_out;

// Position of light
uniform vec3 light_pos = vec3(100.0, 100.0, 100.0);

void main(void)
{
	vs_out.vertex_colour = colour;

    // Calculate view-space coordinate
    vec4 P = mv_matrix * position;

    // Calculate normal in view-space
    vs_out.N = mat3(mv_matrix) * normal;

    // Calculate light vector
    vs_out.L = light_pos - P.xyz;

    // Calculate view vector
    vs_out.V = -P.xyz;

    // Calculate the clip-space position of each vertex
    gl_Position = proj_matrix * P;
}

Geometry shader:

#version 430 core

layout (triangles) in;
layout (triangle_strip) out;
layout (max_vertices = 3) out;


in VS_OUT {
    vec3 N;
    vec3 L;
    vec3 V;
    vec3 vertex_colour;
} gs_in[]; 

out vec3 N;
out vec3 L;
out vec3 V;
out vec3 vertex_colour;

void main(void)
{
    for (int i = 0; i < gl_in.length(); i++)
    {
        gl_Position = gl_in[i].gl_Position;

        N = gs_in[i].N;
        L = gs_in[i].L;
        V = gs_in[i].V;
        vertex_colour = gs_in[i].vertex_colour;

        EmitVertex();
    }

    EndPrimitive();
}  

Fragment shader:

#version 430 core

// Output
layout (location = 0) out vec4 color;
layout (location = 1) out vec4 normal_depth;

// Input from geometry shader

in vec3 N;
in vec3 L;
in vec3 V;
in vec3 vertex_colour;

// Material properties
uniform vec3 diffuse_albedo = vec3(0.0, 0.8, 1.0);
uniform vec3 specular_albedo = vec3(1.0, 1.0, 1.0);
uniform float specular_power = 8.0;
uniform float shading_level = 1.0;

void main(void)
{
    // Normalize the incoming N, L and V vectors
    vec3 N_ = normalize(N);
    vec3 L_ = normalize(L);
    vec3 V_ = normalize(V);

    // Calculate R locally
    vec3 R_ = reflect(-L_, N_);

    // Compute the diffuse and specular components for each fragment
    vec3 diffuse = max(dot(N_, L_), 0.0) * vertex_colour;//diffuse_albedo;
    diffuse *= diffuse;
    vec3 specular = pow(max(dot(R_, V_), 0.0), specular_power) * specular_albedo;

    // Write final color to the framebuffer
    color = mix(vec4(0.0), vec4(diffuse + specular, 1.0), shading_level);
    normal_depth = vec4(N_, V.z);
}

This topic is closed to new replies.

Advertisement