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);
        }
Пример #2
0
        private void AssertNoWebGLError()
        {
            var error = gl.GetError();

            Assert.Equal(WebGLRenderingContextBase.NO_ERROR, error);
        }
Пример #3
0
        // https://github.com/WaveEngine/WebGL.NET/issues/5
        public void GetErrorRegressionTest()
        {
            var error = gl.GetError();

            Assert.Equal(WebGLRenderingContextBase.NO_ERROR, error);
        }