Guest User

Firefly_3D.c

a guest
Mar 16th, 2014
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Firefly_3D.h"
  2.  
  3. void FF_translate_point(Point_3D* point, Translation_vector translation)
  4. {
  5.     point->x += translation.x;
  6.     point->y += translation.y;
  7.     point->z += translation.z;
  8. }
  9.  
  10. void FF_rotation_point(Point_3D* point, Point_3D rotation_center, Rotation_vector rotation)
  11. {
  12.     Point_3D tmp, tmp2;
  13.    
  14.     // rotation X
  15.     tmp.x = point->x;
  16.     tmp.y = cos(rotation.x) * (point->y - rotation_center.y) - sin(rotation.x) * (point->z - rotation_center.z) + rotation_center.y;
  17.     tmp.z = sin(rotation.x) * (point->y - rotation_center.y) + cos(rotation.x) * (point->z - rotation_center.z) + rotation_center.z;
  18.    
  19.     // rotation Y
  20.     tmp2.x = cos(rotation.y) * (tmp.x - rotation_center.x) + sin(rotation.y) * (tmp.z - rotation_center.z) + rotation_center.x;
  21.     tmp2.y = tmp.y;
  22.     tmp2.z = cos(rotation.y) * (tmp.z - rotation_center.z) - sin(rotation.y) * (tmp.x - rotation_center.x) + rotation_center.z;
  23.    
  24.     // rotation Z
  25.     point->x = cos(rotation.z) * (tmp2.x - rotation_center.x) + sin(rotation.z) * (tmp2.y - rotation_center.y) + rotation_center.x;
  26.     point->y = cos(rotation.z) * (tmp2.y - rotation_center.y) - sin(rotation.z) * (tmp2.x - rotation_center.x) + rotation_center.y;
  27.     point->z = tmp2.z;
  28. }
  29.  
  30.  
  31. void FF_place_point_3D(Point_3D* point, int x, int y, int z, int color)
  32. {
  33.     point->x = x;
  34.     point->y = y;
  35.     point->z = z;
  36.     point->color = color;
  37. }
  38.  
  39. Point_3D* FF_new_point_3D(int x, int y, int z, int color)
  40. {
  41.     Point_3D point;
  42.     FF_place_point_3D(&point, x, y, z, color);
  43.     return &point;
  44. }
  45.  
  46.  
  47. void FF_alloc_line_3D(Line_3D* line, Point_3D* point_0, Point_3D* point_1, int color)
  48. {
  49.     line->points[0] = point_0;
  50.     line->points[1] = point_1;
  51.     line->color = color;
  52. }
  53.  
  54.  
  55. void FF_alloc_face_3D(Face_3D* face, Line_3D* line_0, Line_3D* line_1, Line_3D* line_2, int color)
  56. {
  57.     face->lines[0] = line_0;
  58.     face->lines[1] = line_1;
  59.     face->lines[2] = line_2;
  60.     face->color = color;
  61. }
  62.  
  63. void FF_draw_point_fixed_camera(Point_3D point, int focale)
  64. {
  65.     ML_pixel
  66.         (
  67.             (focale * point.x) / (point.z) + (FIREFLY3D_SCREEN_WIDTH>>1),
  68.             (focale * point.y) / (point.z) + (FIREFLY3D_SCREEN_HEIGHT>>1),
  69.             point.color
  70.         );
  71. }
  72.  
  73. void FF_draw_line_fixed_camera(Line_3D line, int focale)
  74. {
  75.     int i;
  76.    
  77.     ML_line
  78.         (
  79.             (focale * line.points[0]->x) / (line.points[0]->z) + (FIREFLY3D_SCREEN_WIDTH>>1),
  80.             (focale * line.points[0]->y) / (line.points[0]->z) + (FIREFLY3D_SCREEN_HEIGHT>>1),
  81.             (focale * line.points[1]->x) / (line.points[1]->z) + (FIREFLY3D_SCREEN_WIDTH>>1),
  82.             (focale * line.points[1]->y) / (line.points[1]->z) + (FIREFLY3D_SCREEN_HEIGHT>>1),
  83.             line.color
  84.         );
  85.    
  86.     for(i=0; i<2; i++) if(line.color != line.points[i]->color) FF_draw_point_fixed_camera(*(line.points[i]), focale);
  87. }
  88.  
  89. void FF_draw_face_fixed_camera(Face_3D face, int focale)
  90. {
  91.     int x[3], y[3];
  92.     int i;
  93.    
  94.     for(i=0; i<3; i++)
  95.     {
  96.         x[i] = (focale * face.lines[i]->points[0]->x) / (face.lines[i]->points[0]->z) + (FIREFLY3D_SCREEN_WIDTH>>1);
  97.         y[i] = (focale * face.lines[i]->points[0]->y) / (face.lines[i]->points[0]->z) + (FIREFLY3D_SCREEN_HEIGHT>>1);
  98.     }
  99.    
  100.     ML_filled_polygon(x, y, 3, face.color);
  101.    
  102.     for(i=0; i<3; i++) if(face.color != face.lines[i]->color) FF_draw_line_fixed_camera(*(face.lines[i]), focale);
  103. }
RAW Paste Data