protected override void OnLoad() { base.OnLoad(); // initialize shader (load sources, create/compile/link shader program, error checking) // when using the factory method the shader sources are retrieved from the ShaderSourceAttributes _program = ProgramFactory.Create <SimpleColorProgram>(); // this program will be used all the time so just activate it once and for all _program.Use(); //arbitrary black box to load mesh data string jsonString = File.ReadAllText("./Data/Meshes/OpenTK.fakeformat"); MeshData meshData = JsonSerializer.Deserialize <MeshData>(jsonString); Buffer <uint> IndexBuffer = new Buffer <uint>(); IndexBuffer.Init(BufferTarget.ElementArrayBuffer, meshData.Indices.Select(index => index - 1).ToArray()); Buffer <Vector3> VertBuffer = new Buffer <Vector3>(); VertBuffer.Init(BufferTarget.ArrayBuffer, Enumerable.Range(0, meshData.Vertices.Count / 3).Select(a => new Vector3(meshData.Vertices[a * 3], meshData.Vertices[a * 3 + 1], meshData.Vertices[a * 3 + 2])).ToArray()); //a bit of a hack, i wanted the mesh to have some visual depth. //the only reason this works is I just happen to know the Z coordinate for the mesh is in a certain range //other meshes will either look stupid or just throw exceptions because the color values are out of range Buffer <uint> ColorBuffer = new Buffer <uint>(); ColorBuffer.Init(BufferTarget.ArrayBuffer, VertBuffer.Content.Select(vertex => (uint)Color.FromArgb((int)(vertex.Z * 500) + 100, (int)(vertex.Z * 500) + 100, (int)(vertex.Z * 500) + 100).ToArgb()).ToArray()); mesh = new DynamicShape() .WithVertexAttrib(_program.InPosition, VertBuffer) .WithVertexAttrib(_program.InColor, ColorBuffer) .WithElementBuffer(IndexBuffer) .WithDisposeFunction(() => { VertBuffer?.Dispose(); IndexBuffer?.Dispose(); ColorBuffer?.Dispose(); }) .SetDrawFunction((VAO) => { VAO.DrawElements(PrimitiveType.Triangles, IndexBuffer.ElementCount); }); // set camera position ActiveCamera.Position = new Vector3(0, 0, 3); // set a nice clear color GL.ClearColor(Color.MidnightBlue); GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Back); }
private static DynamicShape CreateBasicCube(VertexAttrib VertexPositionAttrib) { Vector3[] Vertices = new[] { new Vector3(-1.0f, -1.0f, 1.0f), new Vector3(1.0f, -1.0f, 1.0f), new Vector3(1.0f, 1.0f, 1.0f), new Vector3(-1.0f, 1.0f, 1.0f), new Vector3(-1.0f, -1.0f, -1.0f), new Vector3(1.0f, -1.0f, -1.0f), new Vector3(1.0f, 1.0f, -1.0f), new Vector3(-1.0f, 1.0f, -1.0f) }; uint[] Indices = new uint[] { // front face 0, 1, 2, 2, 3, 0, // top face 3, 2, 6, 6, 7, 3, // back face 7, 6, 5, 5, 4, 7, // left face 4, 0, 3, 3, 7, 4, // bottom face 5, 1, 0, 0, 4, 5, // right face 1, 5, 6, 6, 2, 1 }; Buffer <Vector3> VertexBuffer = new Buffer <Vector3>(); VertexBuffer.Init(BufferTarget.ArrayBuffer, Vertices); Buffer <uint> IndexBuffer = new Buffer <uint>(); IndexBuffer.Init(BufferTarget.ElementArrayBuffer, Indices); return(new DynamicShape() .WithElementBuffer(IndexBuffer) .WithVertexAttrib(VertexPositionAttrib, VertexBuffer) .WithDisposeFunction(() => { VertexBuffer?.Dispose(); IndexBuffer?.Dispose(); }) .SetDrawFunction((VertexArray VAO) => { VAO.DrawElements(PrimitiveType.Triangles, IndexBuffer.ElementCount); })); }