Android Open Source - ExpertAndroid E S20 Controlled Animated Textured Cube Renderer






From Project

Back to project page ExpertAndroid.

License

The source code is released under:

MIT License

If you think the Android project ExpertAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.iuriio.demos.expertandroid.ch9openglexperiments;
//www.j  av  a  2 s .co m
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.Matrix;
import android.os.SystemClock;

//filename: SimpleTriangleRenderer.java
public class ES20ControlledAnimatedTexturedCubeRenderer extends ES20SingleTextureAbstractRenderer
{
    //A raw native buffer to hold the point coordinates
    private FloatBuffer mFVertexBuffer;
    private FloatBuffer mFTextureBuffer;
    private static final int FLOAT_SIZE_BYTES = 4;
    
    private boolean stopFlag = false;
    private float curAngle = 0;
    private float stoppedAtAngle = 0;
    

//front-face: f1,f2,f3,f4
//starting top-right-anti-clockwise    
//f1(1,1) f2(-1,1) f3(-1,-1) f4(1,-1): z plane 0
    
//back-face: b1,b2,b3,b4
//starting bottom-right-anti-clockwise    
//b1(1,-1) b2(1,1) b3(-1,1) b4(-1,-1) : z plane 1
    private float z = 2.0f;
    private final float[] mTriangleVerticesData = {
        //1. front-triangles
        //f1,f2,f3
        1,1,0,      -1,1,0,          -1,-1,0,
        
        //f3,f4,f1
        -1,-1,0,     1,-1,0,        1,1,0,        
        
        //2. back-triangles
        //b1,b2,b3
        1,-1,z,        1,1,z,        -1,1,z,
        //b3,b4,b1
        -1,1,z,        -1,-1,z,        1,-1,z,
        
        //3. right-triangles
        //b2,f1,f4
        1,1,z,        1,1,0,        1,-1,0,
        //b2,f4,b1
        1,1,z,      1,-1,0,      1,-1,z,
        
        //4. left-triangles
        //b3, f2, b4
        -1,1,z,        -1,1,0,        -1,-1,z,
        //b4 f2 f3
        -1,-1,z,        -1,1,0,        -1,-1,0,
        
        //5. top-triangles
        //b2, b3, f2
        1,1,z,      -1,1,z,        -1,1,0,
        //b2, f2, f1
        1,1,z,      -1,1,0,      1,1,0,
        
        //6. bottom-triangles
        //b1, b4, f3
        1,-1,z,    -1,-1,z,  -1,-1,0,
        //b1, f3, f4
        1,-1,z,    -1,-1,0,  1,-1,0
/*
 */        
            };

    //f1(1,1) f2(0,1) f3(0,0) f4(1,0) 
    //b1(1,0) b2(1,1) b3(0,1) b4(0,0)
    private final float[] mTextureData = {
        //1. front-triangles
        //f1,f2,f3
        1,0,      0,0,        0,1,
        //f3,f4,f1
        0,1, 1,1,1,0,               
        
        //2. back-triangles
        //b1,b2,b3
        1,0,  1,1,  0,1,
        //b3,b4,b1
        0,1,  0,0,  1,0,
        
        //3. right-triangles
        //b2,f1,f4
        1,1,  0,1,  0,0,
        //b2,f4,b1
        1,1,  0,0,  1,0,
        
        //4. left-triangles
        //b3, f2, b4
        0,1,   1,1,   0,0,
        //b4 f2 f3
        0,0,   1,1,   1,0,
        
        //5. top-triangles
        //b2, b3, f2
        1,1,   0,1,   0,0,
        //b2, f2, f1
        1,1,   0,0,   1,0,
        
        //6. bottom-triangles
        //b1, b4, f3
        1,1,  0,1,  0,0,
        //b1, f3, f4
        1,1,  0,0,  1,0
        /*        
*/        
            };
    public ES20ControlledAnimatedTexturedCubeRenderer(Context context) 
    {
      setupVertexBuffer();
      setupTextureBuffer();
    }
    private void setupTextureBuffer()
    {
      //Allocate and handle texture buffer
        ByteBuffer vbb1 = ByteBuffer.allocateDirect(mTextureData.length
                * FLOAT_SIZE_BYTES);
        vbb1.order(ByteOrder.nativeOrder());
        mFTextureBuffer = vbb1.asFloatBuffer();
        mFTextureBuffer.put(mTextureData);
        mFTextureBuffer.position(0);
    }
    
    private void setupVertexBuffer()
    {
      //Allocate and handle vertex buffer
        ByteBuffer vbb = ByteBuffer.allocateDirect(mTriangleVerticesData.length
                * FLOAT_SIZE_BYTES);
        vbb.order(ByteOrder.nativeOrder());
        mFVertexBuffer = vbb.asFloatBuffer();
        mFVertexBuffer.put(mTriangleVerticesData);
        mFVertexBuffer.position(0);
    }
    private void transferVertexPoints(int vertexPositionHandle)
    {
        GLES20.glVertexAttribPointer(vertexPositionHandle, 3, 
            GLES20.GL_FLOAT, false,
                0, mFVertexBuffer);
        checkGlError("glVertexAttribPointer maPosition");
        //mFVertexBuffer.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
        GLES20.glEnableVertexAttribArray(vertexPositionHandle);
        checkGlError("glEnableVertexAttribArray maPositionHandle");
    }

    private void transferTexturePoints(int texturePositionHandle)
    {
        GLES20.glVertexAttribPointer(texturePositionHandle, 2, GLES20.GL_FLOAT, false,
                0, mFTextureBuffer);
        checkGlError("glVertexAttribPointer texture array");
        //mFVertexBuffer.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
        GLES20.glEnableVertexAttribArray(texturePositionHandle);
        checkGlError("glEnableVertexAttribArray textures");
        //this.bindTextureSamplerToTextureId();
    }
    protected void draw(GL10 gl, int positionHandle)
    {
      GLES20.glEnable(GLES20.GL_DEPTH_TEST);
      GLES20.glDepthFunc(GLES20.GL_LESS);
      transferVertexPoints(positionHandle);
      transferTexturePoints(getTextureHandle());
      
        //Break time into 4000 parts
        //each part is .090 so that in 4000 parts it will be 360
        
      if (stopFlag == true)
      {
        //stop rotation
        curAngle = stoppedAtAngle;
      }
      else
      {
            curAngle += 1.0f;
      }
      if (curAngle > 360)
      {
        curAngle = 0;
      }
        
        this.initializeMatrices();
        //Center the cube
        this.trnslate(0,0,-1);
        
        //Rotate it around y axis
        this.rotate(curAngle, 0,-1,0);
        
        //Decenter it to where ever you want
        this.trnslate(0,-2,2);
        
        this.setupMatrices();
        int vertexCount = mTriangleVerticesData.length/3;
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
        checkGlError("glDrawArrays");
    }
    @Override
    protected FrustumDimensions getFrustumDimensions() 
    {
      return FrustumDimensions.getMedium();
    }
    
    private static final String VERTEX_SHADER_FILENAME = "tex_vertex_shader_1.txt";
    private static final String FRAGMENT_SHADER_FILENAME = "tex_fragment_shader_1.txt";
    @Override
    protected String getFragmentShaderCodeString()
    {
      return this.getStringFromAssetFile(FRAGMENT_SHADER_FILENAME);
      //return this.getDefaultFragmentShaderCodeString();
    }
    @Override
    protected String getVertexShaderCodeString()
    {
      return this.getStringFromAssetFile(VERTEX_SHADER_FILENAME);
      //return this.getDefaultVertexShaderCodeString();
    }
    public void stop()
    {
      this.stopFlag = true;
      this.stoppedAtAngle = curAngle;
    }
    public void start()
    {
      this.stopFlag = false;
      this.curAngle = this.stoppedAtAngle;
    }
}




Java Source Code List

