Advertisement
Tavi33

EGIOC

Mar 24th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. package demo.com.cube;
  2.  
  3. import android.content.Context;
  4. import android.opengl.GLSurfaceView;
  5.  
  6. import java.nio.ByteBuffer;
  7. import java.nio.ByteOrder;
  8. import java.nio.FloatBuffer;
  9.  
  10. import javax.microedition.khronos.egl.EGLConfig;
  11. import javax.microedition.khronos.opengles.GL10;
  12.  
  13. /**
  14. * Created by octavian.salcianu on 3/17/2017.
  15. */
  16.  
  17. public class SquareRenderer implements GLSurfaceView.Renderer{
  18.  
  19. private Square square;
  20. private float mTransY;
  21. private float mAngle;
  22. private Context context;
  23.  
  24. public final static int SS_SUNLIGHT = GL10.GL_LIGHT0;
  25.  
  26. public SquareRenderer(Context context) {
  27. this.context = context;
  28. this.square = new Square();
  29.  
  30. }
  31.  
  32. @Override
  33. public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  34. //gl.glDisable(GL10.GL_DITHER);
  35. gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
  36. gl.glClearColor(256,256,256,0);
  37. gl.glEnable(GL10.GL_CULL_FACE);
  38.  
  39. gl.glShadeModel(GL10.GL_SMOOTH);
  40. gl.glEnable(GL10.GL_DEPTH_TEST);
  41.  
  42. int resid = R.drawable.hedly;
  43. square.createTexture(gl, this.context, resid);
  44. }
  45.  
  46. @Override
  47. public void onSurfaceChanged(GL10 gl, int width, int height) {
  48. gl.glViewport(0, 0, width, height);
  49. gl.glMatrixMode(GL10.GL_PROJECTION);
  50. gl.glLoadIdentity();
  51. float ratio = (float) width / height;
  52. gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
  53. }
  54.  
  55. /*
  56.  
  57. private void initLighting(GL10 gl) {
  58. float[] green = {1.0f, 1.0f, 1.0f, 1.0f};
  59. float[] position = {0.0f, 5.0f, 0.0f, 1.0f};
  60. float[] red = {1.0f, 0.0f, 0,0f, 1.0f};
  61.  
  62. gl.glLightfv(SS_SUNLIGHT, GL10.GL_POSITION, makeFloatBuffer(position));
  63. gl.glLightfv(SS_SUNLIGHT, GL10.GL_DIFFUSE, makeFloatBuffer(green));
  64.  
  65. //gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, makeFloatBuffer(red));
  66.  
  67. //gl.glLightfv(SS_SUNLIGHT,GL10.GL_SPECULAR, makeFloatBuffer(red));
  68.  
  69. //gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, makeFloatBuffer(green));
  70.  
  71. gl.glShadeModel(GL10.GL_SMOOTH);
  72. gl.glEnable(GL10.GL_LIGHTING);
  73. gl.glEnable(SS_SUNLIGHT);
  74. //gl.glEnable(GL10.GL_COLOR_MATERIAL);
  75. }
  76.  
  77. protected static FloatBuffer makeFloatBuffer(float[] array) {
  78. ByteBuffer bb = ByteBuffer.allocateDirect(array.length*4);
  79. bb.order(ByteOrder.nativeOrder());
  80. FloatBuffer fb = bb.asFloatBuffer();
  81. fb.put(array);
  82. fb.position(0);
  83.  
  84. return fb;
  85.  
  86. }
  87. */
  88.  
  89. @Override
  90. public void onDrawFrame(GL10 gl) {
  91.  
  92. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  93. gl.glMatrixMode(GL10.GL_MODELVIEW);
  94. gl.glLoadIdentity();
  95. gl.glTranslatef(0.0f, (float) Math.sin(mTransY), -3.0f);
  96.  
  97. mTransY += 0.075f;
  98.  
  99. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  100. gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  101. square.draw(gl);
  102.  
  103.  
  104. }
  105. }
  106.  
  107.  
  108.  
  109. -----------
  110.  
  111. package demo.com.cube;
  112.  
  113. import android.content.Context;
  114. import android.graphics.Bitmap;
  115. import android.graphics.BitmapFactory;
  116. import android.opengl.GLUtils;
  117.  
  118. import java.nio.ByteBuffer;
  119. import java.nio.ByteOrder;
  120. import java.nio.FloatBuffer;
  121.  
  122. import javax.microedition.khronos.opengles.GL10;
  123. import javax.microedition.khronos.opengles.GL11;
  124.  
  125. /**
  126. * Created by octavian.salcianu on 3/17/2017.
  127. */
  128.  
  129. public class Square {
  130. private FloatBuffer mFVertexBuffer;
  131. private ByteBuffer mColorBuffer;
  132. private ByteBuffer mIndexBuffer;
  133.  
  134. public FloatBuffer mTextureBuffer;
  135.  
  136. float[] textureCoords = {
  137. 0.0f, 0.0f,
  138. 1.0f, 0.0f,
  139. 0.0f, 1.0f,
  140. 1.0f, 1.0f
  141. };
  142.  
  143. private int[] textures = new int[1];
  144.  
  145. public Square() {
  146. float vertices[] = {
  147. -1.0f, -1.0f,
  148. 1.0f, -1.0f,
  149. -1.0f, 1.0f,
  150. 1.0f, 1.0f
  151. };
  152.  
  153. byte maxColor=(byte)255;
  154. byte colors[] = {
  155. maxColor, maxColor, maxColor, 0,
  156. maxColor, maxColor, maxColor, 0,
  157. maxColor, maxColor, maxColor, 0,
  158. maxColor, maxColor, maxColor, 0,
  159. };
  160.  
  161. byte indices[] = {
  162. 0, 3, 1,
  163. 0, 2, 3
  164. };
  165.  
  166. ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
  167. vbb.order(ByteOrder.nativeOrder());
  168. mFVertexBuffer = vbb.asFloatBuffer();
  169. mFVertexBuffer.put(vertices);
  170. mFVertexBuffer.position(0);
  171. mColorBuffer = ByteBuffer.allocateDirect(colors.length);
  172. mColorBuffer.put(colors);
  173. mColorBuffer.position(0);
  174. mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
  175. mIndexBuffer.put(indices);
  176. mIndexBuffer.position(0);
  177.  
  178. ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length * 4);
  179. tbb.order(ByteOrder.nativeOrder());
  180. mTextureBuffer = tbb.asFloatBuffer();
  181. mTextureBuffer.put(textureCoords);
  182. mTextureBuffer.position(0);
  183. }
  184.  
  185. public void draw(GL10 gl) {
  186. gl.glFrontFace(GL11.GL_CW);
  187. gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer);
  188. //gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);
  189. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  190. gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  191. gl.glEnable(GL10.GL_TEXTURE_2D);
  192.  
  193. gl.glEnable(GL10.GL_BLEND);
  194. gl.glBlendFunc(GL10.GL_ONE, GL10.GL_SRC_COLOR);
  195.  
  196. gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
  197. gl.glTexCoordPointer(2, GL10.GL_FLOAT,0, mTextureBuffer);
  198.  
  199. gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  200.  
  201. gl.glDrawElements(gl.GL_TRIANGLES, 6, gl.GL_UNSIGNED_BYTE, mIndexBuffer);
  202. }
  203.  
  204. public void createTexture(GL10 gl, Context contextRegf, int resource) {
  205. Bitmap image = BitmapFactory.decodeResource(contextRegf.getResources(), resource);
  206. gl.glGenTextures(1, textures, 0);
  207. gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
  208. GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0);
  209.  
  210. gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
  211. gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
  212. image.recycle();
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement