예제 #1
0
            private void initTriangle()
            {
                // float has 4 bytes
                ByteBuffer vbb = ByteBuffer.allocateDirect(_nrOfVertices * 3 * 4);

                vbb.order(ByteOrder.nativeOrder());
                _vertexBuffer = vbb.asFloatBuffer();

                // short has 2 bytes
                ByteBuffer ibb = ByteBuffer.allocateDirect(_nrOfVertices * 2);

                ibb.order(ByteOrder.nativeOrder());
                _indexBuffer = ibb.asShortBuffer();

                float[] coords =
                {
                    -0.5f, -0.5f, 0f, // (x1, y1, z1)
                    0.5f,  -0.5f, 0f, // (x2, y2, z2)
                    0f,     0.5f, 0f  // (x3, y3, z3)
                };

                _vertexBuffer.put(coords);
                _indexBuffer.put(_indicesArray);

                _vertexBuffer.position(0);
                _indexBuffer.position(0);
            }
        private J3DBuffer createNioFloatBuffer(float[] paramArrayOfFloat, long paramLong)
        {
            FloatBuffer floatBuffer = ByteBuffer.allocateDirect((int)paramLong).order(ByteOrder.nativeOrder()).asFloatBuffer();

            floatBuffer.put(paramArrayOfFloat, 0, paramArrayOfFloat.Length);
            return(new J3DBuffer(floatBuffer));
        }
예제 #3
0
        /**
         * Copies a specified array of vertices to a specified vertex buffer. This method calls {@link
         * java.nio.FloatBuffer#flip()} prior to returning.
         *
         * @param array  the vertices to copy.
         * @param buffer the buffer to copy the vertices to. Must have enough remaining space to hold the vertices.
         *
         * @return the buffer specified as input, with its limit incremented by the number of vertices copied, and its
         *         position set to 0.
         */
        public static FloatBuffer copyArrayToBuffer(Vec4[] array, FloatBuffer buffer)
        {
            if (array == null)
            {
                String message = Logging.getMessage("nullValue.ArrayIsNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }

            if (buffer == null)
            {
                String message = Logging.getMessage("nullValue.BufferIsNull");
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }

            foreach (Vec4 v in array)
            {
                buffer.put((float)v.x).put((float)v.y).put((float)v.z);
            }

            buffer.flip(); // sets the limit to the position and then the position to 0.

            return(buffer);
        }
        /**
         * Retrieve numbers from an accessor.
         *
         * @param buffer          Buffer to receive floats.
         * @param accessor        Accessor that will provide floats.
         * @param semantic        Semantic that identifiers the set of indices to use (for example, "VERTEX" or "NORMAL").
         * @param floatsPerVertex Number of floats to read for each vertex.
         */
        protected void getFloatFromAccessor(FloatBuffer buffer, ColladaAccessor accessor, String semantic,
                                            int floatsPerVertex)
        {
            if (buffer == null)
            {
                String msg = Logging.getMessage("nullValue.BufferIsNull");
                Logging.logger().severe(msg);
                throw new ArgumentException(msg);
            }

            int vertsPerShape = this.getVerticesPerShape();
            int indexCount    = this.getCount() * vertsPerShape;

            if (buffer.remaining() < indexCount * floatsPerVertex)
            {
                String msg = Logging.getMessage("generic.BufferSize");
                Logging.logger().severe(msg);
                throw new ArgumentException(msg);
            }

            int[]   indices      = this.getIndices(semantic);
            float[] vertexCoords = accessor.getFloats();

            foreach (int i in indices)
            {
                buffer.put(vertexCoords, i * floatsPerVertex, floatsPerVertex);
            }
        }
예제 #5
0
        // all setup and data loading goes here
        public void onSurfaceCreated(GL10 arg0, EGLConfig arg1)
        {
            var shaderProgram = gl.createProgram();


            var vs = gl.createShader(new Shaders.GeometryVertexShader());
            var fs = gl.createShader(new Shaders.GeometryVertexShader());


            gl.attachShader(shaderProgram, vs);
            gl.attachShader(shaderProgram, fs);


            gl.linkProgram(shaderProgram);


            gl.useProgram(shaderProgram);
            positionAttribLocation = gl.getAttribLocation(shaderProgram, "position");

            // setup geometry
            float[] verticesData =
            {
                0.0f,   0.5f, 0.0f,
                -0.5f, -0.5f, 0.0f,
                0.5f,  -0.5f, 0.0f
            };

            vertices = ByteBuffer
                       .allocateDirect(verticesData.Length * 4)
                       .order(ByteOrder.nativeOrder()).asFloatBuffer();
            vertices.put(verticesData).position(0);
        }
예제 #6
0
            public MyRenderer(BasicCustomVideoRenderer parent)
            {
                this.mCustomVideoRenderer = parent;

                ByteBuffer bb = ByteBuffer.allocateDirect(mXYZCoords.Length * 4);

                bb.order(ByteOrder.nativeOrder());
                mVertexBuffer = bb.asFloatBuffer();
                mVertexBuffer.put(mXYZCoords);
                mVertexBuffer.position(0);

                ByteBuffer tb = ByteBuffer.allocateDirect(mUVCoords.Length * 4);

                tb.order(ByteOrder.nativeOrder());
                mTextureBuffer = tb.asFloatBuffer();
                mTextureBuffer.put(mUVCoords);
                mTextureBuffer.position(0);

                ByteBuffer dlb = ByteBuffer.allocateDirect(mVertexIndex.Length * 2);

                dlb.order(ByteOrder.nativeOrder());
                mDrawListBuffer = dlb.asShortBuffer();
                mDrawListBuffer.put(mVertexIndex);
                mDrawListBuffer.position(0);
            }
예제 #7
0
        /**
         * Returns a copy of the specified buffer, with the specified new size. The new size must be greater than or equal
         * to the specified buffer's size. If the new size is greater than the specified buffer's size, this returns a new
         * buffer which is partially filled with the contents of the specified buffer. The returned buffer is a backed by a
         * direct ByteBuffer if and only if the specified buffer is direct.
         *
         * @param buffer  the buffer to copy.
         * @param newSize the new buffer's size, in floats.
         *
         * @return the new buffer, with the specified size.
         *
         * @throws ArgumentException if the buffer is null, if the new size is negative, or if the new size is less
         *                                  than the buffer's remaing elements.
         */
        public static FloatBuffer copyOf(FloatBuffer buffer, int newSize)
        {
            if (newSize < 0 || newSize < buffer.remaining())
            {
                String message = Logging.getMessage("generic.SizeOutOfRange", newSize);
                Logging.logger().severe(message);
                throw new ArgumentException(message);
            }

            FloatBuffer newBuffer = newFloatBuffer(newSize, buffer.isDirect());

            int pos = buffer.position(); // Save the input buffer's current position.

            try
            {
                newBuffer.put(buffer);
                newBuffer.rewind();
            }
            finally
            {
                buffer.position(pos); // Restore the input buffer's original position.
            }

            return(newBuffer);
        }
예제 #8
0
            protected int replaceNaN(FloatBuffer floatBuffer, float value)
            {
                int length    = floatBuffer.remaining();
                int numValues = 0;

                if (this.tmpBuffer == null || this.tmpBuffer.length < floatBuffer.remaining())
                {
                    this.tmpBuffer = new float[length];
                }

                floatBuffer.get(this.tmpBuffer, 0, length);
                floatBuffer.flip();

                for (int i = 0; i < length; i++)
                {
                    if (isNoValueFloat(this.tmpBuffer[i]))
                    {
                        this.tmpBuffer[i] = value;
                    }
                    else
                    {
                        numValues++;
                    }
                }

                floatBuffer.put(this.tmpBuffer, 0, length);
                floatBuffer.flip();

                return(numValues);
            }
예제 #9
0
 public static void copyBuffer(FloatBuffer dstBuffer, FloatBuffer srcBuffer)
 {
     if (dstBuffer != srcBuffer)
     {
         srcBuffer.rewind();
         dstBuffer.put(srcBuffer);
     }
 }
예제 #10
0
        public static FloatBuffer getDirectBuffer(float[] values, int Length)
        {
            directFloatBuffer.clear();
            directFloatBuffer.limit(Length);
            directFloatBuffer.put(values, 0, Length);
            directFloatBuffer.rewind();

            return(directFloatBuffer);
        }
예제 #11
0
        /**
         * Expands a buffer of indexed triangle fan vertices to a buffer of non-indexed general-triangle vertices.
         *
         * @param indices the triangle indices.
         * @param inBuf   the vertex buffer the indices refer to, in the order x, y, z, x, y, z, ...
         * @param outBuf  the buffer in which to place the expanded triangle vertices. The buffer must have a limit
         *                sufficient to hold the output vertices.
         *
         * @throws ArgumentException if the index list or the input or output buffer is null, or if the output buffer
         *                                  size is insufficient.
         */
        public static void expandTriangleFan(List <int> indices, FloatBuffer inBuf, FloatBuffer outBuf)
        {
            if (indices == null)
            {
                string msg = Logging.getMessage("nullValue.ListIsNull");
                Logging.logger().severe(msg);
                throw new ArgumentException(msg);
            }

            if (inBuf == null || outBuf == null)
            {
                string msg = Logging.getMessage("nullValue.BufferIsNull");
                Logging.logger().severe(msg);
                throw new ArgumentException(msg);
            }

            int nunTriangles = indices.Count - 2;

            if (nunTriangles * 3 * 3 > outBuf.limit() - outBuf.position())
            {
                string msg = Logging.getMessage("generic.BufferSize", outBuf.limit() - outBuf.position());
                Logging.logger().severe(msg);
                throw new ArgumentException(msg);
            }

            int   k   = indices[0] * 3;
            float v0x = inBuf.get(k);
            float v0y = inBuf.get(k + 1);
            float v0z = inBuf.get(k + 2);

            for (int i = 1; i < indices.Count - 1; i++)
            {
                outBuf.put(v0x).put(v0y).put(v0z);

                k = indices[i] * 3;
                outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));

                k = indices[i + 1] * 3;
                outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));
            }
        }
예제 #12
0
            private void initTriangle()
            {
                float[] coords =
                {
                    -0.5f, -0.5f, 0.5f,  // 0
                    0.5f,  -0.5f, 0.5f,  // 1
                    0f,    -0.5f, -0.5f, // 2
                    0f,     0.5f, 0f,    // 3
                };
                _nrOfVertices = coords.Length;

                float[] colors =
                {
                    1f, 0f, 0f, 1f,     // point 0 red
                    0f, 1f, 0f, 1f,     // point 1 green
                    0f, 0f, 1f, 1f,     // point 2 blue
                    1f, 1f, 1f, 1f,     // point 3 white
                };

                short[] indices = new short[] {
                    0, 1, 3,     // rwg
                    0, 2, 1,     // rbg
                    0, 3, 2,     // rbw
                    1, 2, 3,     // bwg
                };

                // float has 4 bytes, coordinate * 4 bytes
                ByteBuffer vbb = ByteBuffer.allocateDirect(coords.Length * 4);

                vbb.order(ByteOrder.nativeOrder());
                _vertexBuffer = vbb.asFloatBuffer();

                // short has 2 bytes, indices * 2 bytes
                ByteBuffer ibb = ByteBuffer.allocateDirect(indices.Length * 2);

                ibb.order(ByteOrder.nativeOrder());
                _indexBuffer = ibb.asShortBuffer();

                // float has 4 bytes, colors (RGBA) * 4 bytes
                ByteBuffer cbb = ByteBuffer.allocateDirect(colors.Length * 4);

                cbb.order(ByteOrder.nativeOrder());
                _colorBuffer = cbb.asFloatBuffer();

                _vertexBuffer.put(coords);
                _indexBuffer.put(indices);
                _colorBuffer.put(colors);

                _vertexBuffer.position(0);
                _indexBuffer.position(0);
                _colorBuffer.position(0);
            }
예제 #13
0
        /**
         * Expands a buffer of indexed triangle strip vertices to a buffer of non-indexed general-triangle vertices.
         *
         * @param indices the triangle indices.
         * @param inBuf   the vertex buffer the indices refer to, in the order x, y, z, x, y, z, ...
         * @param outBuf  the buffer in which to place the expanded triangle vertices. The buffer must have a limit
         *                sufficient to hold the output vertices.
         *
         * @throws ArgumentException if the index list or the input or output buffer is null, or if the output buffer
         *                                  size is insufficient.
         */
        public static void expandTriangleStrip(List <int> indices, FloatBuffer inBuf, FloatBuffer outBuf)
        {
            if (indices == null)
            {
                string msg = Logging.getMessage("nullValue.ListIsNull");
                Logging.logger().severe(msg);
                throw new ArgumentException(msg);
            }

            if (inBuf == null || outBuf == null)
            {
                string msg = Logging.getMessage("nullValue.BufferIsNull");
                Logging.logger().severe(msg);
                throw new ArgumentException(msg);
            }

            int nunTriangles = indices.Count - 2;

            if (nunTriangles * 3 * 3 > outBuf.limit() - outBuf.position())
            {
                string msg = Logging.getMessage("generic.BufferSize", outBuf.limit() - outBuf.position());
                Logging.logger().severe(msg);
                throw new ArgumentException(msg);
            }

            for (int i = 2; i < indices.Count; i++)
            {
                int k = indices[i - 2] * 3;
                outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));

                k = indices[i % 2 == 0 ? i : i - 1] * 3;
                outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));

                k = indices[i % 2 == 0 ? i - 1 : i] * 3;
                outBuf.put(inBuf.get(k)).put(inBuf.get(k + 1)).put(inBuf.get(k + 2));
            }
        }
            private void initTriangle()
            {
                // float has 4 bytes
                ByteBuffer vbb = ByteBuffer.allocateDirect(_nrOfVertices * 3 * 4);

                vbb.order(ByteOrder.nativeOrder());
                _vertexBuffer = vbb.asFloatBuffer();

                // short has 4 bytes
                ByteBuffer ibb = ByteBuffer.allocateDirect(_nrOfVertices * 2);

                ibb.order(ByteOrder.nativeOrder());
                _indexBuffer = ibb.asShortBuffer();

                // float has 4 bytes, 4 colors (RGBA) * number of vertices * 4 bytes
                ByteBuffer cbb = ByteBuffer.allocateDirect(4 * _nrOfVertices * 4);

                cbb.order(ByteOrder.nativeOrder());
                _colorBuffer = cbb.asFloatBuffer();

                float[] coords =
                {
                    -0.5f, -0.5f, 0f, // (x1, y1, z1)
                    0.5f,  -0.5f, 0f, // (x2, y2, z2)
                    0.5f,   0.5f, 0f  // (x3, y3, z3)
                };

                float[] colors =
                {
                    1f, 0f, 0f, 1f, // point 1
                    0f, 1f, 0f, 1f, // point 2
                    0f, 0f, 1f, 1f, // point 3
                };

                _vertexBuffer.put(coords);
                _indexBuffer.put(_indicesArray);
                _colorBuffer.put(colors);

                _vertexBuffer.position(0);
                _indexBuffer.position(0);
                _colorBuffer.position(0);
            }
 public virtual J3DBuffer getSelectedColorBuffer(Color paramColor)
 {
     if (this.selectedColorBuffer == null)
     {
         FloatBuffer floatBuffer1 = (FloatBuffer)ColorBuffer.Buffer;
         float[]     arrayOfFloat = new float[this.triangleGeometry.VertexCount * 4];
         for (sbyte b = 0; b < this.triangleGeometry.VertexCount; b++)
         {
             sbyte b1 = b * 4;
             arrayOfFloat[b1]     = (paramColor.Red / 255);
             arrayOfFloat[b1 + 1] = (paramColor.Green / 255);
             arrayOfFloat[b1 + 2] = (paramColor.Blue / 255);
             arrayOfFloat[b1 + 3] = floatBuffer1.get(b1 + 3);
         }
         FloatBuffer floatBuffer2 = ByteBuffer.allocateDirect(32 * arrayOfFloat.Length).order(ByteOrder.nativeOrder()).asFloatBuffer();
         floatBuffer2.put(arrayOfFloat, 0, arrayOfFloat.Length);
         this.selectedColorBuffer = new J3DBuffer(floatBuffer2);
     }
     return(this.selectedColorBuffer);
 }
			public MyRenderer()
			{
				ByteBuffer bb = ByteBuffer.allocateDirect(mXYZCoords.Length * 4);
				bb.order(ByteOrder.nativeOrder());
				mVertexBuffer = bb.asFloatBuffer();
				mVertexBuffer.put(mXYZCoords);
				mVertexBuffer.position(0);

				ByteBuffer tb = ByteBuffer.allocateDirect(mUVCoords.Length * 4);
				tb.order(ByteOrder.nativeOrder());
				mTextureBuffer = tb.asFloatBuffer();
				mTextureBuffer.put(mUVCoords);
				mTextureBuffer.position(0);

				ByteBuffer dlb = ByteBuffer.allocateDirect(mVertexIndex.Length * 2);
				dlb.order(ByteOrder.nativeOrder());
				mDrawListBuffer = dlb.asShortBuffer();
				mDrawListBuffer.put(mVertexIndex);
				mDrawListBuffer.position(0);
			}
예제 #17
0
        public static FloatBuffer getDirectBuffer(int size, FloatBuffer buffer)
        {
            if (buffer == null)
            {
                return(buffer);
            }

            size = Round4(size);

            if (buffer.Direct)
            {
                buffer.limit((size >> 2) + buffer.position());
                return(buffer);
            }

            FloatBuffer directBuffer = allocateDirectBuffer(size).asFloatBuffer();

            directBuffer.put((FloatBuffer)((FloatBuffer)buffer).slice().limit(size >> 2));
            directBuffer.rewind();

            return(directBuffer);
        }
        public void onSurfaceCreated(javax.microedition.khronos.egl.EGLConfig value)
        {
            Console.WriteLine("enter AndroidCardboardExperiment onSurfaceCreated");

            GLES20.glClearColor(0.1f, 0.1f, 0.1f, 0.5f); // Dark background so text shows up well.

            ByteBuffer bbVertices = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_COORDS.Length * 4);

            bbVertices.order(ByteOrder.nativeOrder());
            cubeVertices = bbVertices.asFloatBuffer();
            cubeVertices.put(WorldLayoutData.CUBE_COORDS);
            cubeVertices.position(0);

            ByteBuffer bbColors = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_COLORS.Length * 4);

            bbColors.order(ByteOrder.nativeOrder());
            cubeColors = bbColors.asFloatBuffer();
            cubeColors.put(WorldLayoutData.CUBE_COLORS);
            cubeColors.position(0);

            ByteBuffer bbFoundColors = ByteBuffer.allocateDirect(
                WorldLayoutData.CUBE_FOUND_COLORS.Length * 4);

            bbFoundColors.order(ByteOrder.nativeOrder());
            cubeFoundColors = bbFoundColors.asFloatBuffer();
            cubeFoundColors.put(WorldLayoutData.CUBE_FOUND_COLORS);
            cubeFoundColors.position(0);

            ByteBuffer bbNormals = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_NORMALS.Length * 4);

            bbNormals.order(ByteOrder.nativeOrder());
            cubeNormals = bbNormals.asFloatBuffer();
            cubeNormals.put(WorldLayoutData.CUBE_NORMALS);
            cubeNormals.position(0);

            // make a floor
            ByteBuffer bbFloorVertices = ByteBuffer.allocateDirect(WorldLayoutData.FLOOR_COORDS.Length * 4);

            bbFloorVertices.order(ByteOrder.nativeOrder());
            floorVertices = bbFloorVertices.asFloatBuffer();
            floorVertices.put(WorldLayoutData.FLOOR_COORDS);
            floorVertices.position(0);

            ByteBuffer bbFloorNormals = ByteBuffer.allocateDirect(WorldLayoutData.FLOOR_NORMALS.Length * 4);

            bbFloorNormals.order(ByteOrder.nativeOrder());
            floorNormals = bbFloorNormals.asFloatBuffer();
            floorNormals.put(WorldLayoutData.FLOOR_NORMALS);
            floorNormals.position(0);

            var fcolors = 0xA26D41;

            // rgb to float

            //[javac]         return  __Enumerable.<Float>AsEnumerable(__SZArrayEnumerator_1.<Float>Of(x));
            //[javac]                                                                       ^
            //[javac]   required: T#1[]
            //[javac]   found: float[]
            //[javac]   reason: actual argument float[] cannot be converted to Float[] by method invocation conversion

            //          var FLOOR_COLORS = (
            //              from i in Enumerable.Range(0, 6)
            //              select new float[] { 0xA2 / 1.0f, 0x6D / 1.0f, 0x41 / 1.0f, 1.0f }
            //).SelectMany(x => x).ToArray();

            #region floorColors
            var FLOOR_COLORS = new float[4 * 6];

            for (int i = 0; i < FLOOR_COLORS.Length; i += 4)
            {
                FLOOR_COLORS[i + 0] = 0xA2 / 100.0f;
                FLOOR_COLORS[i + 1] = 0x6D / 100.0f;
                FLOOR_COLORS[i + 2] = 0x41 / 100.0f;
                FLOOR_COLORS[i + 3] = 1.0f;
            }



            FloatBuffer floorColors;

            ByteBuffer bbFloorColors = ByteBuffer.allocateDirect(WorldLayoutData.FLOOR_COLORS.Length * 4);
            bbFloorColors.order(ByteOrder.nativeOrder());
            floorColors = bbFloorColors.asFloatBuffer();
            //floorColors.put(WorldLayoutData.FLOOR_COLORS);
            floorColors.put(FLOOR_COLORS);
            floorColors.position(0);
            #endregion


            #region loadGLShader
            Func <int, ScriptCoreLib.GLSL.Shader, int> loadGLShader = (type, xshader) =>
            {
                var code = xshader.ToString();

                int shader = GLES20.glCreateShader(type);
                GLES20.glShaderSource(shader, code);
                GLES20.glCompileShader(shader);

                // Get the compilation status.
                int[] compileStatus = new int[1];
                GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

                // If the compilation failed, delete the shader.
                if (compileStatus[0] == 0)
                {
                    Console.WriteLine("Error compiling shader: " + GLES20.glGetShaderInfoLog(shader));
                    GLES20.glDeleteShader(shader);
                    shader = 0;
                }

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

                return(shader);
            };
            #endregion


            int vertexShader      = loadGLShader(GLES20.GL_VERTEX_SHADER, new AndroidCardboardExperiment.Shaders.light_vertexVertexShader());
            int gridShader        = loadGLShader(GLES20.GL_FRAGMENT_SHADER, new Shaders.xgrid_fragmentFragmentShader());
            int passthroughShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, new AndroidCardboardExperiment.Shaders.passthrough_fragmentFragmentShader());

            cubeProgram = GLES20.glCreateProgram();
            GLES20.glAttachShader(cubeProgram, vertexShader);
            GLES20.glAttachShader(cubeProgram, passthroughShader);
            GLES20.glLinkProgram(cubeProgram);
            GLES20.glUseProgram(cubeProgram);

            checkGLError("Cube program");

            cubePositionParam = GLES20.glGetAttribLocation(cubeProgram, "a_Position");
            cubeNormalParam   = GLES20.glGetAttribLocation(cubeProgram, "a_Normal");
            cubeColorParam    = GLES20.glGetAttribLocation(cubeProgram, "a_Color");

            cubeModelParam               = GLES20.glGetUniformLocation(cubeProgram, "u_Model");
            cubeModelViewParam           = GLES20.glGetUniformLocation(cubeProgram, "u_MVMatrix");
            cubeModelViewProjectionParam = GLES20.glGetUniformLocation(cubeProgram, "u_MVP");
            cubeLightPosParam            = GLES20.glGetUniformLocation(cubeProgram, "u_LightPos");

            GLES20.glEnableVertexAttribArray(cubePositionParam);
            GLES20.glEnableVertexAttribArray(cubeNormalParam);
            GLES20.glEnableVertexAttribArray(cubeColorParam);

            checkGLError("Cube program params");

            floorProgram = GLES20.glCreateProgram();
            GLES20.glAttachShader(floorProgram, vertexShader);
            GLES20.glAttachShader(floorProgram, gridShader);
            GLES20.glLinkProgram(floorProgram);
            GLES20.glUseProgram(floorProgram);

            checkGLError("Floor program");

            floorModelParam               = GLES20.glGetUniformLocation(floorProgram, "u_Model");
            floorModelViewParam           = GLES20.glGetUniformLocation(floorProgram, "u_MVMatrix");
            floorModelViewProjectionParam = GLES20.glGetUniformLocation(floorProgram, "u_MVP");
            floorLightPosParam            = GLES20.glGetUniformLocation(floorProgram, "u_LightPos");

            floorPositionParam = GLES20.glGetAttribLocation(floorProgram, "a_Position");
            floorNormalParam   = GLES20.glGetAttribLocation(floorProgram, "a_Normal");
            floorColorParam    = GLES20.glGetAttribLocation(floorProgram, "a_Color");

            GLES20.glEnableVertexAttribArray(floorPositionParam);
            GLES20.glEnableVertexAttribArray(floorNormalParam);
            GLES20.glEnableVertexAttribArray(floorColorParam);

            checkGLError("Floor program params");

            GLES20.glEnable(GLES20.GL_DEPTH_TEST);
            //GLES20.glEnable(GLES20.GL_FOG);



            checkGLError("onSurfaceCreated");

            Console.WriteLine("exit AndroidCardboardExperiment onSurfaceCreated");


            vFinishFrame = (com.google.vrtoolkit.cardboard.Viewport v) =>
            {
                // GPU thread stops now..
                FrameOne.Stop();
            };

            // I/System.Console(28103): CardboardForEdgeExperiment { ProcessorCount = 8, MODEL = SM-G925F, CurrentManagedThreadId = 11305, FrameCounter = 28, LastFrameMilliseconds = 40, codeFPS = 25.0, pitch = 1.579644, yaw = 1.6225219 }

            #region vNewFrame
            vNewFrame = (com.google.vrtoolkit.cardboard.HeadTransform headTransform) =>
            {
                // http://stackoverflow.com/questions/11851343/raise-fps-on-android-tablet-above-60-for-opengl-game
                // http://gafferongames.com/game-physics/fix-your-timestep/

                #region FrameWatch
                if (FrameWatch.ElapsedMilliseconds >= 1000)
                {
                    var codeFPS = 1000.0 / FrameOne.ElapsedMilliseconds;

                    // we now know how many frames did fit into it
                    // need 60 or more!
                    Console.WriteLine("CardboardForEdgeExperiment " + new
                    {
                        // static
                        System.Environment.ProcessorCount,

                        android.os.Build.MODEL,

                        System.Environment.CurrentManagedThreadId,

                        FrameCounter,

                        // dynamic
                        LastFrameMilliseconds = FrameOne.ElapsedMilliseconds,
                        codeFPS,

                        // very dynamic
                        pitch,
                        yaw
                    });

                    // I/System.Console(28117): CardboardForEdgeExperiment { ProcessorCount = 2, MODEL = Nexus 9, CurrentManagedThreadId = 1647, FrameCounter = 60, LastFrameMilliseconds = 6, codeFPS = 166.66666666666666, pitch = 1.5978987, yaw = -2.0770574 }

                    FrameWatch.Restart();
                    FrameCounter = 0;
                }

                #endregion
                // GPU thread starts now..
                FrameOne.Restart();
                FrameCounter++;


                //Console.WriteLine("AndroidCardboardExperiment onNewFrame");



                headTransform.getHeadView(headView, 0);

                checkGLError("onReadyToDraw");

                // I/System.Console(27769): CardboardForEdgeExperiment { FrameCounter = 61, LastFrameMilliseconds = 0, codeFPS = Infinity, CurrentManagedThreadId = 1637, ProcessorCount = 2, MODEL = Nexus 9 }

                // add placeholder slowdown
                //System.Threading.Thread.Sleep(5);
                // I/System.Console(27840): CardboardForEdgeExperiment { FrameCounter = 60, LastFrameMilliseconds = 6, codeFPS = 166.66666666666666, CurrentManagedThreadId = 1642, ProcessorCount = 2, MODEL = Nexus 9 }
            };
            #endregion

            // if we define it here, we get to see it in vr...
            var modelCube = new float[16];

            // I/System.Console(19917): CardboardForEdgeExperiment { ProcessorCount = 8, MODEL = SM-G925F, CurrentManagedThreadId = 9959, FrameCounter = 46, LastFrameMilliseconds = 6, codeFPS = 166.66666666666666, pitch = 0.9070491, yaw = -0.3660261 }

            #region vDrawEye
            vDrawEye = (com.google.vrtoolkit.cardboard.Eye eye) =>
            {
                // VIDEO via "X:\util\android-sdk-windows\tools\ddms.bat"

                var camera = new float[16];


                // static void	setLookAtM(float[] rm, int rmOffset, float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ)
                // Build the camera matrix and apply it to the ModelView.
                Matrix.setLookAtM(camera, 0,

                                  0.0f, 0.0f, CAMERA_Z,

                                  0f, 0.0f, 0.0f,

                                  0.0f, 1.0f, 0.0f);


                #region glClearColor
                // skybox/video instead?
                GLES20.glClearColor(
                    0x87 / 255f,
                    0xCE / 255f,
                    0xEB / 255f, 1.0f
                    );

                GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
                #endregion



                var view = new float[16];

                // can we strafe?



                // Apply the eye transformation to the camera.
                Matrix.multiplyMM(view, 0, eye.getEyeView(), 0, camera, 0);


                // we tapped into it. this strafes ius!
                Matrix.translateM(view, 0,

                                  (float)Math.Sin(TotalTime.ElapsedMilliseconds * 0.0001f) * objectDistance * 2.5f,


                                  // up down
                                  //(float)Math.Sin(TotalTime.ElapsedMilliseconds * 0.001f) * floorDepth * 0.5f,
                                  (float)Math.Cos(TotalTime.ElapsedMilliseconds * 0.001f) * floorDepth * 0.1f,

                                  0
                                  );


                // Set the position of the light
                Matrix.multiplyMV(lightPosInEyeSpace, 0, view, 0, LIGHT_POS_IN_WORLD_SPACE, 0);

                // Build the ModelView and ModelViewProjection matrices
                // for calculating cube position and light.
                float[] perspective = eye.getPerspective(Z_NEAR, Z_FAR);


                // just a buffer?
                var modelView = new float[16];


                #region drawCube()
                Action <float, float, float> drawCube = (tx, ty, tz) =>
                {
                    #region isLookingAtObject
                    Func <bool> isLookingAtObject = () =>
                    {
                        float[] initVec = { 0, 0, 0, 1.0f };



                        float[] objPositionVec = new float[4];

                        // Convert object space to camera space. Use the headView from onNewFrame.
                        Matrix.multiplyMM(modelView, 0, headView, 0, modelCube, 0);
                        Matrix.multiplyMV(objPositionVec, 0, modelView, 0, initVec, 0);



                        pitch = (float)Math.Atan2(objPositionVec[1], -objPositionVec[2]);
                        yaw   = (float)Math.Atan2(objPositionVec[0], -objPositionVec[2]);

                        if (Math.Abs(pitch) < PITCH_LIMIT)
                        {
                            if (Math.Abs(yaw) < YAW_LIMIT)
                            {
                                return(true);
                            }
                        }
                        return(false);
                    };
                    #endregion



                    // Object first appears directly in front of user.
                    Matrix.setIdentityM(modelCube, 0);
                    // cant see it?
                    var scale = 5.0f;
                    //Matrix.scaleM(modelCube, 0, scale, scale, scale);

                    Matrix.translateM(modelCube, 0, tx, ty, tz);


                    Matrix.multiplyMM(modelView, 0, view, 0, modelCube, 0);
                    Matrix.multiplyMM(modelViewProjection, 0, perspective, 0, modelView, 0);


                    // public static void scaleM (float[] m, int mOffset, float x, float y, float z)

                    // Build the Model part of the ModelView matrix.
                    //Matrix.rotateM(modelCube, 0, TIME_DELTA, 0.5f, 0.5f, 1.0f);

                    // cant see rotation?
                    Matrix.rotateM(modelCube, 0, TotalTime.ElapsedMilliseconds * 0.01f,
                                   // upwards rot.
                                   //0.5f,

                                   0f,

                                   // sideways, left to right
                                   0.5f
                                   , 0.0f);


                    // http://developer.android.com/reference/android/opengl/Matrix.html#translateM(float[], int, float, float, float)


                    // the cube rotates in front of us.
                    // do we need to use a special program to draw a cube?
                    // how can we make it bigger?

                    GLES20.glUseProgram(cubeProgram);

                    GLES20.glUniform3fv(cubeLightPosParam, 1, lightPosInEyeSpace, 0);

                    // Set the Model in the shader, used to calculate lighting
                    GLES20.glUniformMatrix4fv(cubeModelParam, 1, false, modelCube, 0);

                    // Set the ModelView in the shader, used to calculate lighting
                    GLES20.glUniformMatrix4fv(cubeModelViewParam, 1, false, modelView, 0);

                    // Set the position of the cube
                    GLES20.glVertexAttribPointer(cubePositionParam, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, cubeVertices);

                    // Set the ModelViewProjection matrix in the shader.
                    GLES20.glUniformMatrix4fv(cubeModelViewProjectionParam, 1, false, modelViewProjection, 0);

                    // Set the normal positions of the cube, again for shading
                    GLES20.glVertexAttribPointer(cubeNormalParam, 3, GLES20.GL_FLOAT, false, 0, cubeNormals);


                    #region cubeColors
                    var cc = cubeColors;
                    if (!isLookingAtObject())
                    {
                        cc = cubeFoundColors;
                    }

                    GLES20.glVertexAttribPointer(cubeColorParam, 4, GLES20.GL_FLOAT, false, 0, cc);
                    #endregion

                    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 36);
                    checkGLError("Drawing cube");
                };


                #endregion

                #region drawCube
                drawCube(0, objectDistance, objectDistance * -1.0f);


                drawCube(0, 0, objectDistance * -2.0f);

                // looks like an airstrip

                // low fps?
                //var endOfMatrix = 64;
                var endOfMatrix = 20;
                for (int i = -endOfMatrix; i < endOfMatrix; i++)
                {
                    drawCube(objectDistance, -floorDepth, objectDistance * -2.0f * i);
                    drawCube(-objectDistance, -floorDepth, objectDistance * -2.0f * i);


                    drawCube(objectDistance * 0.5f, 0, objectDistance * -2.0f * i);
                    drawCube(objectDistance * -0.5f, 0, objectDistance * -2.0f * i);
                }
                #endregion



                var modelFloor = new float[16];

                Matrix.setIdentityM(modelFloor, 0);
                Matrix.translateM(modelFloor, 0,

                                                      // the floor escapes!
                                                      //TotalTime.ElapsedMilliseconds * 0.01f,
                                  0, -floorDepth, 0); // Floor appears below user.

                // Set modelView for the floor, so we draw floor in the correct location
                Matrix.multiplyMM(modelView, 0, view, 0, modelFloor, 0);
                Matrix.multiplyMM(modelViewProjection, 0, perspective, 0, modelView, 0);

                #region drawFloor
                // called by onDrawEye
                Action drawFloor = delegate
                {
                    GLES20.glUseProgram(floorProgram);

                    // Set ModelView, MVP, position, normals, and color.
                    GLES20.glUniform3fv(floorLightPosParam, 1, lightPosInEyeSpace, 0);
                    GLES20.glUniformMatrix4fv(floorModelParam, 1, false, modelFloor, 0);
                    GLES20.glUniformMatrix4fv(floorModelViewParam, 1, false, modelView, 0);
                    GLES20.glUniformMatrix4fv(floorModelViewProjectionParam, 1, false,
                                              modelViewProjection, 0);
                    GLES20.glVertexAttribPointer(floorPositionParam, COORDS_PER_VERTEX, GLES20.GL_FLOAT,
                                                 false, 0, floorVertices);
                    GLES20.glVertexAttribPointer(floorNormalParam, 3, GLES20.GL_FLOAT, false, 0,
                                                 floorNormals);
                    GLES20.glVertexAttribPointer(floorColorParam, 4, GLES20.GL_FLOAT, false, 0, floorColors);

                    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);

                    checkGLError("drawing floor");
                };

                drawFloor();
                #endregion
            };
            #endregion
        }
            public LessonOneRenderer()
            {
                this.gl = (ScriptCoreLib.JavaScript.WebGL.WebGLRenderingContext)(object) __gl;


                #region Define points for equilateral triangles.

                // This triangle is red, green, and blue.
                float[] triangle1VerticesData =
                {
                    // X, Y, Z,
                    // R, G, B, A
                    -0.5f,       -0.25f, 0.0f,
                    1.0f,          0.0f, 0.0f, 1.0f,

                    0.5f,        -0.25f, 0.0f,
                    0.0f,          0.0f, 1.0f, 1.0f,

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

                // This triangle is yellow, cyan, and magenta.
                float[] triangle2VerticesData =
                {
                    // X, Y, Z,
                    // R, G, B, A
                    -0.5f,       -0.25f, 0.0f,
                    1.0f,          1.0f, 0.0f, 1.0f,

                    0.5f,        -0.25f, 0.0f,
                    0.0f,          1.0f, 1.0f, 1.0f,

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

                // This triangle is white, gray, and black.
                float[] triangle3VerticesData =
                {
                    // X, Y, Z,
                    // R, G, B, A
                    -0.5f,       -0.25f, 0.0f,
                    1.0f,          1.0f, 1.0f, 1.0f,

                    0.5f,        -0.25f, 0.0f,
                    0.5f,          0.5f, 0.5f, 1.0f,

                    0.0f,  0.559016994f, 0.0f,
                    0.0f,          0.0f, 0.0f, 1.0f
                };
                #endregion

                // Initialize the buffers.
                mTriangle1Vertices = ByteBuffer.allocateDirect(triangle1VerticesData.Length * mBytesPerFloat)
                                     .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mTriangle2Vertices = ByteBuffer.allocateDirect(triangle2VerticesData.Length * mBytesPerFloat)
                                     .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mTriangle3Vertices = ByteBuffer.allocateDirect(triangle3VerticesData.Length * mBytesPerFloat)
                                     .order(ByteOrder.nativeOrder()).asFloatBuffer();

                mTriangle1Vertices.put(triangle1VerticesData).position(0);
                mTriangle2Vertices.put(triangle2VerticesData).position(0);
                mTriangle3Vertices.put(triangle3VerticesData).position(0);
            }
예제 #20
0
            public LessonTwoRenderer()
            {
                this.gl = (ScriptCoreLib.JavaScript.WebGL.WebGLRenderingContext)(object) __gl;

                #region  Define points for a cube.

                // X, Y, Z
                float[] cubePositionData =
                {
                    // In OpenGL counter-clockwise winding is default. This means that when we look at a triangle,
                    // if the points are counter-clockwise we are looking at the "front". If not we are looking at
                    // the back. OpenGL has an optimization where all back-facing triangles are culled, since they
                    // usually represent the backside of an object and aren't visible anyways.

                    // Front face
                    -1.0f,  1.0f,  1.0f,
                    -1.0f, -1.0f,  1.0f,
                    1.0f,   1.0f,  1.0f,
                    -1.0f, -1.0f,  1.0f,
                    1.0f,  -1.0f,  1.0f,
                    1.0f,   1.0f,  1.0f,

                    // Right face
                    1.0f,   1.0f,  1.0f,
                    1.0f,  -1.0f,  1.0f,
                    1.0f,   1.0f, -1.0f,
                    1.0f,  -1.0f,  1.0f,
                    1.0f,  -1.0f, -1.0f,
                    1.0f,   1.0f, -1.0f,

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

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

                    // Top face
                    -1.0f,  1.0f, -1.0f,
                    -1.0f,  1.0f,  1.0f,
                    1.0f,   1.0f, -1.0f,
                    -1.0f,  1.0f,  1.0f,
                    1.0f,   1.0f,  1.0f,
                    1.0f,   1.0f, -1.0f,

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

                // R, G, B, A
                float[] cubeColorData =
                {
                    // Front face (red)
                    1.0f, 0.0f, 0.0f, 1.0f,
                    1.0f, 0.0f, 0.0f, 1.0f,
                    1.0f, 0.0f, 0.0f, 1.0f,
                    1.0f, 0.0f, 0.0f, 1.0f,
                    1.0f, 0.0f, 0.0f, 1.0f,
                    1.0f, 0.0f, 0.0f, 1.0f,

                    // Right face (green)
                    0.0f, 1.0f, 0.0f, 1.0f,
                    0.0f, 1.0f, 0.0f, 1.0f,
                    0.0f, 1.0f, 0.0f, 1.0f,
                    0.0f, 1.0f, 0.0f, 1.0f,
                    0.0f, 1.0f, 0.0f, 1.0f,
                    0.0f, 1.0f, 0.0f, 1.0f,

                    // Back face (blue)
                    0.0f, 0.0f, 1.0f, 1.0f,
                    0.0f, 0.0f, 1.0f, 1.0f,
                    0.0f, 0.0f, 1.0f, 1.0f,
                    0.0f, 0.0f, 1.0f, 1.0f,
                    0.0f, 0.0f, 1.0f, 1.0f,
                    0.0f, 0.0f, 1.0f, 1.0f,

                    // Left face (yellow)
                    1.0f, 1.0f, 0.0f, 1.0f,
                    1.0f, 1.0f, 0.0f, 1.0f,
                    1.0f, 1.0f, 0.0f, 1.0f,
                    1.0f, 1.0f, 0.0f, 1.0f,
                    1.0f, 1.0f, 0.0f, 1.0f,
                    1.0f, 1.0f, 0.0f, 1.0f,

                    // Top face (cyan)
                    0.0f, 1.0f, 1.0f, 1.0f,
                    0.0f, 1.0f, 1.0f, 1.0f,
                    0.0f, 1.0f, 1.0f, 1.0f,
                    0.0f, 1.0f, 1.0f, 1.0f,
                    0.0f, 1.0f, 1.0f, 1.0f,
                    0.0f, 1.0f, 1.0f, 1.0f,

                    // Bottom face (magenta)
                    1.0f, 0.0f, 1.0f, 1.0f,
                    1.0f, 0.0f, 1.0f, 1.0f,
                    1.0f, 0.0f, 1.0f, 1.0f,
                    1.0f, 0.0f, 1.0f, 1.0f,
                    1.0f, 0.0f, 1.0f, 1.0f,
                    1.0f, 0.0f, 1.0f, 1.0f
                };

                // X, Y, Z
                // The normal is used in light calculations and is a vector which points
                // orthogonal to the plane of the surface. For a cube model, the normals
                // should be orthogonal to the points of each face.
                float[] cubeNormalData =
                {
                    // Front 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,

                    // 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
                };
                #endregion

                // Initialize the buffers.
                mCubePositions = ByteBuffer.allocateDirect(cubePositionData.Length * mBytesPerFloat)
                                 .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mCubePositions.put(cubePositionData).position(0);

                mCubeColors = ByteBuffer.allocateDirect(cubeColorData.Length * mBytesPerFloat)
                              .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mCubeColors.put(cubeColorData).position(0);

                mCubeNormals = ByteBuffer.allocateDirect(cubeNormalData.Length * mBytesPerFloat)
                               .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mCubeNormals.put(cubeNormalData).position(0);
            }
예제 #21
0
        public void onSurfaceCreated(javax.microedition.khronos.egl.EGLConfig value)
        {
            Console.WriteLine("onSurfaceCreated");

            GLES20.glClearColor(0.1f, 0.1f, 0.1f, 0.5f);             // Dark background so text shows up well.

            ByteBuffer bbVertices = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_COORDS.Length * 4);

            bbVertices.order(ByteOrder.nativeOrder());
            cubeVertices = bbVertices.asFloatBuffer();
            cubeVertices.put(WorldLayoutData.CUBE_COORDS);
            cubeVertices.position(0);

            ByteBuffer bbColors = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_COLORS.Length * 4);

            bbColors.order(ByteOrder.nativeOrder());
            cubeColors = bbColors.asFloatBuffer();
            cubeColors.put(WorldLayoutData.CUBE_COLORS);
            cubeColors.position(0);

            ByteBuffer bbFoundColors = ByteBuffer.allocateDirect(
                WorldLayoutData.CUBE_FOUND_COLORS.Length * 4);

            bbFoundColors.order(ByteOrder.nativeOrder());
            cubeFoundColors = bbFoundColors.asFloatBuffer();
            cubeFoundColors.put(WorldLayoutData.CUBE_FOUND_COLORS);
            cubeFoundColors.position(0);

            ByteBuffer bbNormals = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_NORMALS.Length * 4);

            bbNormals.order(ByteOrder.nativeOrder());
            cubeNormals = bbNormals.asFloatBuffer();
            cubeNormals.put(WorldLayoutData.CUBE_NORMALS);
            cubeNormals.position(0);

            // make a floor
            ByteBuffer bbFloorVertices = ByteBuffer.allocateDirect(WorldLayoutData.FLOOR_COORDS.Length * 4);

            bbFloorVertices.order(ByteOrder.nativeOrder());
            floorVertices = bbFloorVertices.asFloatBuffer();
            floorVertices.put(WorldLayoutData.FLOOR_COORDS);
            floorVertices.position(0);

            ByteBuffer bbFloorNormals = ByteBuffer.allocateDirect(WorldLayoutData.FLOOR_NORMALS.Length * 4);

            bbFloorNormals.order(ByteOrder.nativeOrder());
            floorNormals = bbFloorNormals.asFloatBuffer();
            floorNormals.put(WorldLayoutData.FLOOR_NORMALS);
            floorNormals.position(0);

            ByteBuffer bbFloorColors = ByteBuffer.allocateDirect(WorldLayoutData.FLOOR_COLORS.Length * 4);

            bbFloorColors.order(ByteOrder.nativeOrder());
            floorColors = bbFloorColors.asFloatBuffer();
            floorColors.put(WorldLayoutData.FLOOR_COLORS);
            floorColors.position(0);



            Func <int, string, int> loadGLShader = (int type, string code) =>
            {
                int shader = GLES20.glCreateShader(type);
                GLES20.glShaderSource(shader, code);
                GLES20.glCompileShader(shader);

                // Get the compilation status.
                int[] compileStatus = new int[1];
                GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);

                // If the compilation failed, delete the shader.
                if (compileStatus[0] == 0)
                {
                    Console.WriteLine("Error compiling shader: " + GLES20.glGetShaderInfoLog(shader));
                    GLES20.glDeleteShader(shader);
                    shader = 0;
                }

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

                return(shader);
            };


            int vertexShader      = loadGLShader(GLES20.GL_VERTEX_SHADER, new Shaders.light_vertexVertexShader().ToString());
            int gridShader        = loadGLShader(GLES20.GL_FRAGMENT_SHADER, new Shaders.grid_fragmentFragmentShader().ToString());
            int passthroughShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, new Shaders.passthrough_fragmentFragmentShader().ToString());

            cubeProgram = GLES20.glCreateProgram();
            GLES20.glAttachShader(cubeProgram, vertexShader);
            GLES20.glAttachShader(cubeProgram, passthroughShader);
            GLES20.glLinkProgram(cubeProgram);
            GLES20.glUseProgram(cubeProgram);

            checkGLError("Cube program");

            cubePositionParam = GLES20.glGetAttribLocation(cubeProgram, "a_Position");
            cubeNormalParam   = GLES20.glGetAttribLocation(cubeProgram, "a_Normal");
            cubeColorParam    = GLES20.glGetAttribLocation(cubeProgram, "a_Color");

            cubeModelParam               = GLES20.glGetUniformLocation(cubeProgram, "u_Model");
            cubeModelViewParam           = GLES20.glGetUniformLocation(cubeProgram, "u_MVMatrix");
            cubeModelViewProjectionParam = GLES20.glGetUniformLocation(cubeProgram, "u_MVP");
            cubeLightPosParam            = GLES20.glGetUniformLocation(cubeProgram, "u_LightPos");

            GLES20.glEnableVertexAttribArray(cubePositionParam);
            GLES20.glEnableVertexAttribArray(cubeNormalParam);
            GLES20.glEnableVertexAttribArray(cubeColorParam);

            checkGLError("Cube program params");

            floorProgram = GLES20.glCreateProgram();
            GLES20.glAttachShader(floorProgram, vertexShader);
            GLES20.glAttachShader(floorProgram, gridShader);
            GLES20.glLinkProgram(floorProgram);
            GLES20.glUseProgram(floorProgram);

            checkGLError("Floor program");

            floorModelParam               = GLES20.glGetUniformLocation(floorProgram, "u_Model");
            floorModelViewParam           = GLES20.glGetUniformLocation(floorProgram, "u_MVMatrix");
            floorModelViewProjectionParam = GLES20.glGetUniformLocation(floorProgram, "u_MVP");
            floorLightPosParam            = GLES20.glGetUniformLocation(floorProgram, "u_LightPos");

            floorPositionParam = GLES20.glGetAttribLocation(floorProgram, "a_Position");
            floorNormalParam   = GLES20.glGetAttribLocation(floorProgram, "a_Normal");
            floorColorParam    = GLES20.glGetAttribLocation(floorProgram, "a_Color");

            GLES20.glEnableVertexAttribArray(floorPositionParam);
            GLES20.glEnableVertexAttribArray(floorNormalParam);
            GLES20.glEnableVertexAttribArray(floorColorParam);

            checkGLError("Floor program params");

            GLES20.glEnable(GLES20.GL_DEPTH_TEST);

            // Object first appears directly in front of user.
            Matrix.setIdentityM(modelCube, 0);
            Matrix.translateM(modelCube, 0, 0, 0, -objectDistance);

            Matrix.setIdentityM(modelFloor, 0);
            Matrix.translateM(modelFloor, 0, 0, -floorDepth, 0);             // Floor appears below user.

            checkGLError("onSurfaceCreated");
        }
            //script: error JSC1000: Java : Opcode not implemented: stelem.r4 at AndroidOpenGLESLesson5Activity.Activities.AndroidOpenGLESLesson5Activity+LessonFiveRenderer+<>c.<.ctor>b__17_0

            public LessonFiveRenderer(Context activityContext)
            {
                this.gl = (ScriptCoreLib.JavaScript.WebGL.WebGLRenderingContext)(object) __gl;

                mActivityContext = activityContext;

                #region generateCubeData
                Func <f[], f[], f[], f[], f[], f[], f[], f[], int, f[]> generateCubeData =
                    (f[] point1,
                     f[] point2,
                     f[] point3,
                     f[] point4,
                     f[] point5,
                     f[] point6,
                     f[] point7,
                     f[] point8,
                     int elementsPerPoint) =>
                {
                    // Given a cube with the points defined as follows:
                    // front left top, front right top, front left bottom, front right bottom,
                    // back left top, back right top, back left bottom, back right bottom,
                    // return an array of 6 sides, 2 triangles per side, 3 vertices per triangle, and 4 floats per vertex.
                    int FRONT  = 0;
                    int RIGHT  = 1;
                    int BACK   = 2;
                    int LEFT   = 3;
                    int TOP    = 4;
                    int BOTTOM = 5;

                    int     size     = elementsPerPoint * 6 * 6;
                    float[] cubeData = new float[size];

                    for (int face = 0; face < 6; face++)
                    {
                        // Relative to the side, p1 = top left, p2 = top right, p3 = bottom left, p4 = bottom right
                        float[] p1, p2, p3, p4;

                        // Select the points for this face
                        if (face == FRONT)
                        {
                            p1 = point1; p2 = point2; p3 = point3; p4 = point4;
                        }
                        else if (face == RIGHT)
                        {
                            p1 = point2; p2 = point6; p3 = point4; p4 = point8;
                        }
                        else if (face == BACK)
                        {
                            p1 = point6; p2 = point5; p3 = point8; p4 = point7;
                        }
                        else if (face == LEFT)
                        {
                            p1 = point5; p2 = point1; p3 = point7; p4 = point3;
                        }
                        else if (face == TOP)
                        {
                            p1 = point5; p2 = point6; p3 = point1; p4 = point2;
                        }
                        else                                 // if (side == BOTTOM)
                        {
                            p1 = point8; p2 = point7; p3 = point4; p4 = point3;
                        }

                        // In OpenGL counter-clockwise winding is default. This means that when we look at a triangle,
                        // if the points are counter-clockwise we are looking at the "front". If not we are looking at
                        // the back. OpenGL has an optimization where all back-facing triangles are culled, since they
                        // usually represent the backside of an object and aren't visible anyways.

                        // Build the triangles
                        //  1---3,6
                        //  | / |
                        // 2,4--5
                        int offset = face * elementsPerPoint * 6;

                        for (int i = 0; i < elementsPerPoint; i++)
                        {
                            cubeData[offset++] = p1[i];
                        }
                        for (int i = 0; i < elementsPerPoint; i++)
                        {
                            cubeData[offset++] = p3[i];
                        }
                        for (int i = 0; i < elementsPerPoint; i++)
                        {
                            cubeData[offset++] = p2[i];
                        }
                        for (int i = 0; i < elementsPerPoint; i++)
                        {
                            cubeData[offset++] = p3[i];
                        }
                        for (int i = 0; i < elementsPerPoint; i++)
                        {
                            cubeData[offset++] = p4[i];
                        }
                        for (int i = 0; i < elementsPerPoint; i++)
                        {
                            cubeData[offset++] = p2[i];
                        }
                    }

                    return(cubeData);
                };
                #endregion


                // Define points for a cube.
                // X, Y, Z
                float[] p1p = { -1.0f, 1.0f, 1.0f };
                float[] p2p = { 1.0f, 1.0f, 1.0f };
                float[] p3p = { -1.0f, -1.0f, 1.0f };
                float[] p4p = { 1.0f, -1.0f, 1.0f };
                float[] p5p = { -1.0f, 1.0f, -1.0f };
                float[] p6p = { 1.0f, 1.0f, -1.0f };
                float[] p7p = { -1.0f, -1.0f, -1.0f };
                float[] p8p = { 1.0f, -1.0f, -1.0f };

                float[] cubePositionData = generateCubeData(p1p, p2p, p3p, p4p, p5p, p6p, p7p, p8p, p1p.Length);

                // Points of the cube: color information
                // R, G, B, A
                float[] p1c = { 1.0f, 0.0f, 0.0f, 1.0f };                               // red
                float[] p2c = { 1.0f, 0.0f, 1.0f, 1.0f };                               // magenta
                float[] p3c = { 0.0f, 0.0f, 0.0f, 1.0f };                               // black
                float[] p4c = { 0.0f, 0.0f, 1.0f, 1.0f };                               // blue
                float[] p5c = { 1.0f, 1.0f, 0.0f, 1.0f };                               // yellow
                float[] p6c = { 1.0f, 1.0f, 1.0f, 1.0f };                               // white
                float[] p7c = { 0.0f, 1.0f, 0.0f, 1.0f };                               // green
                float[] p8c = { 0.0f, 1.0f, 1.0f, 1.0f };                               // cyan

                float[] cubeColorData = generateCubeData(p1c, p2c, p3c, p4c, p5c, p6c, p7c, p8c, p1c.Length);

                // Initialize the buffers.
                mCubePositions = ByteBuffer.allocateDirect(cubePositionData.Length * mBytesPerFloat)
                                 .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mCubePositions.put(cubePositionData).position(0);

                mCubeColors = ByteBuffer.allocateDirect(cubeColorData.Length * mBytesPerFloat)
                              .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mCubeColors.put(cubeColorData).position(0);
            }
            /**
             * Initialize the model data.
             */
            public LessonFourRenderer(Context activityContext)
            {
                mActivityContext = activityContext;

                // Define points for a cube.

                // X, Y, Z
                float[] cubePositionData =
                {
                    // In OpenGL counter-clockwise winding is default. This means that when we look at a triangle,
                    // if the points are counter-clockwise we are looking at the "front". If not we are looking at
                    // the back. OpenGL has an optimization where all back-facing triangles are culled, since they
                    // usually represent the backside of an object and aren't visible anyways.

                    // Front face
                    -1.0f,  1.0f,  1.0f,
                    -1.0f, -1.0f,  1.0f,
                    1.0f,   1.0f,  1.0f,
                    -1.0f, -1.0f,  1.0f,
                    1.0f,  -1.0f,  1.0f,
                    1.0f,   1.0f,  1.0f,

                    // Right face
                    1.0f,   1.0f,  1.0f,
                    1.0f,  -1.0f,  1.0f,
                    1.0f,   1.0f, -1.0f,
                    1.0f,  -1.0f,  1.0f,
                    1.0f,  -1.0f, -1.0f,
                    1.0f,   1.0f, -1.0f,

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

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

                    // Top face
                    -1.0f,  1.0f, -1.0f,
                    -1.0f,  1.0f,  1.0f,
                    1.0f,   1.0f, -1.0f,
                    -1.0f,  1.0f,  1.0f,
                    1.0f,   1.0f,  1.0f,
                    1.0f,   1.0f, -1.0f,

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

                // R, G, B, A
                float[] cubeColorData =
                {
                    // Front face (red)
                    1.0f, 0.0f, 0.0f, 0.0f,
                    1.0f, 0.0f, 0.0f, 0.0f,
                    1.0f, 0.0f, 0.0f, 0.0f,
                    1.0f, 0.0f, 0.0f, 0.0f,
                    1.0f, 0.0f, 0.0f, 0.0f,
                    1.0f, 0.0f, 0.0f, 0.0f,

                    // Right face (green)
                    0.0f, 1.0f, 0.0f, 1.0f,
                    0.0f, 1.0f, 0.0f, 1.0f,
                    0.0f, 1.0f, 0.0f, 1.0f,
                    0.0f, 1.0f, 0.0f, 1.0f,
                    0.0f, 1.0f, 0.0f, 1.0f,
                    0.0f, 1.0f, 0.0f, 1.0f,

                    // Back face (blue)
                    0.0f, 0.0f, 1.0f, 1.0f,
                    0.0f, 0.0f, 1.0f, 1.0f,
                    0.0f, 0.0f, 1.0f, 1.0f,
                    0.0f, 0.0f, 1.0f, 1.0f,
                    0.0f, 0.0f, 1.0f, 1.0f,
                    0.0f, 0.0f, 1.0f, 1.0f,

                    // Left face (yellow)
                    1.0f, 1.0f, 0.0f, 1.0f,
                    1.0f, 1.0f, 0.0f, 1.0f,
                    1.0f, 1.0f, 0.0f, 1.0f,
                    1.0f, 1.0f, 0.0f, 1.0f,
                    1.0f, 1.0f, 0.0f, 1.0f,
                    1.0f, 1.0f, 0.0f, 1.0f,

                    // Top face (cyan)
                    0.0f, 1.0f, 1.0f, 1.0f,
                    0.0f, 1.0f, 1.0f, 1.0f,
                    0.0f, 1.0f, 1.0f, 1.0f,
                    0.0f, 1.0f, 1.0f, 1.0f,
                    0.0f, 1.0f, 1.0f, 1.0f,
                    0.0f, 1.0f, 1.0f, 1.0f,

                    // Bottom face (magenta)
                    1.0f, 0.0f, 1.0f, 1.0f,
                    1.0f, 0.0f, 1.0f, 1.0f,
                    1.0f, 0.0f, 1.0f, 1.0f,
                    1.0f, 0.0f, 1.0f, 1.0f,
                    1.0f, 0.0f, 1.0f, 1.0f,
                    1.0f, 0.0f, 1.0f, 1.0f
                };

                // X, Y, Z
                // The normal is used in light calculations and is a vector which points
                // orthogonal to the plane of the surface. For a cube model, the normals
                // should be orthogonal to the points of each face.
                float[] cubeNormalData =
                {
                    // Front 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,

                    // 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
                };

                // S, T (or X, Y)
                // Texture coordinate data.
                // Because images have a Y axis pointing downward (values increase as you move down the image) while
                // OpenGL has a Y axis pointing upward, we adjust for that here by flipping the Y axis.
                // What's more is that the texture coordinates are the same for every face.
                float[] cubeTextureCoordinateData =
                {
                    // Front face
                    0.0f, 0.0f,
                    0.0f, 1.0f,
                    1.0f, 0.0f,
                    0.0f, 1.0f,
                    1.0f, 1.0f,
                    1.0f, 0.0f,

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

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

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

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

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

                // Initialize the buffers.
                mCubePositions = ByteBuffer.allocateDirect(cubePositionData.Length * mBytesPerFloat)
                                 .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mCubePositions.put(cubePositionData).position(0);

                mCubeColors = ByteBuffer.allocateDirect(cubeColorData.Length * mBytesPerFloat)
                              .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mCubeColors.put(cubeColorData).position(0);

                mCubeNormals = ByteBuffer.allocateDirect(cubeNormalData.Length * mBytesPerFloat)
                               .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mCubeNormals.put(cubeNormalData).position(0);

                mCubeTextureCoordinates = ByteBuffer.allocateDirect(cubeTextureCoordinateData.Length * mBytesPerFloat)
                                          .order(ByteOrder.nativeOrder()).asFloatBuffer();
                mCubeTextureCoordinates.put(cubeTextureCoordinateData).position(0);
            }
예제 #24
0
        private void drawTexture(IRenderingEngine re, int x, int y, int projectionWidth, int projectionHeight, bool scaleToCanvas, bool redWriteEnabled, bool greenWriteEnabled, bool blueWriteEnabled, bool alphaWriteEnabled)
        {
            re.startDirectRendering(true, false, true, true, true, projectionWidth, projectionHeight);
            re.setColorMask(redWriteEnabled, greenWriteEnabled, blueWriteEnabled, alphaWriteEnabled);
            if (scaleToCanvas)
            {
                re.setViewport(0, 0, Modules.sceDisplayModule.CanvasWidth, Modules.sceDisplayModule.CanvasHeight);
            }
            else
            {
                re.setViewport(0, 0, projectionWidth, projectionHeight);
            }

            IREBufferManager bufferManager  = re.BufferManager;
            ByteBuffer       drawByteBuffer = bufferManager.getBuffer(drawBufferId);

            drawByteBuffer.clear();
            FloatBuffer drawFloatBuffer = drawByteBuffer.asFloatBuffer();

            drawFloatBuffer.clear();
            drawFloatBuffer.put(texS);
            drawFloatBuffer.put(texT);
            drawFloatBuffer.put(x + width);
            drawFloatBuffer.put(y + height);

            drawFloatBuffer.put(0.0f);
            drawFloatBuffer.put(texT);
            drawFloatBuffer.put(x);
            drawFloatBuffer.put(y + height);

            drawFloatBuffer.put(0.0f);
            drawFloatBuffer.put(0.0f);
            drawFloatBuffer.put(x);
            drawFloatBuffer.put(y);

            drawFloatBuffer.put(texS);
            drawFloatBuffer.put(0.0f);
            drawFloatBuffer.put(x + width);
            drawFloatBuffer.put(y);

            if (re.VertexArrayAvailable)
            {
                re.bindVertexArray(0);
            }
            re.setVertexInfo(null, false, false, true, -1);
            re.enableClientState(pspsharp.graphics.RE.IRenderingEngine_Fields.RE_TEXTURE);
            re.disableClientState(pspsharp.graphics.RE.IRenderingEngine_Fields.RE_COLOR);
            re.disableClientState(pspsharp.graphics.RE.IRenderingEngine_Fields.RE_NORMAL);
            re.enableClientState(pspsharp.graphics.RE.IRenderingEngine_Fields.RE_VERTEX);
            bufferManager.setTexCoordPointer(drawBufferId, 2, pspsharp.graphics.RE.IRenderingEngine_Fields.RE_FLOAT, 4 * SIZEOF_FLOAT, 0);
            bufferManager.setVertexPointer(drawBufferId, 2, pspsharp.graphics.RE.IRenderingEngine_Fields.RE_FLOAT, 4 * SIZEOF_FLOAT, 2 * SIZEOF_FLOAT);
            bufferManager.setBufferData(pspsharp.graphics.RE.IRenderingEngine_Fields.RE_ARRAY_BUFFER, drawBufferId, drawFloatBuffer.position() * SIZEOF_FLOAT, drawByteBuffer.rewind(), pspsharp.graphics.RE.IRenderingEngine_Fields.RE_DYNAMIC_DRAW);
            re.drawArrays(pspsharp.graphics.RE.IRenderingEngine_Fields.RE_QUADS, 0, 4);

            re.endDirectRendering();
        }