protected override void OnLoad(EventArgs e) { base.OnLoad(e); GL.ClearColor(0.2f, 0.2f, 0.2f, 1.0f); GL.Enable(EnableCap.DepthTest); GL.DepthFunc(DepthFunction.Less); //! Load model for a boid vao = GL.GenVertexArray(); GL.BindVertexArray(vao); vbo = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * modelVertices.Length, modelVertices, BufferUsageHint.DynamicDraw); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0); GL.EnableVertexArrayAttrib(vao, 0); //! Set up matrix instanced arrays _ModelMatrixArrayVBO = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, _ModelMatrixArrayVBO); GL.BufferData(BufferTarget.ArrayBuffer, flock.Boids.Count * (Vector4.SizeInBytes * 4), IntPtr.Zero, BufferUsageHint.DynamicDraw); GL.EnableVertexArrayAttrib(vao, 1); GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, 4 * Vector4.SizeInBytes, 0); GL.EnableVertexArrayAttrib(vao, 2); GL.VertexAttribPointer(2, 4, VertexAttribPointerType.Float, false, 4 * Vector4.SizeInBytes, Vector4.SizeInBytes); GL.EnableVertexArrayAttrib(vao, 3); GL.VertexAttribPointer(3, 4, VertexAttribPointerType.Float, false, 4 * Vector4.SizeInBytes, Vector4.SizeInBytes * 2); GL.EnableVertexArrayAttrib(vao, 4); GL.VertexAttribPointer(4, 4, VertexAttribPointerType.Float, false, 4 * Vector4.SizeInBytes, Vector4.SizeInBytes * 3); GL.VertexAttribDivisor(1, 1); GL.VertexAttribDivisor(2, 1); GL.VertexAttribDivisor(3, 1); GL.VertexAttribDivisor(4, 1); shader = ShaderHandler.LoadShader("vertex.shader", "fragment.shader"); view_id = GL.GetUniformLocation(shader, "view"); projection_id = GL.GetUniformLocation(shader, "projection"); }
protected override void OnLoad() { // Debug stuff _debugProcCallbackHandle = GCHandle.Alloc(_debugProcCallback); GL.DebugMessageCallback(_debugProcCallback, IntPtr.Zero); GL.Enable(EnableCap.DebugOutput); //GL.Enable(EnableCap.DebugOutputSynchronous); // End debug stuff //GL.ClearColor(0.3f, 0.3f, 0.3f, 1.0f); GL.Enable(EnableCap.DepthTest); // Create spheres var bigspheresize = 1f; Spheres.Add(new Sphere(0.0f, bigspheresize + 1.0f, 0.0f, bigspheresize)); Spheres.Add(new Sphere(0.5f, bigspheresize + 1.0f, -5.0f, bigspheresize)); Spheres.Last().Velocity.X = 1.0f; texturedata = new float[] { //! Positions texture coords -1.0f, -1.0f, 0.0f, //! bottom left corner -1.0f, 1.0f, 0.0f, //! top left corner 1.0f, 1.0f, 0.0f, //! top right corner 1.0f, -1.0f, 0.0f, //! bottom right corner }; // Create texture vao GL.GenVertexArrays(1, out texture_vao); GL.BindVertexArray(texture_vao); // Texture vbo GL.GenBuffers(1, out texture_vbo); GL.BindBuffer(BufferTarget.ArrayBuffer, texture_vbo); GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * texturedata.Length, texturedata, BufferUsageHint.DynamicDraw); // Vertex position attributes GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0); GL.EnableVertexArrayAttrib(texture_vao, 0); // Create texture to draw pixels onto GL.GenTextures(1, out texture); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, texture); GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, new int[] { (int)TextureParameterName.ClampToEdge }); GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, new int[] { (int)TextureParameterName.ClampToEdge }); GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, new int[] { (int)All.Linear }); GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, new int[] { (int)All.Linear }); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba32f, Width, Height, 0, PixelFormat.Rgba, PixelType.Float, IntPtr.Zero); GL.BindImageTexture(0, texture, 0, false, 0, TextureAccess.WriteOnly, SizedInternalFormat.Rgba32f); texture_shader = ShaderHandler.LoadShader("vertex.shader", "fragment.shader"); rays_compute_shader = ShaderHandler.LoadSingleShader("compute.shader", ShaderType.ComputeShader); //! Send texture resolution to fragment shader GL.UseProgram(texture_shader); GL.Uniform1(GL.GetUniformLocation(texture_shader, "width"), (float)Width); GL.Uniform1(GL.GetUniformLocation(texture_shader, "height"), (float)Height); // Initialise Camera camera = new Camera(this); // Initialise compute shader uniforms GL.UseProgram(rays_compute_shader); GL.Uniform1(GL.GetUniformLocation(rays_compute_shader, "_randseed"), (float)random_seed); camera_id = GL.GetUniformLocation(rays_compute_shader, "camera"); rayBottomLeft = GL.GetUniformLocation(rays_compute_shader, "rayBottomLeft"); rayBottomRight = GL.GetUniformLocation(rays_compute_shader, "rayBottomRight"); rayTopLeft = GL.GetUniformLocation(rays_compute_shader, "rayTopLeft"); rayTopRight = GL.GetUniformLocation(rays_compute_shader, "rayTopRight"); sphereUniform = GL.GetUniformLocation(rays_compute_shader, "spheres"); //skybox_id = GL.GetUniformLocation(rays_compute_shader, "skybox"); base.OnLoad(); }