Guest User

red color wtf

a guest
Sep 29th, 2012
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.51 KB | None | 0 0
  1. package prod.demo.intro;
  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.opengl.GLES20;
  10. import android.opengl.GLSurfaceView;
  11. import android.opengl.Matrix;
  12. import android.os.SystemClock;
  13. import android.util.Log;
  14. class IntroRenderer implements GLSurfaceView.Renderer {
  15.   private float[] v = {
  16.     -1.0f,-1.0f,
  17.     -1.0f,1.0f,
  18.     1.0f,1.0f,
  19.     -1.0f,-1.0f,
  20.     1.0f,1.0f,
  21.     1.0f,-1.0f
  22.   };
  23.   private float[] uv = {
  24.     -1.0f,-1.0f,
  25.     -1.0f,1.0f,
  26.     1.0f,1.0f,
  27.     -1.0f,-1.0f,
  28.     1.0f,1.0f,
  29.     1.0f,-1.0f
  30.   };
  31.   private float[] texuv = {
  32.     0.0f,1.0f,
  33.     0.0f,0.0f,
  34.     1.0f,0.0f,
  35.     0.0f,1.0f,
  36.     1.0f,0.0f,
  37.     1.0f,1.0f
  38.   };
  39.   private int texscale = 8;
  40.   private FloatBuffer vbuf;
  41.   private FloatBuffer uvbuf;
  42.   private FloatBuffer texuvbuf;
  43.   private double nearz = -1.0;
  44.   private double farz = 1.0;
  45.   private double cameraz = -1.2;
  46.   private int xres, yres;
  47.   private double delta = 0.1;
  48.   private int iterations = (int)((int)(farz-nearz)/delta);
  49.   private long startTime = SystemClock.uptimeMillis();
  50.   private int fb[] = new int[1];
  51.   private int drb[] = new int[1];
  52.   private int tex[] = new int[1];
  53.   static long lastTime = SystemClock.uptimeMillis();
  54.   static long frameCount = 0;
  55.   int texresx, texresy;
  56.   private IntBuffer texbuf;
  57.   private int prog, texprog;
  58.   private static String TAG = "intro";
  59.   private boolean downscale = true;
  60.   private final String vs =
  61.     "  attribute vec4 apos;\n"+
  62.       "attribute vec2 atex;\n"+
  63.       "varying vec2 vtex;\n"+
  64.       "void main() {\n"+
  65.       "  gl_Position = apos;\n"+
  66.       "  vtex = atex;\n"+
  67.       "}\n";
  68.   private final String fs =
  69.     "  precision mediump float;\n"+
  70.       "uniform float t;\n"+
  71.       "varying vec2 vtex;\n"+
  72.       "mat3 genRotMat(float a0,float x,float y,float z){\n"+
  73.       "  float a=a0*3.1415926535897932384626433832795/180.0;\n"+
  74.       "  return mat3(\n"+
  75.       "    1.0+(1.0-cos(a))*(x*x-1.0),\n"+
  76.       "    -z*sin(a)+(1.0-cos(a))*x*y,\n"+
  77.       "    y*sin(a)+(1.0-cos(a))*x*z,\n"+
  78.       "    z*sin(a)+(1.0-cos(a))*x*y,\n"+
  79.       "    1.0+(1.0-cos(a))*(y*y-1.0),\n"+
  80.       "    -x*sin(a)+(1.0-cos(a))*y*z,\n"+
  81.       "    -y*sin(a)+(1.0-cos(a))*x*z,\n"+
  82.       "    x*sin(a)+(1.0-cos(a))*y*z,\n"+
  83.       "    1.0+(1.0-cos(a))*(z*z-1.0)\n"+
  84.       "  );\n"+
  85.       "}\n"+
  86.       "float cubeDist(vec3 p){\n"+
  87.       "  float t0 = 0.5;\n"+
  88.       "  float t1 = 0.6;\n"+
  89.       "  float max = max(abs(p.x),max(abs(p.y),abs(p.z)));\n"+
  90.       "  if(max > t1) return 0.0;\n"+
  91.       "  else if(max > t0) return (t1-max)/(t1-t0);\n"+
  92.       "  else return 1.0;\n"+
  93.       "};\n"+
  94.       "void main() {\n"+
  95.       "  float spread = 1.0;\n"+
  96.       "  float val=0.0;\n"+
  97.       "  float delta="+delta+";\n"+
  98.       "  float cameraz="+cameraz+";\n"+
  99.       "  float nearz="+nearz+";\n"+
  100.       "  float farz="+farz+";\n"+
  101.       "  float r=0.0,g=0.0,b=0.0;"+
  102.       "  vec3 ray=vec3(0.0,0.0,0.0);\n"+
  103.       "  mat3 rot=genRotMat(sin(t/3.13)*360.0,1.0,0.0,0.0);\n"+
  104.       "  rot=rot*genRotMat(sin(t/3.64)*360.0,0.0,1.0,0.0);\n"+
  105.       "  rot=rot*genRotMat(sin(t/3.24)*360.0,0.0,0.0,1.0);\n"+
  106.       "  ray.z=nearz;\n"+
  107.       "  ray.xy+=vtex.xy*spread*(nearz-cameraz);\n"+
  108.       "  for(int i=0;i<"+iterations+";i++){\n"+
  109.       "    vec3 temp;\n"+
  110.       "    ray.xy+=vtex.xy*spread*delta;\n"+
  111.       "    temp=ray*rot;\n"+
  112.       "    val+=cubeDist(temp);\n"+
  113.       "    r+=abs(temp.x*delta*delta*val);\n"+
  114.       "    g+=abs(temp.y*delta*delta*val);\n"+
  115.       "    b+=abs(temp.z*delta*delta*val);\n"+
  116.       "    ray.z+=delta;\n"+
  117.       "  }\n"+
  118.       "  gl_FragColor=vec4(r,g,b,1.0);\n"+
  119.       "}\n";
  120.   private final String texvs =
  121.     "  attribute vec4 aPosition;\n"+
  122.       "attribute vec2 aTextureCoord;\n"+
  123.       "varying vec2 vTextureCoord;\n"+
  124.       "void main() {\n"+
  125.       "  gl_Position = aPosition;\n"+
  126.       "  vTextureCoord = aTextureCoord;\n"+
  127.       "}\n";
  128.   private final String texfs =
  129.     "  precision mediump float;\n"+
  130.       "varying vec2 vTextureCoord;\n"+
  131.       "uniform sampler2D sTexture;\n"+
  132.       "void main() {\n"+
  133.       "  gl_FragColor = texture2D(sTexture, vTextureCoord);\n"+
  134.       "}\n";
  135.   public IntroRenderer(Context context) {
  136.     vbuf = ByteBuffer.allocateDirect(v.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  137.     vbuf.put(v).position(0);
  138.     uvbuf = ByteBuffer.allocateDirect(uv.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  139.     uvbuf.put(uv).position(0);
  140.     texuvbuf = ByteBuffer.allocateDirect(texuv.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  141.     texuvbuf.put(texuv).position(0);
  142.   }
  143.   private void generateTexture() {
  144.     GLES20.glGenFramebuffers(1,fb,0);
  145.     GLES20.glGenRenderbuffers(1,drb,0);
  146.     GLES20.glGenTextures(1,tex,0);
  147.     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,tex[0]);
  148.     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
  149.     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
  150.     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_NEAREST);
  151.     GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
  152.     int[] buf = new int[texresx*texresy];
  153.     texbuf = ByteBuffer.allocateDirect(buf.length*4).order(ByteOrder.nativeOrder()).asIntBuffer();
  154.     GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D,0,GLES20.GL_RGB,texresx,texresy,0,GLES20.GL_RGB,GLES20.GL_UNSIGNED_SHORT_5_6_5,texbuf);
  155.     GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER,drb[0]);
  156.     GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER,GLES20.GL_DEPTH_COMPONENT16,texresx,texresy);
  157.   }
  158.   public void onDrawFrame(GL10 glUnused) {
  159.     frameCount++;
  160.     long time = SystemClock.uptimeMillis();
  161.     long relTime = time-startTime;
  162.     if(time>lastTime+1000) {
  163.       System.out.println("fps: "+frameCount);
  164.       frameCount = 0;
  165.       lastTime = time;
  166.     }
  167.     float tf = (float)relTime/1000;
  168.     if(downscale) {
  169.       GLES20.glViewport(0,0,texresx,texresy);
  170.       GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,fb[0]);
  171.       GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,GLES20.GL_COLOR_ATTACHMENT0,GLES20.GL_TEXTURE_2D,tex[0],0);
  172.       GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER,GLES20.GL_DEPTH_ATTACHMENT,GLES20.GL_RENDERBUFFER,drb[0]);
  173.     } else {
  174.       GLES20.glViewport(0,0,texresx,texresy);
  175.     }
  176.     GLES20.glUseProgram(prog);
  177.     GLES20.glClearColor(0.0f,0.0f,0.0f,1.0f);
  178.     GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT|GLES20.GL_COLOR_BUFFER_BIT);
  179.     GLES20.glUniform1f(GLES20.glGetUniformLocation(prog,"t"),tf);
  180.     GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(prog,"apos"),2,GLES20.GL_FLOAT,false,2*4,vbuf);
  181.     GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(prog,"apos"));
  182.     GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(prog,"atex"),2,GLES20.GL_FLOAT,false,2*4,uvbuf);
  183.     GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(prog,"atex"));
  184.     GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,v.length/2);
  185.     if(downscale) {
  186.       GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,0);
  187.       GLES20.glViewport(0,0,xres,yres);
  188.       GLES20.glUseProgram(texprog);
  189.       GLES20.glClearColor(0.0f,0.0f,0.0f,1.0f);
  190.       GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT|GLES20.GL_COLOR_BUFFER_BIT);
  191.       GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  192.       GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,tex[0]);
  193.       GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(texprog,"aPositionCoord"),2,GLES20.GL_FLOAT,false,2*4,vbuf);
  194.       GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(texprog,"aPositionCoord"));
  195.       GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(texprog,"aTextureCoord"),2,GLES20.GL_FLOAT,false,2*4,texuvbuf);
  196.       GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(texprog,"aTextureCoord"));
  197.       GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,v.length/2);
  198.     }
  199.   }
  200.   public void onSurfaceChanged(GL10 glUnused, int width, int height) {
  201.     GLES20.glViewport(0,0,width,height);
  202.     xres = width;
  203.     yres = height;
  204.     float ratio = (float)width/height;
  205.     float tempuv[] = {
  206.       -ratio,-1.0f,
  207.       -ratio,1.0f,
  208.       ratio,1.0f,
  209.       -ratio,-1.0f,
  210.       ratio,1.0f,
  211.       ratio,-1.0f
  212.     };
  213.     uv = tempuv;
  214.     uvbuf = ByteBuffer.allocateDirect(uv.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  215.     uvbuf.put(uv).position(0);
  216.     texresx = xres/texscale;
  217.     texresy = yres/texscale;
  218.     generateTexture();
  219.   }
  220.   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
  221.     prog = createProgram(vs,fs);
  222.     texprog = createProgram(texvs,texfs);
  223.   }
  224.   private int loadShader(int shaderType, String source) {
  225.     int shader = GLES20.glCreateShader(shaderType);
  226.     if(shader!=0) {
  227.       GLES20.glShaderSource(shader,source);
  228.       GLES20.glCompileShader(shader);
  229.       int[] compiled = new int[1];
  230.       GLES20.glGetShaderiv(shader,GLES20.GL_COMPILE_STATUS,compiled,0);
  231.       if(compiled[0]==0) {
  232.         Log.e(TAG,"Could not compile shader "+shaderType+":");
  233.         Log.e(TAG,GLES20.glGetShaderInfoLog(shader));
  234.         GLES20.glDeleteShader(shader);
  235.         shader = 0;
  236.       }
  237.     }
  238.     return shader;
  239.   }
  240.   private int createProgram(String vertexSource, String fragmentSource) {
  241.     int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER,vertexSource);
  242.     if(vertexShader==0) {
  243.       return 0;
  244.     }
  245.     int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentSource);
  246.     if(pixelShader==0) {
  247.       return 0;
  248.     }
  249.     int program = GLES20.glCreateProgram();
  250.     if(program!=0) {
  251.       GLES20.glAttachShader(program,vertexShader);
  252.       checkGlError("glAttachShader");
  253.       GLES20.glAttachShader(program,pixelShader);
  254.       checkGlError("glAttachShader");
  255.       GLES20.glLinkProgram(program);
  256.       int[] linkStatus = new int[1];
  257.       GLES20.glGetProgramiv(program,GLES20.GL_LINK_STATUS,linkStatus,0);
  258.       if(linkStatus[0]!=GLES20.GL_TRUE) {
  259.         Log.e(TAG,"Could not link program: ");
  260.         Log.e(TAG,GLES20.glGetProgramInfoLog(program));
  261.         GLES20.glDeleteProgram(program);
  262.         program = 0;
  263.       }
  264.     }
  265.     return program;
  266.   }
  267.   private void checkGlError(String op) {
  268.     int error;
  269.     while ((error = GLES20.glGetError())!=GLES20.GL_NO_ERROR) {
  270.       Log.e(TAG,op+": glError "+error);
  271.       throw new RuntimeException(op+": glError "+error);
  272.     }
  273.   }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment