POV-Ray : Newsgroups : povray.text.scene-files : mesh2 angular abstract Server Time
1 Jul 2024 02:44:42 EDT (-0400)
  mesh2 angular abstract (Message 1 to 1 of 1)  
From: PeterC
Subject: mesh2 angular abstract
Date: 19 Feb 2002 21:08:29
Message: <3c730515.3205917@localhost>
Source for the image posted on the binaries group.
Makes a mesh2 sphere and then distorts it.  Makes a second
mesh2 sphere and distorts that as well.  Then scatters a
bunch of them around.

-- 
/* testing mesh2 object */

//#include "environment.pov"
#declare already_rendered = false;
#declare mesh_file = "mesh2_sphere_zoom1.inc"
#declare rseed = seed(86047920);
#declare rot_seed = seed(39121374);


#if (already_rendered = false)
  #declare txtr_output_file = mesh_file
  #fopen _txtr_file txtr_output_file write
#end


#macro rnd(n)
  #local rr_return = rand(rseed); // throw away rnd to stir up RNG
  #local rr_return = int((rand(rseed) * n));
  rr_return
#end


#macro rrand(rlo, rhi)
  #local real_lo = min(rlo, rhi);
  #local real_hi = max(rlo, rhi);
  #local rresult = (rand(rseed) * (rhi - rlo)) + rlo;
  rresult
#end


camera {
  location 0
  direction <0, 0, 0.3> // wongo ulta-wide
  up <0, 1, 0>
  right <(image_width / image_height), 0, 0>
  translate <0, 0, -6>
}


/* now lets get tricky.  This will build a mesh2 in some
   arrays, then at the end convert the arrays to a mesh2.
   We use a do_triangle routine that takes the given
   three points and adds them to the arrays.  It matches
   to existing points, optimizing for the mesh2.
   I hope it works.
   */
   
   
#declare max_pts = 300;
#declare num_pts = 0;
#declare pts_array = array[max_pts]
#declare max_faces = 800;
#declare num_faces = 0;
#declare faces_array = array[max_faces]
#declare pts_array[0] = <0, 0, 0>;  // force type to be 3-vector
#declare faces_array[0] = <0, 0, 0>; // force type to be 3-vector


// add_triangle: add triangle to the existing mesh2 arrays.
// It will check if any of the points exist in the mesh2
#macro add_triangle(p1, p2, p3)
  // phase one, add points (vertexes)
  #local closeness = 0.01;
  // search for first point
  #local cc = 0;
  #local point_found = false;
  #while (cc < num_pts)
    #if (vlength((pts_array[cc] - p1)) < closeness)
      #local point_found = true;
      #local p1_index = cc;
      #local cc = num_pts;  // signal end of loop
    #else
      #local cc = cc + 1; // keep searching
    #end
  #end
  #if (point_found = false) // add new point
    #declare pts_array[num_pts] = p1;
    #local p1_index = num_pts;
    #declare num_pts = num_pts + 1;
  #end
  // search for second point
  #local cc = 0;
  #local point_found = false;
  #while (cc < num_pts)
    #if (vlength((pts_array[cc] - p2)) < closeness)
      #local point_found = true;
      #local p2_index = cc;
      #local cc = num_pts;  // signal end of loop
    #else
      #local cc = cc + 1; // keep searching
    #end
  #end
  #if (point_found = false) // add new point
    #declare pts_array[num_pts] = p2;
    #local p2_index = num_pts;    
    #declare num_pts = num_pts + 1;
  #end
  // search for third point
  #local cc = 0;
  #local point_found = false;
  #while (cc < num_pts)
    #if (vlength((pts_array[cc] - p3)) < closeness)
      #local point_found = true;
      #local p3_index = cc;
      #local cc = num_pts;  // signal end of loop
    #else
      #local cc = cc + 1; // keep searching
    #end
  #end
  #if (point_found = false) // add new point
    #declare pts_array[num_pts] = p3;
    #local p3_index = num_pts;    
    #declare num_pts = num_pts + 1;
  #end
  // all three points have either been found in the points
  // array or have been added to the points array. In either
  // case, the p1_index, p2_index, p3_index variables now
  // hold the indexes to the points.
  // that was hard

  // phase two, add faces (face = triangle)
  #declare faces_array[num_faces] = <p1_index, p2_index, p3_index>;
  #declare num_faces = num_faces + 1;  
  // that was easy
#end


#macro do_quad(p1, p2, p3, p4)
  add_triangle(p1, p2, p3)
  add_triangle(p1, p3, p4)
#end


