public static Mesh Compute(Vector3D length) { if (length.X < 0 || length.Y < 0 || length.Z < 0) { throw new ArgumentOutOfRangeException("length"); } Mesh mesh = new Mesh(); mesh.PrimitiveType = PrimitiveType.Triangles; mesh.FrontFaceWindingOrder = WindingOrder.Counterclockwise; VertexAttributeDoubleVector3 positionsAttribute = new VertexAttributeDoubleVector3("position", 8); mesh.Attributes.Add(positionsAttribute); IndicesUnsignedShort indices = new IndicesUnsignedShort(36); mesh.Indices = indices; // // 8 corner points // IList <Vector3D> positions = positionsAttribute.Values; Vector3D corner = 0.5 * length; positions.Add(new Vector3D(-corner.X, -corner.Y, -corner.Z)); positions.Add(new Vector3D(corner.X, -corner.Y, -corner.Z)); positions.Add(new Vector3D(corner.X, corner.Y, -corner.Z)); positions.Add(new Vector3D(-corner.X, corner.Y, -corner.Z)); positions.Add(new Vector3D(-corner.X, -corner.Y, corner.Z)); positions.Add(new Vector3D(corner.X, -corner.Y, corner.Z)); positions.Add(new Vector3D(corner.X, corner.Y, corner.Z)); positions.Add(new Vector3D(-corner.X, corner.Y, corner.Z)); // // 6 faces, 2 triangles each // indices.AddTriangle(new TriangleIndicesUnsignedShort(4, 5, 6)); // Top: plane z = corner.Z indices.AddTriangle(new TriangleIndicesUnsignedShort(4, 6, 7)); indices.AddTriangle(new TriangleIndicesUnsignedShort(1, 0, 3)); // Bottom: plane z = -corner.Z indices.AddTriangle(new TriangleIndicesUnsignedShort(1, 3, 2)); indices.AddTriangle(new TriangleIndicesUnsignedShort(1, 6, 5)); // Side: plane x = corner.X indices.AddTriangle(new TriangleIndicesUnsignedShort(1, 2, 6)); indices.AddTriangle(new TriangleIndicesUnsignedShort(2, 3, 7)); // Side: plane y = corner.Y indices.AddTriangle(new TriangleIndicesUnsignedShort(2, 7, 6)); indices.AddTriangle(new TriangleIndicesUnsignedShort(3, 0, 4)); // Side: plane x = -corner.X indices.AddTriangle(new TriangleIndicesUnsignedShort(3, 4, 7)); indices.AddTriangle(new TriangleIndicesUnsignedShort(0, 1, 5)); // Side: plane y = -corner.Y indices.AddTriangle(new TriangleIndicesUnsignedShort(0, 5, 4)); return(mesh); }
public static Mesh Compute(Vector3D length) { if (length.X < 0 || length.Y < 0 || length.Z < 0) { throw new ArgumentOutOfRangeException("length"); } Mesh mesh = new Mesh(); mesh.PrimitiveType = PrimitiveType.Triangles; mesh.FrontFaceWindingOrder = WindingOrder.Counterclockwise; VertexAttributeDoubleVector3 positionsAttribute = new VertexAttributeDoubleVector3("position", 8); mesh.Attributes.Add(positionsAttribute); IndicesUnsignedShort indices = new IndicesUnsignedShort(24); mesh.Indices = indices; // // 8 corner points // IList<Vector3D> positions = positionsAttribute.Values; Vector3D corner = 0.5 * length; positions.Add(new Vector3D(-corner.X, -corner.Y, -corner.Z)); positions.Add(new Vector3D(corner.X, -corner.Y, -corner.Z)); positions.Add(new Vector3D(corner.X, corner.Y, -corner.Z)); positions.Add(new Vector3D(-corner.X, corner.Y, -corner.Z)); positions.Add(new Vector3D(-corner.X, -corner.Y, corner.Z)); positions.Add(new Vector3D(corner.X, -corner.Y, corner.Z)); positions.Add(new Vector3D(corner.X, corner.Y, corner.Z)); positions.Add(new Vector3D(-corner.X, corner.Y, corner.Z)); // // 6 faces, 2 triangles each // indices.AddTriangle(new TriangleIndicesUnsignedShort(4, 5, 6)); // Top: plane z = corner.Z indices.AddTriangle(new TriangleIndicesUnsignedShort(4, 6, 7)); indices.AddTriangle(new TriangleIndicesUnsignedShort(1, 0, 3)); // Bottom: plane z = -corner.Z indices.AddTriangle(new TriangleIndicesUnsignedShort(1, 3, 2)); indices.AddTriangle(new TriangleIndicesUnsignedShort(1, 6, 5)); // Side: plane x = corner.X indices.AddTriangle(new TriangleIndicesUnsignedShort(1, 2, 6)); indices.AddTriangle(new TriangleIndicesUnsignedShort(2, 3, 7)); // Side: plane y = corner.Y indices.AddTriangle(new TriangleIndicesUnsignedShort(2, 7, 6)); indices.AddTriangle(new TriangleIndicesUnsignedShort(3, 0, 4)); // Side: plane x = -corner.X indices.AddTriangle(new TriangleIndicesUnsignedShort(3, 4, 7)); indices.AddTriangle(new TriangleIndicesUnsignedShort(0, 1, 5)); // Side: plane y = -corner.Y indices.AddTriangle(new TriangleIndicesUnsignedShort(0, 5, 4)); return mesh; }
public void MeshIndices() { Mesh mesh = new Mesh(); IndicesUnsignedShort indicesShort = new IndicesUnsignedShort(); mesh.Indices = indicesShort; Assert.AreEqual(IndicesType.UnsignedShort, mesh.Indices.Datatype); IndicesUnsignedInt indicesInt = new IndicesUnsignedInt(); mesh.Indices = indicesInt; Assert.AreEqual(IndicesType.UnsignedInt, mesh.Indices.Datatype); indicesInt.AddTriangle(new TriangleIndicesUnsignedInt(0, 1, 2)); Assert.AreEqual(0, indicesInt.Values[0]); Assert.AreEqual(1, indicesInt.Values[1]); Assert.AreEqual(2, indicesInt.Values[2]); }
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; }
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); }