Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// @function face(p1, p2, p3, col1, col2, col3, a1, a2, a3, u1, v1, u2, v2, u3, v3)
- /// @description builds a face that holds data for the model object
- /// @param {int} p1 index of point 1
- /// @param {int} p2 index of point 2
- /// @param {int} p3 index of point 3
- /// @param {col1} color of point 1
- /// @param {col2} color of point 2
- /// @param {col3} color of point 3
- /// @param {a1} alpha of point 1
- /// @param {a2} alpha of point 2
- /// @param {a3} alpha of point 3
- /// @param {u1} u tex coordinate of point 1
- /// @param {v1} v tex coordinate of point 1
- /// @param {u2} u tex coordinate of point 2
- /// @param {v2} v tex coordinate of point 2
- /// @param {u3} u tex coordinate of point 3
- /// @param {v3} v tex coordinate of point 3
- function face(p1, p2, p3, col1, col2, col3, a1, a2, a3, u1, v1, u2, v2, u3, v3) constructor{
- // use index of each point, not the original vectors...
- point_1 = p1;
- point_2 = p2;
- point_3 = p3;
- color_1 = col1;
- color_2 = col2;
- color_3 = col3;
- alpha_1 = a1;
- alpha_2 = a2;
- alpha_3 = a3;
- tex_u_1 = u1;
- tex_u_2 = u2;
- tex_u_3 = u3;
- tex_v_1 = v1;
- tex_v_2 = v2;
- tex_v_3 = v3;
- };
- /// @function model(_format)
- /// @description builds a 3D model
- /// @param {pointer} _format the vertex buffer format
- function model(_format) constructor{
- // position of the model in the world
- position = new vec3d(0, 0, 0);
- // x, y, and z scale
- scale = new vec3d(1, 1, 1);
- // rotation of the forward, up, and right vectors around global X, Y, and Z axes
- rot = new vec3d(0, 0, 0);
- forward = new vec3d(1, 0, 0);
- up = new vec3d(0, 0, 1);
- right = new vec3d(0, 1, 0);
- // rotation around the forward, up, and right vectors.
- pitch = 0;
- roll = 0;
- yaw = 0;
- // grab the format pointer
- format = _format;
- // array of unaltered points for the model
- points = -1;
- // array of faces
- faces = -1;
- // pointer for the vector buffer
- vbuff = -1;
- /// @function add_point(p)
- /// @description adds a point to the point array
- /// @param {pointer} p a vec3d for the point
- static add_point = function(p){
- points[array_length(points)] = p;
- }
- /// @function add_face(f)
- /// @description adds a face to the face array
- /// @param {int} p1 index of point 1
- /// @param {int} p2 index of point 2
- /// @param {int} p3 index of point 3
- /// @param {col1} color of point 1
- /// @param {col2} color of point 2
- /// @param {col3} color of point 3
- /// @param {a1} alpha of point 1
- /// @param {a2} alpha of point 2
- /// @param {a3} alpha of point 3
- /// @param {u1} u tex coordinate of point 1
- /// @param {v1} v tex coordinate of point 1
- /// @param {u2} u tex coordinate of point 2
- /// @param {v2} v tex coordinate of point 2
- /// @param {u3} u tex coordinate of point 3
- /// @param {v3} v tex coordinate of point 3
- static add_face = function(p1, p2, p3, col1, col2, col3, a1, a2, a3, u1, v1, u2, v2, u3, v3){
- var f = new face(p1, p2, p3, col1, col2, col3, a1, a2, a3, u1, v1, u2, v2, u3, v3);
- faces[array_length(faces)] = f;
- }
- /// @function build_buffer()
- /// @description builds a vertex buffer based on the points, faces, etc...
- static build_buffer = function(){
- // create transformed direction vectors so that the original vectors remain unchanged.
- transformed_forward = forward.rotate(rot.X, rot.Y, rot.Z);
- transformed_up = up.rotate(rot.X, rot.Y, rot.Z);
- transformed_right = right.rotate(rot.X, rot.Y, rot.Z);
- // set up quaternions for the roll, pitch, and yaw rotations
- var qroll = new quat4d();
- qroll.rotate_self(roll, transformed_forward);
- var qpitch = new quat4d();
- qpitch.rotate_self(pitch, transformed_right);
- var qyaw = new quat4d();
- qyaw.rotate_self(yaw, transformed_up);
- // create a single quaternion incorporating all rotations.
- q = new quat4d();
- // rotation of direction vectors
- q.xrotate_self(rot.X);
- q.yrotate_self(rot.Y);
- q.zrotate_self(rot.Z)
- // pitch, yaw, and roll
- q = qpitch.multiply(q);
- q = qyaw.multiply(q);
- q = qroll.multiply(q);
- q.normalize_self();
- // here is where we transform the points of the model
- // create an array to hold the transformed points so that the orginal points remain unchanged.
- var transformed_points = [];
- // iterate through the points
- for(var p = 0; p < array_length(points); p++){
- // grab a copy of the orginal point
- transformed_points[p] = points[p].copy();
- // scale the point
- transformed_points[p].set(transformed_points[p].X * scale.X, transformed_points[p].Y * scale.Y, transformed_points[p].Z * scale.Z);
- // rotate the point...
- // create a new quaternion by extending the transformed point
- var pq = new quat4d();
- pq.X = transformed_points[p].X;
- pq.Y = transformed_points[p].Y;
- pq.Z = transformed_points[p].Z;
- pq.W = 0;
- // rotate our point quaternion by the q quaternion created earlier
- var tq = (q.multiply(pq));
- tq = tq.multiply(q.conjugate());
- // convert the transformed quaternion back to a vector
- transformed_points[p].set(tq.X, tq.Y, tq.Z);
- // translate the transformed point by adding the position vector
- transformed_points[p].add_self(position);
- // clean up...
- delete pq;
- delete tq;
- }
- // clean up...
- delete q;
- // begin the vertex buffer
- vbuff = vertex_create_buffer();
- vertex_begin(vbuff, format);
- // iterate through the faces
- for(var f = 0; f < array_length(faces); f++){
- var face = faces[f];
- // recall that each point in a face is defined by its index in the point array
- // point 1
- vertex_position_3d(vbuff, transformed_points[face.point_1].X, transformed_points[face.point_1].Y, transformed_points[face.point_1].Z)
- vertex_color(vbuff, face.color_1, face.alpha_1);
- vertex_texcoord(vbuff, face.tex_u_1, face.tex_v_1);
- // point 2
- vertex_position_3d(vbuff, transformed_points[face.point_2].X, transformed_points[face.point_2].Y, transformed_points[face.point_2].Z)
- vertex_color(vbuff, face.color_2, face.alpha_2);
- vertex_texcoord(vbuff, face.tex_u_2, face.tex_v_2);
- // point 3
- vertex_position_3d(vbuff, transformed_points[face.point_3].X, transformed_points[face.point_3].Y, transformed_points[face.point_3].Z)
- vertex_color(vbuff, face.color_3, face.alpha_3);
- vertex_texcoord(vbuff, face.tex_u_3, face.tex_v_3);
- }
- // finish up the vertex buffer and return it
- vertex_end(vbuff);
- vertex_freeze(vbuff);
- return(vbuff);
- }
- /// @function build_cube()
- /// @description builds a cube for testing purposes
- static build_cube = function(){
- // add the 8 points of the cube
- self.add_point(new vec3d(-1, -1, -1)); //0
- self.add_point(new vec3d(-1, -1, 1)); //1
- self.add_point(new vec3d(-1, 1, -1)); //2
- self.add_point(new vec3d(-1, 1, 1)); //3
- self.add_point(new vec3d( 1, -1, -1)); //4
- self.add_point(new vec3d( 1, -1, 1)); //5
- self.add_point(new vec3d( 1, 1, -1)); //6
- self.add_point(new vec3d( 1, 1, 1)); //7
- // define the faces.
- // each side will have a different color
- self.add_face(0, 1, 2, c_red, c_red, c_red, 1, 1, 1, 0, 0, 0, 0, 0, 0);
- self.add_face(1, 3, 2, c_red, c_red, c_red, 1, 1, 1, 0, 0, 0, 0, 0, 0);
- self.add_face(2, 3, 6, c_blue, c_blue, c_blue, 1, 1, 1, 0, 0, 0, 0, 0, 0);
- self.add_face(3, 7, 6, c_blue, c_blue, c_blue, 1, 1, 1, 0, 0, 0, 0, 0, 0);
- self.add_face(6, 7, 5, c_green, c_green, c_green, 1, 1, 1, 0, 0, 0, 0, 0, 0);
- self.add_face(6, 5, 4, c_green, c_green, c_green, 1, 1, 1, 0, 0, 0, 0, 0, 0);
- self.add_face(4, 5, 1, c_yellow, c_yellow, c_yellow, 1, 1, 1, 0, 0, 0, 0, 0, 0);
- self.add_face(4, 1, 0, c_yellow, c_yellow, c_yellow, 1, 1, 1, 0, 0, 0, 0, 0, 0);
- self.add_face(1, 7, 3, c_navy, c_navy, c_navy, 1, 1, 1, 0, 0, 0, 0, 0, 0);
- self.add_face(1, 5, 7, c_navy, c_navy, c_navy, 1, 1, 1, 0, 0, 0, 0, 0, 0);
- self.add_face(0, 2, 6, c_lime, c_lime, c_lime, 1, 1, 1, 0, 0, 0, 0, 0, 0);
- self.add_face(0, 6, 4, c_lime, c_lime, c_lime, 1, 1, 1, 0, 0, 0, 0, 0, 0);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment