Guest User

julia set android shader

a guest
Sep 29th, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.75 KB | None | 0 0
  1. package prod.intro.wallpaper;
  2. import java.nio.ByteBuffer;
  3. import java.nio.ByteOrder;
  4. import java.nio.FloatBuffer;
  5. import java.nio.IntBuffer;
  6. import javax.microedition.khronos.egl.EGLConfig;
  7. import javax.microedition.khronos.opengles.GL10;
  8. import android.content.Context;
  9. import android.content.SharedPreferences;
  10. import android.opengl.GLES20;
  11. import android.os.SystemClock;
  12. import android.util.FloatMath;
  13. import android.util.Log;
  14. public class GLSLRenderer implements GLWallpaperService.Renderer {
  15.   private SharedPreferences preferences_;
  16.   private SettingsUpdater settingsUpdater_;
  17.   private static String TAG = "JuliaSetWallpaper";
  18.   private static float pi = (float)Math.PI;
  19.   private float[] v = {
  20.     -1.0f,-1.0f,
  21.     -1.0f,1.0f,
  22.     1.0f,1.0f,
  23.     -1.0f,-1.0f,
  24.     1.0f,1.0f,
  25.     1.0f,-1.0f
  26.   };
  27.   private float[] uv = {
  28.     -1.0f,-1.0f,
  29.     -1.0f,1.0f,
  30.     1.0f,1.0f,
  31.     -1.0f,-1.0f,
  32.     1.0f,1.0f,
  33.     1.0f,-1.0f
  34.   };
  35.   private float[] texuv = {
  36.     0.0f,1.0f,
  37.     0.0f,0.0f,
  38.     1.0f,0.0f,
  39.     0.0f,1.0f,
  40.     1.0f,0.0f,
  41.     1.0f,1.0f
  42.   };
  43.   private int texscale = 2;
  44.   private FloatBuffer vbuf;
  45.   private FloatBuffer uvbuf;
  46.   private FloatBuffer texuvbuf;
  47.   private long startTime = SystemClock.uptimeMillis();
  48.   private int fb[] = new int[1];
  49.   private int drb[] = new int[1];
  50.   private int tex[] = new int[1];
  51.   private float cx = 0.5f, cy = -0.5f, dcx = 0.0f, dcy = 0.0f;
  52.   private boolean downscale = true;
  53.   private int iter = 5;
  54.   private int xres, yres;
  55.   private final String vs =
  56.     "  attribute vec4 apos;\n"+
  57.       "attribute vec2 atex;\n"+
  58.       "varying vec2 vtex;\n"+
  59.       "void main() {\n"+
  60.       "  gl_Position = apos;\n"+
  61.       "  vtex = atex;\n"+
  62.       "}\n";
  63.   private final String fs =
  64.     "  precision highp float;\n"+
  65.       "uniform vec2 center;\n"+
  66.       "uniform vec2 c;\n"+
  67.       "uniform float scale;\n"+
  68.       "uniform float esc_radius;\n"+
  69.       "varying vec2 vtex;\n"+
  70.       "void main() {\n"+
  71.       "  float pi = 3.14;\n"+
  72.       "  vec2 z;\n"+
  73.       "  z.y=vtex.y/scale+center.y;\n"+
  74.       "  z.x=vtex.x/scale+center.x;\n"+
  75.       "  //z.y=vtex.y;\n"+
  76.       "  //z.x=vtex.x;\n"+
  77.       "  float x,y;\n"+
  78.       "  int escTime=0;\n"+
  79.       "  int i;\n"+
  80.       "  for(i=0;i<"+iter+";i++){\n"+
  81.       "    if((x*x+y*y)<=esc_radius){\n"+
  82.       "      y=(z.y*z.x+z.x*z.y)+float(c.y);\n"+
  83.       "      x=(z.x*z.x-z.y*z.y)+float(c.x);\n"+
  84.       "      z.y=y;\n"+
  85.       "      z.x=x;\n"+
  86.       "      escTime++;\n"+
  87.       "    }\n"+
  88.       "  }\n"+
  89.       "  float r,g,b;\n"+
  90.       "  float z_abs=sqrt(float(x*x+y*y));\n"+
  91.       "  float i_norm=float(escTime)-log(log(z_abs))/log(2.0);\n"+
  92.       "  float var1=pi/3.0;"+
  93.       "  //if(escTime >= "+iter+"){\n"+
  94.       "    r=-cos(z_abs+var1*1.0);"+
  95.       "    g=-cos(z_abs+var1*2.0);"+
  96.       "    b=-cos(z_abs+var1*3.0);"+
  97.       "  //}else{\n"+
  98.       "    //r=-cos(i_norm/8.0+var1*1.0);\n"+
  99.       "    //g=-cos(i_norm/8.0+var1*2.0);\n"+
  100.       "    //b=-cos(i_norm/8.0+var1*3.0);\n"+
  101.       "  //}\n"+
  102.       "  gl_FragColor=vec4(r,g,b,1.0);\n"+
  103.       "  //gl_FragColor=vec4(vtex.x,vtex.y,0.0,1.0);\n"+
  104.       "};\n";
  105.   private final String texvs =
  106.     "  attribute vec4 aPosition;\n"+
  107.       "attribute vec2 aTextureCoord;\n"+
  108.       "varying vec2 vTextureCoord;\n"+
  109.       "void main() {\n"+
  110.       "  gl_Position = aPosition;\n"+
  111.       "  vTextureCoord = aTextureCoord;\n"+
  112.       "}\n";
  113.   private final String texfs =
  114.     "  precision highp float;\n"+
  115.       "varying vec2 vTextureCoord;\n"+
  116.       "uniform sampler2D sTexture;\n"+
  117.       "void main() {\n"+
  118.       "  gl_FragColor = texture2D(sTexture, vTextureCoord);\n"+
  119.       "}\n";
  120.   private int prog, texprog;
  121.   public GLSLRenderer(Context context) {
  122.     vbuf = ByteBuffer.allocateDirect(v.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  123.     vbuf.put(v).position(0);
  124.     uvbuf = ByteBuffer.allocateDirect(uv.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  125.     uvbuf.put(uv).position(0);
  126.     texuvbuf = ByteBuffer.allocateDirect(texuv.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  127.     texuvbuf.put(texuv).position(0);
  128.   }
  129.   static long lastTime = SystemClock.uptimeMillis();
  130.   static long frameCount = 0;
  131.   int texresx, texresy;
  132.   IntBuffer texbuf;
  133.   private int closestPowerOfTwo(int in) {
  134.     int res = 2;
  135.     do {
  136.       res = res*2;
  137.     } while (res*2<in);
  138.     return res;
  139.   }
  140.   public void setSharedPreferences(SharedPreferences preferences)
  141.   {
  142.     settingsUpdater_ = new SettingsUpdater();
  143.     preferences_ = preferences;
  144.     preferences_.registerOnSharedPreferenceChangeListener(settingsUpdater_);
  145.     settingsUpdater_.onSharedPreferenceChanged(preferences_,null);
  146.   }
  147.   private class SettingsUpdater implements SharedPreferences.OnSharedPreferenceChangeListener {
  148.     @Override
  149.     public void onSharedPreferenceChanged(
  150.       SharedPreferences sharedPreferences, String key) {
  151.       try
  152.       {
  153.         int backgroundInt = sharedPreferences.getInt("backgroundColor",0);
  154.         int linesInt = sharedPreferences.getInt("linesColor",-1);
  155.         Log.i(TAG,"PREF back = "+backgroundInt+" lines = "+linesInt);
  156.       } catch (final Exception e)
  157.       {
  158.         Log.e(TAG,"PREF init error: "+e);
  159.       }
  160.     }
  161.   }
  162.   @Override
  163.   public void onDrawFrame(GL10 gl) {
  164.     frameCount++;
  165.     long time = SystemClock.uptimeMillis();
  166.     long relTime = time-startTime;
  167.     if(time>lastTime+1000) {
  168.       // System.out.println("fps: "+frameCount);
  169.       frameCount = 0;
  170.       lastTime = time;
  171.     }
  172.     float tf = (float)relTime/1000;
  173.     if(downscale) {
  174.       GLES20.glViewport(0,0,texresx,texresy);
  175.       GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,fb[0]);
  176.       GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,GLES20.GL_COLOR_ATTACHMENT0,GLES20.GL_TEXTURE_2D,tex[0],0);
  177.       GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER,GLES20.GL_DEPTH_ATTACHMENT,GLES20.GL_RENDERBUFFER,drb[0]);
  178.     } else {
  179.       GLES20.glViewport(0,0,texresx,texresy);
  180.     }
  181.     GLES20.glUseProgram(prog);
  182.     GLES20.glClearColor(0.0f,0.0f,0.0f,1.0f);
  183.     GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT|GLES20.GL_COLOR_BUFFER_BIT);
  184.     // dcx = 1.0f-Math.abs(Math.abs(cx)-0.5f)/0.5f;
  185.     // dcy = 1.0f-Math.abs(Math.abs(cy)-0.5f)/0.5f;
  186.     cx = FloatMath.cos(tf/2.234f)/1.5f;
  187.     cy = FloatMath.sin(tf/2.823f)/1.5f;
  188.     GLES20.glUniform2f(GLES20.glGetUniformLocation(prog,"c"),cx,cy);
  189.     GLES20.glUniform2f(GLES20.glGetUniformLocation(prog,"center"),0.0f,0.0f);
  190.     GLES20.glUniform1f(GLES20.glGetUniformLocation(prog,"scale"),0.75f);
  191.     GLES20.glUniform1f(GLES20.glGetUniformLocation(prog,"esc_radius"),1.1f);
  192.     GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(prog,"apos"),2,GLES20.GL_FLOAT,false,2*4,vbuf);
  193.     GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(prog,"apos"));
  194.     GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(prog,"atex"),2,GLES20.GL_FLOAT,false,2*4,uvbuf);
  195.     GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(prog,"atex"));
  196.     GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,v.length/2);
  197.     if(downscale) {
  198.       GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,0);
  199.       GLES20.glViewport(0,0,xres,yres);
  200.       GLES20.glUseProgram(texprog);
  201.       GLES20.glClearColor(0.0f,0.0f,0.0f,1.0f);
  202.       GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT|GLES20.GL_COLOR_BUFFER_BIT);
  203.       GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  204.       GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,tex[0]);
  205.       GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(texprog,"aPositionCoord"),2,GLES20.GL_FLOAT,false,2*4,vbuf);
  206.       GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(texprog,"aPositionCoord"));
  207.       GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(texprog,"aTextureCoord"),2,GLES20.GL_FLOAT,false,2*4,texuvbuf);
  208.       GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(texprog,"aTextureCoord"));
  209.       GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,v.length/2);
  210.     }
  211.   }
  212.   @Override
  213.   public void onSurfaceChanged(GL10 gl, int width, int height) {
  214.     if(settingsUpdater_!=null&&preferences_!=null) {
  215.       settingsUpdater_.onSharedPreferenceChanged(preferences_,null);
  216.     }
  217.     GLES20.glViewport(0,0,width,height);
  218.     xres = width;
  219.     yres = height;
  220.     float ratio = (float)width/height;
  221.     float tempuv[] = {
  222.       -ratio,-1.0f,
  223.       -ratio,1.0f,
  224.       ratio,1.0f,
  225.       -ratio,-1.0f,
  226.       ratio,1.0f,
  227.       ratio,-1.0f
  228.     };
  229.     uv = tempuv;
  230.     uvbuf = ByteBuffer.allocateDirect(uv.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  231.     uvbuf.put(uv).position(0);
  232.     // texresx = closestPowerOfTwo(xres/texscale);
  233.     // texresy = closestPowerOfTwo(yres/texscale);
  234.     texresx = xres/texscale+10;
  235.     texresy = yres/texscale;
  236.     generateTexture();
  237.   }
  238.   private void generateTexture() {
  239.     GLES20.glGenFramebuffers(1,fb,0);
  240.     GLES20.glGenRenderbuffers(1,drb,0);
  241.     GLES20.glGenTextures(1,tex,0);
  242.     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,tex[0]);
  243.     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
  244.     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
  245.     // GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_NEAREST);
  246.     // GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
  247.     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
  248.     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_LINEAR);
  249.     int[] buf = new int[texresx*texresy];
  250.     texbuf = ByteBuffer.allocateDirect(buf.length*4).order(ByteOrder.nativeOrder()).asIntBuffer();
  251.     GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D,0,GLES20.GL_RGB,texresx,texresy,0,GLES20.GL_RGB,GLES20.GL_UNSIGNED_SHORT_5_6_5,texbuf);
  252.     GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER,drb[0]);
  253.     GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER,GLES20.GL_DEPTH_COMPONENT16,texresx,texresy);
  254.   }
  255.   @Override
  256.   public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  257.     prog = createProgram(vs,fs);
  258.     texprog = createProgram(texvs,texfs);
  259.   }
  260.   private int createProgram(String vertexSource, String fragmentSource) {
  261.     int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER,vertexSource);
  262.     if(vertexShader==0) {
  263.       return 0;
  264.     }
  265.     int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentSource);
  266.     if(pixelShader==0) {
  267.       return 0;
  268.     }
  269.     int program = GLES20.glCreateProgram();
  270.     if(program!=0) {
  271.       GLES20.glAttachShader(program,vertexShader);
  272.       GLES20.glAttachShader(program,pixelShader);
  273.       GLES20.glLinkProgram(program);
  274.       int[] linkStatus = new int[1];
  275.       GLES20.glGetProgramiv(program,GLES20.GL_LINK_STATUS,linkStatus,0);
  276.       if(linkStatus[0]!=GLES20.GL_TRUE) {
  277.         Log.e(TAG,"Could not link program: ");
  278.         Log.e(TAG,GLES20.glGetProgramInfoLog(program));
  279.         GLES20.glDeleteProgram(program);
  280.         program = 0;
  281.       }
  282.     }
  283.     return program;
  284.   }
  285.   private void checkGlError(String op) {
  286.     int error;
  287.     while ((error = GLES20.glGetError())!=GLES20.GL_NO_ERROR) {
  288.       Log.e(TAG,op+": glError "+error);
  289.       throw new RuntimeException(op+": glError "+error);
  290.     }
  291.   }
  292.   private int loadShader(int shaderType, String source) {
  293.     int shader = GLES20.glCreateShader(shaderType);
  294.     if(shader!=0) {
  295.       GLES20.glShaderSource(shader,source);
  296.       GLES20.glCompileShader(shader);
  297.       int[] compiled = new int[1];
  298.       GLES20.glGetShaderiv(shader,GLES20.GL_COMPILE_STATUS,compiled,0);
  299.       if(compiled[0]==0) {
  300.         Log.e(TAG,"Could not compile shader "+shaderType+":");
  301.         Log.e(TAG,GLES20.glGetShaderInfoLog(shader));
  302.         GLES20.glDeleteShader(shader);
  303.         shader = 0;
  304.       }
  305.     }
  306.     return shader;
  307.   }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment