Advertisement
esepich

Untitled

Jul 2nd, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2013 Dan Ginsburg, Budirijanto Purnomo
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22.  
  23. //
  24. // Book: OpenGL(R) ES 3.0 Programming Guide, 2nd Edition
  25. // Authors: Dan Ginsburg, Budirijanto Purnomo, Dave Shreiner, Aaftab Munshi
  26. // ISBN-10: 0-321-93388-5
  27. // ISBN-13: 978-0-321-93388-1
  28. // Publisher: Addison-Wesley Professional
  29. // URLs: http://www.opengles-book.com
  30. // http://my.safaribooksonline.com/book/animation-and-3d/9780133440133
  31. //
  32.  
  33. // Hello_Triangle
  34. //
  35. // This is a simple example that draws a single triangle with
  36. // a minimal vertex/fragment shader. The purpose of this
  37. // example is to demonstrate the basic concepts of
  38. // OpenGL ES 3.0 rendering.
  39.  
  40. package com.openglesbook.hellotriangle;
  41.  
  42. import java.nio.ByteBuffer;
  43. import java.nio.ByteOrder;
  44. import java.nio.FloatBuffer;
  45.  
  46. import javax.microedition.khronos.egl.EGLConfig;
  47. import javax.microedition.khronos.opengles.GL10;
  48.  
  49. import android.content.Context;
  50. import android.opengl.GLES30;
  51. import android.opengl.GLSurfaceView;
  52. import android.util.Log;
  53.  
  54. public class HelloTriangleRenderer implements GLSurfaceView.Renderer
  55. {
  56.  
  57. ///
  58. // Constructor
  59. //
  60. public HelloTriangleRenderer ( Context context )
  61. {
  62. mVertices = ByteBuffer.allocateDirect ( mVerticesData.length * 4 )
  63. .order ( ByteOrder.nativeOrder() ).asFloatBuffer();
  64. mVertices.put ( mVerticesData ).position ( 0 );
  65. }
  66.  
  67. ///
  68. // Create a shader object, load the shader source, and
  69. // compile the shader.
  70. //
  71. private int LoadShader ( int type, String shaderSrc )
  72. {
  73. int shader;
  74. int[] compiled = new int[1];
  75.  
  76. // Create the shader object
  77. shader = GLES30.glCreateShader ( type );
  78.  
  79. if ( shader == 0 )
  80. {
  81. return 0;
  82. }
  83.  
  84. // Load the shader source
  85. GLES30.glShaderSource ( shader, shaderSrc );
  86.  
  87. // Compile the shader
  88. GLES30.glCompileShader ( shader );
  89.  
  90. // Check the compile status
  91. GLES30.glGetShaderiv ( shader, GLES30.GL_COMPILE_STATUS, compiled, 0 );
  92.  
  93. if ( compiled[0] == 0 )
  94. {
  95. Log.e ( TAG, GLES30.glGetShaderInfoLog ( shader ) );
  96. GLES30.glDeleteShader ( shader );
  97. return 0;
  98. }
  99.  
  100. return shader;
  101. }
  102.  
  103. ///
  104. // Initialize the shader and program object
  105. //
  106. public void onSurfaceCreated ( GL10 glUnused, EGLConfig config )
  107. {
  108. String vShaderStr =
  109. "#version 300 es \n"
  110. + "in vec4 vPosition; \n"
  111. + "void main() \n"
  112. + "{ \n"
  113. + " gl_Position = vPosition; \n"
  114. + "} \n";
  115.  
  116. String fShaderStr =
  117. "#version 300 es \n"
  118. + "precision mediump float; \n"
  119. + "out vec4 fragColor; \n"
  120. + "void main() \n"
  121. + "{ \n"
  122. + " fragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 ); \n"
  123. + "} \n";
  124.  
  125. int vertexShader;
  126. int fragmentShader;
  127. int programObject;
  128. int[] linked = new int[1];
  129.  
  130. // Load the vertex/fragment shaders
  131. vertexShader = LoadShader ( GLES30.GL_VERTEX_SHADER, vShaderStr );
  132. fragmentShader = LoadShader ( GLES30.GL_FRAGMENT_SHADER, fShaderStr );
  133.  
  134. // Create the program object
  135. programObject = GLES30.glCreateProgram();
  136.  
  137. if ( programObject == 0 )
  138. {
  139. return;
  140. }
  141.  
  142. GLES30.glAttachShader ( programObject, vertexShader );
  143. GLES30.glAttachShader ( programObject, fragmentShader );
  144.  
  145. // Bind vPosition to attribute 0
  146. GLES30.glBindAttribLocation ( programObject, 0, "vPosition" );
  147.  
  148. // Link the program
  149. GLES30.glLinkProgram ( programObject );
  150.  
  151. // Check the link status
  152. GLES30.glGetProgramiv ( programObject, GLES30.GL_LINK_STATUS, linked, 0 );
  153.  
  154. if ( linked[0] == 0 )
  155. {
  156. Log.e ( TAG, "Error linking program:" );
  157. Log.e ( TAG, GLES30.glGetProgramInfoLog ( programObject ) );
  158. GLES30.glDeleteProgram ( programObject );
  159. return;
  160. }
  161.  
  162. // Store the program object
  163. mProgramObject = programObject;
  164.  
  165. GLES30.glClearColor ( 1.0f, 1.0f, 1.0f, 0.0f );
  166. }
  167.  
  168. // /
  169. // Draw a triangle using the shader pair created in onSurfaceCreated()
  170. //
  171. public void onDrawFrame ( GL10 glUnused )
  172. {
  173. // Set the viewport
  174. GLES30.glViewport ( 0, 0, mWidth, mHeight );
  175.  
  176. // Clear the color buffer
  177. GLES30.glClear ( GLES30.GL_COLOR_BUFFER_BIT );
  178.  
  179. // Use the program object
  180. GLES30.glUseProgram ( mProgramObject );
  181.  
  182. // Load the vertex data
  183. GLES30.glVertexAttribPointer ( 0, 3, GLES30.GL_FLOAT, false, 0, mVertices );
  184. GLES30.glEnableVertexAttribArray ( 0 );
  185.  
  186. GLES30.glDrawArrays ( GLES30.GL_TRIANGLES, 0, 3 );
  187. }
  188.  
  189. // /
  190. // Handle surface changes
  191. //
  192. public void onSurfaceChanged ( GL10 glUnused, int width, int height )
  193. {
  194. mWidth = width;
  195. mHeight = height;
  196. }
  197.  
  198. // Member variables
  199. private int mProgramObject;
  200. private int mWidth;
  201. private int mHeight;
  202. private FloatBuffer mVertices;
  203. private static String TAG = "HelloTriangleRenderer";
  204.  
  205. private final float[] mVerticesData =
  206. { 0.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f };
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement