Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. glActiveTexture(GL_TEXTURE1);
  2. glUniform1i(uTextureLocation, 1);
  3.  
  4. public class RendererClass implements Renderer {
  5.  
  6. Context context;
  7.  
  8. FloatBuffer verticesInBuffer;
  9.  
  10. int aPositionLocation;
  11. int aTextureLocation;
  12. int uTextureLocation;
  13.  
  14. int program;
  15.  
  16. public RendererClass(Context context){
  17. this.context = context;
  18. }
  19.  
  20. @Override
  21. public void onSurfaceCreated(GL10 arg0, EGLConfig config) {
  22.  
  23. GLES20.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
  24.  
  25. float[] vertices = {
  26.  
  27. -0.5f, 0.5f, 0.5f, 0.5f,
  28. -1.0f, 0.0f, 0.0f, 0.0f,
  29. 0.0f, 0.0f, 1.0f, 0.0f,
  30. 0.0f, 1.0f, 1.0f, 1.0f,
  31. -1.0f, 1.0f, 0.0f, 1.0f,
  32. -1.0f, 0.0f, 0.0f, 0.0f
  33.  
  34. };
  35.  
  36. verticesInBuffer = ByteBuffer.allocateDirect(vertices.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer().put(vertices);
  37.  
  38. String vss = "attribute vec4 a_Position;" +
  39. "attribute vec2 a_Texture;" +
  40. "varying vec2 v_Texture;" +
  41. "void main(){" +
  42. " v_Texture = a_Texture;" +
  43. " gl_Position = a_Position;" +
  44. "}";
  45.  
  46. String fss = "precision mediump float;" +
  47. "varying vec2 v_Texture;" +
  48. "uniform sampler2D u_Texture;" +
  49. "void main(){" +
  50. " gl_FragColor = texture2D(u_Texture, v_Texture);" +
  51. "}";
  52.  
  53. int vs = glCreateShader(GL_VERTEX_SHADER);
  54. int fs = glCreateShader(GL_FRAGMENT_SHADER);
  55.  
  56. glShaderSource(vs, vss);
  57. glShaderSource(fs, fss);
  58.  
  59. glCompileShader(vs);
  60. glCompileShader(fs);
  61.  
  62. program = glCreateProgram();
  63.  
  64. glAttachShader(program, vs);
  65. glAttachShader(program, fs);
  66.  
  67. glLinkProgram(program);
  68.  
  69. aPositionLocation = glGetAttribLocation(program, "a_Position");
  70.  
  71.  
  72. // ***** Texture stuff starts here </</</</
  73.  
  74.  
  75. // Fase 1
  76. glActiveTexture(GL_TEXTURE0);
  77.  
  78. int[] genTextures = new int[1];
  79. glGenTextures(1, genTextures, 0);
  80.  
  81. glBindTexture(GL_TEXTURE_2D, genTextures[0]);
  82.  
  83. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  84. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  85.  
  86.  
  87. // Fase 2
  88. BitmapFactory.Options options = new BitmapFactory.Options();
  89. options.inScaled = false;
  90.  
  91. Bitmap bitmap1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.res_for_test_1, options);
  92.  
  93.  
  94. // Fase 3
  95. texImage2D(GL_TEXTURE_2D, 0, bitmap1, 0);
  96. glGenerateMipmap(GL_TEXTURE_2D);
  97.  
  98.  
  99. // Fase 4
  100. aTextureLocation = glGetAttribLocation(program, "a_Texture");
  101. uTextureLocation = glGetUniformLocation(program, "u_Texture");
  102.  
  103. glUniform1i(uTextureLocation, 0);
  104.  
  105. verticesInBuffer.position(2);
  106. glEnableVertexAttribArray(aTextureLocation);
  107. glVertexAttribPointer(aTextureLocation, 2, GL_FLOAT, false, 16, verticesInBuffer);
  108.  
  109.  
  110. // ***** Texture stuff ends here </</</</
  111.  
  112.  
  113. }
  114.  
  115. @Override
  116. public void onSurfaceChanged(GL10 arg0, int width, int height) {
  117.  
  118. GLES20.glViewport(0, 0, width, height);
  119.  
  120. }
  121.  
  122. @Override
  123. public void onDrawFrame(GL10 glUnused) {
  124.  
  125. GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
  126.  
  127. glUseProgram(program);
  128.  
  129. verticesInBuffer.position(0);
  130. glEnableVertexAttribArray(aPositionLocation);
  131. glVertexAttribPointer(aPositionLocation, 2, GL_FLOAT, false, 16, verticesInBuffer);
  132.  
  133. glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
  134.  
  135. }
  136.  
  137. }
  138.  
  139. GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + textureUnit);
  140. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);
  141. GLES20.glUniform1i(uniformID, textureUnit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement