shader.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifndef SHADER_H
  2. #define SHADER_H
  3. #include "vector3.h"
  4. #include "matrix.h"
  5. #include "rasterizer.h"
  6. #include <array>
  7. struct IShader {
  8. virtual ~IShader() {};
  9. virtual Vector3 vertex(Vector3 &vertex, Matrix4 &MVP, float intensity, Vector3 &normals, Vector3 &light, int i) = 0;
  10. virtual bool fragment(Vector3 &bari, Vector3 &color, float &depth, Vector3 &zVerts) = 0;
  11. };
  12. struct FlatShader : public IShader {
  13. float varIntensity;
  14. Vector3 rgb{255,255,255};
  15. Vector3 vertex(Vector3 &vertex, Matrix4 &MVP, float intensity, Vector3 &normals, Vector3 &light, int index) override{
  16. varIntensity = intensity;
  17. return MVP.matMultVec(vertex);
  18. }
  19. bool fragment(Vector3 &bari, Vector3 &color, float &depth, Vector3 &zVerts) override{
  20. color = rgb*varIntensity;
  21. depth = bari.dotProduct(zVerts);
  22. return false;
  23. }
  24. };
  25. struct GouraudShader : public IShader {
  26. Vector3 varying_intensity;
  27. Vector3 rgb{255,255,255};
  28. Vector3 vertex(Vector3 &vertex, Matrix4 &MVP,float intensity, Vector3 &normals, Vector3 &light, int index) override{
  29. //normals.print();
  30. varying_intensity.data[index] = std::max(0.0f, normals.dotProduct(light));
  31. return MVP.matMultVec(vertex);
  32. }
  33. bool fragment(Vector3 &bari, Vector3 &color, float &depth, Vector3 &zVerts) override{
  34. //varying_intensity.print();
  35. float intensity = varying_intensity.dotProduct(bari);
  36. color = rgb * intensity;
  37. depth = bari.dotProduct(zVerts);
  38. return false;
  39. }
  40. };
  41. #endif