Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prod.demo.intro;
- 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.opengl.GLES20;
- import android.opengl.GLSurfaceView;
- import android.opengl.Matrix;
- import android.os.SystemClock;
- import android.util.Log;
- class IntroRenderer implements GLSurfaceView.Renderer {
- 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 = 8;
- private FloatBuffer vbuf;
- private FloatBuffer uvbuf;
- private FloatBuffer texuvbuf;
- private double nearz = -1.0;
- private double farz = 1.0;
- private double cameraz = -1.2;
- private int xres, yres;
- private double delta = 0.1;
- private int iterations = (int)((int)(farz-nearz)/delta);
- private long startTime = SystemClock.uptimeMillis();
- private int fb[] = new int[1];
- private int drb[] = new int[1];
- private int tex[] = new int[1];
- static long lastTime = SystemClock.uptimeMillis();
- static long frameCount = 0;
- int texresx, texresy;
- private IntBuffer texbuf;
- private int prog, texprog;
- private static String TAG = "intro";
- private boolean downscale = true;
- 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 mediump float;\n"+
- "uniform float t;\n"+
- "varying vec2 vtex;\n"+
- "mat3 genRotMat(float a0,float x,float y,float z){\n"+
- " float a=a0*3.1415926535897932384626433832795/180.0;\n"+
- " return mat3(\n"+
- " 1.0+(1.0-cos(a))*(x*x-1.0),\n"+
- " -z*sin(a)+(1.0-cos(a))*x*y,\n"+
- " y*sin(a)+(1.0-cos(a))*x*z,\n"+
- " z*sin(a)+(1.0-cos(a))*x*y,\n"+
- " 1.0+(1.0-cos(a))*(y*y-1.0),\n"+
- " -x*sin(a)+(1.0-cos(a))*y*z,\n"+
- " -y*sin(a)+(1.0-cos(a))*x*z,\n"+
- " x*sin(a)+(1.0-cos(a))*y*z,\n"+
- " 1.0+(1.0-cos(a))*(z*z-1.0)\n"+
- " );\n"+
- "}\n"+
- "float cubeDist(vec3 p){\n"+
- " float t0 = 0.5;\n"+
- " float t1 = 0.6;\n"+
- " float max = max(abs(p.x),max(abs(p.y),abs(p.z)));\n"+
- " if(max > t1) return 0.0;\n"+
- " else if(max > t0) return (t1-max)/(t1-t0);\n"+
- " else return 1.0;\n"+
- "};\n"+
- "void main() {\n"+
- " float spread = 1.0;\n"+
- " float val=0.0;\n"+
- " float delta="+delta+";\n"+
- " float cameraz="+cameraz+";\n"+
- " float nearz="+nearz+";\n"+
- " float farz="+farz+";\n"+
- " float r=0.0,g=0.0,b=0.0;"+
- " vec3 ray=vec3(0.0,0.0,0.0);\n"+
- " mat3 rot=genRotMat(sin(t/3.13)*360.0,1.0,0.0,0.0);\n"+
- " rot=rot*genRotMat(sin(t/3.64)*360.0,0.0,1.0,0.0);\n"+
- " rot=rot*genRotMat(sin(t/3.24)*360.0,0.0,0.0,1.0);\n"+
- " ray.z=nearz;\n"+
- " ray.xy+=vtex.xy*spread*(nearz-cameraz);\n"+
- " for(int i=0;i<"+iterations+";i++){\n"+
- " vec3 temp;\n"+
- " ray.xy+=vtex.xy*spread*delta;\n"+
- " temp=ray*rot;\n"+
- " val+=cubeDist(temp);\n"+
- " r+=abs(temp.x*delta*delta*val);\n"+
- " g+=abs(temp.y*delta*delta*val);\n"+
- " b+=abs(temp.z*delta*delta*val);\n"+
- " ray.z+=delta;\n"+
- " }\n"+
- " gl_FragColor=vec4(r,g,b,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 mediump float;\n"+
- "varying vec2 vTextureCoord;\n"+
- "uniform sampler2D sTexture;\n"+
- "void main() {\n"+
- " gl_FragColor = texture2D(sTexture, vTextureCoord);\n"+
- "}\n";
- public IntroRenderer(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);
- }
- 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);
- 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);
- }
- public void onDrawFrame(GL10 glUnused) {
- 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);
- GLES20.glUniform1f(GLES20.glGetUniformLocation(prog,"t"),tf);
- 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);
- }
- }
- public void onSurfaceChanged(GL10 glUnused, int width, int height) {
- 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 = xres/texscale;
- texresy = yres/texscale;
- generateTexture();
- }
- public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
- prog = createProgram(vs,fs);
- texprog = createProgram(texvs,texfs);
- }
- 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;
- }
- 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);
- checkGlError("glAttachShader");
- GLES20.glAttachShader(program,pixelShader);
- checkGlError("glAttachShader");
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment