clipping.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * 3D Engine
  3. * clipping.h:
  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. #ifndef THREEDENGINE_SOURCE_INCLUDE_CLIPPING_H
  10. #define THREEDENGINE_SOURCE_INCLUDE_CLIPPING_H
  11. #include <vector.h>
  12. #include <triangle.h>
  13. /***********************************************************************************************************************
  14. * Constants
  15. **********************************************************************************************************************/
  16. #define MAX_NUM_POLYGON_VERTICES (10)
  17. #define MAX_NUM_POLYGON_TRIANGLES (10)
  18. /***********************************************************************************************************************
  19. * Data types
  20. **********************************************************************************************************************/
  21. typedef enum faceenum_t
  22. {
  23. TOP_PLANE = 0,
  24. RIGHT_PLANE,
  25. BOTTOM_PLANE,
  26. LEFT_PLANE,
  27. NEAR_PLANE,
  28. FAR_PLANE,
  29. /* Keep at the end */
  30. TOTAL_PLANES
  31. } faceenum_t;
  32. typedef struct plane_t
  33. {
  34. vec3_t point;
  35. vec3_t normal;
  36. } plane_t;
  37. typedef struct polygon_t
  38. {
  39. vec3_t vertices[MAX_NUM_POLYGON_VERTICES];
  40. tex2_t textureCoords[MAX_NUM_POLYGON_VERTICES];
  41. int num_vertices;
  42. } polygon_t;
  43. /***********************************************************************************************************************
  44. * Prototypes
  45. **********************************************************************************************************************/
  46. void initFrustumPlanes(double fovY, double aspectRatio, double zNear, double zFar);
  47. /* ---- Public polygon stuff ---- */
  48. void clipPolygon(polygon_t *poly);
  49. void createTriangleFromPolygon(polygon_t *polygon, triangle_t *triangleList, uint32_t *outNumber);
  50. static inline polygon_t createPolygonFromTriangle(vec3_t p1, vec3_t p2, vec3_t p3, tex2_t a_uv, tex2_t b_uv, tex2_t c_uv)
  51. {
  52. polygon_t ret =
  53. {
  54. .vertices =
  55. {
  56. [0] = p1,
  57. [1] = p2,
  58. [2] = p3,
  59. },
  60. .textureCoords =
  61. {
  62. [0] = a_uv,
  63. [1] = b_uv,
  64. [2] = c_uv,
  65. },
  66. .num_vertices = 3,
  67. };
  68. return ret;
  69. }
  70. #endif /* THREEDENGINE_SOURCE_INCLUDE_CLIPPING_H */