rasterizer.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef RASTERIZER_H
  2. #define RASTERIZER_H
  3. #include "SDL.h"
  4. #include "buffer.h"
  5. #include "vector3.h"
  6. #include "shader.h"
  7. //Takes in vertex data, rasterizes the surface and applies the fragment shader at
  8. //each fragment. If it passes the depth test the fragment is written to the pixel buffer.
  9. class Rasterizer{
  10. public:
  11. //Simple full screen effects that don't need any vertex data
  12. static void makeCoolPattern(Buffer<Uint32> *pixelBuffer);
  13. static void testPattern(Buffer<Uint32> *pixelBuffer);
  14. //Bresenham's line drawing algorithm using only int arithmetic
  15. static void drawLine(Vector3 &vertex1, Vector3 &vertex2, const Uint32 &color, Buffer<Uint32> *pixelBuffer);
  16. //Draws wireframe rendering of triangle by calling the line drawer for each
  17. //Line in a triangle.(AB, BC, AC)
  18. static void drawWireFrame(Vector3 *vertices, IShader &shader, Buffer<Uint32> *pixelBuffer);
  19. //Proper triangle rasterization with vertex interpolation.
  20. static void drawTriangles(Vector3 *vertices, IShader &shader, Buffer<Uint32> *pixelBuffer, Buffer<float> *zBuffer);
  21. //Transforms coordinates from NDC to pixel values(Integers)
  22. static void viewportTransform(Buffer<Uint32> *pixelBuffer, Vector3 *vertices,std::array<int, 3> &xV,std::array<int, 3> &yV, Vector3 &zV);
  23. //Given a set of vertex values, the triangle area in screen space
  24. //and a target point returns the barycentric coordinates calculated using
  25. //Edge functions
  26. static void barycentric(Vector3 &lambdas, float InvArea, int x, int y,
  27. std::array<int, 3> &xV, std::array<int, 3> &yV);
  28. private:
  29. Rasterizer(){}; //Ensuring an object can never be instanced accidentally
  30. //Setting this equal to the same pixel format our screen texture is in
  31. static const Uint32 PIXEL_FORMAT = SDL_PIXELFORMAT_RGBA8888;
  32. static const SDL_PixelFormat* mappingFormat;
  33. //Some basic colors
  34. static const Uint32 white;
  35. static const Uint32 red;
  36. static const Uint32 green;
  37. static const Uint32 blue;
  38. };
  39. #endif