CCVRDistortionMesh.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /****************************************************************************
  2. Copyright (c) 2016 Google Inc.
  3. Copyright (c) 2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "vr/CCVRDistortionMesh.h"
  23. #include <vector>
  24. #include "vr/CCVRDistortion.h"
  25. #include "math/Vec2.h"
  26. #include "platform/CCGL.h"
  27. NS_CC_BEGIN
  28. DistortionMesh::DistortionMesh(Distortion *distortion,
  29. float screenWidth, float screenHeight,
  30. float xEyeOffsetScreen, float yEyeOffsetScreen,
  31. float textureWidth, float textureHeight,
  32. float xEyeOffsetTexture, float yEyeOffsetTexture,
  33. float viewportXTexture, float viewportYTexture,
  34. float viewportWidthTexture, float viewportHeightTexture,
  35. bool vignetteEnabled)
  36. : _indices(-1)
  37. , _arrayBufferID(-1)
  38. , _elementBufferID(-1)
  39. {
  40. const int rows = 40;
  41. const int cols = 40;
  42. GLfloat vertexData[rows * cols * 5];
  43. int vertexOffset = 0;
  44. const float vignetteSizeTanAngle = 0.05f;
  45. const float maxDistance = sqrtf(textureWidth * textureWidth + textureHeight * textureHeight) / 4;
  46. for (int row = 0; row < rows; row++)
  47. {
  48. for (int col = 0; col < cols; col++)
  49. {
  50. const float uTexture = col / (cols-1.0f) * (viewportWidthTexture / textureWidth) + viewportXTexture / textureWidth;
  51. const float vTexture = row / (rows-1.0f) * (viewportHeightTexture / textureHeight) + viewportYTexture / textureHeight;
  52. const float xTexture = uTexture * textureWidth - xEyeOffsetTexture;
  53. const float yTexture = vTexture * textureHeight - yEyeOffsetTexture;
  54. const float rTexture = sqrtf(xTexture * xTexture + yTexture * yTexture) / maxDistance;
  55. const float textureToScreen = (rTexture > 0.0f) ? distortion->distortInverse(rTexture) / rTexture : 1.0f;
  56. const float xScreen = xTexture * textureToScreen;
  57. const float yScreen = yTexture * textureToScreen;
  58. const float uScreen = (xScreen + xEyeOffsetScreen) / screenWidth;
  59. const float vScreen = (yScreen + yEyeOffsetScreen) / screenHeight;
  60. const float vignetteSizeTexture = vignetteSizeTanAngle / textureToScreen;
  61. const float dxTexture = xTexture + xEyeOffsetTexture - clampf(xTexture + xEyeOffsetTexture,
  62. viewportXTexture + vignetteSizeTexture,
  63. viewportXTexture + viewportWidthTexture - vignetteSizeTexture);
  64. const float dyTexture = yTexture + yEyeOffsetTexture - clampf(yTexture + yEyeOffsetTexture,
  65. viewportYTexture + vignetteSizeTexture,
  66. viewportYTexture + viewportHeightTexture - vignetteSizeTexture);
  67. const float drTexture = sqrtf(dxTexture * dxTexture + dyTexture * dyTexture);
  68. float vignette = 1.0f;
  69. if (vignetteEnabled)
  70. vignette = 1.0f - clampf(drTexture / vignetteSizeTexture, 0.0f, 1.0f);
  71. // position x,y (vertices)
  72. vertexData[(vertexOffset + 0)] = 2.0f * uScreen - 1.0f;
  73. vertexData[(vertexOffset + 1)] = 2.0f * vScreen - 1.0f;
  74. // texture u,v
  75. vertexData[(vertexOffset + 2)] = uTexture;
  76. vertexData[(vertexOffset + 3)] = vTexture;
  77. // vignette
  78. vertexData[(vertexOffset + 4)] = vignette;
  79. vertexOffset += 5;
  80. }
  81. }
  82. _indices = (rows-1)*cols*2+rows-2;
  83. // GLshort indexData[_indices];
  84. std::vector<GLshort> indexData(_indices);
  85. const int indexDataSize = _indices * sizeof(GLshort);
  86. int indexOffset = 0;
  87. vertexOffset = 0;
  88. for (int row = 0; row < rows-1; row++)
  89. {
  90. if (row > 0)
  91. {
  92. indexData[indexOffset] = indexData[(indexOffset - 1)];
  93. indexOffset++;
  94. }
  95. for (int col = 0; col < cols; col++)
  96. {
  97. if (col > 0)
  98. {
  99. if (row % 2 == 0)
  100. vertexOffset++;
  101. else
  102. vertexOffset--;
  103. }
  104. indexData[(indexOffset++)] = vertexOffset;
  105. indexData[(indexOffset++)] = (vertexOffset + cols);
  106. }
  107. vertexOffset += rows;
  108. }
  109. GLuint bufferIDs[2] = { 0, 0 };
  110. glGenBuffers(2, bufferIDs);
  111. _arrayBufferID = bufferIDs[0];
  112. _elementBufferID = bufferIDs[1];
  113. glBindBuffer(GL_ARRAY_BUFFER, _arrayBufferID);
  114. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
  115. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _elementBufferID);
  116. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexDataSize, &indexData[0], GL_STATIC_DRAW);
  117. glBindBuffer(GL_ARRAY_BUFFER, 0);
  118. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  119. // GLCheckForError();
  120. }
  121. NS_CC_END