[12115] | 1 | #version 150 |
---|
| 2 | |
---|
| 3 | // Ogre port of Nvidia's IsoSurf.cg file |
---|
| 4 | // Modified code follows. See http://developer.download.nvidia.com/SDK/10/opengl/samples.html for original |
---|
| 5 | // |
---|
| 6 | // Cg port of Yury Uralsky's metaball FX shader |
---|
| 7 | // |
---|
| 8 | // Authors: Simon Green and Yury Urlasky |
---|
| 9 | // Email: sdkfeedback@nvidia.com |
---|
| 10 | // |
---|
| 11 | // Copyright (c) NVIDIA Corporation. All rights reserved. |
---|
| 12 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 13 | |
---|
| 14 | in vec4 vertex; |
---|
| 15 | |
---|
| 16 | out VertexData { |
---|
| 17 | vec3 N; |
---|
| 18 | vec2 Field; |
---|
| 19 | // vec4 Color; |
---|
| 20 | } VertexOut; |
---|
| 21 | |
---|
| 22 | uniform float IsoValue = 1.0; |
---|
| 23 | uniform mat4 WorldViewProj; |
---|
| 24 | uniform mat4 origWorldViewIT; |
---|
| 25 | uniform vec4 Metaballs[2] = vec4[](vec4( -0.5, 0, 0, 0.2 ), vec4( 0.6, 0, 0, 0.1 )); |
---|
| 26 | |
---|
| 27 | // Size of the sampling grid |
---|
| 28 | const ivec3 SizeMask = ivec3( 63, 63, 63 ); |
---|
| 29 | const ivec3 SizeShift = ivec3( 0, 6, 12 ); |
---|
| 30 | |
---|
| 31 | // Metaball function |
---|
| 32 | // Returns metaball function value in .w and its gradient in .xyz |
---|
| 33 | vec4 Metaball(vec3 Pos, vec3 Center, float RadiusSq) |
---|
| 34 | { |
---|
| 35 | const float epsilon = 0.001; |
---|
| 36 | |
---|
| 37 | vec3 Dist = Pos - Center; |
---|
| 38 | float InvDistSq = 1 / (dot(Dist, Dist) + epsilon); |
---|
| 39 | |
---|
| 40 | vec4 o; |
---|
| 41 | o.xyz = -2 * RadiusSq * InvDistSq * InvDistSq * Dist; |
---|
| 42 | o.w = RadiusSq * InvDistSq; |
---|
| 43 | return o; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | void main() |
---|
| 47 | { |
---|
| 48 | vec4 Pos; |
---|
| 49 | |
---|
| 50 | // Generate sampling point position based on its index |
---|
| 51 | // Pos.x = float((gl_VertexID >> SizeShift.x) & SizeMask.x) / (SizeMask.x + 1); |
---|
| 52 | // Pos.y = float((gl_VertexID >> SizeShift.y) & SizeMask.y) / (SizeMask.y + 1); |
---|
| 53 | // Pos.z = float((gl_VertexID >> SizeShift.z) & SizeMask.z) / (SizeMask.z + 1); |
---|
| 54 | // Pos = Pos*2 - 1; |
---|
| 55 | Pos = vertex; |
---|
| 56 | |
---|
| 57 | // Sum up contributions from all metaballs |
---|
| 58 | vec4 Field = vec4(0,0,0,0); |
---|
| 59 | for (int i = 0; i < 2; i++) |
---|
| 60 | Field += Metaball(Pos.xyz, Metaballs[i].xyz, Metaballs[i].w); |
---|
| 61 | |
---|
| 62 | mat3 WorldViewIT = mat3(origWorldViewIT[0].xyz, origWorldViewIT[1].xyz, origWorldViewIT[2].xyz); |
---|
| 63 | |
---|
| 64 | // Transform position and normals |
---|
| 65 | gl_Position = WorldViewProj * vec4(Pos.xyz, 1); |
---|
| 66 | VertexOut.N = WorldViewIT * Field.xyz; // we want normals in world space |
---|
| 67 | VertexOut.Field.x = Field.w; |
---|
| 68 | |
---|
| 69 | // Generate in-out flags |
---|
| 70 | VertexOut.Field.y = (Field.w < IsoValue) ? 1 : 0; |
---|
| 71 | |
---|
| 72 | // VertexOut.Color = (Field*0.5+0.5) * (Field.w / 10.0); |
---|
| 73 | } |
---|