[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 | // Size of the sampling grid |
---|
| 15 | in VertexData |
---|
| 16 | { |
---|
| 17 | vec3 N; |
---|
| 18 | vec2 Field; |
---|
| 19 | } VertexIn[]; |
---|
| 20 | |
---|
| 21 | out vec3 oNormal; |
---|
| 22 | |
---|
| 23 | uniform float IsoValue; |
---|
| 24 | |
---|
| 25 | layout(lines_adjacency) in; |
---|
| 26 | layout(triangle_strip, max_vertices = 4) out; |
---|
| 27 | |
---|
| 28 | // Estimate where isosurface intersects grid edge with endpoints v0, v1. |
---|
| 29 | void CalcIntersection(vec4 Pos0, |
---|
| 30 | vec3 N0, |
---|
| 31 | vec2 Field0, |
---|
| 32 | vec4 Pos1, |
---|
| 33 | vec3 N1, |
---|
| 34 | vec2 Field1) |
---|
| 35 | { |
---|
| 36 | float t = (IsoValue - Field0.x) / (Field1.x - Field0.x); |
---|
| 37 | if ((Field0.x < IsoValue) && (Field1.x > Field0.x)) |
---|
| 38 | { |
---|
| 39 | if (t > 0 && t < 1) |
---|
| 40 | { |
---|
| 41 | gl_Position = mix(Pos0, Pos1, t); |
---|
| 42 | oNormal = mix(N0, N1, t); |
---|
| 43 | EmitVertex(); |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | // Geometry shader |
---|
| 49 | // input: line with adjacency (tetrahedron) |
---|
| 50 | // outputs: zero, one or two triangles depending if isosurface intersects tetrahedron |
---|
| 51 | void main() |
---|
| 52 | { |
---|
| 53 | |
---|
| 54 | // Construct index for this tetrahedron. |
---|
| 55 | uint index = uint((uint(VertexIn[0].Field.y) << 3) | |
---|
| 56 | (uint(VertexIn[1].Field.y) << 2) | |
---|
| 57 | (uint(VertexIn[2].Field.y) << 1) | |
---|
| 58 | uint(VertexIn[3].Field.y)); |
---|
| 59 | |
---|
| 60 | // Don't bother if all vertices out or all vertices inside isosurface. |
---|
| 61 | if (index > uint(0) && index < uint(15)) |
---|
| 62 | { |
---|
| 63 | // Uber-compressed version of the edge table. |
---|
| 64 | uint edgeListHex[8] = |
---|
| 65 | uint[8](uint(0x0001cde0), uint(0x98b08c9d), uint(0x674046ce), uint(0x487bc480), |
---|
| 66 | uint(0x21301d2e), uint(0x139bd910), uint(0x26376e20), uint(0x3b700000)); |
---|
| 67 | |
---|
| 68 | uint edgeValFull = edgeListHex[index/uint(2)]; |
---|
| 69 | uint three = uint(0x3); |
---|
| 70 | uint edgeVal = (index % uint(2) == uint(1)) ? (edgeValFull & uint(0xFFFF)) : ((edgeValFull >> 16) & uint(0xFFFF)); |
---|
| 71 | ivec4 e0 = ivec4((edgeVal >> 14) & three, (edgeVal >> 12) & three, (edgeVal >> 10) & three, (edgeVal >> 8) & three); |
---|
| 72 | ivec4 e1 = ivec4((edgeVal >> 6) & three, (edgeVal >> 4) & three, (edgeVal >> 2) & three, (edgeVal >> 0) & three); |
---|
| 73 | |
---|
| 74 | CalcIntersection(gl_in[e0.x].gl_Position, VertexIn[e0.x].N, VertexIn[e0.x].Field, |
---|
| 75 | gl_in[e0.y].gl_Position, VertexIn[e0.y].N, VertexIn[e0.y].Field); |
---|
| 76 | CalcIntersection(gl_in[e0.z].gl_Position, VertexIn[e0.z].N, VertexIn[e0.z].Field, |
---|
| 77 | gl_in[e0.w].gl_Position, VertexIn[e0.w].N, VertexIn[e0.w].Field); |
---|
| 78 | CalcIntersection(gl_in[e1.x].gl_Position, VertexIn[e1.x].N, VertexIn[e1.x].Field, |
---|
| 79 | gl_in[e1.y].gl_Position, VertexIn[e1.y].N, VertexIn[e1.y].Field); |
---|
| 80 | |
---|
| 81 | // Emit additional triangle, if necessary. |
---|
| 82 | if (e1.z != -1) { |
---|
| 83 | CalcIntersection(gl_in[e1.z].gl_Position, VertexIn[e1.z].N, VertexIn[e1.z].Field, |
---|
| 84 | gl_in[e1.w].gl_Position, VertexIn[e1.w].N, VertexIn[e1.w].Field); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | EndPrimitive(); |
---|
| 88 | } |
---|
| 89 | } |
---|