rasterizer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef RASTERIZER_H
  2. #define RASTERIZER_H
  3. // ===============================
  4. // AUTHOR : Angel Ortiz (angelo12 AT vt DOT edu)
  5. // CREATE DATE : 2018-07-03
  6. // PURPOSE : To break down the vertex data into fragments. Determine which of those
  7. // fragments are visible within the screen and within a given line/triangle.
  8. // apply the fragment shader to each triangle and post process using gamma
  9. // correction. Can rasterize points, lines and triangles.
  10. // ===============================
  11. // SPECIAL NOTES: Although line rasterization is possible the engine currently only supports
  12. // triangle rasterization through the higher level software rendering class. It's important
  13. // to also note that this rasterizer is nowhere close to optimized. On a future iteration,
  14. // it would an ideal candidate for a total makeover, reducing the coupling with other classes
  15. // and transforming the rasterizer pipeline from a float and scalar one to a fixed point
  16. // and vectorized one. This would simplify the implementation of of many advanced features
  17. // such as mip-mapping, sub-pixel precision, gradient based shading and antialiasing which
  18. // are now very complicated to implement without it.
  19. // ===============================
  20. //Headers
  21. #include "SDL.h"
  22. #include "buffer.h"
  23. #include "vector3D.h"
  24. #include "shader.h"
  25. //Takes in vertex data, rasterizes the surface and applies the fragment shader at
  26. //each fragment. If it passes the depth test the fragment is written to the pixel buffer.
  27. class Rasterizer{
  28. public:
  29. //Simple full screen effects that don't need any vertex data
  30. static void makeCoolPattern(Buffer<Uint32> *pixelBuffer);
  31. static void testPattern(Buffer<Uint32> *pixelBuffer);
  32. //Bresenham's line drawing algorithm using only int arithmetic
  33. static void drawLine(Vector3f &vertex1, Vector3f &vertex2, const Uint32 &color, Buffer<Uint32> *pixelBuffer);
  34. //Draws wireframe rendering of triangle by calling the line drawer for each
  35. //Line in a triangle.(AB, BC, AC)
  36. static void drawWireFrame(Vector3f *vertices, IShader &shader, Buffer<Uint32> *pixelBuffer);
  37. //Proper triangle rasterization with vertex interpolation. Zbuffering, post processing
  38. //Sadly, not very efficient since it uses floats and scalar operations instead of SIMD
  39. static void drawTriangles(Vector3f *vertices, IShader &shader, Buffer<Uint32> *pixelBuffer, Buffer<float> *zBuffer);
  40. //Transforms coordinates from NDC to pixel values
  41. static void viewportTransform(Buffer<Uint32> *pixelBuffer, Vector3f *vertices);
  42. //Builds the triangle bounding box
  43. static void triBoundBox(int &xMax, int &xMin, int &yMax, int &yMin, Vector3f *vertices, Buffer<Uint32> *pixelBuffer);
  44. //Baricentric coordinates by use of pineda style edge detection
  45. static float edge(Vector3f &a, Vector3f &b, Vector3f &c);
  46. static bool inside(float edge, float a, float b);
  47. //Post processing operations
  48. static float clamp(float n, float lower, float upper);
  49. static int gammaAdjust(float n);
  50. private:
  51. Rasterizer(){}; //Ensuring an object can never be instanced accidentally
  52. //Setting this equal to the same pixel format our screen texture is in
  53. static const Uint32 PIXEL_FORMAT = SDL_PIXELFORMAT_RGB888;
  54. static const SDL_PixelFormat* mappingFormat;
  55. //Some basic colors
  56. static const Uint32 white;
  57. static const Uint32 red;
  58. static const Uint32 green;
  59. static const Uint32 blue;
  60. //Gamma correction look-up table for gamma = 2.2
  61. static const int gammaTable[256];
  62. };
  63. #endif