Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. public class CurlRenderer implements GLSurfaceView.Renderer {
  2.  
  3. // Constant for requesting right page rect.
  4. public static final int PAGE = 1;
  5. // Set to true for checking quickly how perspective projection looks.
  6. private static final boolean USE_PERSPECTIVE_PROJECTION = false;
  7. // Background fill color.
  8. private int mBackgroundColor;
  9. // Curl meshes used for static and dynamic rendering.
  10. private CurlMesh mCurlMesh;
  11. private RectF mMargins = new RectF();
  12. private CurlRenderer.Observer mObserver;
  13. // Page rectangles.
  14. private RectF mPageRect;
  15. // View mode.
  16. // Screen size.
  17. private int mViewportWidth, mViewportHeight;
  18. // Rect for render area.
  19. private RectF mViewRect = new RectF();
  20. private boolean first = true;
  21. int[] fb, renderTex;
  22. int texW = 300;
  23. int texH = 256;
  24. IntBuffer texBuffer;
  25. int[] buf = new int[texW * texH];
  26. GL11ExtensionPack gl11ep ;
  27. /**
  28. * Basic constructor.
  29. */
  30. public CurlRenderer(CurlRenderer.Observer observer) {
  31. mObserver = observer;
  32. mCurlMesh = new CurlMesh(0);
  33. mPageRect = new RectF();
  34. }
  35.  
  36. /**
  37. * Adds CurlMesh to this renderer.
  38. */
  39. public synchronized void addCurlMesh(CurlMesh mesh) {
  40. mCurlMesh = mesh;
  41. }
  42.  
  43. /**
  44. * Returns rect reserved for left or right page. Value page should be
  45. * PAGE_LEFT or PAGE_RIGHT.
  46. */
  47. public RectF getPageRect(int page) {
  48. if (page == PAGE) {
  49. return mPageRect;
  50. }
  51. return null;
  52. }
  53. public void setup(GL10 gl){
  54. fb = new int[1];
  55. renderTex = new int[1];
  56. // generate
  57. ((GL11ExtensionPack)gl).glGenFramebuffersOES(1, fb, 0);
  58. gl.glEnable(GL10.GL_TEXTURE_2D);
  59. gl.glGenTextures(1, renderTex, 0);// generate texture
  60. gl.glBindTexture(GL10.GL_TEXTURE_2D, renderTex[0]);
  61. gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
  62. gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
  63. gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
  64. gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
  65. // texBuffer = ByteBuffer.allocateDirect(buf.length*4).order(ByteOrder.nativeOrder()).asIntBuffer();
  66. // gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,GL10.GL_MODULATE);
  67. gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, texW, texH, 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_SHORT_4_4_4_4, null);
  68. gl.glDisable(GL10.GL_TEXTURE_2D);
  69. }
  70. boolean RenderStart(GL10 gl){
  71. // Bind the framebuffer
  72. ((GL11ExtensionPack)gl).glBindFramebufferOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, fb[0]);
  73.  
  74. // specify texture as color attachment
  75. ((GL11ExtensionPack)gl).glFramebufferTexture2DOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, GL11ExtensionPack.GL_COLOR_ATTACHMENT0_OES, GL10.GL_TEXTURE_2D, renderTex[0], 0);
  76.  
  77.  
  78. int error = gl.glGetError();
  79. if (error != GL10.GL_NO_ERROR) {
  80. Log.d("err", "Background Load GLError: " + error+" ");
  81. }
  82. int status = ((GL11ExtensionPack)gl).glCheckFramebufferStatusOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES);
  83. if (status != GL11ExtensionPack.GL_FRAMEBUFFER_COMPLETE_OES)
  84. {
  85. Log.d("err", "Background Load GLError: " + status+" ");;
  86. return true;
  87. }
  88. gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  89. return true;
  90. }
  91. void RenderEnd(GL10 gl){
  92. ((GL11ExtensionPack)gl).glBindFramebufferOES(GL11ExtensionPack.GL_FRAMEBUFFER_OES, 0);
  93.  
  94. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  95.  
  96. gl.glEnable(GL10.GL_TEXTURE_2D);
  97. gl.glBindTexture(GL10.GL_TEXTURE_2D, renderTex[0]);
  98. gl.glColor4f(1,1,1,1);
  99. gl.glDisable(GL10.GL_TEXTURE_2D);
  100. }
  101.  
  102. @Override
  103. public synchronized void onDrawFrame(GL10 gl) {
  104. if(first){
  105. int h = GLES20.glGetError();
  106. this.setup(gl);
  107. if(h!=0){
  108. Log.d("ERROR", "ERROR Happend"+h+"");
  109. }
  110. first = false;
  111. }
  112. mObserver.onDrawFrame();
  113. //glClearColor miad rangi ke maa entekhaab kardim ro tooye carde Graphic register mikone
  114. gl.glClearColor(Color.red(mBackgroundColor) / 255f,
  115. Color.green(mBackgroundColor) / 255f,
  116. Color.blue(mBackgroundColor) / 255f,
  117. Color.alpha(mBackgroundColor) / 255f);
  118. //glClear miad oon rangi ke bala register karde boodim ro dige az buffer paak mikone
  119. gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  120.  
  121. //miad matris ro be MabdaEsh barmigardoone, ke bAd baraye glRotate va glTranslate moshkeli ijaad nashe
  122. //chon maa asle jaabejaa kardan hamoon baraye safhe, baste be makaane avalieye
  123. // kaaghazemoon hast, na oon makani ke dar haale hazer gharaar dare
  124. gl.glLoadIdentity();
  125.  
  126. if (USE_PERSPECTIVE_PROJECTION) {
  127. gl.glTranslatef(0, 0, -6f);
  128. }
  129.  
  130.  
  131. RenderStart(gl);
  132. mCurlMesh.onDrawFrame(gl);
  133. RenderEnd(gl);
  134. mCurlMesh.onDrawFrame(gl);
  135.  
  136. }
  137.  
  138. @Override
  139. public void onSurfaceChanged(GL10 gl, int width, int height) {
  140. gl.glViewport(0, 0, width, height);
  141. mViewportWidth = width;
  142. mViewportHeight = height;
  143.  
  144. float ratio = (float) width / height;
  145. mViewRect.top = 1.0f;
  146. mViewRect.bottom = -1.0f;
  147. mViewRect.left = -ratio;
  148. mViewRect.right = ratio;
  149. updatePageRects();
  150.  
  151. gl.glMatrixMode(GL10.GL_PROJECTION);
  152. gl.glLoadIdentity();
  153. if (USE_PERSPECTIVE_PROJECTION) {
  154. GLU.gluPerspective(gl, 20f, (float) width / height, .1f, 100f);
  155. } else {
  156. GLU.gluOrtho2D(gl, mViewRect.left, mViewRect.right,
  157. mViewRect.bottom, mViewRect.top);
  158. }
  159.  
  160. gl.glMatrixMode(GL10.GL_MODELVIEW);
  161. gl.glLoadIdentity();
  162. }
  163.  
  164. @Override
  165. public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  166. // mCurlMesh.setup(gl);
  167. gl.glClearColor(0f, 0f, 0f, 1f);
  168. gl.glShadeModel(GL10.GL_SMOOTH);
  169. gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
  170. gl.glHint(GL10.GL_LINE_SMOOTH_HINT, GL10.GL_NICEST);
  171. //gl.glHint(GL10.GL_POLYGON_SMOOTH_HINT, GL10.GL_NICEST);
  172. gl.glEnable(GL10.GL_LINE_SMOOTH);
  173. gl.glDisable(GL10.GL_DEPTH_TEST);
  174. gl.glDisable(GL10.GL_CULL_FACE);
  175. }
  176.  
  177.  
  178. /**
  179. * Change background/clear color.
  180. */
  181. public void setBackgroundColor(int color) {
  182. mBackgroundColor = color;
  183. }
  184.  
  185. /**
  186. * Set margins or padding. Note: margins are proportional. Meaning a value
  187. * of .1f will produce a 10% margin.
  188. */
  189. public synchronized void setMargins(float left, float top, float right,
  190. float bottom) {
  191. mMargins.left = left;
  192. mMargins.top = top;
  193. mMargins.right = right;
  194. mMargins.bottom = bottom;
  195. updatePageRects();
  196. }
  197. /**
  198. * Translates screen coordinates into view coordinates.
  199. * mokhtassate ye noghte (masalan pointer Position) roye safhe ro, be moAdele mokhtasaatesh
  200. * rooye CurlView Tabdil mikene
  201. */
  202. public void translate(PointF pt) {
  203. pt.x = mViewRect.left + (mViewRect.width() * pt.x / mViewportWidth);
  204. pt.y = mViewRect.top - (-mViewRect.height() * pt.y / mViewportHeight);
  205. }
  206.  
  207. /**
  208. * Recalculates page rectangles.
  209. */
  210. private void updatePageRects() {
  211. if (mViewRect.width() == 0 || mViewRect.height() == 0) {
  212. return;
  213. }
  214. /**
  215. * @ TODO inja daghighan hamnoon kaari ke mikham, yAni size dadan be Page ro anjaam mide
  216. * mpageRect... khode meshe va mViewRect view E layout
  217. */
  218. mPageRect.set(mViewRect);
  219. mPageRect.left += mViewRect.width() * mMargins.left;
  220. mPageRect.right -= mViewRect.width() * mMargins.right;
  221. mPageRect.top += mViewRect.height() * mMargins.top;
  222. mPageRect.bottom -= mViewRect.height() * mMargins.bottom;
  223.  
  224. int bitmapW = (int) ((mPageRect.width() * mViewportWidth) / mViewRect.width());
  225. int bitmapH = (int) ((mPageRect.height() * mViewportHeight) / mViewRect.height());
  226. mObserver.onPageSizeChanged(bitmapW, bitmapH);
  227.  
  228. }
  229.  
  230. /**
  231. * Observer for waiting render engine/state updates.
  232. */
  233. public interface Observer {
  234. /**
  235. * Called from onDrawFrame called before rendering is started. This is
  236. * intended to be used for animation purposes.
  237. */
  238. public void onDrawFrame();
  239.  
  240. /**
  241. * Called once page size is changed. Width and height tell the page size
  242. * in pixels making it possible to update textures accordingly.
  243. */
  244. public void onPageSizeChanged(int width, int height);
  245.  
  246. }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement