Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prod.intro.wallpaper;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.opengl.GLES20;
- import android.os.SystemClock;
- import android.util.FloatMath;
- import android.util.Log;
- public class GLSLRenderer implements GLWallpaperService.Renderer {
- private SharedPreferences preferences_;
- private SettingsUpdater settingsUpdater_;
- private static String TAG = "JuliaSetWallpaper";
- private static float pi = (float)Math.PI;
- private float[] v = {
- -1.0f,-1.0f,
- -1.0f,1.0f,
- 1.0f,1.0f,
- -1.0f,-1.0f,
- 1.0f,1.0f,
- 1.0f,-1.0f
- };
- private float[] uv = {
- -1.0f,-1.0f,
- -1.0f,1.0f,
- 1.0f,1.0f,
- -1.0f,-1.0f,
- 1.0f,1.0f,
- 1.0f,-1.0f
- };
- private float[] texuv = {
- 0.0f,1.0f,
- 0.0f,0.0f,
- 1.0f,0.0f,
- 0.0f,1.0f,
- 1.0f,0.0f,
- 1.0f,1.0f
- };
- private int texscale = 2;
- private FloatBuffer vbuf;
- private FloatBuffer uvbuf;
- private FloatBuffer texuvbuf;
- private long startTime = SystemClock.uptimeMillis();
- private int fb[] = new int[1];
- private int drb[] = new int[1];
- private int tex[] = new int[1];
- private float cx = 0.5f, cy = -0.5f, dcx = 0.0f, dcy = 0.0f;
- private boolean downscale = true;
- private int iter = 5;
- private int xres, yres;
- private final String vs =
- " attribute vec4 apos;\n"+
- "attribute vec2 atex;\n"+
- "varying vec2 vtex;\n"+
- "void main() {\n"+
- " gl_Position = apos;\n"+
- " vtex = atex;\n"+
- "}\n";
- private final String fs =
- " precision highp float;\n"+
- "uniform vec2 center;\n"+
- "uniform vec2 c;\n"+
- "uniform float scale;\n"+
- "uniform float esc_radius;\n"+
- "varying vec2 vtex;\n"+
- "void main() {\n"+
- " float pi = 3.14;\n"+
- " vec2 z;\n"+
- " z.y=vtex.y/scale+center.y;\n"+
- " z.x=vtex.x/scale+center.x;\n"+
- " //z.y=vtex.y;\n"+
- " //z.x=vtex.x;\n"+
- " float x,y;\n"+
- " int escTime=0;\n"+
- " int i;\n"+
- " for(i=0;i<"+iter+";i++){\n"+
- " if((x*x+y*y)<=esc_radius){\n"+
- " y=(z.y*z.x+z.x*z.y)+float(c.y);\n"+
- " x=(z.x*z.x-z.y*z.y)+float(c.x);\n"+
- " z.y=y;\n"+
- " z.x=x;\n"+
- " escTime++;\n"+
- " }\n"+
- " }\n"+
- " float r,g,b;\n"+
- " float z_abs=sqrt(float(x*x+y*y));\n"+
- " float i_norm=float(escTime)-log(log(z_abs))/log(2.0);\n"+
- " float var1=pi/3.0;"+
- " //if(escTime >= "+iter+"){\n"+
- " r=-cos(z_abs+var1*1.0);"+
- " g=-cos(z_abs+var1*2.0);"+
- " b=-cos(z_abs+var1*3.0);"+
- " //}else{\n"+
- " //r=-cos(i_norm/8.0+var1*1.0);\n"+
- " //g=-cos(i_norm/8.0+var1*2.0);\n"+
- " //b=-cos(i_norm/8.0+var1*3.0);\n"+
- " //}\n"+
- " gl_FragColor=vec4(r,g,b,1.0);\n"+
- " //gl_FragColor=vec4(vtex.x,vtex.y,0.0,1.0);\n"+
- "};\n";
- private final String texvs =
- " attribute vec4 aPosition;\n"+
- "attribute vec2 aTextureCoord;\n"+
- "varying vec2 vTextureCoord;\n"+
- "void main() {\n"+
- " gl_Position = aPosition;\n"+
- " vTextureCoord = aTextureCoord;\n"+
- "}\n";
- private final String texfs =
- " precision highp float;\n"+
- "varying vec2 vTextureCoord;\n"+
- "uniform sampler2D sTexture;\n"+
- "void main() {\n"+
- " gl_FragColor = texture2D(sTexture, vTextureCoord);\n"+
- "}\n";
- private int prog, texprog;
- public GLSLRenderer(Context context) {
- vbuf = ByteBuffer.allocateDirect(v.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- vbuf.put(v).position(0);
- uvbuf = ByteBuffer.allocateDirect(uv.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- uvbuf.put(uv).position(0);
- texuvbuf = ByteBuffer.allocateDirect(texuv.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- texuvbuf.put(texuv).position(0);
- }
- static long lastTime = SystemClock.uptimeMillis();
- static long frameCount = 0;
- int texresx, texresy;
- IntBuffer texbuf;
- private int closestPowerOfTwo(int in) {
- int res = 2;
- do {
- res = res*2;
- } while (res*2<in);
- return res;
- }
- public void setSharedPreferences(SharedPreferences preferences)
- {
- settingsUpdater_ = new SettingsUpdater();
- preferences_ = preferences;
- preferences_.registerOnSharedPreferenceChangeListener(settingsUpdater_);
- settingsUpdater_.onSharedPreferenceChanged(preferences_,null);
- }
- private class SettingsUpdater implements SharedPreferences.OnSharedPreferenceChangeListener {
- @Override
- public void onSharedPreferenceChanged(
- SharedPreferences sharedPreferences, String key) {
- try
- {
- int backgroundInt = sharedPreferences.getInt("backgroundColor",0);
- int linesInt = sharedPreferences.getInt("linesColor",-1);
- Log.i(TAG,"PREF back = "+backgroundInt+" lines = "+linesInt);
- } catch (final Exception e)
- {
- Log.e(TAG,"PREF init error: "+e);
- }
- }
- }
- @Override
- public void onDrawFrame(GL10 gl) {
- frameCount++;
- long time = SystemClock.uptimeMillis();
- long relTime = time-startTime;
- if(time>lastTime+1000) {
- // System.out.println("fps: "+frameCount);
- frameCount = 0;
- lastTime = time;
- }
- float tf = (float)relTime/1000;
- if(downscale) {
- GLES20.glViewport(0,0,texresx,texresy);
- GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,fb[0]);
- GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,GLES20.GL_COLOR_ATTACHMENT0,GLES20.GL_TEXTURE_2D,tex[0],0);
- GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER,GLES20.GL_DEPTH_ATTACHMENT,GLES20.GL_RENDERBUFFER,drb[0]);
- } else {
- GLES20.glViewport(0,0,texresx,texresy);
- }
- GLES20.glUseProgram(prog);
- GLES20.glClearColor(0.0f,0.0f,0.0f,1.0f);
- GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT|GLES20.GL_COLOR_BUFFER_BIT);
- // dcx = 1.0f-Math.abs(Math.abs(cx)-0.5f)/0.5f;
- // dcy = 1.0f-Math.abs(Math.abs(cy)-0.5f)/0.5f;
- cx = FloatMath.cos(tf/2.234f)/1.5f;
- cy = FloatMath.sin(tf/2.823f)/1.5f;
- GLES20.glUniform2f(GLES20.glGetUniformLocation(prog,"c"),cx,cy);
- GLES20.glUniform2f(GLES20.glGetUniformLocation(prog,"center"),0.0f,0.0f);
- GLES20.glUniform1f(GLES20.glGetUniformLocation(prog,"scale"),0.75f);
- GLES20.glUniform1f(GLES20.glGetUniformLocation(prog,"esc_radius"),1.1f);
- GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(prog,"apos"),2,GLES20.GL_FLOAT,false,2*4,vbuf);
- GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(prog,"apos"));
- GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(prog,"atex"),2,GLES20.GL_FLOAT,false,2*4,uvbuf);
- GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(prog,"atex"));
- GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,v.length/2);
- if(downscale) {
- GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,0);
- GLES20.glViewport(0,0,xres,yres);
- GLES20.glUseProgram(texprog);
- GLES20.glClearColor(0.0f,0.0f,0.0f,1.0f);
- GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT|GLES20.GL_COLOR_BUFFER_BIT);
- GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
- GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,tex[0]);
- GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(texprog,"aPositionCoord"),2,GLES20.GL_FLOAT,false,2*4,vbuf);
- GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(texprog,"aPositionCoord"));
- GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(texprog,"aTextureCoord"),2,GLES20.GL_FLOAT,false,2*4,texuvbuf);
- GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(texprog,"aTextureCoord"));
- GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,v.length/2);
- }
- }
- @Override
- public void onSurfaceChanged(GL10 gl, int width, int height) {
- if(settingsUpdater_!=null&&preferences_!=null) {
- settingsUpdater_.onSharedPreferenceChanged(preferences_,null);
- }
- GLES20.glViewport(0,0,width,height);
- xres = width;
- yres = height;
- float ratio = (float)width/height;
- float tempuv[] = {
- -ratio,-1.0f,
- -ratio,1.0f,
- ratio,1.0f,
- -ratio,-1.0f,
- ratio,1.0f,
- ratio,-1.0f
- };
- uv = tempuv;
- uvbuf = ByteBuffer.allocateDirect(uv.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- uvbuf.put(uv).position(0);
- // texresx = closestPowerOfTwo(xres/texscale);
- // texresy = closestPowerOfTwo(yres/texscale);
- texresx = xres/texscale+10;
- texresy = yres/texscale;
- generateTexture();
- }
- private void generateTexture() {
- GLES20.glGenFramebuffers(1,fb,0);
- GLES20.glGenRenderbuffers(1,drb,0);
- GLES20.glGenTextures(1,tex,0);
- GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,tex[0]);
- GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
- GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
- // GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_NEAREST);
- // GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
- GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
- GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_LINEAR);
- int[] buf = new int[texresx*texresy];
- texbuf = ByteBuffer.allocateDirect(buf.length*4).order(ByteOrder.nativeOrder()).asIntBuffer();
- GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D,0,GLES20.GL_RGB,texresx,texresy,0,GLES20.GL_RGB,GLES20.GL_UNSIGNED_SHORT_5_6_5,texbuf);
- GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER,drb[0]);
- GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER,GLES20.GL_DEPTH_COMPONENT16,texresx,texresy);
- }
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig config) {
- prog = createProgram(vs,fs);
- texprog = createProgram(texvs,texfs);
- }
- private int createProgram(String vertexSource, String fragmentSource) {
- int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER,vertexSource);
- if(vertexShader==0) {
- return 0;
- }
- int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentSource);
- if(pixelShader==0) {
- return 0;
- }
- int program = GLES20.glCreateProgram();
- if(program!=0) {
- GLES20.glAttachShader(program,vertexShader);
- GLES20.glAttachShader(program,pixelShader);
- GLES20.glLinkProgram(program);
- int[] linkStatus = new int[1];
- GLES20.glGetProgramiv(program,GLES20.GL_LINK_STATUS,linkStatus,0);
- if(linkStatus[0]!=GLES20.GL_TRUE) {
- Log.e(TAG,"Could not link program: ");
- Log.e(TAG,GLES20.glGetProgramInfoLog(program));
- GLES20.glDeleteProgram(program);
- program = 0;
- }
- }
- return program;
- }
- private void checkGlError(String op) {
- int error;
- while ((error = GLES20.glGetError())!=GLES20.GL_NO_ERROR) {
- Log.e(TAG,op+": glError "+error);
- throw new RuntimeException(op+": glError "+error);
- }
- }
- private int loadShader(int shaderType, String source) {
- int shader = GLES20.glCreateShader(shaderType);
- if(shader!=0) {
- GLES20.glShaderSource(shader,source);
- GLES20.glCompileShader(shader);
- int[] compiled = new int[1];
- GLES20.glGetShaderiv(shader,GLES20.GL_COMPILE_STATUS,compiled,0);
- if(compiled[0]==0) {
- Log.e(TAG,"Could not compile shader "+shaderType+":");
- Log.e(TAG,GLES20.glGetShaderInfoLog(shader));
- GLES20.glDeleteShader(shader);
- shader = 0;
- }
- }
- return shader;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment