Пример #1
0
        private void DrawSprite(Sprite sprite)
        {
            Mat4.Perspective(45, (double)canvas.Width / canvas.Height, 0.1, 1000, pMatrix);
            Mat4.Identity(mvMatrix);
            Scale(mvMatrix, sprite.Width, sprite.Height);
            Mat4.Translate(mvMatrix, new double[] { sprite.X / sprite.Width, sprite.Y / sprite.Height, -12 });
            //Mat4.Rotate(mvMatrix, this.DegToRad(xRotation), new double[] { 1, 0, 0 });
            //Mat4.Rotate(mvMatrix, this.DegToRad(yRotation), new double[] { 0, 1, 0 });

            WebGlRenderingContext.BindBuffer(WebGlRenderingContext.ARRAY_BUFFER, this.cubeVertexPositionBuffer);
            WebGlRenderingContext.VertexAttribPointer(this.vertexPositionAttribute, 3, WebGlRenderingContext.FLOAT, false, 0, 0);

            WebGlRenderingContext.BindBuffer(WebGlRenderingContext.ARRAY_BUFFER, this.cubeVertexNormalBuffer);
            WebGlRenderingContext.VertexAttribPointer(this.vertexNormalAttribute, 3, WebGlRenderingContext.FLOAT, false, 0, 0);

            WebGlRenderingContext.BindBuffer(WebGlRenderingContext.ARRAY_BUFFER, this.cubeVertexTextureCoordBuffer);
            WebGlRenderingContext.VertexAttribPointer(this.textureCoordAttribute, 2, WebGlRenderingContext.FLOAT, false, 0, 0);

            WebGlRenderingContext.ActiveTexture(WebGlRenderingContext.TEXTURE0);
            WebGlRenderingContext.BindTexture(WebGlRenderingContext.TEXTURE_2D, sprite.Texture.WebGLTexture);

            WebGlRenderingContext.Uniform1i(this.samplerUniform, 0);

            // Add Blending
            if (this.useBlending)
            {
                WebGlRenderingContext.BlendFunc(WebGlRenderingContext.SRC_ALPHA, WebGlRenderingContext.ONE);
                WebGlRenderingContext.Enable(WebGlRenderingContext.BLEND);
                WebGlRenderingContext.Disable(WebGlRenderingContext.DEPTH_TEST);
                WebGlRenderingContext.Uniform1f(this.alphaUniform, this.alpha);
            }
            else
            {
                WebGlRenderingContext.Disable(WebGlRenderingContext.BLEND);
                WebGlRenderingContext.Enable(WebGlRenderingContext.DEPTH_TEST);
                WebGlRenderingContext.Uniform1f(this.alphaUniform, 1);
            }

            // Add Lighting
            WebGlRenderingContext.Uniform1i(this.useLightingUniform, this.useLighting);

            if (this.useLighting)
            {
                WebGlRenderingContext.Uniform3f(this.ambientColorUniform, this.ambientR, this.ambientG, this.ambientB);

                var lightingDirection = new double[] { this.lightDirectionX, this.lightDirectionY, this.lightDirectionZ };
                var adjustedLD        = Vec3.Create();

                Vec3.Normalize(lightingDirection, adjustedLD);
                Vec3.Scale(adjustedLD, -1);

                WebGlRenderingContext.Uniform3fv(this.lightingDirectionUniform, adjustedLD);
                WebGlRenderingContext.Uniform3f(this.directionalColorUniform, this.directionalR, this.directionalG, this.directionalB);
            }

            WebGlRenderingContext.BindBuffer(WebGlRenderingContext.ELEMENT_ARRAY_BUFFER, this.cubeVertexIndexBuffer);

            this.SetMatrixUniforms();

            WebGlRenderingContext.DrawElements(WebGlRenderingContext.TRIANGLES, 6, WebGlRenderingContext.UNSIGNED_SHORT, 0);
        }
Пример #2
0
        public void DrawScene()
        {
            gl.Viewport(0, 0, canvas.Width, canvas.Height);
            gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

            Mat4.Perspective(45, (double)canvas.Width / canvas.Height, 0.1, 100, pMatrix);
            Mat4.Identity(mvMatrix);
            Mat4.Translate(mvMatrix, new double[] { 0.0, 0.0, z });
            Mat4.Rotate(mvMatrix, this.DegToRad(xRotation), new double[] { 1, 0, 0 });
            Mat4.Rotate(mvMatrix, this.DegToRad(yRotation), new double[] { 0, 1, 0 });

            gl.BindBuffer(gl.ARRAY_BUFFER, this.cubeVertexPositionBuffer);
            gl.VertexAttribPointer(this.vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0);

            gl.BindBuffer(gl.ARRAY_BUFFER, this.cubeVertexNormalBuffer);
            gl.VertexAttribPointer(this.vertexNormalAttribute, 3, gl.FLOAT, false, 0, 0);

            gl.BindBuffer(gl.ARRAY_BUFFER, this.cubeVertexTextureCoordBuffer);
            gl.VertexAttribPointer(this.textureCoordAttribute, 2, gl.FLOAT, false, 0, 0);

            gl.ActiveTexture(gl.TEXTURE0);
            gl.BindTexture(gl.TEXTURE_2D, this.texture);

            gl.Uniform1i(this.samplerUniform, 0);

            // Add Blending
            if (this.useBlending)
            {
                gl.BlendFunc(gl.SRC_ALPHA, gl.ONE);
                gl.Enable(gl.BLEND);
                gl.Disable(gl.DEPTH_TEST);
                gl.Uniform1f(this.alphaUniform, this.alpha);
            }
            else
            {
                gl.Disable(gl.BLEND);
                gl.Enable(gl.DEPTH_TEST);
                gl.Uniform1f(this.alphaUniform, 1);
            }

            // Add Lighting
            gl.Uniform1i(this.useLightingUniform, this.useLighting);

            if (this.useLighting)
            {
                gl.Uniform3f(this.ambientColorUniform, this.ambientR, this.ambientG, this.ambientB);

                var lightingDirection = new double[] { this.lightDirectionX, this.lightDirectionY, this.lightDirectionZ };
                var adjustedLD        = Vec3.Create();

                Vec3.Normalize(lightingDirection, adjustedLD);
                Vec3.Scale(adjustedLD, -1);

                gl.Uniform3fv(this.lightingDirectionUniform, adjustedLD);
                gl.Uniform3f(this.directionalColorUniform, this.directionalR, this.directionalG, this.directionalB);
            }

            gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.cubeVertexIndexBuffer);

            this.SetMatrixUniforms();

            gl.DrawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0);
        }
        private void InitShaders()
        {
            // create our shaders
            WebGLShader vertexShader   = LoadShader(GLES20.VERTEX_SHADER, @"
        attribute vec4 a_position;
        attribute vec4 a_color;
        attribute vec2 a_texCoord0;
        attribute vec2 a_texCoord1;
        uniform mat4 u_mvpMatrix;
        varying vec4 v_color;
        varying vec2 v_texCoord0;
        varying vec2 v_texCoord1;
        void main()
        {
            gl_Position = u_mvpMatrix * a_position;
            v_color = a_color;
            v_texCoord0 = a_texCoord0;
            v_texCoord1 = a_texCoord1;
        }");
            WebGLShader fragmentShader = LoadShader(GLES20.FRAGMENT_SHADER, @"
        #ifdef GL_ES
        precision mediump float;
        #endif
        uniform sampler2D s_texture0;
        uniform sampler2D s_texture1;
        uniform int s_texEnv0;
        uniform int s_texEnv1;
        uniform int u_enable_texture_0;
        uniform int u_enable_texture_1;
        varying vec4 v_color;
        varying vec2 v_texCoord0;
        varying vec2 v_texCoord1;
        vec4 finalColor;
        void main()
        {
            finalColor = v_color;
            if (u_enable_texture_0 == 1)
            {
                vec4 texel = texture2D(s_texture0, v_texCoord0);
                if (s_texEnv0 == 1) {
                    finalColor = finalColor * texel;
                } else if (s_texEnv0 == 2) {
                    finalColor = vec4(texel.r, texel.g, texel.b, finalColor.a);
                } else {
                    finalColor = texel;
                }
            }
            if (u_enable_texture_1 == 1) {
                vec4 texel = texture2D(s_texture1, v_texCoord1);
                if (s_texEnv1 == 1) {
                    finalColor = finalColor * texel;
                } else if (s_texEnv1 == 2) {
                    finalColor = vec4(texel.r, texel.g, texel.b, finalColor.a);
                } else {
                    finalColor = texel;
                }
            }
            // simple alpha check
            if (finalColor.a == 0.0) {
                discard;
            }
            float gamma = 1.5;
            float igamma = 1.0 / gamma;
            gl_FragColor = vec4(pow(finalColor.r, igamma), pow(finalColor.g, igamma), pow(finalColor.b, igamma), finalColor.a);
        }");

            if ((vertexShader == null) || (fragmentShader == null))
            {
                throw new Exception("RuntimeException: shader error");
            }
            // Create the program object
            WebGLProgram programObject = gl.CreateProgram();

            if ((programObject == null) || (gl.GetError() != GLES20.NO_ERROR))
            {
                throw new Exception("RuntimeException: program error");
            }
            // Attach our two shaders to the program
            gl.AttachShader(programObject, vertexShader);
            gl.AttachShader(programObject, fragmentShader);
            // Bind "vPosition" to attribute 0
            gl.BindAttribLocation(programObject, ARRAY_POSITION, "a_position");
            gl.BindAttribLocation(programObject, ARRAY_COLOR, "a_color");
            gl.BindAttribLocation(programObject, ARRAY_TEXCOORD_0, "a_texCoord0");
            gl.BindAttribLocation(programObject, ARRAY_TEXCOORD_1, "a_texCoord1");
            // Link the program
            gl.LinkProgram(programObject);
            // TODO(haustein) get position, color from the linker, too
            _uMvpMatrix      = gl.GetUniformLocation(programObject, "u_mvpMatrix");
            _uSampler0       = gl.GetUniformLocation(programObject, "s_texture0");
            _uSampler1       = gl.GetUniformLocation(programObject, "s_texture1");
            _uTexEnv0        = gl.GetUniformLocation(programObject, "s_texEnv0");
            _uTexEnv1        = gl.GetUniformLocation(programObject, "s_texEnv1");
            _uEnableTexture0 = gl.GetUniformLocation(programObject, "u_enable_texture_0");
            _uEnableTexture1 = gl.GetUniformLocation(programObject, "u_enable_texture_1");
            // Check the link status
            bool linked = (bool)gl.GetProgramParameter(programObject, GLES20.LINK_STATUS);

            if (!linked)
            {
                throw new Exception("RuntimeException: linker Error: " + gl.GetProgramInfoLog(programObject));
            }
            gl.UseProgram(programObject);
            gl.Uniform1i(_uSampler0, 0);
            gl.Uniform1i(_uSampler1, 1);
            gl.ActiveTexture(GLES20.TEXTURE0);
        }