Jump to content
Korean Random
Sign in to follow this  
SkepticalFox

Draw camera frustum in opengl (glsl)

Recommended Posts

Geometric shader:

#version 450 core

layout (points) in;
layout (line_strip, max_vertices = 24) out;

uniform mat4 invViewProj_to_draw;
uniform mat4 viewProj;

// in case of reversed-z it should be 0
#define NEAR_DEPTH -1

const vec4 f[8] = vec4[8](
    // near
    vec4(-1, -1, NEAR_DEPTH, 1),
    vec4(1,  -1, NEAR_DEPTH, 1),
    vec4(1,   1, NEAR_DEPTH, 1),
    vec4(-1,  1, NEAR_DEPTH, 1),
    // far
    vec4(-1, -1,  1, 1),
    vec4(1,  -1,  1, 1),
    vec4(1,   1,  1, 1),
    vec4(-1,  1,  1, 1)
);

void main(void)
{
    vec4 v[8];

    for (int i = 0; i < 8; i++) {
      vec4 ff = invViewProj_to_draw * f[i];
      v[i].xyz = ff.xyz / ff.w;
      v[i].w = 1.0f;
      v[i] = viewProj * v[i];
    }
  
    gl_Position = v[0];
    EmitVertex();
    gl_Position = v[1];
    EmitVertex();
    EndPrimitive(); // 1

    gl_Position = v[1];
    EmitVertex();
    gl_Position = v[2];
    EmitVertex();
    EndPrimitive(); // 2

    gl_Position = v[2];
    EmitVertex();
    gl_Position = v[3];
    EmitVertex();
    EndPrimitive(); // 3

    gl_Position = v[3];
    EmitVertex();
    gl_Position = v[0];
    EmitVertex();
    EndPrimitive(); // 4

    gl_Position = v[4];
    EmitVertex();
    gl_Position = v[5];
    EmitVertex();
    EndPrimitive(); // 5

    gl_Position = v[5];
    EmitVertex();
    gl_Position = v[6];
    EmitVertex();
    EndPrimitive(); // 6

    gl_Position = v[6];
    EmitVertex();
    gl_Position = v[7];
    EmitVertex();
    EndPrimitive(); // 7

    gl_Position = v[7];
    EmitVertex();
    gl_Position = v[4];
    EmitVertex();
    EndPrimitive(); // 8

    gl_Position = v[0];
    EmitVertex();
    gl_Position = v[4];
    EmitVertex();
    EndPrimitive(); // 9

    gl_Position = v[1];
    EmitVertex();
    gl_Position = v[5];
    EmitVertex();
    EndPrimitive(); // 10

    gl_Position = v[2];
    EmitVertex();
    gl_Position = v[6];
    EmitVertex();
    EndPrimitive(); // 11

    gl_Position = v[3];
    EmitVertex();
    gl_Position = v[7];
    EmitVertex();
    EndPrimitive(); // 12
}
Since the shader is useful, I post it here. I don’t remember I took it, or wrote it myself.
  • Upvote 3

Share this post


Link to post

Short link
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...