protected override void OnLoad() { base.OnLoad(); // initialize and bind framebuffer _framebuffer = new Framebuffer(); _framebuffer.Bind(FramebufferTarget.Framebuffer); // initialize a renderbuffer and bind it to the depth attachment // to support depth testing while rendering to the texture _depthBuffer = new Renderbuffer(); _depthBuffer.Init(RenderbufferStorage.DepthComponent, FramebufferWidth, FramebufferHeight); _framebuffer.Attach(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, _depthBuffer); // initialize texture and bind it to the color attachment _texture = new Texture2D(SizedInternalFormat.Rgba8, FramebufferWidth, FramebufferHeight, 1); _framebuffer.Attach(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, _texture); Framebuffer.Unbind(FramebufferTarget.Framebuffer); // initialize shaders _colorProgram = ProgramFactory.Create <SimpleColorProgram>(); _textureProgram = ProgramFactory.Create <SimpleTextureProgram>(); // initialize demonstration geometry _cube = ShapeBuilder.CreateColoredCube(_colorProgram.InPosition, _colorProgram.InColor); _quad = ShapeBuilder.CreateTexturedQuad(_textureProgram.InPosition, _textureProgram.InTexCoord); // set camera position ActiveCamera.Position = new Vector3(0, 0, 3); // enable depth testing GL.Enable(EnableCap.DepthTest); }
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 void OnLoad(object sender, EventArgs e) { // initialize and bind framebuffer _framebuffer = new Framebuffer(); _framebuffer.Bind(FramebufferTarget.Framebuffer); // initialize a renderbuffer and bind it to the depth attachment // to support depth testing while rendering to the texture _depthBuffer = new Renderbuffer(); _depthBuffer.Init(RenderbufferStorage.DepthComponent, FramebufferWidth, FramebufferHeight); _framebuffer.Attach(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, _depthBuffer); // initialize texture and bind it to the color attachment _texture = new Texture2D(SizedInternalFormat.Rgba8, FramebufferWidth, FramebufferHeight, 1); _framebuffer.Attach(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, _texture); Framebuffer.Unbind(FramebufferTarget.Framebuffer); // initialize demonstration geometry _cube = new ColorCube(); _cube.UpdateBuffers(); _quad = new TexturedQuad(); _quad.UpdateBuffers(); // initialize shaders _colorProgram = ProgramFactory.Create <SimpleColorProgram>(); _textureProgram = ProgramFactory.Create <SimpleTextureProgram>(); // set up vertex attributes for the cube _cubeVao = new VertexArray(); _cubeVao.Bind(); _cubeVao.BindAttribute(_colorProgram.InPosition, _cube.VertexBuffer); _cubeVao.BindAttribute(_colorProgram.InColor, _cube.ColorBuffer); _cubeVao.BindElementBuffer(_cube.IndexBuffer); // set up vertex attributes for the quad _quadVao = new VertexArray(); _quadVao.Bind(); _quadVao.BindAttribute(_textureProgram.InPosition, _quad.VertexBuffer); _quadVao.BindAttribute(_textureProgram.InTexCoord, _quad.TexCoordBuffer); // set camera position Camera.DefaultState.Position = new Vector3(0, 0, 3); Camera.ResetToDefault(); // enable depth testing GL.Enable(EnableCap.DepthTest); }