#macro emit_mesh2()
  #write(_txtr_file, "mesh2 {\n")
  #write(_txtr_file, "  vertex_vectors {\n")
  #write(_txtr_file, "    ", str(num_pts, 0, 0), ",\n")
  #local cc = 0;
  #while (cc < num_pts)
    #if (cc = (num_pts - 1))
      #write(_txtr_file, "    ", pts_array[cc], "\n")  // end; no
comma
    #else
      #write(_txtr_file, "    ", pts_array[cc], ",\n") // comma
    #end
    #local cc = cc + 1;
  #end
  #write(_txtr_file, "  }\n") // end of points/vertexes
  #write(_txtr_file, "  face_indices {\n")
  #write(_txtr_file, "    ", str(num_faces, 0, 0), "\n")
  #local cc = 0;
  #while (cc < num_faces)
    #if (cc = (num_faces - 1))
      #write(_txtr_file, "    ", faces_array[cc], "\n")  // end; no
comma
    #else
      #write(_txtr_file, "    ", faces_array[cc], ",\n") // comma
    #end
    #local cc = cc + 1;
  #end
  #write(_txtr_file, "  }\n") // end of faces
  #write(_txtr_file, "}\n\n\n") // end of mesh
#end // end of macro


// now use macros and arrays to build a mesh ...
#macro build_mesh()
  #debug "building mesh object\n"
  #local elivation_steps = 12;
  #local roty_steps = 24;
  #local elivation = 0;
  #while (elivation < 180)
    #local roty = 0;
    #while (roty < 360)
      #local height1 = cos(radians(elivation));
      #local height2 = cos(radians(elivation +
(180/elivation_steps)));
      #local scale1 = sin(radians(elivation));
      #local scale2 = sin(radians(elivation + (180/elivation_steps)));
      #local XY1 = <cos(radians(roty)), 1, sin(radians(roty))>;
      #local XY2 = <
        cos(radians(roty + (360/roty_steps))),
        1,
        sin(radians(roty + (360/roty_steps)))
      >;
      do_quad(
        <XY1.x * scale1, height1, XY1.z * scale1>,
        <XY2.x * scale1, height1, XY2.z * scale1>,
        <XY2.x * scale2, height2, XY2.z * scale2>,
        <XY1.x * scale2, height2, XY1.z * scale2>
      )
      #local roty = roty + (360 / roty_steps);
    #end
    #local elivation = elivation + (180 / elivation_steps); // degrees
  #end
#end


// perturb mesh points
#macro perturb_mesh()
  #debug "randomizing points\n"
  #local cc = 0;
  #while (cc < 50) // number of gravity/perturbing sources
    // first determine center of a gravity source
    #local grav_source = <rrand(-3, 3), rrand(-3, 3), rrand(-3, 3)>;
    // Now perturb all points towards the gravity source, based on
    // their closeness to the gravity source
    #local ck = 0;
    #while (ck < num_pts)
      #local force = vlength((pts_array[ck] - grav_source));
      #if (force < 2)
        #local force_vector = grav_source - pts_array[ck];
        // next line: scale it to be a unit vector      
        #local force_vector = force_vector / vlength(force_vector);
        #declare pts_array[ck] = pts_array[ck] + (force_vector * (2 -
force));
      #end
      #local ck = ck + 1;
    #end
    #local cc = cc + 1;
  #end
#end


#if (already_rendered = false)
  build_mesh()
  perturb_mesh()
  #write(_txtr_file, "#declare test1 = ")
  emit_mesh2()

  #declare num_pts = 0;
  #declare num_faces = 0;
  build_mesh()
  perturb_mesh()
  #write(_txtr_file, "#declare test2 = ")
  emit_mesh2()


  #fclose _txtr_file
#end


#include mesh_file

#declare h_displacement = 3;


#declare cc = 0;
#while (cc < 31)
  object {
    #if (mod(cc, 2) = 0)
      test1
    #else
      test2
    #end
    scale 2
    rotate x*360*rand(rot_seed)
    rotate y*360*rand(rot_seed)
    translate <8 * sin((cc/31) * 5 * 2 * pi), 8 * cos((cc/31) * 5 * 2
* pi), cc/2>
    pigment { color rgb <0.9, 0.95, 1> }
    finish { ambient 0.5 diffuse 0.5 }
  }
  #declare cc = cc + 1;
#end
object {
  test1
  scale 2
  rotate x*360*rand(rot_seed)
  rotate y*360*rand(rot_seed)
  translate <0, 0, 10>
  pigment { color rgb <0.9, 0.95, 1> }
  finish { ambient 0.5 diffuse 0.5 }
}


light_source {
  <-3000, 3000, -3000>
  color rgb <0.45, 0.5, 0.55>
  shadowless
}


light_source {
  <2801, 2900, -3010>
  color rgb <0.55, 0.5, 0.45>
  shadowless
}


/* actual end of this file */


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.