Advertisement
Tavi33

EGIOC 6

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