private void BuildQuad() { VertexP3N3G3T2[] Vertices = new VertexP3N3G3T2[4]; Vertices[0] = new VertexP3N3G3T2() { P = new float3(-1, +1, 0), N = new float3(0, 0, 1), UV = new float2(0, 0) }; // Top-Left Vertices[1] = new VertexP3N3G3T2() { P = new float3(-1, -1, 0), N = new float3(0, 0, 1), UV = new float2(0, 1) }; // Bottom-Left Vertices[2] = new VertexP3N3G3T2() { P = new float3(+1, +1, 0), N = new float3(0, 0, 1), UV = new float2(1, 0) }; // Top-Right Vertices[3] = new VertexP3N3G3T2() { P = new float3(+1, -1, 0), N = new float3(0, 0, 1), UV = new float2(1, 1) }; // Bottom-Right ByteBuffer VerticesBuffer = VertexP3N3G3T2.FromArray(Vertices); m_Prim_Quad = new Primitive(m_Device, Vertices.Length, VerticesBuffer, null, Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.P3N3G3T2); }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); // Initialize the device m_device.Init(panelOutput1.Handle, false, true); // Build cube primitive { float3 colorXp = new float3(1, 0, 0); float3 colorXn = new float3(1, 1, 0); float3 colorYp = new float3(0, 1, 0); float3 colorYn = new float3(0, 1, 1); float3 colorZp = new float3(0, 0, 1); float3 colorZn = new float3(1, 0, 1); VertexP3N3G3T2[] vertices = new VertexP3N3G3T2[6 * 4] { // +X new VertexP3N3G3T2() { P = new float3(1, 1, 1), N = new float3(1, 0, 0), T = colorXp, UV = new float2(0, 0) }, new VertexP3N3G3T2() { P = new float3(1, -1, 1), N = new float3(1, 0, 0), T = colorXp, UV = new float2(0, 1) }, new VertexP3N3G3T2() { P = new float3(1, -1, -1), N = new float3(1, 0, 0), T = colorXp, UV = new float2(1, 1) }, new VertexP3N3G3T2() { P = new float3(1, 1, -1), N = new float3(1, 0, 0), T = colorXp, UV = new float2(1, 0) }, // -X new VertexP3N3G3T2() { P = new float3(-1, 1, -1), N = new float3(-1, 0, 0), T = colorXn, UV = new float2(0, 0) }, new VertexP3N3G3T2() { P = new float3(-1, -1, -1), N = new float3(-1, 0, 0), T = colorXn, UV = new float2(0, 1) }, new VertexP3N3G3T2() { P = new float3(-1, -1, 1), N = new float3(-1, 0, 0), T = colorXn, UV = new float2(1, 1) }, new VertexP3N3G3T2() { P = new float3(-1, 1, 1), N = new float3(-1, 0, 0), T = colorXn, UV = new float2(1, 0) }, // +Y new VertexP3N3G3T2() { P = new float3(-1, 1, -1), N = new float3(0, 1, 0), T = colorYp, UV = new float2(0, 0) }, new VertexP3N3G3T2() { P = new float3(-1, 1, 1), N = new float3(0, 1, 0), T = colorYp, UV = new float2(0, 1) }, new VertexP3N3G3T2() { P = new float3(1, 1, 1), N = new float3(0, 1, 0), T = colorYp, UV = new float2(1, 1) }, new VertexP3N3G3T2() { P = new float3(1, 1, -1), N = new float3(0, 1, 0), T = colorYp, UV = new float2(1, 0) }, // -Y new VertexP3N3G3T2() { P = new float3(-1, -1, 1), N = new float3(0, -1, 0), T = colorYn, UV = new float2(0, 0) }, new VertexP3N3G3T2() { P = new float3(-1, -1, -1), N = new float3(0, -1, 0), T = colorYn, UV = new float2(0, 1) }, new VertexP3N3G3T2() { P = new float3(1, -1, -1), N = new float3(0, -1, 0), T = colorYn, UV = new float2(1, 1) }, new VertexP3N3G3T2() { P = new float3(1, -1, 1), N = new float3(0, -1, 0), T = colorYn, UV = new float2(1, 0) }, // +Z new VertexP3N3G3T2() { P = new float3(-1, 1, 1), N = new float3(0, 0, 1), T = colorZp, UV = new float2(0, 0) }, new VertexP3N3G3T2() { P = new float3(-1, -1, 1), N = new float3(0, 0, 1), T = colorZp, UV = new float2(0, 1) }, new VertexP3N3G3T2() { P = new float3(1, -1, 1), N = new float3(0, 0, 1), T = colorZp, UV = new float2(1, 1) }, new VertexP3N3G3T2() { P = new float3(1, 1, 1), N = new float3(0, 0, 1), T = colorZp, UV = new float2(1, 0) }, // -Z new VertexP3N3G3T2() { P = new float3(1, 1, -1), N = new float3(0, 0, -1), T = colorZn, UV = new float2(0, 0) }, new VertexP3N3G3T2() { P = new float3(1, -1, -1), N = new float3(0, 0, -1), T = colorZn, UV = new float2(0, 1) }, new VertexP3N3G3T2() { P = new float3(-1, -1, -1), N = new float3(0, 0, -1), T = colorZn, UV = new float2(1, 1) }, new VertexP3N3G3T2() { P = new float3(-1, 1, -1), N = new float3(0, 0, -1), T = colorZn, UV = new float2(1, 0) }, }; uint[] indices = new uint[3 * 2 * 6] { 4 * 0 + 0, 4 * 0 + 1, 4 * 0 + 2, 4 * 0 + 0, 4 * 0 + 2, 4 * 0 + 3, 4 * 1 + 0, 4 * 1 + 1, 4 * 1 + 2, 4 * 1 + 0, 4 * 1 + 2, 4 * 1 + 3, 4 * 2 + 0, 4 * 2 + 1, 4 * 2 + 2, 4 * 2 + 0, 4 * 2 + 2, 4 * 2 + 3, 4 * 3 + 0, 4 * 3 + 1, 4 * 3 + 2, 4 * 3 + 0, 4 * 3 + 2, 4 * 3 + 3, 4 * 4 + 0, 4 * 4 + 1, 4 * 4 + 2, 4 * 4 + 0, 4 * 4 + 2, 4 * 4 + 3, 4 * 5 + 0, 4 * 5 + 1, 4 * 5 + 2, 4 * 5 + 0, 4 * 5 + 2, 4 * 5 + 3, }; m_prim_cube = new Primitive(m_device, 6 * 4, VertexP3N3G3T2.FromArray(vertices), indices, Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3G3T2); } // Build the shader to render the cube m_shader_renderCube = new Shader(m_device, new System.IO.FileInfo(@".\Shaders\RenderCube.hlsl"), VERTEX_FORMAT.P3N3G3T2, "VS", null, "PS", null); // Build constant buffer to provide camera transform m_CB_Camera = new ConstantBuffer <CBCamera>(m_device, 0); Application.Idle += Application_Idle; }
void BuildPrimitives() { { // Post-process quad List <VertexPt4> Vertices = new List <VertexPt4>(); Vertices.Add(new VertexPt4() { Pt = new float4(-1, +1, 0, 1) }); Vertices.Add(new VertexPt4() { Pt = new float4(-1, -1, 0, 1) }); Vertices.Add(new VertexPt4() { Pt = new float4(+1, +1, 0, 1) }); Vertices.Add(new VertexPt4() { Pt = new float4(+1, -1, 0, 1) }); m_Prim_Quad = new Primitive(m_Device, 4, VertexPt4.FromArray(Vertices.ToArray()), null, Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.Pt4); } { // Sphere Primitive List <VertexP3N3G3T2> Vertices = new List <VertexP3N3G3T2>(); List <uint> Indices = new List <uint>(); const int SUBDIVS_THETA = 80; const int SUBDIVS_PHI = 160; for (int Y = 0; Y <= SUBDIVS_THETA; Y++) { double Theta = Y * Math.PI / SUBDIVS_THETA; float CosTheta = (float)Math.Cos(Theta); float SinTheta = (float)Math.Sin(Theta); for (int X = 0; X <= SUBDIVS_PHI; X++) { double Phi = X * 2.0 * Math.PI / SUBDIVS_PHI; float CosPhi = (float)Math.Cos(Phi); float SinPhi = (float)Math.Sin(Phi); float3 N = new float3(SinTheta * SinPhi, CosTheta, SinTheta * CosPhi); float3 T = new float3(CosPhi, 0.0f, -SinPhi); float2 UV = new float2((float)X / SUBDIVS_PHI, (float)Y / SUBDIVS_THETA); Vertices.Add(new VertexP3N3G3T2() { P = N, N = N, T = T, UV = UV }); } } for (int Y = 0; Y < SUBDIVS_THETA; Y++) { int CurrentLineOffset = Y * (SUBDIVS_PHI + 1); int NextLineOffset = (Y + 1) * (SUBDIVS_PHI + 1); for (int X = 0; X <= SUBDIVS_PHI; X++) { Indices.Add((uint)(CurrentLineOffset + X)); Indices.Add((uint)(NextLineOffset + X)); } if (Y < SUBDIVS_THETA - 1) { Indices.Add((uint)(NextLineOffset - 1)); // Degenerate triangle to end the line Indices.Add((uint)NextLineOffset); // Degenerate triangle to start the next line } } m_Prim_Sphere = new Primitive(m_Device, Vertices.Count, VertexP3N3G3T2.FromArray(Vertices.ToArray()), Indices.ToArray(), Primitive.TOPOLOGY.TRIANGLE_STRIP, VERTEX_FORMAT.P3N3G3T2); } { // Build the cube float3[] Normals = new float3[6] { -float3.UnitX, float3.UnitX, -float3.UnitY, float3.UnitY, -float3.UnitZ, float3.UnitZ, }; float3[] Tangents = new float3[6] { float3.UnitZ, -float3.UnitZ, float3.UnitX, -float3.UnitX, -float3.UnitX, float3.UnitX, }; VertexP3N3G3T2[] Vertices = new VertexP3N3G3T2[6 * 4]; uint[] Indices = new uint[2 * 6 * 3]; for (int FaceIndex = 0; FaceIndex < 6; FaceIndex++) { float3 N = Normals[FaceIndex]; float3 T = Tangents[FaceIndex]; float3 B = N.Cross(T); Vertices[4 * FaceIndex + 0] = new VertexP3N3G3T2() { P = N - T + B, N = N, T = T, // B = B, UV = new float2(0, 0) }; Vertices[4 * FaceIndex + 1] = new VertexP3N3G3T2() { P = N - T - B, N = N, T = T, // B = B, UV = new float2(0, 1) }; Vertices[4 * FaceIndex + 2] = new VertexP3N3G3T2() { P = N + T - B, N = N, T = T, // B = B, UV = new float2(1, 1) }; Vertices[4 * FaceIndex + 3] = new VertexP3N3G3T2() { P = N + T + B, N = N, T = T, // B = B, UV = new float2(1, 0) }; Indices[2 * 3 * FaceIndex + 0] = (uint)(4 * FaceIndex + 0); Indices[2 * 3 * FaceIndex + 1] = (uint)(4 * FaceIndex + 1); Indices[2 * 3 * FaceIndex + 2] = (uint)(4 * FaceIndex + 2); Indices[2 * 3 * FaceIndex + 3] = (uint)(4 * FaceIndex + 0); Indices[2 * 3 * FaceIndex + 4] = (uint)(4 * FaceIndex + 2); Indices[2 * 3 * FaceIndex + 5] = (uint)(4 * FaceIndex + 3); } ByteBuffer VerticesBuffer = VertexP3N3G3T2.FromArray(Vertices); m_Prim_Cube = new Primitive(m_Device, Vertices.Length, VerticesBuffer, Indices, Primitive.TOPOLOGY.TRIANGLE_LIST, VERTEX_FORMAT.P3N3G3T2); } }