Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.Bauzoid.graphics;

import javax.microedition.khronos.opengles.GL10;

import com.gebauz.Bauzoid.graphics.model.SimpleGeometry;
import com.gebauz.Bauzoid.math.Vector2;
import com.gebauz.Bauzoid.math.Vector3;
import com.gebauz.Bauzoid.math.Vector4;

public class RenderImmediate
{
        public static final int MAX_VERTICES = 512;
        private static float mVertices[] = new float[MAX_VERTICES * SimpleGeometry.COORD_ELEMENTS_COUNT];
        private static float mNormals[] = new float[MAX_VERTICES * SimpleGeometry.NORMAL_ELEMENTS_COUNT];
        private static float mColors[] = new float[MAX_VERTICES * SimpleGeometry.COLOR_ELEMENTS_COUNT];
        private static float mTexCoords[] = new float[MAX_VERTICES * SimpleGeometry.TEX_COORD_ELEMENTS_COUNT];
       
        private static Vector3 mCurrentNormal = new Vector3(0.0f, 1.0f, 0.0f);
        private static Vector4 mCurrentColor = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);
        private static Vector2 mCurrentTexCoord = new Vector2(0.0f, 0.0f);
       
        private static short mNumVertices = 0;
        private static boolean mInsideBeginEnd = false;
       
        private static SimpleGeometry mMesh = new SimpleGeometry();
        private static int mPrimitiveType = GL10.GL_TRIANGLES;
       
        private RenderImmediate()
        {              
        }
       
        public static void begin(int primitiveType)
        {
                if (mInsideBeginEnd)
                {
                        throw new RuntimeException("Inside begin/end block!");
                }
               
                mInsideBeginEnd = true;
                mNumVertices = 0;
                mPrimitiveType = primitiveType;
        }
       
        public static void end()
        {
                mMesh.setVertices(mVertices);
                mMesh.setNormals(mNormals);
                mMesh.setColors(mColors);
                mMesh.setTexCoords(mTexCoords);
               
                mMesh.setPrimitiveType(mPrimitiveType);
                mMesh.render(mNumVertices);

                mInsideBeginEnd = false;
                mNumVertices = 0;
        }
       
        public static void vertex(float x, float y, float z)
        {
                mVertices[mNumVertices * SimpleGeometry.COORD_ELEMENTS_COUNT + 0] = x;
                mVertices[mNumVertices * SimpleGeometry.COORD_ELEMENTS_COUNT + 1] = y;
                mVertices[mNumVertices * SimpleGeometry.COORD_ELEMENTS_COUNT + 2] = z;

                mNormals[mNumVertices * SimpleGeometry.NORMAL_ELEMENTS_COUNT + 0] = mCurrentNormal.x;
                mNormals[mNumVertices * SimpleGeometry.NORMAL_ELEMENTS_COUNT + 1] = mCurrentNormal.y;
                mNormals[mNumVertices * SimpleGeometry.NORMAL_ELEMENTS_COUNT + 2] = mCurrentNormal.z;

                mColors[mNumVertices * SimpleGeometry.COLOR_ELEMENTS_COUNT + 0] = mCurrentColor.x;
                mColors[mNumVertices * SimpleGeometry.COLOR_ELEMENTS_COUNT + 1] = mCurrentColor.y;
                mColors[mNumVertices * SimpleGeometry.COLOR_ELEMENTS_COUNT + 2] = mCurrentColor.z;
                mColors[mNumVertices * SimpleGeometry.COLOR_ELEMENTS_COUNT + 3] = mCurrentColor.w;
               
                mTexCoords[mNumVertices * SimpleGeometry.TEX_COORD_ELEMENTS_COUNT + 0] = mCurrentTexCoord.x;
                mTexCoords[mNumVertices * SimpleGeometry.TEX_COORD_ELEMENTS_COUNT + 1] = mCurrentTexCoord.y;           
               
                mNumVertices++;
        }
       
        public static void normal(float x, float y, float z)
        {
                mCurrentNormal.x = x;
                mCurrentNormal.y = y;
                mCurrentNormal.z = z;
        }

        public static void color(float r, float g, float b, float a)
        {
                mCurrentColor.x = r;
                mCurrentColor.y = g;
                mCurrentColor.z = b;
                mCurrentColor.w = a;
        }
       
        public static void texCoords(float u, float v)
        {
                mCurrentTexCoord.x = u;
                mCurrentTexCoord.y = v;
        }
}