Subversion Repositories AndroidProjects

Rev

Rev 278 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.libGDXtest;

import java.io.IOException;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.gebauz.Bauzoid.app.Consts;
import com.gebauz.Bauzoid.app.Game;
import com.gebauz.Bauzoid.gamestates.BaseGameState;
import com.gebauz.Bauzoid.graphics.model.Geometry;
import com.gebauz.Bauzoid.graphics.model.Model;
import com.gebauz.Bauzoid.graphics.model.ModelUtil;
import com.gebauz.Bauzoid.graphics.model.SimpleGeometry;
import com.gebauz.Bauzoid.graphics.shader.ShaderProgram;
import com.gebauz.Bauzoid.graphics.shader.ShaderUtil;
import com.gebauz.Bauzoid.graphics.sprite.AtlasSprite;
import com.gebauz.Bauzoid.graphics.sprite.AtlasSpriteInstance;
import com.gebauz.Bauzoid.graphics.sprite.Sprite;
import com.gebauz.Bauzoid.math.MathUtil;
import com.gebauz.Bauzoid.math.Matrix4;


public class TestState extends BaseGameState
{
        private Matrix4 mProjection;
        private Matrix4 mView;
        private Matrix4 mModel;

        private Model mTestModel = null;
       
        private float mAngle = 0.0f;
       
        private SimpleGeometry mTestMesh = null;
        private ShaderProgram mShaderProgram;
       
        private Sprite mSprite;
        private AtlasSprite mSpriteAtlas;
        private AtlasSpriteInstance mSprite2;
        private AtlasSpriteInstance mSprite3;
       
        public TestState(Game game)
        {
                super(game);
                setFading(false, false);
        }
       
        @Override
        public void init()
        {
                //create shader program
                String vertexShader =
                        "uniform mat4 uMVPMatrix;               \n" +
                        "attribute vec4 vPosition;              \n" +
                        "attribute vec4 vColor;                 \n" +
                        "varying vec4 v_color;                  \n" +                  
                        "void main()                                    \n" +
                        "{                                                              \n" +
                        "       gl_Position = uMVPMatrix * vPosition;   \n" +
                        "       v_color = vColor;                       \n" +
                        "}                                                              \n";
                String fragmentShader =
                        "#ifdef GL_ES                                                           \n"+
                        "precision mediump float;                                       \n"+
                        "#endif                                                                         \n"+
                        "varying vec4 v_color;                                          \n"+
                        "void main()                                                            \n"+
                        "{                                                                                      \n"+
                        "       gl_FragColor = v_color;                                 \n"+
                        "}                                                                                      \n";
               
                mShaderProgram = ShaderUtil.createShaderFromString(getGame().getGraphics(), vertexShader, fragmentShader);
               
                try
                {
                        mTestModel = ModelUtil.createModelFromFile(getGame().getGraphics(), Gdx.files.internal("data/conifer.model"));
                        mTestModel.upload();
                }
                catch (IOException e)
                {
                        Gdx.app.log("GBZ", "exception");
                }
               
                float[] verts = new float[]
                {
                        1.5f, -3.5f, 0,
                        3.5f,  3.5f, 0,
                        7.5f, -3.5f, 0
                };
               
                float[] colors = new float[]
                {
                        1.0f, 0.5f, 0.75f, 1.0f,
                        0.3f, 0.75f, 1.0f, 1.0f,
                        0.3f, 0.75f, 0.5f, 1.0f,                       
                };
               
                mTestMesh = new SimpleGeometry(getGraphics(), Geometry.PrimitiveType.TRIANGLES);
                mTestMesh.setPositions(verts);
                mTestMesh.setColors(colors);
               
                mSprite = new Sprite(getGraphics(), new Texture(Gdx.files.internal("data/testsprite.png")));
               
                Texture texture = new Texture(Gdx.files.internal("data/testsprite2.png"));
               
                AtlasSprite.Region[] regions = {
                        new AtlasSprite.Region(texture, 0, 20, 62, 62),
                        new AtlasSprite.Region(texture, 3, 88, 80, 82)
                };
               
                mSpriteAtlas = new AtlasSprite(getGraphics(), texture, regions);
               
                mSprite2 = mSpriteAtlas.createSpriteInstance(0);
                mSprite3 = mSpriteAtlas.createSpriteInstance(1);
        }
       
        @Override
        public void exit()
        {
                mTestMesh.dispose();
                mSprite.dispose();
                mShaderProgram.dispose();
        }

        @Override
        public void update(float deltaTime)
        {
                mAngle += 90.0f * (deltaTime);
        }

        @Override
        public void render()
        {
                int w = getGame().getGraphics().getWidth();
                int h = getGame().getGraphics().getHeight();
               
                Gdx.gl20.glViewport(0, 0, w, h);
               
                float ratio = (float)w / (float)h;
               
                mModel = Matrix4.createRotationY(mAngle);
                mProjection = Matrix4.createPerspective(45.0f, ratio, 1.0f, 300.0f);
                mView = Matrix4.createLookAt(0, 5, -10.0f, 0.0f, 2.0f, 0.0f, 0.0f, 1.0f, 0.0f);        
               
                Gdx.gl20.glClearColor(0.0f, 0.3f, 0.5f, 0.0f);
                Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
               
                getRenderStates().model = mModel;
                getRenderStates().view = mView;
                getRenderStates().projection = mProjection;
               
                if (mTestModel != null)
                {
                        mTestModel.render();
                       
                        mShaderProgram.activate();

                        getRenderStates().culling.setEnabled(false);
                        getRenderStates().activate();
                        //mTestMesh.render();
                        getRenderStates().deactivate();
                        mShaderProgram.deactivate();
                       
                        getRenderStates().view = Matrix4.createIdentity();
                        getRenderStates().projection = Matrix4.createOrtho(0.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 0.0f, 0.0f, 1.0f);
                       
                        mSprite.angle = mAngle;
                        mSprite.x = 400.0f;
                        mSprite.y = 240.0f;            
                        mSprite.alpha = 0.5f * MathUtil.sin(mAngle) + 0.5f;
                        mSprite.render();
                       

                        mSprite2.x = 200.0f;
                        mSprite2.y = 100.0f;
                        mSprite2.render();
                       
                        mSprite3.x = 200.0f;
                        mSprite3.y = 350.0f;
                        mSprite3.angle = mAngle;
                        mSprite3.render();
                }
               
        int error = Gdx.gl.glGetError();
        if (error != GL20.GL_NO_ERROR)
        {
                Gdx.app.log(Consts.LOG_TAG, "gl error: " + error);
        }
        }
}