GLES2.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * GLES2Renderer.cpp
  3. * @author jarnoh
  4. *
  5. */
  6. #include "GLES2.h"
  7. #include "TileInterfaces.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include "utility.h"
  12. #if defined(_WIN32) && !defined(__SYMBIAN32__)
  13. #include <windows.h>
  14. #include <mmsystem.h>
  15. #endif
  16. extern float matrixProjection[16];
  17. float matrixProjection[16]=
  18. {
  19. 2/65536.0f, 0, 0, 0,
  20. //0, -2/(640.0f/360.0f*65536.0f), 0, 0,
  21. 0, -2/(1.5f*65536.0f), 0, 0,
  22. 0, 0, 1, 0,
  23. -1, 1, 0, 1,
  24. };
  25. const int vboStride = 2 * sizeof(GLfloat);
  26. const GLfloat vboVertices[] = {
  27. -1, -1,
  28. 1, -1,
  29. 1, 1,
  30. -1, 1,
  31. };
  32. const char *glslPixel =
  33. "uniform sampler2D texture;"
  34. "varying mediump vec2 uv;"
  35. "varying mediump float alpha;"
  36. "void main(void)"
  37. "{"
  38. "gl_FragColor = texture2D(texture,uv)*vec4(1.0,1.0,1.0,alpha);"
  39. "}";
  40. const char *glslVertex =
  41. "uniform mat4 m_projection;"
  42. "uniform vec4 size_tscale;"
  43. "uniform vec4 tpos_position;"
  44. "uniform vec2 angle_alpha;"
  45. "attribute vec4 vert;"
  46. "varying vec2 uv;"
  47. "varying float alpha;"
  48. "void main(void) { "
  49. "float a=angle_alpha.x*(3.1415*2.0/65536.0); "
  50. "vec2 rot = vec2(vert.x*cos(a) + vert.y*sin(a), -vert.x*sin(a) + vert.y*cos(a)); "
  51. "gl_Position = m_projection*vec4(tpos_position.zw+rot.xy*size_tscale.xy,0.0,1.0);"
  52. "uv = (vert.xy+vec2(1.0,1.0))*vec2(0.5,0.5) * size_tscale.zw + tpos_position.xy; alpha = angle_alpha.y; }";
  53. GameTexture *bind(TextureID id)
  54. {
  55. static TextureID prevId=(TextureID)-1;
  56. static GameTexture *t;
  57. if(id==prevId) return t;
  58. prevId=(TextureID)-1;
  59. t=&gameTextures[0];
  60. while(1)
  61. {
  62. if(t->id==TexEndOfList) return NULL;
  63. if(t->id==id)
  64. {
  65. glBindTexture(GL_TEXTURE_2D, t->glId);
  66. prevId=id;
  67. return t;
  68. }
  69. t++;
  70. }
  71. return NULL;
  72. }
  73. void load(TextureID id)
  74. {
  75. GameTexture *t=&gameTextures[0];
  76. while(1)
  77. {
  78. if(t->id==TexEndOfList) return;
  79. if(t->id==id)
  80. {
  81. glBindTexture(GL_TEXTURE_2D, t->id);
  82. char s[256];
  83. sprintf(s, "%s.pvr", t->name);
  84. t->glId=loadTexture(s);
  85. if(!t->tilesX) t->tilesX=1;
  86. if(!t->tilesY) t->tilesY=1;
  87. // glUniform4f(glGetUniformLocation(program, "tscale"), 1.0f/gt->tilesX, 1.0f/gt->tilesY, 0, 0);
  88. // glUniform4f(glGetUniformLocation(program, "tpos"), (t%gt->tilesX)/(float)gt->tilesX, (t/gt->tilesX%gt->tilesY)/(float)gt->tilesY, 0, 0);
  89. for(int i=0;i<16;i++) t->matrix[i]=0;
  90. t->matrix[0]=1.0f/t->tilesX;
  91. t->matrix[4*1+1]=1.0f/t->tilesY;
  92. t->matrix[4*2+2]=1.0f;
  93. t->matrix[4*3+3]=1.0f;
  94. //t->matrix[4*3+0]=(id%t->tilesX)/(float)t->tilesX;
  95. //t->matrix[4*3+1]=(id/t->tilesX%t->tilesY)/(float)t->tilesY;
  96. return;
  97. }
  98. t++;
  99. }
  100. }
  101. GLES2Renderer::GLES2Renderer()
  102. {
  103. previousMode=-1;
  104. for(int i=0;i<TexEndOfList;i++) load((TextureID)i);
  105. pixelShader = glCreateShader(GL_FRAGMENT_SHADER);
  106. glShaderSource(pixelShader, 1, (const char**)&glslPixel, NULL);
  107. //glShaderSource(pixelShader, 1, (const char**)&pszFragShader, NULL);//glslPixel
  108. glCompileShader(pixelShader);
  109. vertexShader = glCreateShader(GL_VERTEX_SHADER);
  110. glShaderSource(vertexShader, 1, (const char**)&glslVertex, NULL);
  111. glCompileShader(vertexShader);
  112. program = glCreateProgram();
  113. // Attach the fragment and vertex shaders to it
  114. glAttachShader(program, pixelShader);
  115. glAttachShader(program, vertexShader);
  116. glBindAttribLocation(program, 0, "vert");
  117. //glBindAttribLocation(program, 1, "uv");
  118. glLinkProgram(program);
  119. glUseProgram(program);
  120. glUniform1i(glGetUniformLocation(program, "texture"), 0);
  121. glUniformMatrix4fv(glGetUniformLocation(program, "m_projection"), 1, GL_FALSE, matrixProjection);
  122. glGenBuffers(1, &vbo);
  123. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  124. glBufferData(GL_ARRAY_BUFFER, 4 * vboStride, vboVertices, GL_STATIC_DRAW);
  125. glEnableVertexAttribArray(0);
  126. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, vboStride, 0);
  127. GLint bLinked;
  128. glGetShaderiv(pixelShader, GL_COMPILE_STATUS, &bLinked);
  129. glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &bLinked);
  130. glGetProgramiv(program, GL_LINK_STATUS, &bLinked);
  131. angleAlphaLocation = glGetUniformLocation(program, "angle_alpha");
  132. sizeTScaleLocation = glGetUniformLocation(program, "size_tscale");
  133. tposPositionLocation = glGetUniformLocation(program, "tpos_position");
  134. }
  135. GLES2Renderer::~GLES2Renderer()
  136. {
  137. }
  138. void GLES2Renderer::renderBackground( unsigned int index )
  139. {
  140. glClearColor(0,0,0, 1.0f);
  141. glClear(GL_COLOR_BUFFER_BIT);
  142. glDisable( GL_DEPTH_TEST );
  143. glEnable(GL_BLEND);
  144. }
  145. void GLES2Renderer::effectNotify( eTILEGAME_EFFECT effect, unsigned int arg1, unsigned int arg2 )
  146. {
  147. // printf("effectNotify %x\n", effect);
  148. switch(effect)
  149. {
  150. default:
  151. break;
  152. }
  153. }
  154. void GLES2Renderer::renderTile( int x, int y,
  155. int w, int h,
  156. int angle,
  157. int mode,
  158. unsigned int tileIndex, unsigned int arg )
  159. {
  160. TextureID texture=(TextureID)((tileIndex>>16)&0xff);
  161. //if(texture!=TexBackground) return;
  162. GameTexture *gt=bind(texture);
  163. if(!gt) return;
  164. if(mode!=previousMode)
  165. {
  166. if(mode==0)
  167. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  168. else
  169. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  170. previousMode=mode;
  171. }
  172. w>>=1;
  173. h>>=1;
  174. int t=(tileIndex&65535);
  175. glUniform2f(angleAlphaLocation, angle,1-((tileIndex>>24)&0xff)/255.0f);
  176. glUniform4f(sizeTScaleLocation, w, h, 1.0f/gt->tilesX, 1.0f/gt->tilesY);
  177. glUniform4f(tposPositionLocation,
  178. (t%gt->tilesX)/(float)gt->tilesX, (t/gt->tilesX%gt->tilesY)/(float)gt->tilesY,
  179. x+w, y+h );
  180. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  181. }
  182. void GLES2Renderer::renderForeground( unsigned int index )
  183. {
  184. return;
  185. }
  186. int GLES2Renderer::run( int fixedFrameTime16Bit )
  187. {
  188. return 1;
  189. }