Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. package cg.myapplication;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.opengl.GLSurfaceView;
  9.  
  10. import android.os.Bundle;
  11.  
  12. import android.view.WindowManager;
  13.  
  14. public class BouncySquareActivity extends Activity {
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState)
  18. {
  19. super.onCreate(savedInstanceState);
  20. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  21. GLSurfaceView view = new GLSurfaceView(this);
  22. view.setRenderer(new SquareRenderer(this.getApplicationContext()));
  23. setContentView(view);
  24. }
  25. }
  26.  
  27. package cg.myapplication;
  28.  
  29. /**
  30. * Created by P17-7 on 3/27/2017.
  31. */
  32. import javax.microedition.khronos.opengles.GL10;
  33. import javax.microedition.khronos.opengles.GL11;
  34.  
  35. import javax.microedition.khronos.egl.EGLConfig;
  36.  
  37. import android.opengl.GLSurfaceView;
  38.  
  39. import java.lang.Math;
  40.  
  41. import android.content.Context;
  42.  
  43. public class SquareRenderer implements GLSurfaceView.Renderer {
  44. private Square mSquare;
  45. private float mTransY;
  46. private Context context;
  47.  
  48. public SquareRenderer(Context context)
  49.  
  50. {
  51.  
  52. this.context = context;
  53.  
  54. this.mSquare = new Square();
  55.  
  56. }
  57.  
  58. public void onDrawFrame(GL10 gl) {
  59. //gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  60. //gl.glMatrixMode(GL10.GL_MODELVIEW);
  61. gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  62. gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  63. gl.glMatrixMode(GL11.GL_MODELVIEW);
  64. gl.glLoadIdentity();
  65. gl.glTranslatef(0.0f, (float) Math.sin(mTransY), -3.0f);
  66. gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
  67. mSquare.draw(gl);
  68.  
  69. gl.glLoadIdentity();
  70.  
  71. gl.glTranslatef((float)(Math.sin(mTransY)/2.0f),0.0f, -2.9f);
  72.  
  73. gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
  74.  
  75. mSquare.draw(gl);
  76.  
  77. mTransY += .075f;
  78.  
  79. /* mTransY += 0.075f;
  80. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  81. gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  82. mSquare.draw(gl); */
  83. }
  84.  
  85. public void onSurfaceChanged(GL10 gl, int width, int height) {
  86. gl.glViewport(0, 0, width, height);
  87. gl.glMatrixMode(GL10.GL_PROJECTION);
  88. gl.glLoadIdentity();
  89. float ratio = (float) width / height;
  90. gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
  91. int resid = R.drawable.hedly;
  92. mSquare.createTexture(gl, this.context, resid);
  93.  
  94. /*int resid1 = R.drawable.splash;
  95. mSquare.createTexture(gl, this.context, resid1);*/
  96. }
  97.  
  98. public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  99.  
  100. gl.glDisable(GL10.GL_DITHER);
  101. gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
  102. gl.glClearColor(0, 0, 0, 0);
  103. gl.glEnable(GL10.GL_CULL_FACE);
  104. gl.glShadeModel(GL10.GL_SMOOTH);
  105. gl.glEnable(GL10.GL_DEPTH_TEST);
  106. }
  107. }
  108.  
  109. package cg.myapplication;
  110.  
  111. /**
  112. * Created by P17-7 on 3/27/2017.
  113. */
  114.  
  115. import javax.microedition.khronos.opengles.GL10;
  116.  
  117. import javax.microedition.khronos.opengles.GL11;
  118.  
  119. import java.nio.ByteBuffer;
  120.  
  121. import java.nio.ByteOrder;
  122.  
  123. import java.nio.FloatBuffer;
  124.  
  125. import android.graphics.*;
  126.  
  127. import android.opengl.*;
  128.  
  129. import android.content.Context;
  130.  
  131.  
  132. public class Square {
  133.  
  134. private FloatBuffer mFVertexBuffer;
  135.  
  136. private ByteBuffer mColorBuffer;
  137.  
  138. private ByteBuffer mIndexBuffer;
  139.  
  140. private int[] textures = new int[1];
  141.  
  142. public FloatBuffer mTextureBuffer;
  143.  
  144.  
  145.  
  146. float[] textureCoords =
  147.  
  148. {
  149.  
  150. 0.0f, 0.0f,
  151.  
  152. 1.0f, 0.0f,
  153.  
  154. 0.0f, 1.0f,
  155.  
  156. 1.0f, 1.0f
  157.  
  158. };
  159.  
  160.  
  161. /*float[] textureCoords =
  162.  
  163. {
  164.  
  165. 0.0f , 0.0f,
  166.  
  167. 0.5f , 0.0f,
  168.  
  169. 0.0f , 0.5f,
  170.  
  171. 0.5f , 0.5f
  172.  
  173. };*/
  174.  
  175. /*float[] textureCoords =
  176.  
  177. {
  178.  
  179. 0.0f , 2.0f,
  180.  
  181. 2.0f , 2.0f,
  182.  
  183. 0.0f , 0.0f,
  184.  
  185. 2.0f , 0.0f
  186.  
  187. };*/
  188.  
  189.  
  190.  
  191. private float texIncrease=0.2f;
  192.  
  193.  
  194. public Square()
  195. {
  196.  
  197.  
  198.  
  199.  
  200. float vertices[] =
  201.  
  202. {
  203.  
  204. -1.0f, -1.0f,
  205.  
  206. 1.0f, -1.0f,
  207.  
  208. -1.0f, 1.0f,
  209.  
  210. 1.0f, 1.0f
  211.  
  212. };
  213.  
  214.  
  215.  
  216. /*float vertices[] =
  217.  
  218. {
  219.  
  220. -1.0f, -0.7f,
  221.  
  222. 1.0f, -0.30f,
  223.  
  224. -1.0f, 0.70f,
  225.  
  226. 1.0f, 0.30f,
  227.  
  228. };*/
  229. float squareColorsYMCA[] =
  230.  
  231. {
  232.  
  233. 1.0f, 1.0f, 0.0f, 1.0f,
  234.  
  235. 0.0f, 1.0f, 1.0f, 1.0f,
  236.  
  237. 0.0f, 0.0f, 0.0f, 1.0f,
  238.  
  239. 1.0f, 0.0f, 1.0f, 1.0f
  240.  
  241. };
  242.  
  243. float squareColorsRGBA[] =
  244.  
  245. {
  246.  
  247. 1.0f, 0.0f, 0.0f, 1.0f,
  248.  
  249. 0.0f, 1.0f, 0.0f, 1.0f,
  250.  
  251. 0.0f, 0.0f, 1.0f, 1.0f,
  252.  
  253. 1.0f, 1.0f, 1.0f, 1.0f
  254.  
  255. };
  256.  
  257. byte maxColor=(byte)255;
  258.  
  259. byte colors[] =
  260.  
  261. {
  262.  
  263. 0, 0, 0, maxColor,
  264.  
  265. maxColor, 0, 0, maxColor,
  266.  
  267. 0, 0, 0, maxColor,
  268.  
  269. maxColor, 0, 0, maxColor,
  270.  
  271. };
  272.  
  273. byte indices[] =
  274.  
  275. {
  276.  
  277. 0, 3, 1,
  278.  
  279. 0, 2, 3
  280.  
  281. };
  282.  
  283. ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
  284.  
  285. vbb.order(ByteOrder.nativeOrder());
  286.  
  287. mFVertexBuffer = vbb.asFloatBuffer();
  288.  
  289. mFVertexBuffer.put(vertices);
  290.  
  291. mFVertexBuffer.position(0);
  292.  
  293. ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length * 4);
  294.  
  295. tbb.order(ByteOrder.nativeOrder());
  296.  
  297. mTextureBuffer = tbb.asFloatBuffer();
  298.  
  299. mTextureBuffer.put(textureCoords);
  300.  
  301. mTextureBuffer.position(0);
  302.  
  303. mColorBuffer = ByteBuffer.allocateDirect(colors.length);
  304.  
  305. mColorBuffer.put(colors);
  306.  
  307. mColorBuffer.position(0);
  308.  
  309. mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
  310.  
  311. mIndexBuffer.put(indices);
  312.  
  313. mIndexBuffer.position(0);
  314. }
  315.  
  316. public void createTexture(GL10 gl, Context contextRegf, int resource)
  317. {
  318. Bitmap image = BitmapFactory.decodeResource(contextRegf.getResources(), resource);
  319. gl.glGenTextures(1, textures, 0);
  320. gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
  321. GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0);
  322. gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
  323. gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
  324. image.recycle();
  325. }
  326.  
  327. public void draw(GL10 gl)
  328. {
  329.  
  330. gl.glFrontFace(GL11.GL_CW);
  331. gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer);
  332. //gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);
  333. gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, mIndexBuffer);
  334. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  335. gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  336. gl.glEnable(GL10.GL_TEXTURE_2D);
  337. gl.glEnable(GL10.GL_BLEND);
  338. gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
  339. // gl.glBlendFunc(GL10.GL_ONE, GL10.GL_SRC_COLOR);
  340. gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
  341. gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
  342. gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  343.  
  344. gl.glEnable(GL10.GL_TEXTURE_2D);
  345.  
  346. // gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexture0);
  347.  
  348.  
  349. textureCoords[0]+=texIncrease;
  350.  
  351. textureCoords[1]+=texIncrease;
  352.  
  353. textureCoords[2]+=texIncrease;
  354.  
  355. textureCoords[3]+=texIncrease;
  356.  
  357. textureCoords[4]+=texIncrease;
  358.  
  359. textureCoords[5]+=texIncrease;
  360.  
  361. textureCoords[6]+=texIncrease;
  362.  
  363. textureCoords[7]+=texIncrease;
  364.  
  365. mTextureBuffer.put(textureCoords);
  366.  
  367. mTextureBuffer.position(0);
  368. }
  369. /* public void draw(GL10 gl) {
  370. gl.glEnable(GL10.GL_TEXTURE_2D);
  371.  
  372. gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexture0);
  373.  
  374. gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  375. gl.glFrontFace(GL11.GL_CW);
  376.  
  377. gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer);
  378.  
  379. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  380.  
  381. gl.glColorPointer(4, GL11.GL_FLOAT, 0, mColorBuffer);
  382.  
  383. gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  384. }*/
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement