Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. package zui.polsl.pl.lab5przyklad;
  2.  
  3. import android.opengl.GLSurfaceView;
  4. import android.opengl.GLU;
  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 static java.lang.Math.*;
  13.  
  14.  
  15. public class MyRenderer implements GLSurfaceView.Renderer
  16. {
  17.     private float kat = 0;
  18.     private float aspect = 0;
  19.     int vertexCount = 360;
  20.     float radius = 0.1f;
  21.     float v = 0.02f;
  22.     float center_x = 0.0f;
  23.     float center_y = 0.0f;
  24.     boolean block = false;
  25.  
  26.  
  27.  
  28.     @Override
  29.     public void onSurfaceCreated(GL10 gl, EGLConfig config)
  30.     {
  31.  
  32.     }
  33.  
  34.     @Override
  35.     public void onSurfaceChanged(GL10 gl, int width, int height)
  36.     {
  37.         aspect = width/height*1.8f;
  38.         gl.glViewport(0, 0, width, height);
  39.         gl.glMatrixMode(GL10.GL_PROJECTION);
  40.         gl.glLoadIdentity();
  41.         GLU.gluPerspective(gl,45.0f,(float)width/(float)height,-1.0f, -10.0f);
  42.         gl.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
  43.     }
  44.  
  45.     @Override
  46.     public void onDrawFrame(GL10 gl)
  47.     {
  48.         //float wierzcholki[] = {
  49.         //        -1.0f, 0.0f, -1.0f,
  50.         //        0.0f, 1.0f, -1.0f,
  51.         //        1.0f, 0.0f, -1.0f
  52.         //};
  53.  
  54.         if(center_x >= 0.9f){
  55.             block = true;
  56.         }
  57.         if(center_x <= -0.9f){
  58.             block = false;
  59.         }
  60.         if(block){
  61.             center_x -= v;
  62.         }else{
  63.             center_x += v;
  64.         }
  65.  
  66.         float buffer[] = new float[vertexCount*2];
  67.         int index = 0;
  68.         buffer[index++] = center_x;
  69.         buffer[index++] = center_y;
  70.  
  71.  
  72.         float percent, rad, outer_x, outer_y;
  73.         int outerVertexCount = vertexCount - 1;
  74.         for(int i = 0; i < outerVertexCount; i++){
  75.             percent = (i / (float)(outerVertexCount - 1));
  76.             rad = (float)(percent * 2 * Math.PI);
  77.  
  78.             outer_x = (float)(center_x + radius * cos(rad));
  79.             outer_y = (float)((center_y + radius * sin(rad)) * aspect);
  80.  
  81.             buffer[index++] = outer_x;
  82.             buffer[index++] = outer_y;
  83.         }
  84.  
  85.         ByteBuffer bufor = ByteBuffer.allocateDirect(vertexCount*3*4);
  86.         bufor.order(ByteOrder.nativeOrder());
  87.         FloatBuffer bufor_wierzcholkow = bufor.asFloatBuffer();
  88.         bufor_wierzcholkow.put(buffer);
  89.         bufor_wierzcholkow.position(0);
  90.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  91.         gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
  92.         gl.glLoadIdentity();
  93.         //gl.glRotatef(kat++, 0.0f, 0.0f, 1.0f);
  94.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  95.         gl.glVertexPointer(2, GL10.GL_FLOAT, 0, bufor_wierzcholkow);
  96.         //gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
  97.         gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, vertexCount);
  98.  
  99.         gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement