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); }
private void OnLoad(object sender, EventArgs e) { // load texture from file using (var bitmap = new Bitmap("Data/Textures/checker.jpg")) { BitmapTexture.CreateCompatible(bitmap, out _texture); _texture.LoadBitmap(bitmap); } _texture.GenerateMipMaps(); // initialize sampler _sampler = new Sampler(); _sampler.SetWrapMode(TextureWrapMode.Repeat); // create vertex data for a big plane const int a = 10; const int b = 10; var vertices = new[] { new Vertex(-a, 0,-a, 0, 0), new Vertex( a, 0,-a, b, 0), new Vertex(-a, 0, a, 0, b), new Vertex( a, 0, a, b, b) }; // create buffer object and upload vertex data _vbo = new Buffer<Vertex>(); _vbo.Init(BufferTarget.ArrayBuffer, vertices); // initialize shader _program = ProgramFactory.Create<SimpleTextureProgram>(); // activate shader program _program.Use(); // bind sampler _sampler.Bind(TextureUnit.Texture0); // bind texture _program.Texture.BindTexture(TextureUnit.Texture0, _texture); // which is equivalent to //_program.Texture.Set(TextureUnit.Texture0); //_texture.Bind(TextureUnit.Texture0); // set up vertex array and attributes _vao = new VertexArray(); _vao.Bind(); // memory layout of our data is XYZUVXYZUV... // the buffer abstraction knows the total size of one "pack" of vertex data // and if a vertex attribute is bound without further arguments the first N elements are taken from each pack // where N is provided via the VertexAttribAttribute on the program property: _vao.BindAttribute(_program.InPosition, _vbo); // if data should not be taken from the start of each pack, the offset must be given in bytes // to reach the texture coordinates UV the XYZ coordinates must be skipped, that is 3 floats, i.e. an offset of 12 bytes is needed _vao.BindAttribute(_program.InTexCoord, _vbo, 12); // if needed all the available arguments can be specified manually, e.g. //_vao.BindAttribute(_program.InTexCoord, _vbo, 2, VertexAttribPointerType.Float, Marshal.SizeOf(typeof(Vertex)), 12, false); // set default camera Camera.DefaultState.Position = new Vector3(0, 0.5f, 3); Camera.ResetToDefault(); // set a nice clear color GL.ClearColor(Color.MidnightBlue); }