rasterizer.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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(Integers)
  24. static void viewportTransform(Buffer<Uint32> *pixelBuffer, Vector3f *vertices);
  25. //Given a set of vertex values, the triangle area in screen space
  26. //and a target point returns the barycentric coordinates.
  27. static void barycentric(Vector3f &lambdas, float denom, float d00, float d01, float d11, float d20, float d21);
  28. static void triBoundBox(int &xMax, int &xMin, int &yMax, int &yMin, Vector3f *vertices, Buffer<Uint32> *pixelBuffer);
  29. static float edge(Vector3f &a, Vector3f &b, Vector3f &c);
  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_RGBA8888;
  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. };
  41. #endif