|
| 1 | +// |
| 2 | +// Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. |
| 3 | +// |
| 4 | +// Redistribution and use in source and binary forms, with or without |
| 5 | +// modification, are permitted provided that the following conditions |
| 6 | +// are met: |
| 7 | +// * Redistributions of source code must retain the above copyright |
| 8 | +// notice, this list of conditions and the following disclaimer. |
| 9 | +// * Redistributions in binary form must reproduce the above copyright |
| 10 | +// notice, this list of conditions and the following disclaimer in the |
| 11 | +// documentation and/or other materials provided with the distribution. |
| 12 | +// * Neither the name of NVIDIA CORPORATION nor the names of its |
| 13 | +// contributors may be used to endorse or promote products derived |
| 14 | +// from this software without specific prior written permission. |
| 15 | +// |
| 16 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 17 | +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 19 | +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 20 | +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 23 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 24 | +// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | +// |
| 28 | + |
| 29 | +#include "vec_math.h" |
| 30 | + |
| 31 | + |
| 32 | +enum struct AnimationMode: int |
| 33 | +{ |
| 34 | + NONE = 0, |
| 35 | + DEFORM = 1, |
| 36 | + EXPLODE = 2, |
| 37 | +}; |
| 38 | + |
| 39 | + |
| 40 | +__forceinline__ __device__ float triangle_wave( float x, float shift = 0.f, float period = 2.f * M_PIf, float amplitude = 1.f ) |
| 41 | +{ |
| 42 | + return fabsf( fmodf( ( 4.f / period ) * ( x - shift ), 4.f * amplitude ) - 2.f * amplitude ) - amplitude; |
| 43 | +} |
| 44 | + |
| 45 | +__forceinline__ __device__ void write_animated_triangle( float3* out_vertices, int tidx, float3 v0, float3 v1, float3 v2, AnimationMode mode, float time ) |
| 46 | +{ |
| 47 | + float3 v = make_float3( 0 ); |
| 48 | + |
| 49 | + if( mode == AnimationMode::EXPLODE ) |
| 50 | + { |
| 51 | + // Generate displacement vector from triangle index |
| 52 | + const float theta = ( (float)M_PIf * ( ( tidx + 1 ) * ( 13 / M_PIf ) ) ); |
| 53 | + const float phi = ( (float)( 2.0 * M_PIf ) * ( ( tidx + 1 ) * ( 97 / M_PIf ) ) ); |
| 54 | + |
| 55 | + // Apply displacement to the sphere triangles |
| 56 | + v = make_float3( triangle_wave( phi ) * triangle_wave( theta, M_PIf / 2.f ), |
| 57 | + triangle_wave( phi, M_PIf / 2.f ) * triangle_wave( theta, M_PIf / 2.f ), triangle_wave( theta ) ) |
| 58 | + * triangle_wave( time, M_PIf / 2.f ) * 2.f; |
| 59 | + } |
| 60 | + |
| 61 | + out_vertices[tidx * 3 + 0] = v0 + v; |
| 62 | + out_vertices[tidx * 3 + 1] = v1 + v; |
| 63 | + out_vertices[tidx * 3 + 2] = v2 + v; |
| 64 | +} |
| 65 | + |
| 66 | +__forceinline__ __device__ float3 deform_vertex( const float3& c, AnimationMode mode, float time ) |
| 67 | +{ |
| 68 | + // Apply sine wave to the y coordinate of the sphere vertices |
| 69 | + if( mode == AnimationMode::DEFORM ) |
| 70 | + return make_float3( c.x, c.y * ( 0.5f + 0.4f * cosf( 4 * ( c.x + time ) ) ), c.z ); |
| 71 | + return c; |
| 72 | +} |
| 73 | + |
| 74 | +extern "C" __global__ void generate_vertices(float3* out_vertices, AnimationMode mode, float time, int width, int height) |
| 75 | +{ |
| 76 | + int idx = blockIdx.x * blockDim.x + threadIdx.x; |
| 77 | + |
| 78 | + if( idx < width * height ) |
| 79 | + { |
| 80 | + // generate a single patch (two unindexed triangles) of a tessellated sphere |
| 81 | + |
| 82 | + int x = idx % width; |
| 83 | + int y = idx / width; |
| 84 | + |
| 85 | + const float theta0 = ( ( float )M_PIf * ( y + 0 ) ) / height; |
| 86 | + const float theta1 = ( ( float )M_PIf * ( y + 1 ) ) / height; |
| 87 | + const float phi0 = ( ( float )( 2.0 * M_PIf ) * ( x + 0 ) ) / width; |
| 88 | + const float phi1 = ( ( float )( 2.0 * M_PIf ) * ( x + 1 ) ) / width; |
| 89 | + |
| 90 | + const float ct0 = cosf( theta0 ); |
| 91 | + const float st0 = sinf( theta0 ); |
| 92 | + const float ct1 = cosf( theta1 ); |
| 93 | + const float st1 = sinf( theta1 ); |
| 94 | + |
| 95 | + const float cp0 = cosf( phi0 ); |
| 96 | + const float sp0 = sinf( phi0 ); |
| 97 | + const float cp1 = cosf( phi1 ); |
| 98 | + const float sp1 = sinf( phi1 ); |
| 99 | + |
| 100 | + const float3 v00 = deform_vertex( make_float3( cp0 * st0, sp0 * st0, ct0 ), mode, time ); |
| 101 | + const float3 v10 = deform_vertex( make_float3( cp0 * st1, sp0 * st1, ct1 ), mode, time ); |
| 102 | + const float3 v01 = deform_vertex( make_float3( cp1 * st0, sp1 * st0, ct0 ), mode, time ); |
| 103 | + const float3 v11 = deform_vertex( make_float3( cp1 * st1, sp1 * st1, ct1 ), mode, time ); |
| 104 | + |
| 105 | + write_animated_triangle( out_vertices, idx * 2 + 0, v00, v10, v11, mode, time ); |
| 106 | + write_animated_triangle( out_vertices, idx * 2 + 1, v00, v11, v01, mode, time ); |
| 107 | + } |
| 108 | +} |
0 commit comments