Advertisement
Guest User

Untitled

a guest
Jan 26th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. private boolean drawWallpaperWithOpenGL(SurfaceHolder sh, int w, int h, int left, int top) {
  2. if (!initGL(sh)) return false;
  3.  
  4. final float right = left + mBackgroundWidth;
  5. final float bottom = top + mBackgroundHeight;
  6.  
  7. final Rect frame = sh.getSurfaceFrame();
  8.  
  9. final Matrix4f ortho = new Matrix4f();
  10. ortho.loadOrtho(0.0f, frame.width(), frame.height(), 0.0f, -1.0f, 1.0f);
  11.  
  12. final FloatBuffer triangleVertices = createMesh(left, top, right, bottom);
  13.  
  14. final int texture = loadTexture(mBackground);
  15. final int program = buildProgram(sSimpleVS, sSimpleFS);
  16.  
  17. final int attribPosition = glGetAttribLocation(program, "position");
  18. final int attribTexCoords = glGetAttribLocation(program, "texCoords");
  19. final int uniformTexture = glGetUniformLocation(program, "texture");
  20. final int uniformProjection = glGetUniformLocation(program, "projection");
  21.  
  22. checkGlError();
  23.  
  24. glViewport(0, 0, frame.width(), frame.height());
  25. glBindTexture(GL_TEXTURE_2D, texture);
  26.  
  27. glUseProgram(program);
  28. glEnableVertexAttribArray(attribPosition);
  29.  
  30. glEnableVertexAttribArray(attribTexCoords);
  31. glUniform1i(uniformTexture, 0);
  32. glUniformMatrix4fv(uniformProjection, 1, false, ortho.getArray(), 0);
  33.  
  34. checkGlError();
  35.  
  36. if (w < 0 || h < 0) {
  37. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  38. glClear(GL_COLOR_BUFFER_BIT);
  39. }
  40.  
  41. // drawQuad
  42. triangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
  43. glVertexAttribPointer(attribPosition, 3, GL_FLOAT, false,
  44. TRIANGLE_VERTICES_DATA_STRIDE_BYTES, triangleVertices);
  45.  
  46. triangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
  47. glVertexAttribPointer(attribTexCoords, 3, GL_FLOAT, false,
  48. TRIANGLE_VERTICES_DATA_STRIDE_BYTES, triangleVertices);
  49.  
  50. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  51.  
  52. if (!mEgl.eglSwapBuffers(mEglDisplay, mEglSurface)) {
  53. throw new RuntimeException("Cannot swap buffers");
  54. }
  55. checkEglError();
  56.  
  57. finishGL();
  58.  
  59. return true;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement