0

I'm following this article:

http://paulbourke.net/geometry/polygonise/

However source code provided generates only triangles.

How can I calculate normals, indices and UV coordinates?

Neomex
  • 1,650
  • 6
  • 24
  • 38

1 Answers1

0

For normal calculation, you have two options. You can take the cross product of two edges of the triangle, shown here, which will make each vertex of the triangle have the same normal, thus giving you flat shading. The other option gives you smooth normals and is done by linearly interpolating the differences in each of the 12 vertices' neighbors. The GPU Gems article does this.

float d = 1.0/(float)voxels_per_block;  
float3 grad;  
grad.x = density_vol.Sample(TrilinearClamp, uvw + float3( d, 0, 0)) -  
         density_vol.Sample(TrilinearClamp, uvw + float3(-d, 0, 0));  
grad.y = density_vol.Sample(TrilinearClamp, uvw + float3( 0, d, 0)) -  
         density_vol.Sample(TrilinearClamp, uvw + float3( 0,-d, 0));  
grad.z = density_vol.Sample(TrilinearClamp, uvw + float3( 0, 0, d)) -  
         density_vol.Sample(TrilinearClamp, uvw + float3( 0, 0,-d));  
output.wsNormal = -normalize(grad);

I don't have any first-hand experience with creating an index buffer unfortunately, but for UV coordinates, you're best off using triplanar texture mapping. UV coordinates are calculated in the pixel shader using world coordinates and normals, and the same GPU Gems article provides sample code that you can likely use with little modification:

// Determine the blend weights for the 3 planar projections.  
// N_orig is the vertex-interpolated normal vector.  
float3 blend_weights = abs( N_orig.xyz );   // Tighten up the blending zone:  
blend_weights = (blend_weights - 0.2) * 7;  
blend_weights = max(blend_weights, 0);      // Force weights to sum to 1.0 (very important!)  
blend_weights /= (blend_weights.x + blend_weights.y + blend_weights.z ).xxx;   
// Now determine a color value and bump vector for each of the 3  
// projections, blend them, and store blended results in these two  
// vectors:  
float4 blended_color; // .w hold spec value  
float3 blended_bump_vec;  
{  
// Compute the UV coords for each of the 3 planar projections.  
// tex_scale (default ~ 1.0) determines how big the textures appear.  
float2 coord1 = v2f.wsCoord.yz * tex_scale;  
float2 coord2 = v2f.wsCoord.zx * tex_scale;  
float2 coord3 = v2f.wsCoord.xy * tex_scale;  
// This is where you would apply conditional displacement mapping.  
//if (blend_weights.x > 0) coord1 = . . .  
//if (blend_weights.y > 0) coord2 = . . .  
//if (blend_weights.z > 0) coord3 = . . .  
// Sample color maps for each projection, at those UV coords.  
float4 col1 = colorTex1.Sample(coord1);  
float4 col2 = colorTex2.Sample(coord2);  
float4 col3 = colorTex3.Sample(coord3);
Community
  • 1
  • 1
John
  • 16
  • 2