Renderer.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "Renderer.h"
  2. #include "RendererInitializer.h"
  3. #include "Util/Exception.h"
  4. #include "Scene/PerspectiveCamera.h"
  5. namespace R {
  6. //==============================================================================
  7. // Constructor =
  8. //==============================================================================
  9. Renderer::Renderer():
  10. ms(*this),
  11. is(*this),
  12. pps(*this),
  13. bs(*this),
  14. width(640),
  15. height(480),
  16. sceneDrawer(*this)
  17. {}
  18. //==============================================================================
  19. // init =
  20. //==============================================================================
  21. void Renderer::init(const RendererInitializer& initializer)
  22. {
  23. // set from the initializer
  24. width = initializer.width;
  25. height = initializer.height;
  26. aspectRatio = float(width) / height;
  27. framesNum = 0;
  28. // a few sanity checks
  29. if(width < 10 || height < 10)
  30. {
  31. throw EXCEPTION("Incorrect sizes");
  32. }
  33. // init the stages. Careful with the order!!!!!!!!!!
  34. ms.init(initializer);
  35. is.init(initializer);
  36. pps.init(initializer);
  37. bs.init(initializer);
  38. // quad VBOs and VAO
  39. float quadVertCoords[][2] = {{1.0, 1.0}, {0.0, 1.0}, {0.0, 0.0},
  40. {1.0, 0.0}};
  41. quadPositionsVbo.create(GL_ARRAY_BUFFER, sizeof(quadVertCoords),
  42. quadVertCoords, GL_STATIC_DRAW);
  43. ushort quadVertIndeces[2][3] = {{0, 1, 3}, {1, 2, 3}}; // 2 triangles
  44. quadVertIndecesVbo.create(GL_ELEMENT_ARRAY_BUFFER, sizeof(quadVertIndeces),
  45. quadVertIndeces, GL_STATIC_DRAW);
  46. quadVao.create();
  47. quadVao.attachArrayBufferVbo(quadPositionsVbo, 0, 2, GL_FLOAT, false, 0,
  48. NULL);
  49. quadVao.attachElementArrayBufferVbo(quadVertIndecesVbo);
  50. // Other
  51. skinsDeformer.init();
  52. }
  53. //==============================================================================
  54. // render =
  55. //==============================================================================
  56. void Renderer::render(Camera& cam_)
  57. {
  58. cam = &cam_;
  59. //
  60. // Calc a few vars
  61. //
  62. calcPlanes(Vec2(cam->getZNear(), cam->getZFar()), planes);
  63. ASSERT(cam->getType() == Camera::CT_PERSPECTIVE);
  64. const PerspectiveCamera& pcam = static_cast<const PerspectiveCamera&>(*cam);
  65. calcLimitsOfNearPlane(pcam, limitsOfNearPlane);
  66. limitsOfNearPlane2 = limitsOfNearPlane * 2.0;
  67. viewProjectionMat = cam->getProjectionMatrix() * cam->getViewMatrix();
  68. ms.run();
  69. is.run();
  70. pps.runPrePass();
  71. bs.run();
  72. pps.runPostPass();
  73. ++framesNum;
  74. }
  75. //==============================================================================
  76. // drawQuad =
  77. //==============================================================================
  78. void Renderer::drawQuad()
  79. {
  80. quadVao.bind();
  81. glDrawElements(GL_TRIANGLES, 2 * 3, GL_UNSIGNED_SHORT, 0);
  82. quadVao.unbind();
  83. ON_GL_FAIL_THROW_EXCEPTION();
  84. }
  85. //==============================================================================
  86. // unproject =
  87. //==============================================================================
  88. Vec3 Renderer::unproject(const Vec3& windowCoords, const Mat4& modelViewMat,
  89. const Mat4& projectionMat, const int view[4])
  90. {
  91. Mat4 invPm = projectionMat * modelViewMat;
  92. invPm.invert();
  93. // the vec is in NDC space meaning: -1<=vec.x<=1 -1<=vec.y<=1 -1<=vec.z<=1
  94. Vec4 vec;
  95. vec.x() = (2.0 * (windowCoords.x() - view[0])) / view[2] - 1.0;
  96. vec.y() = (2.0 * (windowCoords.y() - view[1])) / view[3] - 1.0;
  97. vec.z() = 2.0 * windowCoords.z() - 1.0;
  98. vec.w() = 1.0;
  99. Vec4 final = invPm * vec;
  100. final /= final.w();
  101. return Vec3(final);
  102. }
  103. //==============================================================================
  104. // createFai =
  105. //==============================================================================
  106. void Renderer::createFai(uint width, uint height, int internalFormat,
  107. int format, int type, Texture& fai)
  108. {
  109. Texture::Initializer init;
  110. init.width = width;
  111. init.height = height;
  112. init.internalFormat = internalFormat;
  113. init.format = format;
  114. init.type = type;
  115. init.data = NULL;
  116. init.mipmapping = false;
  117. init.filteringType = Texture::TFT_LINEAR;
  118. init.repeat = false;
  119. init.anisotropyLevel = 0;
  120. fai.create(init);
  121. }
  122. //==============================================================================
  123. // calcPlanes =
  124. //==============================================================================
  125. void Renderer::calcPlanes(const Vec2& cameraRange, Vec2& planes)
  126. {
  127. float zNear = cameraRange.x();
  128. float zFar = cameraRange.y();
  129. planes.x() = zFar / (zNear - zFar);
  130. planes.y() = (zFar * zNear) / (zNear -zFar);
  131. }
  132. //==============================================================================
  133. // calcLimitsOfNearPlane =
  134. //==============================================================================
  135. void Renderer::calcLimitsOfNearPlane(const PerspectiveCamera& pcam,
  136. Vec2& limitsOfNearPlane)
  137. {
  138. limitsOfNearPlane.y() = pcam.getZNear() * tan(0.5 * pcam.getFovY());
  139. limitsOfNearPlane.x() = limitsOfNearPlane.y() *
  140. (pcam.getFovX() / pcam.getFovY());
  141. }
  142. } // end namespace