Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.BauzoidTest;

import java.io.IOException;

import android.opengl.GLES20;
import android.os.SystemClock;
import android.util.Log;

import com.gebauz.Bauzoid.app.Game;
import com.gebauz.Bauzoid.file.File;
import com.gebauz.Bauzoid.file.FileUtil;
import com.gebauz.Bauzoid.gamestates.BaseGameState;
import com.gebauz.Bauzoid.gamestates.GameStateManager;
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.ShaderUniform;
import com.gebauz.Bauzoid.graphics.shader.ShaderUtil;
import com.gebauz.Bauzoid.graphics.texture.Texture;
import com.gebauz.Bauzoid.graphics.texture.TextureUtil;
import com.gebauz.Bauzoid.math.Matrix4;
import com.gebauz.Bauzoid.math.Vector3;

public class TestState extends BaseGameState
{
        private SimpleGeometry mMesh;  
        private Matrix4 mProjection;
        private Matrix4 mView;
        private Matrix4 mModel;
       
        private ShaderProgram mShader;
        private ShaderUniform mMVPMatrixUniform;
       
        private Model mTestModel = null;
        private Texture mTestTexture = null;
        private ShaderUniform mTextureSampler;
       
        public TestState(Game game)
        {
                super(game);
        }
       
        @Override
        public void init()
        {
                float coords[] =
                {
                                -8.5f, -8.25f, 0,
                                8.5f, -8.25f, 0,
                                0.0f, 8.559016994f, 0,
                                0.0f, 8.559016994f, 0,
                                8.5f, -8.25f, 0,
                                9.0f, -4.0f, -2.0f
                };
               
                float colors[] =
                {
                                1.0f, 0.0f, 0, 1.0f,
                                1.0f, 0.0f, 1.0f, 1.0f,
                                0, 1.0f, 0, 1.0f,
                                1.0f, 0.0f, 0, 1.0f,
                                1.0f, 0.0f, 1.0f, 1.0f,
                                0, 1.0f, 0, 1.0f
                };
               
                mMesh = new SimpleGeometry();
                mMesh.setVertices(coords);
                mMesh.setColors(colors);
                mMesh.createBuffers();         
               
                try
                {
                        mShader = ShaderUtil.createFromAsset(getGame().getGraphics(), "shaders/default.vs", "shaders/default.fs");
                }
                catch (IOException e)
                {
                        Log.v("GBZ", "exception");     
                }
               
                mMVPMatrixUniform = mShader.getUniform("uMVPMatrix");
                mTextureSampler = mShader.getUniform("uDiffuse");
               
                try
                {
                        mTestModel = ModelUtil.loadModelFromAsset(getGame().getGraphics(), "conifer.model");
                        mTestModel.upload();
                       
                        mTestTexture = TextureUtil.loadTextureFromAsset(getGame().getGraphics(), "tree_green.png");
                }
                catch (IOException e)
                {
                        Log.v("GBZ", "exception");
                }
        }
       
        @Override
        public void exit()
        {
       
               
        }

        @Override
        public void update(float deltaTime)
        {

        }

        @Override
        public void render()
        {
                int w = getGame().getGraphics().getWidth();
                int h = getGame().getGraphics().getHeight();
               
                GLES20.glViewport(0, 0, w, h);
               
                float ratio = (float)w / (float)h;
               
                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);        
               
        GLES20.glClearColor(0.0f, 0.3f, 0.5f, 0.0f);
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
       
        mShader.activate();
               
                long time = SystemClock.uptimeMillis() % 4000L;
                float angle = 0.090f * ((int)time);
               
                mModel = Matrix4.createRotationY(angle);
               
                Matrix4 mvpMatrix = Matrix4.multiply(mModel, mView);
                mvpMatrix = Matrix4.multiply(mvpMatrix, mProjection);
               
                mMVPMatrixUniform.setMatrix(mvpMatrix);
               
                //mMesh.render();
               
                if (mTestModel != null)
                {
                        if (mTextureSampler != null)
                                mTextureSampler.setInt(0);
                        mTestTexture.bind(0);          
                        mTestModel.render();
                        mTestTexture.unbind(0);
                }
        }
}