Advertisement
Guest User

lab6cg

a guest
Mar 27th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.07 KB | None | 0 0
  1. package cg.bouncysquare;
  2.  
  3. import android.opengl.GLSurfaceView;
  4.  
  5. import javax.microedition.khronos.egl.EGLConfig;
  6. import javax.microedition.khronos.opengles.GL10;
  7. import javax.microedition.khronos.opengles.GL11;
  8. import android.content.Context;
  9.  
  10. /**
  11.  * Created by P17-10 on 3/20/2017.
  12.  */
  13. public class SquareRenderer implements GLSurfaceView.Renderer{
  14.     private final Context context;
  15.     private Square mSquare;
  16.     private Square mSquare2;
  17.     private float mTransY;
  18.  
  19.  
  20.  
  21.     public SquareRenderer(Context context) {
  22.         this.context = context;
  23.         this.mSquare = new Square(1);
  24.         this.mSquare2 = new Square(2);
  25.     }
  26.  
  27.     @Override
  28.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  29.         int resid = R.drawable.hedly;
  30.         int resid2 = R.drawable.goldengate;
  31.         mSquare.createTexture(gl, this.context, resid);
  32.         mSquare2.createTexture(gl, this.context, resid2);
  33.         gl.glDisable(GL10.GL_DITHER);
  34.         gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
  35.  
  36.         gl.glClearColor(0, 0, 0, 0);
  37.         gl.glEnable(GL10.GL_CULL_FACE);
  38.  
  39.         gl.glShadeModel(GL10.GL_SMOOTH);
  40.  
  41.         gl.glEnable(GL10.GL_DEPTH_TEST);
  42.     }
  43.  
  44.     @Override
  45.     public void onSurfaceChanged(GL10 gl, int width, int height) {
  46.         gl.glViewport(0, 0, width, height);
  47.         gl.glMatrixMode(GL10.GL_PROJECTION);
  48.  
  49.         gl.glLoadIdentity();
  50.         float ratio = (float) width / height;
  51.  
  52.         gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
  53.     }
  54.  
  55.     @Override
  56.     public void onDrawFrame(GL10 gl) {
  57.         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  58.  
  59.         gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  60.         gl.glEnable(GL10.GL_BLEND);
  61.  
  62.  
  63.         gl.glMatrixMode(GL11.GL_MODELVIEW);
  64.         gl.glLoadIdentity();
  65.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  66.  
  67.         //gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  68.  
  69.         gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -3.0f);
  70.  
  71.         gl.glColor4f(1.0f, 1.0f, 1.0f, 0.75f);
  72.        // gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
  73.         gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
  74.         mSquare.draw(gl);
  75.         mTransY += .075f;
  76.         gl.glLoadIdentity();
  77.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  78.  
  79.         //gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  80.  
  81.         gl.glTranslatef((float)(Math.sin(mTransY)/2.0f),0.0f, -2.9f);
  82.  
  83.         gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  84.  
  85.         mSquare2.draw(gl);
  86.  
  87.     }
  88. }
  89.  
  90.  
  91. ****
  92.  
  93.  
  94. package cg.bouncysquare;
  95.  
  96. import java.nio.ByteBuffer;
  97. import java.nio.ByteOrder;
  98. import java.nio.FloatBuffer;
  99. import android.graphics.*;
  100. import android.content.Context;
  101.  
  102. import android.opengl.*;
  103.  
  104. import javax.microedition.khronos.opengles.GL10;
  105. import javax.microedition.khronos.opengles.GL11;
  106.  
  107. /**
  108.  * Created by P17-10 on 3/20/2017.
  109.  */
  110. public class Square {
  111.  
  112.     private FloatBuffer mFVertexBuffer;
  113.  
  114.     private FloatBuffer mColorBuffer;
  115.  
  116.     private ByteBuffer mIndexBuffer;
  117.     public FloatBuffer mTextureBuffer;
  118.     private int[] textures = new int[1];
  119.  
  120.     public Square(int sw) {
  121.         float squareColorsYMCA[] =
  122.  
  123.                 {
  124.  
  125.                         1.0f, 1.0f, 0.0f, 1.0f,
  126.  
  127.                         0.0f, 1.0f, 1.0f, 1.0f,
  128.  
  129.                         0.0f, 0.0f, 0.0f, 1.0f,
  130.  
  131.                         1.0f, 0.0f, 1.0f, 1.0f
  132.  
  133.                 };
  134.  
  135.         float squareColorsRGBA[] =
  136.  
  137.                 {
  138.  
  139.                         1.0f, 0.0f, 0.0f, 1.0f,
  140.  
  141.                         0.0f, 1.0f, 0.0f, 1.0f,
  142.  
  143.                         0.0f, 0.0f, 1.0f, 1.0f,
  144.  
  145.                         1.0f, 1.0f, 1.0f, 1.0f
  146.  
  147.                 };
  148.         float vertices[] =
  149.  
  150.                 {
  151.  
  152.                         -1.0f, -1.0f,
  153.  
  154.                         1.0f, -1.0f,
  155.  
  156.                         -1.0f, 1.0f,
  157.  
  158.                         1.0f, 1.0f
  159.  
  160.                 };
  161.  
  162.         byte indices[] =
  163.  
  164.                 {
  165.  
  166.                         0, 3, 1,
  167.  
  168.                         0, 2, 3
  169.  
  170.                 };
  171.         float[] textureCoords =
  172.                 {
  173.                         0.0f, 0.0f,
  174.                         1.0f, 0.0f,
  175.                         0.0f, 1.0f,
  176.                         1.0f, 1.0f
  177.                 };
  178.  
  179.         ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length * 4);
  180.         tbb.order(ByteOrder.nativeOrder());
  181.         mTextureBuffer = tbb.asFloatBuffer();
  182.         mTextureBuffer.put(textureCoords);
  183.         mTextureBuffer.position(0);
  184.  
  185.         ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
  186.         ByteBuffer vbbYMCA = ByteBuffer.allocateDirect(squareColorsYMCA.length*4);
  187.         vbb.order(ByteOrder.nativeOrder());
  188.         vbbYMCA.order(ByteOrder.nativeOrder());
  189.  
  190.         mFVertexBuffer = vbb.asFloatBuffer();
  191.  
  192.         mFVertexBuffer.put(vertices);
  193.  
  194.         mFVertexBuffer.position(0);
  195.  
  196.         mColorBuffer = vbbYMCA.asFloatBuffer();
  197.  
  198.         if(sw==1)
  199.         mColorBuffer.put(squareColorsRGBA);
  200.         else
  201.         mColorBuffer.put(squareColorsYMCA);
  202.  
  203.         mColorBuffer.position(0);
  204.  
  205.         mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
  206.  
  207.         mIndexBuffer.put(indices);
  208.  
  209.         mIndexBuffer.position(0);
  210.     }
  211.  
  212.     public void createTexture(GL10 gl, Context contextRegf, int resource){
  213.         Bitmap image = BitmapFactory.decodeResource(contextRegf.getResources(), resource);
  214.         gl.glGenTextures(1, textures, 0);
  215.         gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
  216.         GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0);
  217.         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
  218.  
  219.         gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
  220.         image.recycle();
  221.     }
  222.  
  223.     public void draw(GL10 gl) {
  224.         gl.glFrontFace(GL11.GL_CW);
  225.         gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer);
  226.         gl.glColorPointer(4, GL11.GL_FLOAT, 0, mColorBuffer);
  227.         gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, mIndexBuffer);
  228.         gl.glEnable(GL10.GL_TEXTURE_2D);
  229.         gl.glEnable(GL10.GL_BLEND);
  230.         gl.glBlendFunc(GL10.GL_ONE, GL10.GL_SRC_COLOR);
  231.         gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
  232.  
  233.         gl.glTexCoordPointer(2, GL10.GL_FLOAT,0, mTextureBuffer);
  234.         gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  235.     }
  236. }
  237.  
  238.  
  239. ***
  240.  
  241.  
  242. package cg.bouncysquare;
  243.  
  244. import android.app.Activity;
  245. import android.opengl.GLSurfaceView;
  246. import android.support.v7.app.AppCompatActivity;
  247. import android.os.Bundle;
  248. import android.view.WindowManager;
  249.  
  250. public class BouncySquareActivity extends Activity {
  251.  
  252.     @Override
  253.     protected void onCreate(Bundle savedInstanceState) {
  254.         super.onCreate(savedInstanceState);
  255.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  256.  
  257.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
  258.  
  259.         GLSurfaceView view = new GLSurfaceView(this);
  260.  
  261.         view.setRenderer(new SquareRenderer(this.getApplicationContext()));
  262.  
  263.         setContentView(view);
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement