Ejemplo n.º 1
0
        private void ArrayToVBO(Array source, int size)
        {
            float[] floatArray = new float[size / 4];

            System.Buffer.BlockCopy(source, 0, floatArray, 0, (int)size);
            //temp Cut vertex
            int cutSize = 0;

            for (int i = 0; i < size / 4; i += 3)
            {
                if (floatArray[i + 1] < 20.0f)
                {
                    cutSize++;
                }
            }
            float[] cutArray = new float[cutSize * 3];

            int cutArrayIndex = 0;

            for (int i = 0; i < size / 4; i += 3)
            {
                if (floatArray[i + 1] < 20.0f)
                {
                    cutArray[cutArrayIndex]     = floatArray[i];
                    cutArray[cutArrayIndex + 1] = floatArray[i + 1];
                    cutArray[cutArrayIndex + 2] = floatArray[i + 2];
                    cutArrayIndex += 3;
                }
            }
            size       = cutSize * 3 * 4;
            objectSize = (int)(size / 4 / 3);

            FloatBuffer vertexBuffer;

            vertexBuffer = FloatBuffer.Allocate((int)size / 4); // float array to
            vertexBuffer.Put(cutArray, 0, (int)size / 4);
            vertexBuffer.Flip();

            GLES20.GlGenBuffers(1, VBOBuffers, 0);
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[0]);

            //VBOManager.setSize(fileName, vertexBuffer.Capacity() / 4); //is size of vertex count = 1 vertex 4 float x,y,z, 1
            GLES20.GlBufferData(GLES20.GlArrayBuffer, vertexBuffer.Capacity() * mBytesPerFloat, vertexBuffer, GLES20.GlStaticDraw);
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);
            handle = VBOBuffers[0];

            floatArray   = null;
            vertexBuffer = null;
        }
Ejemplo n.º 2
0
        public void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
        {
            const float coord = 1.0f;



            //FloatBuffer mTriangleVertices = ByteBuffer.AllocateDirect(triangleVerticesData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer();
            //mTriangleVertices.Put(triangleVerticesData).Flip();

            // Cube colors
            // R, G, B, A
            float[] triangleColorsData =
            {
                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f
            };

            FloatBuffer mTriangleColors = ByteBuffer.AllocateDirect(triangleColorsData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer();

            mTriangleColors.Put(triangleColorsData).Flip();

            //Cube texture UV Map
            float[] triangleTextureUVMapData =
            {
                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f
            };

            FloatBuffer mTriangleTextureUVMap = ByteBuffer.AllocateDirect(triangleTextureUVMapData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer();

            mTriangleTextureUVMap.Put(triangleTextureUVMapData).Flip();

            //triagles normals
            //This normal array is not right, it is spacialy DO FOR demonstrate how normals work with faces when light is calculated at shader program
            float[] triangleNormalData =
            {
                // Front face
                0.0f,   0.0f,  1.0f,
                0.0f,   0.0f,  1.0f,
                0.0f,   0.0f,  1.0f,
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,

                // Right face
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,

                // Back face
                0.0f,   0.0f, -1.0f,
                0.0f,   0.0f, -1.0f,
                0.0f,   0.0f, -1.0f,
                0.0f,   0.0f, -1.0f,
                0.0f,   0.0f, -1.0f,
                0.0f,   0.0f, -1.0f,

                // Left face
                -1.0f,  0.0f,  0.0f,
                -1.0f,  0.0f,  0.0f,
                -1.0f,  0.0f,  0.0f,
                -1.0f,  0.0f,  0.0f,
                -1.0f,  0.0f,  0.0f,
                -1.0f,  0.0f,  0.0f,

                // Top face
                0.0f,   1.0f,  0.0f,
                0.0f,   1.0f,  0.0f,
                0.0f,   1.0f,  0.0f,
                0.0f,   1.0f,  0.0f,
                0.0f,   1.0f,  0.0f,
                0.0f,   1.0f,  0.0f,

                // Bottom face
                0.0f,  -1.0f,  0.0f,
                0.0f,  -1.0f,  0.0f,
                0.0f,  -1.0f,  0.0f,
                0.0f,  -1.0f,  0.0f,
                0.0f,  -1.0f,  0.0f,
                0.0f,  -1.0f, 0.0f
            };

            FloatBuffer mTriangleNormal = ByteBuffer.AllocateDirect(triangleNormalData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer();

            mTriangleNormal.Put(triangleNormalData).Flip();

            //Data buffers to VBO
            GLES20.GlGenBuffers(4, VBOBuffers, 0); //2 buffers for vertices, texture and colors



            //--------------------------------------------
            //int resourceId = //context.Resources.GetIdentifier("object1_objvertex", "raw", context.PackageName);
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[0]);
            float[] floatArray;
            long    size;
            // Vertex
            FloatBuffer  vertexBuffer;
            Stream       fileIn = context.Resources.OpenRawResource(Resource.Raw.OldHouse_objvertex) as Stream;
            MemoryStream m      = new MemoryStream();

            fileIn.CopyTo(m);
            size       = m.Length;
            floatArray = new float[size / 4];
            objectSize = (int)(size / 4 / 3);
            System.Buffer.BlockCopy(m.ToArray(), 0, floatArray, 0, (int)size);

            vertexBuffer = FloatBuffer.Allocate((int)size / 4); // float array to
            vertexBuffer.Put(floatArray, 0, (int)size / 4);
            vertexBuffer.Flip();

            //VBOManager.setSize(fileName, vertexBuffer.Capacity() / 4); //is size of vertex count = 1 vertex 4 float x,y,z, 1
            GLES20.GlBufferData(GLES20.GlArrayBuffer, vertexBuffer.Capacity() * mBytesPerFloat, vertexBuffer, GLES20.GlStaticDraw);
            floatArray   = null;
            vertexBuffer = null;
            fileIn.Close();
            m.Close();
            //--------------------------------------------
            //GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleVertices.Capacity() * mBytesPerFloat, mTriangleVertices, GLES20.GlStaticDraw);

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[1]);
            GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleColors.Capacity() * mBytesPerFloat, mTriangleColors, GLES20.GlStaticDraw);


            //Textures -----------------------------------------
            //GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[2]);
            //GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleTextureUVMap.Capacity() * mBytesPerFloat, mTriangleTextureUVMap, GLES20.GlStaticDraw);

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[2]);
            // Vertex

            fileIn = context.Resources.OpenRawResource(Resource.Raw.OldHouse_objtexture) as Stream;
            m      = new MemoryStream();
            fileIn.CopyTo(m);
            size       = m.Length;
            floatArray = new float[size / 4];
            //objectSize = (int)(size / 4 / 3);
            System.Buffer.BlockCopy(m.ToArray(), 0, floatArray, 0, (int)size);

            vertexBuffer = FloatBuffer.Allocate((int)size / 4); // float array to
            vertexBuffer.Put(floatArray, 0, (int)size / 4);
            vertexBuffer.Flip();

            //VBOManager.setSize(fileName, vertexBuffer.Capacity() / 4); //is size of vertex count = 1 vertex 4 float x,y,z, 1
            GLES20.GlBufferData(GLES20.GlArrayBuffer, vertexBuffer.Capacity() * mBytesPerFloat, vertexBuffer, GLES20.GlStaticDraw);
            floatArray   = null;
            vertexBuffer = null;
            fileIn.Close();
            m.Close();


            //ENDOF Textures -----------------------------------------

            // GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[3]);
            /// GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleNormal.Capacity() * mBytesPerFloat, mTriangleNormal, GLES20.GlStaticDraw);
            /// Normales
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[3]);
            // Vertex

            fileIn = context.Resources.OpenRawResource(Resource.Raw.OldHouse_objnormal) as Stream;
            m      = new MemoryStream();
            fileIn.CopyTo(m);
            size       = m.Length;
            floatArray = new float[size / 4];
            //objectSize = (int)(size / 4 / 3);
            System.Buffer.BlockCopy(m.ToArray(), 0, floatArray, 0, (int)size);

            vertexBuffer = FloatBuffer.Allocate((int)size / 4); // float array to
            vertexBuffer.Put(floatArray, 0, (int)size / 4);
            vertexBuffer.Flip();

            //VBOManager.setSize(fileName, vertexBuffer.Capacity() / 4); //is size of vertex count = 1 vertex 4 float x,y,z, 1
            GLES20.GlBufferData(GLES20.GlArrayBuffer, vertexBuffer.Capacity() * mBytesPerFloat, vertexBuffer, GLES20.GlStaticDraw);
            floatArray   = null;
            vertexBuffer = null;
            fileIn.Close();
            m.Close();


            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);

            //Load and setup texture

            GLES20.GlGenTextures(1, textureHandle, 0); //init 1 texture storage handle
            if (textureHandle[0] != 0)
            {
                //Android.Graphics cose class Matrix exists at both Android.Graphics and Android.OpenGL and this is only sample of using
                Android.Graphics.BitmapFactory.Options options = new Android.Graphics.BitmapFactory.Options();
                options.InScaled = false; // No pre-scaling
                Android.Graphics.Bitmap bitmap = Android.Graphics.BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.body, options);
                GLES20.GlBindTexture(GLES20.GlTexture2d, textureHandle[0]);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlNearest);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlNearest);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge);
                GLUtils.TexImage2D(GLES20.GlTexture2d, 0, bitmap, 0);
                bitmap.Recycle();
            }

            //Ask android to run RAM garbage cleaner
            System.GC.Collect();

            //Setup OpenGL ES
            GLES20.GlClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            GLES20.GlEnable(GLES20.GlDepthTest); //uncoment if needs enabled dpeth test
            //  GLES20.GlEnable(2884); // GlCullFace == 2884 see OpenGL documentation to this constant value
            //  GLES20.GlCullFace(GLES20.GlFront);


            // Position the eye behind the origin.
            float eyeX = 0.0f;
            float eyeY = 0.0f;
            float eyeZ = 7.5f;

            // We are looking toward the distance
            float lookX = 0.0f;
            float lookY = 0.0f;
            float lookZ = -7.0f;

            // Set our up vector. This is where our head would be pointing were we holding the camera.
            float upX = 0.0f;
            float upY = coord;
            float upZ = 0.0f;

            // Set the view matrix. This matrix can be said to represent the camera position.
            // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and
            // view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose.
            Matrix.SetLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);

            //all "attribute" variables is "triagles" VBO (arrays) items representation
            //a_Possition[0] <=> a_Color[0] <=> a_TextureCoord[0] <=> a_Normal[0]
            //a_Possition[1] <=> a_Color[1] <=> a_TextureCoord[1] <=> a_Normal[1]
            //...
            //a_Possition[n] <=> a_Color[n] <=> a_TextureCoord[n] <=> a_Normal[n] -- where "n" is object buffers length
            //-> HOW MANY faces in your object (model) in VBO -> how many times the vertex shader will be called by OpenGL
            string vertexShader =
                "uniform mat4 u_MVPMatrix;      \n"         // A constant representing the combined model/view/projection matrix.
                + "uniform vec4 u_LightPos;       \n"       // A constant representing the light source position
                + "attribute vec4 a_Position;     \n"       // Per-vertex position information we will pass in. (it means vec4[x,y,z,w] but we put only x,y,z at this sample
                + "attribute vec4 a_Color;        \n"       // Per-vertex color information we will pass in.
                + "varying vec4 v_Color;          \n"       // This will be passed into the fragment shader.
                + "attribute vec2 a_TextureCoord; \n"       // Per-vertex texture UVMap information we will pass in.
                + "varying vec2 v_TextureCoord;   \n"       // This will be passed into the fragment shader.
                + "attribute vec3 a_Normal;       \n"       // Per-vertex normals information we will pass in.
                + "void main()                    \n"       // The entry point for our vertex shader.
                + "{                              \n"
                //light calculation section for fragment shader
                + "   vec3 modelViewVertex = vec3(u_MVPMatrix * a_Position);\n"
                + "   vec3 modelViewNormal = vec3(u_MVPMatrix * vec4(a_Normal, 0.0));\n"
                + "   float distance = length(u_LightPos.xyz - modelViewVertex);\n"
                + "   vec3 lightVector = normalize(u_LightPos.xyz - modelViewVertex);\n"
                + "   float diffuse = max(dot(modelViewNormal, lightVector), 0.1);\n"
                + "   diffuse = diffuse * (1.0 / (1.0 + (u_LightPos.w * distance * distance)));\n"
                + "   v_Color = vec4(diffuse, diffuse, diffuse, diffuse);\n" //Pass the color with light aspect to fragment shader R G B A
                                                                             //+ "   v_Color = vec4(a_Normal, 1.0);\n"   //Test normals array loading
                + "   v_TextureCoord = a_TextureCoord; \n"                   // Pass the texture coordinate through to the fragment shader. It will be interpolated across the triangle.
                + "   gl_Position = u_MVPMatrix   \n"                        // gl_Position is a special variable used to store the final position.
                + "                 * a_Position; \n"                        // Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
                + "}                              \n";

            string fragmentShader =
                "precision mediump float;       \n"                                       // Set the default precision to medium. We don't need as high of a
                                                                                          // precision in the fragment shader.
                + "varying vec4 v_Color;          \n"                                     // This is the color from the vertex shader interpolated across the triangle per fragment.
                + "varying vec2 v_TextureCoord;   \n"                                     // This is the texture coordinate from the vertex shader interpolated across the triangle per fragment.
                + "uniform sampler2D u_Texture;   \n"                                     // This is the texture image handler
                + "void main()                    \n"                                     // The entry point for our fragment shader.
                + "{                              \n"
                + "   gl_FragColor = texture2D(u_Texture, v_TextureCoord) * v_Color;  \n" // Pass the color directly through the pipeline.
                //+ "   gl_FragColor = v_Color;  \n"   // Pass the color directly through the pipeline.
                + "}                              \n";

            int vertexShaderHandle = GLES20.GlCreateShader(GLES20.GlVertexShader);

            if (vertexShaderHandle != 0)
            {
                // Pass in the shader source.
                GLES20.GlShaderSource(vertexShaderHandle, vertexShader);

                // Compile the shader.
                GLES20.GlCompileShader(vertexShaderHandle);

                // Get the compilation status.
                int[] compileStatus = new int[1];
                GLES20.GlGetShaderiv(vertexShaderHandle, GLES20.GlCompileStatus, compileStatus, 0);

                // If the compilation failed, delete the shader.
                if (compileStatus[0] == 0)
                {
                    GLES20.GlDeleteShader(vertexShaderHandle);
                    vertexShaderHandle = 0;
                }
            }

            if (vertexShaderHandle == 0)
            {
                throw new Exception("Error creating vertex shader.");
            }

            // Load in the fragment shader shader.
            int fragmentShaderHandle = GLES20.GlCreateShader(GLES20.GlFragmentShader);

            if (fragmentShaderHandle != 0)
            {
                // Pass in the shader source.
                GLES20.GlShaderSource(fragmentShaderHandle, fragmentShader);

                // Compile the shader.
                GLES20.GlCompileShader(fragmentShaderHandle);

                // Get the compilation status.
                int[] compileStatus = new int[1];
                GLES20.GlGetShaderiv(fragmentShaderHandle, GLES20.GlCompileStatus, compileStatus, 0);

                // If the compilation failed, delete the shader.
                if (compileStatus[0] == 0)
                {
                    GLES20.GlDeleteShader(fragmentShaderHandle);
                    fragmentShaderHandle = 0;
                }
            }

            if (fragmentShaderHandle == 0)
            {
                throw new Exception("Error creating fragment shader.");
            }

            // Create a program object and store the handle to it.
            int programHandle = GLES20.GlCreateProgram();

            if (programHandle != 0)
            {
                // Bind the vertex shader to the program.
                GLES20.GlAttachShader(programHandle, vertexShaderHandle);

                // Bind the fragment shader to the program.
                GLES20.GlAttachShader(programHandle, fragmentShaderHandle);

                // Bind attributes
                GLES20.GlBindAttribLocation(programHandle, 0, "a_Position");
                GLES20.GlBindAttribLocation(programHandle, 1, "a_Color");
                GLES20.GlBindAttribLocation(programHandle, 2, "a_TextureCoord");
                GLES20.GlBindAttribLocation(programHandle, 3, "a_Normal");

                // Link the two shaders together into a program.
                GLES20.GlLinkProgram(programHandle);

                // Get the link status.
                int[] linkStatus = new int[1];
                GLES20.GlGetProgramiv(programHandle, GLES20.GlLinkStatus, linkStatus, 0);

                // If the link failed, delete the program.
                if (linkStatus[0] == 0)
                {
                    GLES20.GlDeleteProgram(programHandle);
                    programHandle = 0;
                }
            }

            if (programHandle == 0)
            {
                throw new Exception("Error creating program.");
            }

            // Set program handles. These will later be used to pass in values to the program.
            mMVPMatrixHandle    = GLES20.GlGetUniformLocation(programHandle, "u_MVPMatrix");
            mLightPos           = GLES20.GlGetUniformLocation(programHandle, "u_LightPos");
            mPositionHandle     = GLES20.GlGetAttribLocation(programHandle, "a_Position");
            mColorHandle        = GLES20.GlGetAttribLocation(programHandle, "a_Color");
            mTextureCoordHandle = GLES20.GlGetAttribLocation(programHandle, "a_TextureCoord");
            mNormalHandle       = GLES20.GlGetAttribLocation(programHandle, "a_Normal");
            mTextureHandle      = GLES20.GlGetUniformLocation(programHandle, "u_Texture");


            // Tell OpenGL to use this program when rendering.
            GLES20.GlUseProgram(programHandle);
        }
