Renderers.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (C) 2012 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "Flatland.h"
  17. #include "GLHelper.h"
  18. namespace android {
  19. static float colors[][4] = {
  20. { .85f, .14f, .44f, 1.0f },
  21. { .91f, .72f, .10f, 1.0f },
  22. { .04f, .66f, .42f, 1.0f },
  23. { .84f, .39f, .68f, 1.0f },
  24. { .38f, .53f, .78f, 1.0f },
  25. };
  26. static size_t g_colorIndex;
  27. const float* genColor() {
  28. float* color = colors[g_colorIndex];
  29. g_colorIndex = (g_colorIndex + 1) % NELEMS(colors);
  30. return color;
  31. }
  32. void resetColorGenerator() {
  33. g_colorIndex = 0;
  34. }
  35. class GradientRenderer {
  36. public:
  37. bool setUp(GLHelper* helper) {
  38. bool result;
  39. result = helper->getShaderProgram("Gradient", &mGradPgm);
  40. if (!result) {
  41. return false;
  42. }
  43. result = helper->getDitherTexture(&mDitherTexName);
  44. if (!result) {
  45. return false;
  46. }
  47. mPosAttribLoc = glGetAttribLocation(mGradPgm, "position");
  48. mUVAttribLoc = glGetAttribLocation(mGradPgm, "uv");
  49. mUVToInterpUniformLoc = glGetUniformLocation(mGradPgm, "uvToInterp");
  50. mObjToNdcUniformLoc = glGetUniformLocation(mGradPgm, "objToNdc");
  51. mDitherKernelSamplerLoc = glGetUniformLocation(mGradPgm, "ditherKernel");
  52. mInvDitherKernelSizeUniformLoc = glGetUniformLocation(mGradPgm,
  53. "invDitherKernelSize");
  54. mInvDitherKernelSizeSqUniformLoc = glGetUniformLocation(mGradPgm,
  55. "invDitherKernelSizeSq");
  56. mColor0UniformLoc = glGetUniformLocation(mGradPgm, "color0");
  57. mColor1UniformLoc = glGetUniformLocation(mGradPgm, "color1");
  58. return true;
  59. }
  60. void tearDown() {
  61. }
  62. bool drawGradient() {
  63. float identity[16] = {
  64. 1.0f, 0.0f, 0.0f, 0.0f,
  65. 0.0f, 1.0f, 0.0f, 0.0f,
  66. 0.0f, 0.0f, 1.0f, 0.0f,
  67. 0.0f, 0.0f, 0.0f, 1.0f,
  68. };
  69. const float pos[] = {
  70. -1.0f, -1.0f,
  71. 1.0f, -1.0f,
  72. -1.0f, 1.0f,
  73. 1.0f, 1.0f,
  74. };
  75. const float uv[] = {
  76. 0.0f, 0.0f,
  77. 1.0f, 0.0f,
  78. 0.0f, 1.0f,
  79. 1.0f, 1.0f,
  80. };
  81. const float* color0 = genColor();
  82. const float* color1 = genColor();
  83. glUseProgram(mGradPgm);
  84. glVertexAttribPointer(mPosAttribLoc, 2, GL_FLOAT, GL_FALSE, 0, pos);
  85. glVertexAttribPointer(mUVAttribLoc, 2, GL_FLOAT, GL_FALSE, 0, uv);
  86. glEnableVertexAttribArray(mPosAttribLoc);
  87. glEnableVertexAttribArray(mUVAttribLoc);
  88. float invDitherKernelSize = 1.0f / float(GLHelper::DITHER_KERNEL_SIZE);
  89. float invDitherKernelSizeSq = invDitherKernelSize * invDitherKernelSize;
  90. glUniformMatrix4fv(mObjToNdcUniformLoc, 1, GL_FALSE, identity);
  91. glUniformMatrix4fv(mUVToInterpUniformLoc, 1, GL_FALSE, identity);
  92. glUniform1f(mInvDitherKernelSizeUniformLoc, invDitherKernelSize);
  93. glUniform1f(mInvDitherKernelSizeSqUniformLoc, invDitherKernelSizeSq);
  94. glUniform4fv(mColor0UniformLoc, 1, color0);
  95. glUniform4fv(mColor1UniformLoc, 1, color1);
  96. if (glGetError() != GL_NO_ERROR) {
  97. fprintf(stderr, "GL error! 0\n");
  98. }
  99. glActiveTexture(GL_TEXTURE0);
  100. glBindTexture(GL_TEXTURE_2D, mDitherTexName);
  101. if (glGetError() != GL_NO_ERROR) {
  102. fprintf(stderr, "GL error! 1\n");
  103. }
  104. glUniform1i(mDitherKernelSamplerLoc, 0);
  105. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  106. glDisableVertexAttribArray(mPosAttribLoc);
  107. glDisableVertexAttribArray(mUVAttribLoc);
  108. if (glGetError() != GL_NO_ERROR) {
  109. fprintf(stderr, "GL error! 2\n");
  110. }
  111. return true;
  112. }
  113. GLuint mGradPgm;
  114. GLuint mDitherTexName;
  115. GLuint mPosAttribLoc;
  116. GLuint mUVAttribLoc;
  117. GLuint mObjToNdcUniformLoc;
  118. GLuint mUVToInterpUniformLoc;
  119. GLuint mDitherKernelSamplerLoc;
  120. GLuint mInvDitherKernelSizeUniformLoc;
  121. GLuint mInvDitherKernelSizeSqUniformLoc;
  122. GLuint mColor0UniformLoc;
  123. GLuint mColor1UniformLoc;
  124. };
  125. Renderer* staticGradient() {
  126. class NoRenderer : public Renderer {
  127. virtual bool setUp(GLHelper* helper) {
  128. mIsFirstFrame = true;
  129. mGLHelper = helper;
  130. return mGradientRenderer.setUp(helper);
  131. }
  132. virtual void tearDown() {
  133. mGradientRenderer.tearDown();
  134. }
  135. virtual bool render(EGLSurface surface) {
  136. if (mIsFirstFrame) {
  137. bool result;
  138. mIsFirstFrame = false;
  139. result = mGLHelper->makeCurrent(surface);
  140. if (!result) {
  141. return false;
  142. }
  143. result = mGradientRenderer.drawGradient();
  144. if (!result) {
  145. return false;
  146. }
  147. result = mGLHelper->swapBuffers(surface);
  148. if (!result) {
  149. return false;
  150. }
  151. }
  152. return true;
  153. }
  154. bool mIsFirstFrame;
  155. GLHelper* mGLHelper;
  156. GradientRenderer mGradientRenderer;
  157. };
  158. return new NoRenderer;
  159. }
  160. } // namespace android