clipping.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. triangleList[i - 1].textureCoordinates[0] = polygon->textureCoords[0];
  42. triangleList[i - 1].textureCoordinates[1] = polygon->textureCoords[i];
  43. triangleList[i - 1].textureCoordinates[2] = polygon->textureCoords[i + 1];
  44. (*outNumber)++;
  45. assert(*outNumber < MAX_NUM_POLYGON_TRIANGLES);
  46. }
  47. }
  48. /* Linear interpolation */
  49. static inline double floatLerp(double a, double b, double t)
  50. {
  51. return a + t * (b - a);
  52. }
  53. static void clipPolygonAgainstPlane(polygon_t *poly, faceenum_t plane)
  54. {
  55. int i;
  56. vec3_t planePoint = frustum[plane].point;
  57. vec3_t planeNormal = frustum[plane].normal;
  58. vec3_t *currentVertex, *previousVertex;
  59. tex2_t *currentTexCoord, *previousTexCoord;
  60. double currentDot, previousDot;
  61. vec3_t newVertices[MAX_NUM_POLYGON_VERTICES];
  62. tex2_t newTexCoords[MAX_NUM_POLYGON_VERTICES];
  63. int newVerticesCount = 0;
  64. previousVertex = &poly->vertices[poly->num_vertices - 1];
  65. previousTexCoord = &poly->textureCoords[poly->num_vertices - 1];
  66. for(i = 0; i < poly->num_vertices; i++)
  67. {
  68. currentVertex = &poly->vertices[i];
  69. currentTexCoord = &poly->textureCoords[i];
  70. currentDot = vec3Dot(vec3SubVectors(*currentVertex, planePoint), planeNormal);
  71. previousDot = vec3Dot(vec3SubVectors(*previousVertex, planePoint), planeNormal);
  72. if ((currentDot * previousDot) < 0.0)
  73. {
  74. double t = previousDot / (previousDot - currentDot);
  75. /* we crossed the plane */
  76. vec3_t intersectionVertex =
  77. {
  78. .x = floatLerp(previousVertex->x, currentVertex->x, t),
  79. .y = floatLerp(previousVertex->y, currentVertex->y, t),
  80. .z = floatLerp(previousVertex->z, currentVertex->z, t),
  81. };
  82. newVertices[newVerticesCount] = intersectionVertex;
  83. tex2_t intersectionTexCoord =
  84. {
  85. .u = floatLerp(previousTexCoord->u, currentTexCoord->u, t),
  86. .v = floatLerp(previousTexCoord->v, currentTexCoord->v, t),
  87. };
  88. newTexCoords[newVerticesCount] = intersectionTexCoord;
  89. newVerticesCount++;
  90. }
  91. if (currentDot > 0.0)
  92. {
  93. newVertices[newVerticesCount] = *currentVertex;
  94. newTexCoords[newVerticesCount] = *currentTexCoord;
  95. newVerticesCount++;
  96. }
  97. /* Just to be sure, should never trigger */
  98. assert(newVerticesCount < MAX_NUM_POLYGON_VERTICES);
  99. previousVertex = currentVertex;
  100. previousTexCoord = currentTexCoord;
  101. }
  102. for(i = 0; i < newVerticesCount; i++)
  103. {
  104. poly->vertices[i] = newVertices[i];
  105. poly->textureCoords[i] = newTexCoords[i];
  106. }
  107. poly->num_vertices = newVerticesCount;
  108. }
  109. void clipPolygon(polygon_t *poly)
  110. {
  111. clipPolygonAgainstPlane(poly, LEFT_PLANE);
  112. clipPolygonAgainstPlane(poly, TOP_PLANE);
  113. clipPolygonAgainstPlane(poly, RIGHT_PLANE);
  114. clipPolygonAgainstPlane(poly, BOTTOM_PLANE);
  115. clipPolygonAgainstPlane(poly, NEAR_PLANE);
  116. clipPolygonAgainstPlane(poly, FAR_PLANE);
  117. }