Ejemplo n.º 3
0
        public override void loadFileToVBO(String fileName)
        {
            if (fileName == "")
            {
                return;
            }
            files.Add(fileName);
            int indexResult = VBOManager.getVBOIndex(fileName);

            if (indexResult == -1)
            {
                List <float[]> vertexes  = new List <float[]>();
                List <float[]> normales  = new List <float[]>();
                List <float[]> textures  = new List <float[]>();
                float          alphaStep = (float)(2 * Math.PI) / steps;
                float          alpha     = 0;

                //front and back faces
                for (int i = 0; i < steps; i++)
                {
                    //triagle face
                    float[] v1 = new float[4]; //vertex 1
                    float[] v2 = new float[4]; //vertex 2
                    float[] v3 = new float[4]; //vertex 3

                    //top face
                    v1[0] = 0;      //x
                    v1[1] = 0;      //y
                    v1[2] = height; //z
                    v1[3] = 1;

                    v2[0] = radius * (float)Math.Sin(alpha);
                    v2[1] = radius * (float)Math.Cos(alpha);
                    v2[2] = height; //z
                    v2[3] = 1;

                    alpha += alphaStep;

                    v3[0] = radius * (float)Math.Sin(alpha);
                    v3[1] = radius * (float)Math.Cos(alpha);
                    v3[2] = height; //z
                    v3[3] = 1;

                    vertexes.Add(v1);
                    vertexes.Add(v2);
                    vertexes.Add(v3);

                    //normales
                    float[] normale = new float[4]; //vertex 1
                    normale[0] = -1;                //x
                    normale[1] = 0;                 //y
                    normale[2] = 0;                 //z
                    normale[3] = 1;

                    normales.Add(normale);
                    normales.Add(normale);
                    normales.Add(normale);

                    float[] t1 = new float[2];
                    float[] t2 = new float[2];
                    float[] t3 = new float[2];
                    t1[0] = 0.0f;
                    t1[1] = 0.0f;
                    t2[0] = 0.0f;
                    t2[1] = 0.0f;
                    t3[0] = 0.0f;
                    t3[1] = 0.0f;
                    textures.Add(t1);
                    textures.Add(t2);
                    textures.Add(t3);


                    //spin face (from top) -----------
                    float[] vS1 = new float[4]; //vertex 1
                    float[] vS2 = new float[4]; //vertex 2
                    float[] vS3 = new float[4]; //vertex 3
                    vS1[0] = radius * (float)Math.Sin(alpha);
                    vS1[1] = radius * (float)Math.Cos(alpha);
                    vS1[2] = 0; //z
                    vS1[3] = 1;
                    vS2    = v2;
                    vS3    = v3;
                    vertexes.Add(vS1);
                    vertexes.Add(vS2);
                    vertexes.Add(vS3);

                    normale = new float[4]; //vertex 1
                    normales.Add(normale);
                    normales.Add(normale);
                    normales.Add(normale);

                    //SPIN TEXTURE FACE
                    //Spin face texture -----------------------------------

                    t1 = new float[2];
                    t2 = new float[2];
                    t3 = new float[2];

                    t1[0] = coordToTexture(textureStep);
                    t1[1] = coordToTexture(textureStep * i);
                    t2[0] = coordToTexture(textureStep * 2);
                    t2[1] = coordToTexture(textureStep * i);
                    t3[0] = coordToTexture(textureStep);                   //texture x
                    t3[1] = coordToTexture(textureStep * i + textureStep); //texture y


                    textures.Add(t3);
                    textures.Add(t2);
                    textures.Add(t1);
                    //--------------------------------------------------------

                    //back face -------------------------------------
                    alpha -= alphaStep;
                    v1     = new float[4]; //vertex 1
                    v2     = new float[4]; //vertex 2
                    v3     = new float[4]; //vertex 3

                    //top face
                    v1[0] = 0; //x
                    v1[1] = 0; //y
                    v1[2] = 0; //z
                    v1[3] = 1;

                    v2[0] = radius * (float)Math.Sin(alpha);
                    v2[1] = radius * (float)Math.Cos(alpha);
                    v2[2] = 0; //z
                    v2[3] = 1;

                    alpha += alphaStep;

                    v3[0] = radius * (float)Math.Sin(alpha);
                    v3[1] = radius * (float)Math.Cos(alpha);
                    v3[2] = 0; //z
                    v3[3] = 1;

                    vertexes.Add(v1);
                    vertexes.Add(v2);
                    vertexes.Add(v3);

                    //normales
                    normale    = new float[4]; //vertex 1
                    normale[0] = -1;           //x
                    normale[1] = 0;            //y
                    normale[2] = 0;            //z
                    normale[3] = 1;

                    normales.Add(normale);
                    normales.Add(normale);
                    normales.Add(normale);

                    t1    = new float[2];
                    t2    = new float[2];
                    t3    = new float[2];
                    t1[0] = 0;
                    t1[1] = 0;
                    t2[0] = 0;
                    t2[1] = 0;
                    t3[0] = 0;
                    t3[1] = 0;
                    textures.Add(t1);
                    textures.Add(t2);
                    textures.Add(t3);


                    //spin face (from top) -----------
                    alpha -= alphaStep;
                    vS1    = new float[4]; //vertex 1
                    vS2    = new float[4]; //vertex 2
                    vS3    = new float[4]; //vertex 3
                    vS1[0] = radius * (float)Math.Sin(alpha);
                    vS1[1] = radius * (float)Math.Cos(alpha);
                    vS1[2] = height; //z
                    vS1[3] = 1;

                    vS2 = v2;
                    vS3 = v3;
                    vertexes.Add(vS1);
                    vertexes.Add(vS2);
                    vertexes.Add(vS3);

                    normale = new float[4]; //vertex 1
                    normales.Add(normale);
                    normales.Add(normale);
                    normales.Add(normale);

                    t1    = new float[2];
                    t2    = new float[2];
                    t3    = new float[2];
                    t1[0] = coordToTexture(textureStep * 2); //texture x
                    t1[1] = coordToTexture(textureStep * i); //texture y
                    t2[0] = coordToTexture(textureStep * 2);
                    t2[1] = coordToTexture(textureStep * i + textureStep);
                    t3[0] = coordToTexture(textureStep);
                    t3[1] = coordToTexture(textureStep * i + textureStep);
                    textures.Add(t1);
                    textures.Add(t2);
                    textures.Add(t3);

                    alpha += alphaStep;
                }

                long size = vertexes.Count;               //all faces (faces*4 = all vertex)

                float[] floatArray = new float[size * 4]; //4 float to one vertex, 3 vertex to one face  slow way
                for (int i = 0; i < vertexes.Count; i++)
                {
                    floatArray[i * 4 + 0] = vertexes[i][0];
                    floatArray[i * 4 + 1] = vertexes[i][1];
                    floatArray[i * 4 + 2] = vertexes[i][2];
                    floatArray[i * 4 + 3] = vertexes[i][3];
                }

                vertexBuffer = FloatBuffer.Allocate((int)size * 4); // float array to
                vertexBuffer.Put(floatArray, 0, (int)size * 4);
                vertexBuffer.Flip();

                GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOManager.getVBO(fileName)[0]);
                GLES20.GlBufferData(GLES20.GlArrayBuffer, vertexBuffer.Capacity() * FLOAT_SIZE, vertexBuffer, GLES20.GlStaticDraw); // vbb.capacity()
                vertexBuffer = null;
                floatArray   = null;

                //-----------------------------------------------------------------------
                floatArray = new float[size * 4]; //4 float to one vertex, 3 vertex to one face  slow way
                for (int i = 0; i < vertexes.Count; i++)
                {
                    floatArray[i * 4 + 0] = normales[i][0];
                    floatArray[i * 4 + 1] = normales[i][1];
                    floatArray[i * 4 + 2] = normales[i][2];
                    floatArray[i * 4 + 3] = normales[i][3];
                }
                normalBuffer = FloatBuffer.Allocate((int)size * 4);
                normalBuffer.Put(floatArray, 0, (int)size * 4);
                normalBuffer.Flip();

                GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOManager.getVBO(fileName)[2]);
                GLES20.GlBufferData(GLES20.GlArrayBuffer, normalBuffer.Capacity() * FLOAT_SIZE, normalBuffer, GLES20.GlStaticDraw); // vbb.capacity()
                normalBuffer = null;

                //-----------------------------------------------------------------------
                floatArray = new float[size * 2]; //2 float
                for (int i = 0; i < textures.Count; i++)
                {
                    floatArray[i * 2 + 0] = textures[i][0];
                    floatArray[i * 2 + 1] = textures[i][1];
                }
                textureBuffer = FloatBuffer.Allocate((int)size * 2);
                textureBuffer.Put(floatArray, 0, (int)size * 2);
                textureBuffer.Flip();

                GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOManager.getVBO(fileName)[1]);
                GLES20.GlBufferData(GLES20.GlArrayBuffer, textureBuffer.Capacity() * FLOAT_SIZE, textureBuffer, GLES20.GlStaticDraw); // vbb.capacity()
                textureBuffer = null;

                VBOManager.setSize(fileName, (int)size);
            }
        }
