protected override void DoInitialize() { { var computeProgram = new ShaderProgram(); var shaderCode = new ShaderCode(File.ReadAllText(@"05ParticleSimulator\particleSimulator.comp"), ShaderType.ComputeShader); var shader = shaderCode.CreateShader(); computeProgram.Create(shader); shader.Delete(); this.computeProgram = computeProgram; } { OpenGL.GenTextures(1, textureBufferPosition); OpenGL.BindTexture(OpenGL.GL_TEXTURE_BUFFER, textureBufferPosition[0]); OpenGL.GetDelegateFor <OpenGL.glTexBuffer>()(OpenGL.GL_TEXTURE_BUFFER, OpenGL.GL_RGBA32F, this.positionBufferPtrId); OpenGL.GenTextures(1, textureBufferVelocity); OpenGL.BindTexture(OpenGL.GL_TEXTURE_BUFFER, textureBufferVelocity[0]); OpenGL.GetDelegateFor <OpenGL.glTexBuffer>()(OpenGL.GL_TEXTURE_BUFFER, OpenGL.GL_RGBA32F, this.velocityBufferPtrId); } { OpenGL.GetDelegateFor <OpenGL.glGenBuffers>()(1, attractor_buffer); OpenGL.BindBuffer(BufferTarget.UniformBuffer, attractor_buffer[0]); OpenGL.GetDelegateFor <OpenGL.glBufferData>()(OpenGL.GL_UNIFORM_BUFFER, 64 * Marshal.SizeOf(typeof(vec4)), IntPtr.Zero, OpenGL.GL_DYNAMIC_COPY); OpenGL.GetDelegateFor <OpenGL.glBindBufferBase>()(OpenGL.GL_UNIFORM_BUFFER, 0, attractor_buffer[0]); } }
protected override void DoInitialize() { { // Initialize our compute program var computeProgram = new ShaderProgram(); var shaderCode = new ShaderCode(File.ReadAllText(@"04SimpleCompute\compute.comp"), ShaderType.ComputeShader); Shader shader = shaderCode.CreateShader(); computeProgram.Create(shader); shader.Delete(); this.computeProgram = computeProgram; } { // Initialize our resetProgram var resetProgram = new ShaderProgram(); var shaderCode = new ShaderCode(File.ReadAllText(@"04SimpleCompute\computeReset.comp"), ShaderType.ComputeShader); Shader shader = shaderCode.CreateShader(); resetProgram.Create(shader); shader.Delete(); this.resetProgram = resetProgram; } { // This is the texture that the compute program will write into OpenGL.GenTextures(1, output_image); OpenGL.BindTexture(OpenGL.GL_TEXTURE_2D, output_image[0]); OpenGL.TexStorage2D(TexStorage2DTarget.Texture2D, 8, OpenGL.GL_RGBA32F, 256, 256); OpenGL.BindTexture(OpenGL.GL_TEXTURE_2D, 0); } { this.GroupX = 1; this.GroupY = 1; this.GroupZ = 1; } base.DoInitialize(); this.SetUniform("output_image", new samplerValue( BindTextureTarget.Texture2D, this.output_image[0], OpenGL.GL_TEXTURE0)); }
protected override void DoInitialize() { { var computeProgram = new ShaderProgram(); var shaderCode = new ShaderCode(File.ReadAllText( @"06ImageProcessing\ImageProcessing.comp"), ShaderType.ComputeShader); var shader = shaderCode.CreateShader(); computeProgram.Create(shader); shader.Delete(); this.computeProgram = computeProgram; } { Bitmap bitmap = new System.Drawing.Bitmap(this.textureFilename); if (bitmap.Width != 512 || bitmap.Height != 512) { bitmap = (Bitmap)bitmap.GetThumbnailImage(512, 512, null, IntPtr.Zero); } OpenGL.GenTextures(1, this.input_image); OpenGL.BindTexture(OpenGL.GL_TEXTURE_2D, this.input_image[0]); // Lock the image bits (so that we can pass them to OGL). BitmapData bitmapData = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); //GL.ActiveTexture(GL.GL_TEXTURE0); OpenGL.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA32F, bitmap.Width, bitmap.Height, 0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, bitmapData.Scan0); // Unlock the image. bitmap.UnlockBits(bitmapData); /* We require 1 byte alignment when uploading texture data */ //GL.PixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); /* Clamping to edges is important to prevent artifacts when scaling */ OpenGL.TexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, (int)OpenGL.GL_CLAMP_TO_EDGE); OpenGL.TexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, (int)OpenGL.GL_CLAMP_TO_EDGE); /* Linear filtering usually looks best for text */ OpenGL.TexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, (int)OpenGL.GL_LINEAR); OpenGL.TexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, (int)OpenGL.GL_LINEAR); bitmap.Dispose(); } { //GL.ActiveTexture(GL.GL_TEXTURE0); OpenGL.GenTextures(1, this.intermediate_image); OpenGL.BindTexture(OpenGL.GL_TEXTURE_2D, this.intermediate_image[0]); OpenGL.TexStorage2D(TexStorage2DTarget.Texture2D, 8, OpenGL.GL_RGBA32F, 512, 512); } { // This is the texture that the compute program will write into //GL.ActiveTexture(GL.GL_TEXTURE0); OpenGL.GenTextures(1, this.output_image); OpenGL.BindTexture(OpenGL.GL_TEXTURE_2D, this.output_image[0]); OpenGL.TexStorage2D(TexStorage2DTarget.Texture2D, 8, OpenGL.GL_RGBA32F, 512, 512); } { var bufferable = new ImageProcessingModel(); ShaderCode[] simpleShader = new ShaderCode[2]; simpleShader[0] = new ShaderCode(File.ReadAllText(@"06ImageProcessing\ImageProcessing.vert"), ShaderType.VertexShader); simpleShader[1] = new ShaderCode(File.ReadAllText(@"06ImageProcessing\ImageProcessing.frag"), ShaderType.FragmentShader); var propertyNameMap = new PropertyNameMap(); propertyNameMap.Add("vert", "position"); propertyNameMap.Add("uv", "uv"); var pickableRenderer = new PickableRenderer( bufferable, simpleShader, propertyNameMap, "position"); pickableRenderer.Name = string.Format("Pickable: [ImageProcessingRenderer]"); pickableRenderer.Initialize(); pickableRenderer.SetUniform("output_image", new samplerValue( BindTextureTarget.Texture2D, this.output_image[0], OpenGL.GL_TEXTURE0)); this.renderer = pickableRenderer; } }
protected override void DoInitialize() { { var computeProgram = new ShaderProgram(); var shaderCode = new ShaderCode(File.ReadAllText(@"05ParticleSimulator\particleSimulator.comp"), ShaderType.ComputeShader); var shader = shaderCode.CreateShader(); computeProgram.Create(shader); shader.Delete(); this.computeProgram = computeProgram; } { //var bufferable = new ParticleSimulatorCompute(); OpenGL.GetDelegateFor <OpenGL.glGenVertexArrays>()(1, render_vao); OpenGL.GetDelegateFor <OpenGL.glBindVertexArray>()(render_vao[0]); // position OpenGL.GetDelegateFor <OpenGL.glGenBuffers>()(1, position_buffer); OpenGL.BindBuffer(BufferTarget.ArrayBuffer, position_buffer[0]); var positions = new UnmanagedArray <vec4>(ParticleSimulatorCompute.particleCount); unsafe { var array = (vec4 *)positions.Header.ToPointer(); for (int i = 0; i < ParticleSimulatorCompute.particleCount; i++) { array[i] = new vec4( (float)(random.NextDouble() - 0.5) * 20, (float)(random.NextDouble() - 0.5) * 20, (float)(random.NextDouble() - 0.5) * 20, (float)(random.NextDouble()) ); } } OpenGL.BufferData(BufferTarget.ArrayBuffer, positions, BufferUsage.DynamicCopy); positions.Dispose(); OpenGL.GetDelegateFor <OpenGL.glVertexAttribPointer>()(0, 4, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); OpenGL.GetDelegateFor <OpenGL.glEnableVertexAttribArray>()(0); // velocity OpenGL.GetDelegateFor <OpenGL.glGenBuffers>()(1, velocity_buffer); OpenGL.BindBuffer(BufferTarget.ArrayBuffer, velocity_buffer[0]); var velocities = new UnmanagedArray <vec4>(ParticleSimulatorCompute.particleCount); unsafe { var array = (vec4 *)velocities.Header.ToPointer(); for (int i = 0; i < ParticleSimulatorCompute.particleCount; i++) { array[i] = new vec4( (float)(random.NextDouble() - 0.5) * 0.2f, (float)(random.NextDouble() - 0.5) * 0.2f, (float)(random.NextDouble() - 0.5) * 0.2f, 0); } } OpenGL.BufferData(BufferTarget.ArrayBuffer, velocities, BufferUsage.DynamicCopy); velocities.Dispose(); //GL.GetDelegateFor<GL.glVertexAttribPointer>()(0, 4, GL.GL_FLOAT, false, 0, IntPtr.Zero); //GL.GetDelegateFor<GL.glEnableVertexAttribArray>()(0); // OpenGL.GenTextures(1, textureBufferPosition); OpenGL.BindTexture(OpenGL.GL_TEXTURE_BUFFER, textureBufferPosition[0]); OpenGL.GetDelegateFor <OpenGL.glTexBuffer>()(OpenGL.GL_TEXTURE_BUFFER, OpenGL.GL_RGBA32F, position_buffer[0]); OpenGL.GenTextures(1, textureBufferVelocity); OpenGL.BindTexture(OpenGL.GL_TEXTURE_BUFFER, textureBufferVelocity[0]); OpenGL.GetDelegateFor <OpenGL.glTexBuffer>()(OpenGL.GL_TEXTURE_BUFFER, OpenGL.GL_RGBA32F, velocity_buffer[0]); OpenGL.GetDelegateFor <OpenGL.glGenBuffers>()(1, attractor_buffer); OpenGL.BindBuffer(BufferTarget.UniformBuffer, attractor_buffer[0]); OpenGL.GetDelegateFor <OpenGL.glBufferData>()(OpenGL.GL_UNIFORM_BUFFER, 64 * Marshal.SizeOf(typeof(vec4)), IntPtr.Zero, OpenGL.GL_DYNAMIC_COPY); OpenGL.GetDelegateFor <OpenGL.glBindBufferBase>()(OpenGL.GL_UNIFORM_BUFFER, 0, attractor_buffer[0]); } { var visualProgram = new ShaderProgram(); var shaderCodes = new ShaderCode[2]; shaderCodes[0] = new ShaderCode(File.ReadAllText(@"05ParticleSimulator\particleSimulator.vert"), ShaderType.VertexShader); shaderCodes[1] = new ShaderCode(File.ReadAllText(@"05ParticleSimulator\particleSimulator.frag"), ShaderType.FragmentShader); var shaders = (from item in shaderCodes select item.CreateShader()).ToArray(); visualProgram.Create(shaders); foreach (var item in shaders) { item.Delete(); } this.visualProgram = visualProgram; } }