Beispiel #1
0
        public static void DrawScene()
        {
            GL.viewport(0, 0, Canvas.width, Canvas.height);
            GL.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);

            GLMatrix4.identity(Matrices.ModelView);
            GLMatrix4.translate(Matrices.ModelView, new [] { 0, 0, Z });
            GLMatrix4.rotate(Matrices.ModelView, DegreesToRadians(RotationX), new [] { 1f, 0, 0 });
            GLMatrix4.rotate(Matrices.ModelView, DegreesToRadians(RotationY), new [] { 0, 1f, 0 });

            GL.bindBuffer(GL.ARRAY_BUFFER, Buffers.CubeVertexPositions);
            GL.vertexAttribPointer(Attributes.VertexPosition, 3, GL.FLOAT, false, 0, 0);

            GL.bindBuffer(GL.ARRAY_BUFFER, Buffers.CubeVertexNormals);
            GL.vertexAttribPointer(Attributes.VertexNormal, 3, GL.FLOAT, false, 0, 0);

            GL.bindBuffer(GL.ARRAY_BUFFER, Buffers.CubeTextureCoords);
            GL.vertexAttribPointer(Attributes.TextureCoord, 2, GL.FLOAT, false, 0, 0);

            GL.activeTexture(GL.TEXTURE0);
            GL.bindTexture(GL.TEXTURE_2D, CrateTexture);
            GL.uniform1i(Uniforms.Sampler, 0);

            bool lighting = Document.getElementById("lighting").@checked;

            GL.uniform1i(Uniforms.UseLighting, lighting ? 1 : 0);

            if (lighting)
            {
                GL.uniform3f(
                    Uniforms.AmbientColor,
                    float.Parse(Document.getElementById("ambientR").value),
                    float.Parse(Document.getElementById("ambientG").value),
                    float.Parse(Document.getElementById("ambientB").value)
                    );

                var lightingDirection = new [] {
                    float.Parse(Document.getElementById("lightDirectionX").value),
                    float.Parse(Document.getElementById("lightDirectionY").value),
                    float.Parse(Document.getElementById("lightDirectionZ").value)
                };
                GLVector3.normalize(lightingDirection, lightingDirection);
                GLVector3.scale(lightingDirection, -1);

                GL.uniform3fv(Uniforms.LightingDirection, lightingDirection);

                GL.uniform3f(
                    Uniforms.DirectionalColor,
                    float.Parse(Document.getElementById("directionalR").value),
                    float.Parse(Document.getElementById("directionalG").value),
                    float.Parse(Document.getElementById("directionalB").value)
                    );
            }

            GL.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, Buffers.CubeIndices);

            GL.uniformMatrix4fv(Uniforms.ProjectionMatrix, false, Matrices.Projection);
            GL.uniformMatrix4fv(Uniforms.ModelViewMatrix, false, Matrices.ModelView);

            GLMatrix4.toInverseMat3(Matrices.ModelView, Matrices.Normal);
            GLMatrix3.transpose(Matrices.Normal);
            GL.uniformMatrix3fv(Uniforms.NormalMatrix, false, Matrices.Normal);

            GL.drawElements(GL.TRIANGLES, CubeData.Indices.Length, GL.UNSIGNED_SHORT, 0);
        }
Beispiel #2
0
        public static void DrawScene()
        {
            GL.viewport(0, 0, Canvas.width, Canvas.height);
            GL.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);

            GLMatrix4.identity(Matrices.ModelView);
            GLMatrix4.translate(Matrices.ModelView, new [] { 0, 0, Z });
            GLMatrix4.rotate(Matrices.ModelView, DegreesToRadians(RotationX), new [] { 1f, 0, 0 });
            GLMatrix4.rotate(Matrices.ModelView, DegreesToRadians(RotationY), new [] { 0, 1f, 0 });

            var sizeofFloat      = Marshal.SizeOf(typeof(float));
            var sizeofVertex     = Marshal.SizeOf(CubeVertex.Exemplar);
            var sizeofPosition   = Marshal.SizeOf(CubeVertex.Exemplar.Position);
            var sizeofNormal     = Marshal.SizeOf(CubeVertex.Exemplar.Normal);
            var sizeofTexCoord   = Marshal.SizeOf(CubeVertex.Exemplar.TexCoord);
            var offsetOfPosition = Marshal.OffsetOf(typeof(CubeVertex), "Position").ToInt32();
            var offsetOfNormal   = Marshal.OffsetOf(typeof(CubeVertex), "Normal").ToInt32();
            var offsetOfTexCoord = Marshal.OffsetOf(typeof(CubeVertex), "TexCoord").ToInt32();

            // Contrary to what you would expect from reading the documentation, and from a stride of 0 being densely-packed elements,
            //  glVertexAttribPointer's 'stride' parameter is the *complete* size of a vertex, not the size excluding the attribute you are describing.

            GL.bindBuffer(GL.ARRAY_BUFFER, Buffers.CubeVertices);
            GL.vertexAttribPointer(Attributes.VertexPosition, sizeofPosition / sizeofFloat, GL.FLOAT, false, sizeofVertex, offsetOfPosition);
            GL.vertexAttribPointer(Attributes.VertexNormal, sizeofNormal / sizeofFloat, GL.FLOAT, false, sizeofVertex, offsetOfNormal);
            GL.vertexAttribPointer(Attributes.TextureCoord, sizeofTexCoord / sizeofFloat, GL.FLOAT, false, sizeofVertex, offsetOfTexCoord);

            GL.activeTexture(GL.TEXTURE0);
            GL.bindTexture(GL.TEXTURE_2D, CrateTexture);
            GL.uniform1i(Uniforms.Sampler, 0);

            bool lighting = Document.getElementById("lighting").@checked;

            GL.uniform1i(Uniforms.UseLighting, lighting ? 1 : 0);

            if (lighting)
            {
                GL.uniform3f(
                    Uniforms.AmbientColor,
                    GetTextboxFloat("ambientR"),
                    GetTextboxFloat("ambientG"),
                    GetTextboxFloat("ambientB")
                    );

                var lightingDirection = new [] {
                    GetTextboxFloat("lightDirectionX"),
                    GetTextboxFloat("lightDirectionY"),
                    GetTextboxFloat("lightDirectionZ")
                };
                GLVector3.normalize(lightingDirection, lightingDirection);
                GLVector3.scale(lightingDirection, -1);

                GL.uniform3fv(Uniforms.LightingDirection, lightingDirection);

                GL.uniform3f(
                    Uniforms.DirectionalColor,
                    GetTextboxFloat("directionalR"),
                    GetTextboxFloat("directionalG"),
                    GetTextboxFloat("directionalB")
                    );
            }

            GL.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, Buffers.CubeIndices);

            GL.uniformMatrix4fv(Uniforms.ProjectionMatrix, false, Matrices.Projection);
            GL.uniformMatrix4fv(Uniforms.ModelViewMatrix, false, Matrices.ModelView);

            GLMatrix4.toInverseMat3(Matrices.ModelView, Matrices.Normal);
            GLMatrix3.transpose(Matrices.Normal);
            GL.uniformMatrix3fv(Uniforms.NormalMatrix, false, Matrices.Normal);

            GL.drawElements(GL.TRIANGLES, CubeData.Indices.Length, GL.UNSIGNED_SHORT, 0);
        }