com.androidbook.notebad.NoteEditor.java
com.androidbook.notebad.NotePadProvider.java
com.androidbook.notebad.NotePad.java
com.androidbook.notebad.NotesList.java
com.androidbook.notebad.TitleEditor.java
com.androidbook.parse.BaseActivity.java
com.androidbook.parse.BaseListActivity.java
com.androidbook.parse.CreateAMeaningActivity.java
com.androidbook.parse.CreateAWordActivity.java
com.androidbook.parse.Field.java
com.androidbook.parse.FormActivity.java
com.androidbook.parse.IReportBack.java
com.androidbook.parse.IValidator.java
com.androidbook.parse.IValueValidator.java
com.androidbook.parse.LoginActivity.java
com.androidbook.parse.ParseApplication.java
com.androidbook.parse.ParseObjectWrapperOld1.java
com.androidbook.parse.ParseObjectWrapper.java
com.androidbook.parse.ParseStarterProjectActivity.java
com.androidbook.parse.PasswordFieldRule.java
com.androidbook.parse.PasswordResetActivity.java
com.androidbook.parse.PasswordResetSuccessActivity.java
com.androidbook.parse.SignupActivity.java
com.androidbook.parse.SignupSuccessActivity.java
com.androidbook.parse.StringUtils.java
com.androidbook.parse.UserListActivity.java
com.androidbook.parse.WelcomeActivity.java
com.androidbook.parse.WordListActivity.java
com.androidbook.parse.WordListAdapter.java
com.androidbook.parse.WordMeaningListAdapter.java
com.androidbook.parse.WordMeaning.java
com.androidbook.parse.WordMeaningsListActivity.java
com.androidbook.parse.Word.java
com.iuriio.demos.expertandroid.ch10search.MainActivity.java
com.iuriio.demos.expertandroid.ch10search.SearchActivity.java
com.iuriio.demos.expertandroid.ch11searchprovider.MainActivity.java
com.iuriio.demos.expertandroid.ch11searchprovider.SearchActivity.java
com.iuriio.demos.expertandroid.ch11searchprovider.SimpleSuggestionProvider.java
com.iuriio.demos.expertandroid.ch11searchprovider.SuggestUrlProvider.java
com.iuriio.demos.expertandroid.ch13parsesimple.BaseActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.LoginActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.MainActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.ParseApp.java
com.iuriio.demos.expertandroid.ch13parsesimple.ParseObjectWrapper.java
com.iuriio.demos.expertandroid.ch13parsesimple.ParseStarterProjectActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.PasswordResetSuccessActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.ResetPasswordActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.SignupActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.StringUtils.java
com.iuriio.demos.expertandroid.ch13parsesimple.WordListActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.Word.java
com.iuriio.demos.expertandroid.ch1circleview.AbstractBaseView.java
com.iuriio.demos.expertandroid.ch1circleview.CircleView.java
com.iuriio.demos.expertandroid.ch1circleview.MainActivity.java
com.iuriio.demos.expertandroid.ch2durationcontrol.DatePickerFragment.java
com.iuriio.demos.expertandroid.ch2durationcontrol.DurationControl.java
com.iuriio.demos.expertandroid.ch2durationcontrol.MainActivity.java
com.iuriio.demos.expertandroid.ch3flowlayout.FlowLayout.java
com.iuriio.demos.expertandroid.ch3flowlayout.MainActivity.java
com.iuriio.demos.expertandroid.ch4gsonserialization.ChildObject.java
com.iuriio.demos.expertandroid.ch4gsonserialization.MainActivity.java
com.iuriio.demos.expertandroid.ch4gsonserialization.MainObject.java
com.iuriio.demos.expertandroid.ch6forms.BaseActivity.java
com.iuriio.demos.expertandroid.ch6forms.Field.java
com.iuriio.demos.expertandroid.ch6forms.FormActivity.java
com.iuriio.demos.expertandroid.ch6forms.IValidator.java
com.iuriio.demos.expertandroid.ch6forms.IValueValidator.java
com.iuriio.demos.expertandroid.ch6forms.MainActivity.java
com.iuriio.demos.expertandroid.ch6forms.PasswordFieldRule.java
com.iuriio.demos.expertandroid.ch6forms.StringUtils.java
com.iuriio.demos.expertandroid.ch6forms.WelcomeActivity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.AbstractRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.AnimatedSimpleTriangleRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20AbstractRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20ControlledAnimatedTexturedCubeRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20SimpleTriangleRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20SingleTextureAbstractRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.FrustumDimensions.java
com.iuriio.demos.expertandroid.ch9openglexperiments.MainActivity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.MyApplication.java
com.iuriio.demos.expertandroid.ch9openglexperiments.OpenGLES10Activity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.OpenGLES20Activity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.PolygonRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.RegularPolygon.java
com.iuriio.demos.expertandroid.ch9openglexperiments.Shape.java
com.iuriio.demos.expertandroid.ch9openglexperiments.SimpleTriangleRenderer.java
converters.FieldTransporter.java
converters.IFieldTransport.java
converters.IntegerFieldTransport.java
converters.ParseObjectEssentials.java
converters.StringFieldTransport.java
converters.User.java
converters.ValueField.java