Ejemplo n.º 4
0
        public bool loadingBinModel(String fileName)
        {
            float[] floatArray;
            long    size;

            try
            {
                // Vertex
                vertexBuffer = null;
                System.GC.Collect();
                int          resourceId = context.Resources.GetIdentifier(fileName + "_vert", "raw", context.PackageName);
                Stream       fileIn     = context.Resources.OpenRawResource(resourceId) as Stream;
                MemoryStream m          = new MemoryStream();
                fileIn.CopyTo(m);
                size       = m.Length;
                floatArray = new float[size / 4];
                System.Buffer.BlockCopy(m.ToArray(), 0, floatArray, 0, (int)size);

                vertexBuffer = FloatBuffer.Allocate((int)size / 4); // float array to
                vertexBuffer.Put(floatArray, 0, (int)size / 4);
                vertexBuffer.Flip();

                VBOManager.setSize(fileName, vertexBuffer.Capacity() / 4); //is size of vertex count = 1 vertex 4 float x,y,z, 1
                floatArray = null;
                fileIn.Close();
                m.Close();
                //----------------------------------------------------------------------------------------------------

                normalBuffer = null;
                System.GC.Collect();
                resourceId = context.Resources.GetIdentifier(fileName + "_norm", "raw", context.PackageName);
                fileIn     = context.Resources.OpenRawResource(resourceId) as Stream;
                m          = new MemoryStream();
                fileIn.CopyTo(m);
                size       = m.Length;
                floatArray = new float[size / 4];
                System.Buffer.BlockCopy(m.ToArray(), 0, floatArray, 0, (int)size);

                normalBuffer = FloatBuffer.Allocate((int)size / 4); // float array to
                normalBuffer.Put(floatArray, 0, (int)size / 4);
                normalBuffer.Flip();

                floatArray = null;
                fileIn.Close();
                m.Close();
                //----------------------------------------------------------------------------------------------------
                textureBuffer = null;
                System.GC.Collect();
                resourceId = context.Resources.GetIdentifier(fileName + "_texture", "raw", context.PackageName);
                fileIn     = context.Resources.OpenRawResource(resourceId) as Stream;
                m          = new MemoryStream();
                fileIn.CopyTo(m);
                size       = m.Length;
                floatArray = new float[size / 4];
                System.Buffer.BlockCopy(m.ToArray(), 0, floatArray, 0, (int)size);

                textureBuffer = FloatBuffer.Allocate((int)size / 4); // float array to
                textureBuffer.Put(floatArray, 0, (int)size / 4);
                textureBuffer.Flip();

                floatArray = null;
                fileIn.Close();
                m.Close();

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }
        }