rasterizer.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef RASTERIZER_H
  2. #define RASTERIZER_H
  3. #include "SDL.h"
  4. #include "buffer.h"
  5. #include "vector3D.h"
  6. #include "shader.h"
  7. //Shorthand of repeated types.
  8. typedef std::array<float, 3> arr3f;
  9. //Takes in vertex data, rasterizes the surface and applies the fragment shader at
  10. //each fragment. If it passes the depth test the fragment is written to the pixel buffer.
  11. class Rasterizer{
  12. public:
  13. //Simple full screen effects that don't need any vertex data
  14. static void makeCoolPattern(Buffer<Uint32> *pixelBuffer);
  15. static void testPattern(Buffer<Uint32> *pixelBuffer);
  16. //Bresenham's line drawing algorithm using only int arithmetic
  17. static void drawLine(Vector3f &vertex1, Vector3f &vertex2, const Uint32 &color, Buffer<Uint32> *pixelBuffer);
  18. //Draws wireframe rendering of triangle by calling the line drawer for each
  19. //Line in a triangle.(AB, BC, AC)
  20. static void drawWireFrame(Vector3f *vertices, IShader &shader, Buffer<Uint32> *pixelBuffer);
  21. //Proper triangle rasterization with vertex interpolation.
  22. static void drawTriangles(Vector3f *vertices, IShader &shader, Buffer<Uint32> *pixelBuffer, Buffer<float> *zBuffer);
  23. //Transforms coordinates from NDC to pixel values
  24. static void viewportTransform(Buffer<Uint32> *pixelBuffer, Vector3f *vertices);
  25. static void triBoundBox(int &xMax, int &xMin, int &yMax, int &yMin, Vector3f *vertices, Buffer<Uint32> *pixelBuffer);
  26. static float edge(Vector3f &a, Vector3f &b, Vector3f &c);
  27. static bool inside(float edge, float a, float b);
  28. static float clamp(float n, float lower, float upper);
  29. static int gammaAdjust(float n);
  30. private:
  31. Rasterizer(){}; //Ensuring an object can never be instanced accidentally
  32. //Setting this equal to the same pixel format our screen texture is in
  33. static const Uint32 PIXEL_FORMAT = SDL_PIXELFORMAT_RGB888;
  34. static const SDL_PixelFormat* mappingFormat;
  35. //Some basic colors
  36. static const Uint32 white;
  37. static const Uint32 red;
  38. static const Uint32 green;
  39. static const Uint32 blue;
  40. //Gamma correction look-up table for gamma = 2.2
  41. static const int gammaTable[256];
  42. };
  43. #endif