Guest User

model script

a guest
Dec 12th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @function face(p1, p2, p3, col1, col2, col3, a1, a2, a3, u1, v1, u2, v2, u3, v3)
  2. /// @description builds a face that holds data for the model object
  3. /// @param {int} p1 index of point 1
  4. /// @param {int} p2 index of point 2
  5. /// @param {int} p3 index of point 3
  6. /// @param {col1} color of point 1
  7. /// @param {col2} color of point 2
  8. /// @param {col3} color of point 3
  9. /// @param {a1} alpha of point 1
  10. /// @param {a2} alpha of point 2
  11. /// @param {a3} alpha of point 3
  12. /// @param {u1} u tex coordinate of point 1
  13. /// @param {v1} v tex coordinate of point 1
  14. /// @param {u2} u tex coordinate of point 2
  15. /// @param {v2} v tex coordinate of point 2
  16. /// @param {u3} u tex coordinate of point 3
  17. /// @param {v3} v tex coordinate of point 3
  18. function face(p1, p2, p3, col1, col2, col3, a1, a2, a3, u1, v1, u2, v2, u3, v3) constructor{
  19.     // use index of each point, not the original vectors...
  20.     point_1 = p1;
  21.     point_2 = p2;
  22.     point_3 = p3;
  23.    
  24.     color_1 = col1;
  25.     color_2 = col2;
  26.     color_3 = col3;
  27.    
  28.     alpha_1 = a1;
  29.     alpha_2 = a2;
  30.     alpha_3 = a3;
  31.    
  32.     tex_u_1 = u1;
  33.     tex_u_2 = u2;
  34.     tex_u_3 = u3;
  35.    
  36.     tex_v_1 = v1;
  37.     tex_v_2 = v2;
  38.     tex_v_3 = v3;
  39. };
  40.  
  41. /// @function model(_format)
  42. /// @description builds a 3D model
  43. /// @param {pointer} _format the vertex buffer format
  44. function model(_format) constructor{
  45.     // position of the model in the world
  46.     position = new vec3d(0, 0, 0);
  47.     // x, y, and z scale
  48.     scale = new vec3d(1, 1, 1);
  49.     // rotation of the forward, up, and right vectors around global X, Y, and Z axes
  50.     rot = new vec3d(0, 0, 0);
  51.     forward = new vec3d(1, 0, 0);
  52.     up = new vec3d(0, 0, 1);
  53.     right = new vec3d(0, 1, 0);
  54.     // rotation around the forward, up, and right vectors.
  55.     pitch = 0;
  56.     roll = 0;
  57.     yaw = 0;
  58.     // grab the format pointer
  59.     format = _format;
  60.    
  61.     // array of unaltered points for the model
  62.     points = -1;
  63.     // array of faces
  64.     faces = -1;
  65.     // pointer for the vector buffer
  66.     vbuff = -1;
  67.  
  68.     /// @function add_point(p)
  69.     /// @description adds a point to the point array
  70.     /// @param {pointer} p a vec3d for the point
  71.     static add_point = function(p){
  72.         points[array_length(points)] = p;
  73.     }
  74.     /// @function add_face(f)
  75.     /// @description adds a face to the face array
  76.     /// @param {int} p1 index of point 1
  77.     /// @param {int} p2 index of point 2
  78.     /// @param {int} p3 index of point 3
  79.     /// @param {col1} color of point 1
  80.     /// @param {col2} color of point 2
  81.     /// @param {col3} color of point 3
  82.     /// @param {a1} alpha of point 1
  83.     /// @param {a2} alpha of point 2
  84.     /// @param {a3} alpha of point 3
  85.     /// @param {u1} u tex coordinate of point 1
  86.     /// @param {v1} v tex coordinate of point 1
  87.     /// @param {u2} u tex coordinate of point 2
  88.     /// @param {v2} v tex coordinate of point 2
  89.     /// @param {u3} u tex coordinate of point 3
  90.     /// @param {v3} v tex coordinate of point 3
  91.     static add_face = function(p1, p2, p3, col1, col2, col3, a1, a2, a3, u1, v1, u2, v2, u3, v3){
  92.         var f = new face(p1, p2, p3, col1, col2, col3, a1, a2, a3, u1, v1, u2, v2, u3, v3);
  93.         faces[array_length(faces)] = f;
  94.     }
  95.     /// @function build_buffer()
  96.     /// @description builds a vertex buffer based on the points, faces, etc...
  97.     static build_buffer = function(){
  98.         // create transformed direction vectors so that the original vectors remain unchanged.
  99.         transformed_forward = forward.rotate(rot.X, rot.Y, rot.Z);
  100.         transformed_up = up.rotate(rot.X, rot.Y, rot.Z);
  101.         transformed_right = right.rotate(rot.X, rot.Y, rot.Z);
  102.        
  103.         // set up quaternions for the roll, pitch, and yaw rotations
  104.         var qroll = new quat4d();
  105.         qroll.rotate_self(roll, transformed_forward);
  106.         var qpitch = new quat4d();
  107.         qpitch.rotate_self(pitch, transformed_right);
  108.         var qyaw = new quat4d();
  109.         qyaw.rotate_self(yaw, transformed_up);
  110.        
  111.         // create a single quaternion incorporating all rotations.
  112.         q = new quat4d();
  113.         // rotation of direction vectors
  114.         q.xrotate_self(rot.X);
  115.         q.yrotate_self(rot.Y);
  116.         q.zrotate_self(rot.Z)
  117.         // pitch, yaw, and roll
  118.         q = qpitch.multiply(q);
  119.         q = qyaw.multiply(q);
  120.         q = qroll.multiply(q);
  121.         q.normalize_self();
  122.        
  123.         // here is where we transform the points of the model
  124.         // create an array to hold the transformed points so that the orginal points remain unchanged.
  125.         var transformed_points = [];
  126.         // iterate through the points
  127.         for(var p = 0; p < array_length(points); p++){
  128.             // grab a copy of the orginal point
  129.             transformed_points[p] = points[p].copy();
  130.             // scale the point
  131.             transformed_points[p].set(transformed_points[p].X * scale.X, transformed_points[p].Y * scale.Y, transformed_points[p].Z * scale.Z);
  132.             // rotate the point...
  133.             // create a new quaternion by extending the transformed point
  134.             var pq = new quat4d();
  135.             pq.X = transformed_points[p].X;
  136.             pq.Y = transformed_points[p].Y;
  137.             pq.Z = transformed_points[p].Z;
  138.             pq.W = 0;
  139.             // rotate our point quaternion by the q quaternion created earlier
  140.             var tq = (q.multiply(pq));
  141.             tq = tq.multiply(q.conjugate());
  142.             // convert the transformed quaternion back to a vector
  143.             transformed_points[p].set(tq.X, tq.Y, tq.Z);
  144.             // translate the transformed point by adding the position vector
  145.             transformed_points[p].add_self(position);
  146.             // clean up...
  147.             delete pq;
  148.             delete tq;
  149.         }
  150.         // clean up...
  151.         delete q;
  152.        
  153.         // begin the vertex buffer
  154.         vbuff = vertex_create_buffer();
  155.         vertex_begin(vbuff, format);
  156.         // iterate through the faces
  157.         for(var f = 0; f < array_length(faces); f++){
  158.             var face = faces[f];
  159.             // recall that each point in a face is defined by its index in the point array
  160.             // point 1
  161.             vertex_position_3d(vbuff, transformed_points[face.point_1].X, transformed_points[face.point_1].Y, transformed_points[face.point_1].Z)
  162.             vertex_color(vbuff, face.color_1, face.alpha_1);
  163.             vertex_texcoord(vbuff, face.tex_u_1, face.tex_v_1);
  164.             // point 2
  165.             vertex_position_3d(vbuff, transformed_points[face.point_2].X, transformed_points[face.point_2].Y, transformed_points[face.point_2].Z)
  166.             vertex_color(vbuff, face.color_2, face.alpha_2);
  167.             vertex_texcoord(vbuff, face.tex_u_2, face.tex_v_2);
  168.             // point 3
  169.             vertex_position_3d(vbuff, transformed_points[face.point_3].X, transformed_points[face.point_3].Y, transformed_points[face.point_3].Z)
  170.             vertex_color(vbuff, face.color_3, face.alpha_3);
  171.             vertex_texcoord(vbuff, face.tex_u_3, face.tex_v_3);
  172.         }
  173.         // finish up the vertex buffer and return it
  174.         vertex_end(vbuff);
  175.         vertex_freeze(vbuff);
  176.         return(vbuff);
  177.     }
  178.    
  179.    
  180.     /// @function build_cube()
  181.     /// @description builds a cube for testing purposes
  182.     static build_cube = function(){
  183.         // add the 8 points of the cube
  184.         self.add_point(new vec3d(-1, -1, -1)); //0
  185.         self.add_point(new vec3d(-1, -1,  1)); //1
  186.         self.add_point(new vec3d(-1,  1, -1)); //2
  187.         self.add_point(new vec3d(-1,  1,  1)); //3
  188.         self.add_point(new vec3d( 1, -1, -1)); //4
  189.         self.add_point(new vec3d( 1, -1,  1)); //5
  190.         self.add_point(new vec3d( 1,  1, -1)); //6
  191.         self.add_point(new vec3d( 1,  1,  1)); //7
  192.         // define the faces.
  193.         // each side will have a different color
  194.         self.add_face(0, 1, 2, c_red, c_red, c_red, 1, 1, 1, 0, 0, 0, 0, 0, 0);
  195.         self.add_face(1, 3, 2, c_red, c_red, c_red, 1, 1, 1, 0, 0, 0, 0, 0, 0);
  196.         self.add_face(2, 3, 6, c_blue, c_blue, c_blue, 1, 1, 1, 0, 0, 0, 0, 0, 0);
  197.         self.add_face(3, 7, 6, c_blue, c_blue, c_blue, 1, 1, 1, 0, 0, 0, 0, 0, 0);
  198.         self.add_face(6, 7, 5, c_green, c_green, c_green, 1, 1, 1, 0, 0, 0, 0, 0, 0);
  199.         self.add_face(6, 5, 4, c_green, c_green, c_green, 1, 1, 1, 0, 0, 0, 0, 0, 0);
  200.         self.add_face(4, 5, 1, c_yellow, c_yellow, c_yellow, 1, 1, 1, 0, 0, 0, 0, 0, 0);
  201.         self.add_face(4, 1, 0, c_yellow, c_yellow, c_yellow, 1, 1, 1, 0, 0, 0, 0, 0, 0);
  202.         self.add_face(1, 7, 3, c_navy, c_navy, c_navy, 1, 1, 1, 0, 0, 0, 0, 0, 0);
  203.         self.add_face(1, 5, 7, c_navy, c_navy, c_navy, 1, 1, 1, 0, 0, 0, 0, 0, 0);
  204.         self.add_face(0, 2, 6, c_lime, c_lime, c_lime, 1, 1, 1, 0, 0, 0, 0, 0, 0);
  205.         self.add_face(0, 6, 4, c_lime, c_lime, c_lime, 1, 1, 1, 0, 0, 0, 0, 0, 0);
  206.        
  207.     }
  208.        
  209. };
Advertisement
Add Comment
Please, Sign In to add comment