our_gl.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <cmath>
  2. #include <limits>
  3. #include <cstdlib>
  4. #include "our_gl.h"
  5. #include "Bullet3Common/b3MinMax.h"
  6. IShader::~IShader() {}
  7. Matrix viewport(int x, int y, int w, int h)
  8. {
  9. Matrix Viewport;
  10. Viewport = Matrix::identity();
  11. Viewport[0][3] = x+w/2.f;
  12. Viewport[1][3] = y+h/2.f;
  13. Viewport[2][3] = 1.f;
  14. Viewport[0][0] = w/2.f;
  15. Viewport[1][1] = h/2.f;
  16. Viewport[2][2] = 0;
  17. return Viewport;
  18. }
  19. Matrix projection(float coeff) {
  20. Matrix Projection;
  21. Projection = Matrix::identity();
  22. Projection[3][2] = coeff;
  23. return Projection;
  24. }
  25. Matrix lookat(Vec3f eye, Vec3f center, Vec3f up) {
  26. Vec3f z = (eye-center).normalize();
  27. Vec3f x = cross(up,z).normalize();
  28. Vec3f y = cross(z,x).normalize();
  29. Matrix Minv = Matrix::identity();
  30. Matrix Tr = Matrix::identity();
  31. for (int i=0; i<3; i++) {
  32. Minv[0][i] = x[i];
  33. Minv[1][i] = y[i];
  34. Minv[2][i] = z[i];
  35. Tr[i][3] = -center[i];
  36. }
  37. Matrix ModelView;
  38. ModelView = Minv*Tr;
  39. return ModelView;
  40. }
  41. Vec3f barycentric(Vec2f A, Vec2f B, Vec2f C, Vec2f P) {
  42. Vec3f s[2];
  43. for (int i=2; i--; ) {
  44. s[i][0] = C[i]-A[i];
  45. s[i][1] = B[i]-A[i];
  46. s[i][2] = A[i]-P[i];
  47. }
  48. Vec3f u = cross(s[0], s[1]);
  49. if (std::abs(u[2])>1e-2) // dont forget that u[2] is integer. If it is zero then triangle ABC is degenerate
  50. return Vec3f(1.f-(u.x+u.y)/u.z, u.y/u.z, u.x/u.z);
  51. return Vec3f(-1,1,1); // in this case generate negative coordinates, it will be thrown away by the rasterizator
  52. }
  53. void triangle(mat<4,3,float> &clipc, IShader &shader, TGAImage &image, float *zbuffer, const Matrix& viewPortMatrix)
  54. {
  55. triangle(clipc,shader,image,zbuffer,0,viewPortMatrix,0);
  56. }
  57. void triangle(mat<4,3,float> &clipc, IShader &shader, TGAImage &image, float *zbuffer, int* segmentationMaskBuffer, const Matrix& viewPortMatrix, int objectIndex) {
  58. mat<3,4,float> pts = (viewPortMatrix*clipc).transpose(); // transposed to ease access to each of the points
  59. //we don't clip triangles that cross the near plane, just discard them instead of showing artifacts
  60. if (pts[0][3]<0 || pts[1][3] <0 || pts[2][3] <0)
  61. return;
  62. mat<3,2,float> pts2;
  63. for (int i=0; i<3; i++) pts2[i] = proj<2>(pts[i]/pts[i][3]);
  64. Vec2f bboxmin( std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
  65. Vec2f bboxmax(-std::numeric_limits<float>::max(), -std::numeric_limits<float>::max());
  66. Vec2f clamp(image.get_width()-1, image.get_height()-1);
  67. for (int i=0; i<3; i++) {
  68. for (int j=0; j<2; j++) {
  69. bboxmin[j] = b3Max(0.f, b3Min(bboxmin[j], pts2[i][j]));
  70. bboxmax[j] = b3Min(clamp[j], b3Max(bboxmax[j], pts2[i][j]));
  71. }
  72. }
  73. Vec2i P;
  74. TGAColor color;
  75. for (P.x=bboxmin.x; P.x<=bboxmax.x; P.x++) {
  76. for (P.y=bboxmin.y; P.y<=bboxmax.y; P.y++) {
  77. Vec3f bc_screen = barycentric(pts2[0], pts2[1], pts2[2], P);
  78. Vec3f bc_clip = Vec3f(bc_screen.x/pts[0][3], bc_screen.y/pts[1][3], bc_screen.z/pts[2][3]);
  79. bc_clip = bc_clip/(bc_clip.x+bc_clip.y+bc_clip.z);
  80. float frag_depth = -1*(clipc[2]*bc_clip);
  81. if (bc_screen.x<0 || bc_screen.y<0 || bc_screen.z<0 ||
  82. zbuffer[P.x+P.y*image.get_width()]>frag_depth)
  83. continue;
  84. bool discard = shader.fragment(bc_clip, color);
  85. if (!discard) {
  86. zbuffer[P.x+P.y*image.get_width()] = frag_depth;
  87. if (segmentationMaskBuffer)
  88. {
  89. segmentationMaskBuffer[P.x+P.y*image.get_width()] = objectIndex;
  90. }
  91. image.set(P.x, P.y, color);
  92. }
  93. }
  94. }
  95. }