WMPointRenderer-geometry.glsl 4.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#version 120

#extension GL_EXT_geometry_shader4 : enable

#include "WGETransformationTools.glsl"

/////////////////////////////////////////////////////////////////////////////
// Uniforms
/////////////////////////////////////////////////////////////////////////////

/**
 * The size of the sprite
 */
38
uniform float u_pointSize = 1.0;
39 40 41 42 43 44 45 46 47 48 49 50 51 52

/////////////////////////////////////////////////////////////////////////////
// Attributes
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// Varyings
/////////////////////////////////////////////////////////////////////////////

/**
 * The normal/tangent in scene-space
 */
varying in vec3 v_normal[];

53 54 55 56 57
/**
 * Clip vertex if not 0.0
 */
varying in float v_clip[];

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/**
 * The vertex coordinates in world-space
 */
varying out vec3 v_vertex;

/**
 * The texture coordinates in [-1,1]
 */
varying out vec3 v_texCoord;

/**
 * The radius of a sphere in world-space
 */
varying out float v_worldSpaceRadius;

/**
 * The center point of the sphere
 */
varying out vec3 v_centerPoint;

/**
 * Depth of the center point
 */
varying out float v_centerVertexDepth;

/**
 * Deptj of the neareast point on the sphere towards the camera
 */
varying out float v_nearestVertexDepth;

/**
 * Difference between nearest depth and center point depth
 */
varying out float v_vertexDepthDiff;

/**
 * The world-space scaling factor
 */
varying out float v_worldScale;

/////////////////////////////////////////////////////////////////////////////
// Variables
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////

/**
 * Main entry point of the geometry shader.
 */
void main()
{
111 112 113 114 115 116
    // clip whole sprite
    if( v_clip[0] != 0.0 )
    {
        return;
    }

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    // grab the info we got from the vertex array:
    vec3 vertex = gl_PositionIn[0].xyz;

    // the plane of the sprite is determined by the tangent ond two orthogonal vectors a and b
    // NOTE: the 1.0 corresponds to a radius of 1 (unit-sphere)
    vec3 a = vec3( 0.0, 1.0, 0.0 );
    vec3 b = vec3( 1.0, 0.0, 0.0 );

    gl_FrontColor = gl_FrontColorIn[0];

    v_worldScale = getModelViewScale();
    v_worldSpaceRadius = v_worldScale * u_pointSize;
    v_centerPoint = vertex;

    // calculate the depth using the point on the sphere most near to the cam
    vec4 nearestVertex = vec4( v_centerPoint.xy, v_centerPoint.z - v_worldSpaceRadius, 1.0 );
    vec4 nearestVertexProjected = gl_ProjectionMatrix * nearestVertex;
    v_nearestVertexDepth = ( 0.5 * nearestVertexProjected.z / nearestVertexProjected.w ) + 0.5;

    // depth at the center point
    vec4 centerVertex = vec4( v_centerPoint.xyz, 1.0 );
    vec4 centerVertexProjected = gl_ProjectionMatrix * centerVertex;
    v_centerVertexDepth = ( 0.5 * centerVertexProjected.z / centerVertexProjected.w ) + 0.5;

    v_vertexDepthDiff = v_centerVertexDepth - v_nearestVertexDepth;

    // vertex 1
    v_texCoord = ( -a ) + ( -b );
    v_vertex = ( v_centerPoint + v_worldSpaceRadius * v_texCoord );
    gl_Position = gl_ProjectionMatrix * vec4( v_vertex, 1.0 );
    EmitVertex();

    // vertex 2
    v_texCoord = ( -a ) + ( +b );
    v_vertex = ( v_centerPoint + v_worldSpaceRadius * v_texCoord );
    gl_Position = gl_ProjectionMatrix * vec4( v_vertex, 1.0 );
    EmitVertex();

    // vertex 3
    v_texCoord = ( +a ) + ( -b );
    v_vertex = ( v_centerPoint + v_worldSpaceRadius * v_texCoord );
    gl_Position = gl_ProjectionMatrix * vec4( v_vertex, 1.0 );
    EmitVertex();

    // vertex 4
    v_texCoord = ( +a ) + ( +b );
    v_vertex = ( v_centerPoint + v_worldSpaceRadius * v_texCoord );
    gl_Position = gl_ProjectionMatrix * vec4( v_vertex, 1.0 );
    EmitVertex();

    EndPrimitive();
}