clipping.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * 3D Engine
  3. * clipping.c:
  4. * Based on pikuma.com 3D software renderer in C
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 11/03/2021.
  8. */
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <math.h>
  12. #include <assert.h>
  13. #include <clipping.h>
  14. static plane_t frustum[TOTAL_PLANES];
  15. void initFrustumPlanes(double fov, double aspectRatio, double zNear, double zFar)
  16. {
  17. double radFOV_Y = (fov * M_PI) / 180.;
  18. double radFOV_X = atan(tan(radFOV_Y/2)* aspectRatio) * 2;
  19. frustum[LEFT_PLANE].point = vec3(0, 0, 0);
  20. frustum[LEFT_PLANE].normal = vec3( cos(radFOV_X / 2.0) , 0, sin(radFOV_X / 2.0));
  21. frustum[RIGHT_PLANE].point = vec3(0, 0, 0);
  22. frustum[RIGHT_PLANE].normal = vec3( -cos(radFOV_X / 2.0), 0, sin(radFOV_X / 2.0));
  23. frustum[TOP_PLANE].point = vec3(0, 0, 0);
  24. frustum[TOP_PLANE].normal = vec3(0, -cos(radFOV_Y / 2.0), sin(radFOV_Y / 2.0));
  25. frustum[BOTTOM_PLANE].point = vec3(0, 0, 0);
  26. frustum[BOTTOM_PLANE].normal = vec3(0, cos(radFOV_Y / 2.0), sin(radFOV_Y / 2.0));
  27. frustum[NEAR_PLANE].point = vec3(0, 0, zNear);
  28. frustum[NEAR_PLANE].normal = vec3(0, 0, 1);
  29. frustum[FAR_PLANE].point = vec3(0, 0, zFar);
  30. frustum[FAR_PLANE].normal = vec3(0, 0, -1);
  31. }
  32. void createTriangleFromPolygon(polygon_t *polygon, triangle_t *triangleList, uint32_t *outNumber)
  33. {
  34. int i;
  35. *outNumber = 0;
  36. for(i = 1; i < (polygon->num_vertices - 1); i++)
  37. {
  38. triangleList[i - 1].points[0] = vec4FromVec3(polygon->vertices[0]);
  39. triangleList[i - 1].points[1] = vec4FromVec3(polygon->vertices[i]);
  40. triangleList[i - 1].points[2] = vec4FromVec3(polygon->vertices[i + 1]);
  41. (*outNumber)++;
  42. assert(*outNumber < MAX_NUM_POLYGON_TRIANGLES);
  43. }
  44. }
  45. static void clipPolygonAgainstPlane(polygon_t *poly, faceenum_t plane)
  46. {
  47. int i;
  48. vec3_t planePoint = frustum[plane].point;
  49. vec3_t planeNormal = frustum[plane].normal;
  50. vec3_t *currentVertex;
  51. vec3_t *previousVertex;
  52. double currentDot, previousDot;
  53. vec3_t newVertices[MAX_NUM_POLYGON_VERTICES];
  54. int newVerticesCount = 0;
  55. previousVertex = &poly->vertices[poly->num_vertices - 1];
  56. for(i = 0; i < poly->num_vertices; i++)
  57. {
  58. currentVertex = &poly->vertices[i];
  59. currentDot = vec3Dot(vec3SubVectors(*currentVertex, planePoint), planeNormal);
  60. previousDot = vec3Dot(vec3SubVectors(*previousVertex, planePoint), planeNormal);
  61. if ((currentDot * previousDot) < 0.0)
  62. {
  63. double t;
  64. /* we crossed the plane */
  65. vec3_t intersectionVertice;
  66. t = previousDot / (previousDot - currentDot);
  67. intersectionVertice = *currentVertex;
  68. intersectionVertice = vec3SubVectors(intersectionVertice, *previousVertex);
  69. intersectionVertice = vec3ScalarMult(intersectionVertice, t);
  70. intersectionVertice = vec3AddVectors(intersectionVertice, *previousVertex);
  71. newVertices[newVerticesCount] = intersectionVertice;
  72. newVerticesCount++;
  73. }
  74. if (currentDot > 0.0)
  75. {
  76. newVertices[newVerticesCount] = *currentVertex;
  77. newVerticesCount++;
  78. }
  79. /* Just to be sure, should never trigger */
  80. assert(newVerticesCount < MAX_NUM_POLYGON_VERTICES);
  81. previousVertex = currentVertex;
  82. }
  83. for(i = 0; i < newVerticesCount; i++)
  84. {
  85. poly->vertices[i] = newVertices[i];
  86. }
  87. poly->num_vertices = newVerticesCount;
  88. }
  89. void clipPolygon(polygon_t *poly)
  90. {
  91. clipPolygonAgainstPlane(poly, LEFT_PLANE);
  92. clipPolygonAgainstPlane(poly, TOP_PLANE);
  93. clipPolygonAgainstPlane(poly, RIGHT_PLANE);
  94. clipPolygonAgainstPlane(poly, BOTTOM_PLANE);
  95. clipPolygonAgainstPlane(poly, NEAR_PLANE);
  96. clipPolygonAgainstPlane(poly, FAR_PLANE);
  97. }