Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://drive.google.com/drive/folders/1Xfy79fJkV83SXWWBQEvZK1OPNWflBLDe
- ATENTIE!! La acest lab trebuie copiate in AndroidStudio in app/res/drawable goldengate.png si splash.png ca sa mearga.
- package com.example.allenpianoman.bouncysquare6;
- import android.os.Bundle;
- import android.app.Activity;
- import android.opengl.GLSurfaceView;
- import android.view.WindowManager;
- public class BouncySquare6Activity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- GLSurfaceView view = new GLSurfaceView(this);
- view.setRenderer(new SquareRenderer(this.getApplicationContext()));
- setContentView(view);
- }
- }
- package com.example.allenpianoman.bouncysquare6;
- import javax.microedition.khronos.opengles.GL10;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL11;
- import android.opengl.GLSurfaceView;
- import java.lang.Math;
- import android.content.Context;
- public class SquareRenderer implements GLSurfaceView.Renderer {
- private Square mSquare1;
- private Square mSquare2;
- private Context mContext;
- private float mTransY;
- private float mAngle;
- public SquareRenderer(Context context)
- {
- float squareColorsYMCA[] =
- {
- 1.0f, 1.0f, 0.0f, 1.0f,
- 0.0f, 1.0f, 1.0f, 1.0f,
- 0.0f, 0.0f, 0.0f, 1.0f,
- 1.0f, 0.0f, 1.0f, 1.0f
- };
- float squareColorsRGBA[] =
- {
- 1.0f, 0.0f, 0.0f, 1.0f,
- 0.0f, 1.0f, 0.0f, 1.0f,
- 0.0f, 0.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f, 1.0f
- };
- mContext = context;
- mSquare1 = new Square(squareColorsYMCA);
- }
- public void onDrawFrame(GL10 gl)
- {
- gl.glClearColor(0.0f,0.0f,0.0f,1.0f);
- gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
- gl.glMatrixMode(GL11.GL_MODELVIEW);
- gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
- gl.glColor4f(1.0f,1.0f,1.0f,1.0f);
- gl.glLoadIdentity();
- gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -3.0f);
- mSquare1.draw(gl);
- gl.glLoadIdentity();
- gl.glTranslatef((float)(Math.sin(mTransY)/2.0f),0.0f, -2.9f);
- gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
- mSquare1.draw(gl);
- mTransY += .075f;
- }
- public void onSurfaceChanged(GL10 gl, int width, int height)
- {
- gl.glViewport(0, 0, width, height);
- float ratio = (float) width / height;
- gl.glMatrixMode(GL11.GL_PROJECTION);
- gl.glLoadIdentity();
- gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
- }
- public void onSurfaceCreated(GL10 gl, EGLConfig config)
- {
- gl.glDisable(GL11.GL_DITHER);
- gl.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT,
- GL11.GL_FASTEST);
- gl.glClearColor(.0f,.5f,.5f,1.0f);
- gl.glEnable(GL11.GL_CULL_FACE);
- gl.glShadeModel(GL11.GL_SMOOTH);
- gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
- gl.glDisable(GL11.GL_DEPTH_TEST);
- gl.glEnable(GL11.GL_BLEND);
- mSquare1.setTextures(gl,mContext,R.drawable.goldengate,R.drawable.splash);
- }
- }
- package com.example.allenpianoman.bouncysquare6;
- import javax.microedition.khronos.opengles.GL10;
- import javax.microedition.khronos.opengles.GL11;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import android.content.Context;
- import android.graphics.*;
- import android.opengl.*;
- public class Square {
- private FloatBuffer mFVertexBuffer;
- private FloatBuffer mColorBuffer;
- private FloatBuffer mTextureCoords0;
- private FloatBuffer mTextureCoords1;
- private ByteBuffer mIndexBuffer;
- private int mTexture0;
- private int mTexture1;
- public Square(float colors[])
- {
- float vertices[] =
- {
- -1.0f, -1.0f,
- 1.0f, -1.0f,
- -1.0f, 1.0f,
- 1.0f, 1.0f
- };
- float textureCoords[] =
- {
- 0.0f, 1.0f,
- 1.0f, 1.0f,
- 0.0f, 0.0f,
- 1.0f, 0.0f
- };
- byte indices[] =
- {
- 0, 3, 1,
- 0, 2, 3
- };
- ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
- vbb.order(ByteOrder.nativeOrder());
- mFVertexBuffer = vbb.asFloatBuffer();
- mFVertexBuffer.put(vertices);
- mFVertexBuffer.position(0);
- vbb = ByteBuffer.allocateDirect(colors.length * 4);
- vbb.order(ByteOrder.nativeOrder());
- mColorBuffer = vbb.asFloatBuffer();
- mColorBuffer.put(colors);
- mColorBuffer.position(0);
- mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
- mIndexBuffer.put(indices);
- mIndexBuffer.position(0);
- vbb = ByteBuffer.allocateDirect(textureCoords.length * 4);
- vbb.order(ByteOrder.nativeOrder());
- mTextureCoords0 = vbb.asFloatBuffer();
- mTextureCoords0.put(textureCoords);
- mTextureCoords0.position(0);
- vbb = ByteBuffer.allocateDirect(textureCoords.length * 4);
- vbb.order(ByteOrder.nativeOrder());
- mTextureCoords1 = vbb.asFloatBuffer();
- mTextureCoords1.put(textureCoords);
- mTextureCoords1.position(0);
- }
- public void draw(GL10 gl)
- {
- gl.glEnable(GL10.GL_TEXTURE_2D);
- gl.glBindTexture(GL10.GL_TEXTURE_2D,mTexture0);
- gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
- gl.glFrontFace(GL11.GL_CW);
- gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer);
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glColorPointer(4, GL11.GL_FLOAT, 0, mColorBuffer);
- gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
- gl.glClientActiveTexture(GL10.GL_TEXTURE0);
- gl.glTexCoordPointer(2, GL10.GL_FLOAT,0,mTextureCoords0);
- gl.glClientActiveTexture(GL10.GL_TEXTURE1);
- gl.glTexCoordPointer(2, GL10.GL_FLOAT,0,mTextureCoords1);
- multiTexture(gl,mTexture0,mTexture1);
- gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, mIndexBuffer);
- gl.glFrontFace(GL11.GL_CCW);
- }
- public void setTextures(GL10 gl,Context context,int resourceID0, int resourceID1)
- {
- mTexture0=createTexture(gl,context,resourceID0);
- mTexture1=createTexture(gl,context,resourceID1);
- }
- public int createTexture(GL10 gl, Context contextRegf, int resource)
- {
- int[] textures = new int[1];
- Bitmap tempImage = BitmapFactory.decodeResource(contextRegf.getResources(), resource); // 1
- gl.glGenTextures(1, textures, 0); // 2
- gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); // 3
- GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, tempImage, 0); // 4
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); // 5a
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); // 5b
- tempImage.recycle();//6
- return textures[0];
- }
- public void multiTexture(GL10 gl, int tex0, int tex1)
- {
- float combineParameter= GL10.GL_DECAL; //1
- // Set up the First Texture.
- gl.glActiveTexture(GL10.GL_TEXTURE0); //2
- gl.glBindTexture(GL10.GL_TEXTURE_2D, tex0); //3
- // Set up the Second Texture.
- gl.glActiveTexture(GL10.GL_TEXTURE1);
- gl.glBindTexture(GL10.GL_TEXTURE_2D, tex1);
- // Set the texture environment mode for this texture to combine.
- gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, combineParameter); //4
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement