コード例 #1
0
        public void Draw()
        {
            Gles.glEnable(Gles.GL_DEPTH_TEST);
            Gles.glClearColor(0, 0, 0.4f, 1);
            Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT);

            if (mProgram == 0)
            {
                return;
            }

            Gles.glUseProgram(mProgram);
            Gles.glThrowError();

            if (UseBufferMethod)
            {
                Gles.glBindBuffer(Gles.GL_ARRAY_BUFFER, mVertexPositionBuffer);
                Gles.glThrowError();

                Gles.glVertexAttribPointer((GLuint)mPositionAttribLocation, 3, Gles.GL_FLOAT, Gles.GL_FALSE, 0, IntPtr.Zero);
                Gles.glThrowError();
            }
            else
            {
                Gles.glVertexAttribPointer((GLuint)mPositionAttribLocation, 3, Gles.GL_FLOAT, Gles.GL_FALSE, 0, mVertexPositions);
                Gles.glThrowError();
            }

            Gles.glEnableVertexAttribArray((GLuint)mPositionAttribLocation);
            Gles.glThrowError();

            Gles.glDrawArrays(Gles.GL_TRIANGLES, 0, 3);
            Gles.glThrowError();
        }
コード例 #2
0
        public SimpleRenderer()
        {
            mWindowWidth  = 0;
            mWindowHeight = 0;

            string vs =
                @"attribute vec4 aPosition;
void main() {
    gl_Position = aPosition;
}";

            string fs =
                @"precision mediump float;
void main() {
    gl_FragColor = vec4(1, 0, 0, 1);
}";

            mProgram = CompileProgram(vs, fs);
            mPositionAttribLocation = Gles.glGetAttribLocation(mProgram, "aPosition");

            if (UseBufferMethod)
            {
                Gles.glGenBuffers(1, out mVertexPositionBuffer);
                Gles.glThrowError();

                Gles.glBindBuffer(Gles.GL_ARRAY_BUFFER, mVertexPositionBuffer);
                Gles.glThrowError();

                Gles.glBufferData(Gles.GL_ARRAY_BUFFER, mVertexPositions, Gles.GL_STATIC_DRAW);
                Gles.glThrowError();
            }
        }
コード例 #3
0
        public void Draw()
        {
            Gles.glEnable(Gles.GL_DEPTH_TEST);
            Gles.glClear(Gles.GL_COLOR_BUFFER_BIT | Gles.GL_DEPTH_BUFFER_BIT);

            if (mProgram == 0)
            {
                return;
            }

            Gles.glUseProgram(mProgram);

            Gles.glBindBuffer(Gles.GL_ARRAY_BUFFER, mVertexPositionBuffer);
            Gles.glEnableVertexAttribArray((GLuint)mPositionAttribLocation);
            Gles.glVertexAttribPointer((GLuint)mPositionAttribLocation, 3, Gles.GL_FLOAT, Gles.GL_FALSE, 0, IntPtr.Zero);

            Gles.glBindBuffer(Gles.GL_ARRAY_BUFFER, mVertexColorBuffer);
            Gles.glEnableVertexAttribArray((GLuint)mColorAttribLocation);
            Gles.glVertexAttribPointer((GLuint)mColorAttribLocation, 3, Gles.GL_FLOAT, Gles.GL_FALSE, 0, IntPtr.Zero);

            float[,] modelMatrix = MathHelpers.SimpleModelMatrix((float)mDrawCount / 50.0f);
            Gles.glUniformMatrix4fv(mModelUniformLocation, 1, Gles.GL_FALSE, modelMatrix);

            float[,] viewMatrix = MathHelpers.SimpleViewMatrix();
            Gles.glUniformMatrix4fv(mViewUniformLocation, 1, Gles.GL_FALSE, viewMatrix);

            float[,] projectionMatrix = MathHelpers.SimpleProjectionMatrix((float)mWindowWidth / (float)mWindowHeight);
            Gles.glUniformMatrix4fv(mProjUniformLocation, 1, Gles.GL_FALSE, projectionMatrix);

            // Draw 36 indices: six faces, two triangles per face, 3 indices per triangle
            Gles.glBindBuffer(Gles.GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
            Gles.glDrawElements(Gles.GL_TRIANGLES, (6 * 2) * 3, Gles.GL_UNSIGNED_SHORT, IntPtr.Zero);

            mDrawCount += 1;
        }
コード例 #4
0
        public SimpleRenderer()
        {
            mWindowWidth  = 0;
            mWindowHeight = 0;
            mDrawCount    = 0;

            // Vertex Shader source
            string vs =
                @"uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uProjMatrix;
attribute vec4 aPosition;
attribute vec4 aColor;
varying vec4 vColor;
void main()
{
    gl_Position = uProjMatrix * uViewMatrix * uModelMatrix * aPosition;
    vColor = aColor;
}";

            // Fragment Shader source
            string fs =
                @"precision mediump float;
varying vec4 vColor;
void main()
{
    gl_FragColor = vColor;
}";

            // Set up the shader and its uniform/attribute locations.
            mProgram = CompileProgram(vs, fs);
            mPositionAttribLocation = Gles.glGetAttribLocation(mProgram, "aPosition");
            mColorAttribLocation    = Gles.glGetAttribLocation(mProgram, "aColor");
            mModelUniformLocation   = Gles.glGetUniformLocation(mProgram, "uModelMatrix");
            mViewUniformLocation    = Gles.glGetUniformLocation(mProgram, "uViewMatrix");
            mProjUniformLocation    = Gles.glGetUniformLocation(mProgram, "uProjMatrix");

            // Then set up the cube geometry.
            GLfloat[] vertexPositions = new[]
            {
                -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,
                1.0f, 1.0f, -1.0f,
                1.0f, 1.0f, 1.0f,
            };

            Gles.glGenBuffers(1, out mVertexPositionBuffer);
            Gles.glBindBuffer(Gles.GL_ARRAY_BUFFER, mVertexPositionBuffer);
            Gles.glBufferData(Gles.GL_ARRAY_BUFFER, vertexPositions, Gles.GL_STATIC_DRAW);

            GLfloat[] vertexColors = new[]
            {
                0.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 1.0f,
                0.0f, 1.0f, 0.0f,
                0.0f, 1.0f, 1.0f,
                1.0f, 0.0f, 0.0f,
                1.0f, 0.0f, 1.0f,
                1.0f, 1.0f, 0.0f,
                1.0f, 1.0f, 1.0f,
            };

            Gles.glGenBuffers(1, out mVertexColorBuffer);
            Gles.glBindBuffer(Gles.GL_ARRAY_BUFFER, mVertexColorBuffer);
            Gles.glBufferData(Gles.GL_ARRAY_BUFFER, vertexColors, Gles.GL_STATIC_DRAW);

            GLshort[] indices = new GLshort[]
            {
                0, 1, 2, // -x
                1, 3, 2,

                4, 6, 5, // +x
                5, 6, 7,

                0, 5, 1, // -y
                0, 4, 5,

                2, 7, 6, // +y
                2, 3, 7,

                0, 6, 4, // -z
                0, 2, 6,

                1, 7, 3, // +z
                1, 5, 7,
            };

            Gles.glGenBuffers(1, out mIndexBuffer);
            Gles.glBindBuffer(Gles.GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
            Gles.glBufferData(Gles.GL_ELEMENT_ARRAY_BUFFER, indices, Gles.GL_STATIC_DRAW);
        }