CCVRGenericRenderer.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /****************************************************************************
  2. Copyright (c) 2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "platform/CCPlatformMacros.h"
  22. #include "vr/CCVRGenericRenderer.h"
  23. #include "vr/CCVRDistortionMesh.h"
  24. #include "vr/CCVRDistortion.h"
  25. #include "vr/CCVRGenericHeadTracker.h"
  26. #include "renderer/CCRenderer.h"
  27. #include "renderer/CCGLProgramState.h"
  28. #include "renderer/ccGLStateCache.h"
  29. #include "base/CCDirector.h"
  30. #include "2d/CCScene.h"
  31. #include "2d/CCCamera.h"
  32. #include "2d/CCSprite.h"
  33. #include "platform/CCGLView.h"
  34. NS_CC_BEGIN
  35. VRGenericRenderer::VRGenericRenderer()
  36. : _vignetteEnabled(true)
  37. , _distortion(nullptr)
  38. , _leftDistortionMesh(nullptr)
  39. , _rightDistortionMesh(nullptr)
  40. , _glProgramState(nullptr)
  41. {
  42. _headTracker = new VRGenericHeadTracker;
  43. }
  44. VRGenericRenderer::~VRGenericRenderer()
  45. {
  46. CC_SAFE_DELETE(_headTracker);
  47. CC_SAFE_RELEASE(_glProgramState);
  48. CC_SAFE_RELEASE(_fb);
  49. CC_SAFE_DELETE(_distortion);
  50. CC_SAFE_DELETE(_leftDistortionMesh);
  51. CC_SAFE_DELETE(_rightDistortionMesh);
  52. }
  53. void VRGenericRenderer::setup(GLView* /*glview*/)
  54. {
  55. // set origin to 0,0 in case origin is not 0,0
  56. auto vp = Camera::getDefaultViewport();
  57. _leftEye.viewport._bottom = vp._bottom/2 + vp._height/4;
  58. _leftEye.viewport._left = vp._left/4;
  59. _leftEye.viewport._width = vp._width/2;
  60. _leftEye.viewport._height = vp._height/2;
  61. _rightEye.viewport._bottom = vp._bottom/2 + vp._height/4;
  62. _rightEye.viewport._left = _leftEye.viewport._width + vp._left/2;
  63. _rightEye.viewport._width = vp._width/2;
  64. _rightEye.viewport._height = vp._height/2;
  65. _texSize = Size(vp._width, vp._height);
  66. _fb = experimental::FrameBuffer::create(1, _texSize.width, _texSize.height);
  67. _fb->retain();
  68. auto rt = experimental::RenderTarget::create(_texSize.width, _texSize.height);
  69. auto ds = experimental::RenderTargetDepthStencil::create(_texSize.width, _texSize.height);
  70. _fb->attachRenderTarget(rt);
  71. _fb->attachDepthStencilTarget(ds);
  72. _fb->setClearColor(Color4F(0,0,0,1));
  73. _distortion = new Distortion;
  74. _leftDistortionMesh = createDistortionMesh(VREye::EyeType::LEFT);
  75. _rightDistortionMesh = createDistortionMesh(VREye::EyeType::RIGHT);
  76. setupGLProgram();
  77. }
  78. void VRGenericRenderer::cleanup()
  79. {
  80. }
  81. VRIHeadTracker* VRGenericRenderer::getHeadTracker()
  82. {
  83. return _headTracker;
  84. }
  85. void VRGenericRenderer::render(Scene* scene, Renderer* renderer)
  86. {
  87. // FIXME: Use correct eye displacement
  88. const float eyeOffset = 0.5;
  89. auto headRotation = _headTracker->getLocalRotation();
  90. Mat4 leftTransform;
  91. Mat4::createTranslation(eyeOffset, 0, 0, &leftTransform);
  92. leftTransform *= headRotation;
  93. Mat4 rightTransform;
  94. Mat4::createTranslation(-eyeOffset, 0, 0, &rightTransform);
  95. rightTransform *= headRotation;
  96. _fb->applyFBO();
  97. auto defaultVP = Camera::getDefaultViewport();
  98. Camera::setDefaultViewport(_leftEye.viewport);
  99. scene->render(renderer, leftTransform, nullptr);
  100. Camera::setDefaultViewport(_rightEye.viewport);
  101. scene->render(renderer, rightTransform, nullptr);
  102. Camera::setDefaultViewport(defaultVP);
  103. _fb->restoreFBO();
  104. auto texture = _fb->getRenderTarget()->getTexture();
  105. GL::bindTexture2D(texture->getName());
  106. _glProgramState->apply(Mat4::IDENTITY);
  107. GLint origViewport[4];
  108. glGetIntegerv(GL_VIEWPORT, origViewport);
  109. glViewport(0, 0, _texSize.width, _texSize.height);
  110. renderDistortionMesh(_leftDistortionMesh, texture);
  111. renderDistortionMesh(_rightDistortionMesh, texture);
  112. glViewport(origViewport[0], origViewport[1], origViewport[2], origViewport[3]);
  113. glBindBuffer(GL_ARRAY_BUFFER, 0);
  114. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  115. CHECK_GL_ERROR_DEBUG();
  116. }
  117. void VRGenericRenderer::renderDistortionMesh(DistortionMesh *mesh, Texture2D* texture)
  118. {
  119. glBindBuffer(GL_ARRAY_BUFFER, mesh->_arrayBufferID);
  120. _glProgramState->setVertexAttribPointer("a_position", 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(0 * sizeof(float)));
  121. _glProgramState->setVertexAttribPointer("a_textureCoord", 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(2 * sizeof(float)));
  122. _glProgramState->setVertexAttribPointer("a_vignette", 1, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(4 * sizeof(float)));
  123. _glProgramState->setUniformTexture("u_textureSampler", texture);
  124. _glProgramState->apply(Mat4::IDENTITY);
  125. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->_elementBufferID);
  126. glDrawElements(GL_TRIANGLE_STRIP, mesh->_indices, GL_UNSIGNED_SHORT, 0);
  127. CHECK_GL_ERROR_DEBUG();
  128. }
  129. DistortionMesh* VRGenericRenderer::createDistortionMesh(VREye::EyeType eyeType)
  130. {
  131. auto vp = Camera::getDefaultViewport();
  132. const float screenWidth = _texSize.width;
  133. const float screenHeight = _texSize.height;
  134. const float xEyeOffsetScreen = (eyeType == VREye::EyeType::LEFT) ? screenWidth/4 + vp._left : screenWidth*3/4 + vp._left;
  135. const float yEyeOffsetScreen = screenHeight/2 + vp._bottom;
  136. const float textureWidth = _texSize.width;
  137. const float textureHeight = _texSize.height;
  138. const float xEyeOffsetTexture = (eyeType == VREye::EyeType::LEFT) ? _texSize.width/4 : _texSize.width*3/4;
  139. const float yEyeOffsetTexture = _texSize.height/2;
  140. const float viewportX = (eyeType == VREye::EyeType::LEFT) ? 0 : textureWidth/2;
  141. const float viewportY = 0;
  142. const float viewportW = textureWidth/2;
  143. const float viewportH = textureHeight;
  144. return new DistortionMesh(_distortion,
  145. screenWidth, screenHeight,
  146. xEyeOffsetScreen, yEyeOffsetScreen,
  147. textureWidth, textureHeight,
  148. xEyeOffsetTexture, yEyeOffsetTexture,
  149. viewportX, viewportY,
  150. viewportW, viewportH,
  151. _vignetteEnabled);
  152. }
  153. void VRGenericRenderer::setupGLProgram()
  154. {
  155. const GLchar *vertexShader =
  156. "\
  157. attribute vec2 a_position;\n\
  158. attribute vec2 a_textureCoord;\n\
  159. attribute float a_vignette;\n\
  160. varying vec2 v_textureCoord;\n\
  161. varying float v_vignette;\n\
  162. void main() {\n\
  163. gl_Position = vec4(a_position, 0.0, 1.0);\n\
  164. v_textureCoord = a_textureCoord.xy;\n\
  165. v_vignette = a_vignette;\n\
  166. }\n";
  167. const GLchar *fragmentShader =
  168. "\
  169. #ifdef GL_ES\n\
  170. precision mediump float;\n\
  171. #endif\n\
  172. varying vec2 v_textureCoord;\n\
  173. varying float v_vignette;\n\
  174. uniform sampler2D u_textureSampler;\n\
  175. void main() {\n\
  176. gl_FragColor = v_vignette * texture2D(u_textureSampler, v_textureCoord);\n\
  177. }\n";
  178. auto program = GLProgram::createWithByteArrays(vertexShader, fragmentShader);
  179. _glProgramState = GLProgramState::getOrCreateWithGLProgram(program);
  180. _glProgramState->retain();
  181. }
  182. NS_CC_END