Example #1
0
        private Mesh CreateDegenerateTriangleMesh()
        {
            Mesh mesh = new Mesh();

            mesh.PrimitiveType         = PrimitiveType.Triangles;
            mesh.FrontFaceWindingOrder = WindingOrder.Counterclockwise;

            int numberOfPositions = _clipmapSegments * 4;
            VertexAttributeFloatVector2 positionsAttribute = new VertexAttributeFloatVector2("position", numberOfPositions);
            IList <Vector2F>            positions          = positionsAttribute.Values;

            mesh.Attributes.Add(positionsAttribute);

            int numberOfIndices          = (_clipmapSegments / 2) * 3 * 4;
            IndicesUnsignedShort indices = new IndicesUnsignedShort(numberOfIndices);

            mesh.Indices = indices;

            for (int i = 0; i < _clipmapPosts; ++i)
            {
                positions.Add(new Vector2F(0.0f, i));
            }

            for (int i = 1; i < _clipmapPosts; ++i)
            {
                positions.Add(new Vector2F(i, _clipmapSegments));
            }

            for (int i = _clipmapSegments - 1; i >= 0; --i)
            {
                positions.Add(new Vector2F(_clipmapSegments, i));
            }

            for (int i = _clipmapSegments - 1; i > 0; --i)
            {
                positions.Add(new Vector2F(i, 0.0f));
            }

            for (int i = 0; i < numberOfIndices; i += 2)
            {
                indices.AddTriangle(new TriangleIndicesUnsignedShort((ushort)i, (ushort)(i + 1), (ushort)(i + 2)));
            }

            return(mesh);
        }
Example #2
0
        public Triangle()
        {
            _window              = Device.CreateWindow(800, 600, "Chapter 3:  Triangle");
            _window.Resize      += OnResize;
            _window.RenderFrame += OnRenderFrame;
            _sceneState          = new SceneState();
            _clearState          = new ClearState();

            string vs =
                @"#version 330

                  layout(location = og_positionVertexLocation) in vec4 position;
                  uniform mat4 og_modelViewPerspectiveMatrix;

                  void main()                     
                  {
                        gl_Position = og_modelViewPerspectiveMatrix * position; 
                  }";

            string fs =
                @"#version 330
                 
                  out vec3 fragmentColor;
                  uniform vec3 u_color;

                  void main()
                  {
                      fragmentColor = u_color;
                  }";
            ShaderProgram sp = Device.CreateShaderProgram(vs, fs);

            ((Uniform <Vector3F>)sp.Uniforms["u_color"]).Value = new Vector3F(1, 0, 0);

            ///////////////////////////////////////////////////////////////////

            Mesh mesh = new Mesh();

            VertexAttributeFloatVector3 positionsAttribute = new VertexAttributeFloatVector3("position", 3);

            mesh.Attributes.Add(positionsAttribute);

            IndicesUnsignedShort indices = new IndicesUnsignedShort(3);

            mesh.Indices = indices;

            IList <Vector3F> positions = positionsAttribute.Values;

            positions.Add(new Vector3F(0, 0, 0));
            positions.Add(new Vector3F(1, 0, 0));
            positions.Add(new Vector3F(0, 0, 1));

            indices.AddTriangle(new TriangleIndicesUnsignedShort(0, 1, 2));

            VertexArray va = _window.Context.CreateVertexArray(mesh, sp.VertexAttributes, BufferHint.StaticDraw);

            ///////////////////////////////////////////////////////////////////

            RenderState renderState = new RenderState();

            renderState.FacetCulling.Enabled = false;
            renderState.DepthTest.Enabled    = false;

            _drawState = new DrawState(renderState, sp, va);

            ///////////////////////////////////////////////////////////////////

            _sceneState.Camera.ZoomToTarget(1);
        }