softwareRenderer.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef SRENDERER_H
  2. #define SRENDERER_H
  3. // ===============================
  4. // AUTHOR : Angel Ortiz (angelo12 AT vt DOT edu)
  5. // CREATE DATE : 2018-07-10
  6. // PURPOSE : Core graphics class that performs rendering for different mesh types.
  7. // The core rendering function is drawTriangularMesh that contains the logic
  8. // for drawing triangular mesh objects as described in the mesh class.
  9. // It attempts to recreate a similar pipeline to the old OpenGL 2 one
  10. // due to its simplicity.
  11. // ===============================
  12. // SPECIAL NOTES: As mentioned in the manager level class, many tasks done here should be
  13. // offloaded to the manager class to reduce coupling. Specifically, the buffers should be
  14. // moved to the manager class and treated simply as rendertargets for the renderer to draw
  15. // to. There is also no real need for the render class to have knowledge of my definition
  16. // of a model and camera. On a second pass I would rebuild this from the ground up with a
  17. // similar structure to opengl's VBO, VAO and EBO's system.
  18. // ===============================
  19. //Headers
  20. #include "rasterizer.h"
  21. #include "buffer.h"
  22. #include "model.h"
  23. #include "camera.h"
  24. #include "light.h"
  25. class SoftwareRenderer {
  26. public:
  27. //Dummy Constructor / Destructor
  28. SoftwareRenderer();
  29. ~SoftwareRenderer();
  30. //Creates all buffers and preps everything for model rendering
  31. bool startUp(int w, int h);
  32. void shutDown();
  33. //Overview:
  34. //01.-Gets pointers to mesh data from model
  35. //02.-Builds Model view and projection matrix
  36. //03.-Iterates through all triangle faces (in parallel!)
  37. //04.-Runs backface culling algo uisng facet normmals
  38. //05.-Applies vertex shader per vertex in face
  39. //06.-Performs clipping of triangles fully outside view frustrum
  40. //07.-Applies perspective divide using "hidden" w coordinate
  41. //08.-Sends shader data and triangle to rasterizer
  42. //09.-NDC to viewport transform
  43. //10.-Builds triangle bounding box and Iterates through
  44. //11.-Calculates perspective correct barycentric coordinates
  45. //12.-Culls pixels outside of triangle or screen
  46. //13.-Runs fragment shader per pixel that passes depth buffer check
  47. //14.-updates zBuffer with new value and gamma corrects color
  48. //15.-Writes to pixel frame buffer
  49. void drawTriangularMesh(Model * currentModel);
  50. //Returns pixel buffer
  51. Buffer<Uint32>* getRenderTarget();
  52. void clearBuffers();
  53. //Set up methods called by rendermanager
  54. void setCameraToRenderFrom(Camera * camera);
  55. void setSceneLights(BaseLight * lights, int numLights);
  56. private:
  57. //Buffer methods
  58. bool createBuffers(int w, int h);
  59. //Primitive level methods
  60. void packDataIntoTris(Vector3i &f, Vector3f *trianglePrim, std::vector<Vector3f> &vals);
  61. void perspectiveDivide(Vector3f *clippedVertices);
  62. //Culling and clipping methods
  63. bool backFaceCulling(Vector3f &facetNormal, Vector3f &vertex, Matrix4 &worldToObject);
  64. bool clipTriangles(Vector3f *clipSpaceVertices);
  65. //Pointer to the scene's target camera
  66. Camera * mCamera;
  67. bool startUpComplete = false;
  68. //Pointer to structs containing all the light info
  69. int mNumLights;
  70. BaseLight *mLights;
  71. //Buffers
  72. Buffer<float> * zBuffer;
  73. Buffer<Uint32> * pixelBuffer;
  74. };
  75. #endif