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; } }
private void Form01Renderer_Load(object sender, EventArgs e) { { var camera = new Camera(CameraType.Perspecitive, this.glCanvas1.Width, this.glCanvas1.Height); camera.Position = new vec3(0, 0, 5); var rotator = new SatelliteRotator(camera); this.camera = camera; this.rotator = rotator; } { // build several models Random random = new Random(); var bufferables = new IBufferable[] { new Axis(), new BigDipper(), new Chain(new ChainModel(random.Next(7, 100), 5, 5)), new Tetrahedron(), new Cube(), new Sphere(), new Teapot(), }; var keys = new GeometryModel[] { GeometryModel.Axis, GeometryModel.BigDipper, GeometryModel.Chain, GeometryModel.Tetrahedron, GeometryModel.Cube, GeometryModel.Sphere, GeometryModel.Teapot, }; ShaderCode[] simpleShader = new ShaderCode[2]; simpleShader[0] = new ShaderCode(File.ReadAllText(@"01Renderer\Simple.vert"), ShaderType.VertexShader); simpleShader[1] = new ShaderCode(File.ReadAllText(@"01Renderer\Simple.frag"), ShaderType.FragmentShader); ShaderCode[] emitNormalLineShader = new ShaderCode[3]; emitNormalLineShader[0] = new ShaderCode(File.ReadAllText(@"01Renderer\EmitNormalLine.vert"), ShaderType.VertexShader); emitNormalLineShader[1] = new ShaderCode(File.ReadAllText(@"01Renderer\EmitNormalLine.geom"), ShaderType.GeometryShader); emitNormalLineShader[2] = new ShaderCode(File.ReadAllText(@"01Renderer\EmitNormalLine.frag"), ShaderType.FragmentShader); var shaderCodesGroup = new ShaderCode[][] { simpleShader, simpleShader, simpleShader, emitNormalLineShader, emitNormalLineShader, emitNormalLineShader, emitNormalLineShader, }; var simpleShaderPropertyNameMap = new PropertyNameMap(); simpleShaderPropertyNameMap.Add("in_Position", "position"); simpleShaderPropertyNameMap.Add("in_Color", "color"); var emitNormalLineShaderPropertyNameMap = new PropertyNameMap(); emitNormalLineShaderPropertyNameMap.Add("in_Position", "position"); emitNormalLineShaderPropertyNameMap.Add("in_Normal", "normal"); var propertyNameMaps = new PropertyNameMap[] { simpleShaderPropertyNameMap, simpleShaderPropertyNameMap, simpleShaderPropertyNameMap, emitNormalLineShaderPropertyNameMap, emitNormalLineShaderPropertyNameMap, emitNormalLineShaderPropertyNameMap, emitNormalLineShaderPropertyNameMap, }; var positionNameInIBufferables = new string[] { "position", "position", "position", "position", "position", "position", "position", }; var uniformTupleList = new List <Tuple <string, ValueType> >() { new Tuple <string, ValueType>("normalLength", 0.5f), new Tuple <string, ValueType>("showModel", true), new Tuple <string, ValueType>("showNormal", false), }; var uniformVariablesList = new List <List <Tuple <string, ValueType> > >() { new List <Tuple <string, ValueType> >(), new List <Tuple <string, ValueType> >(), new List <Tuple <string, ValueType> >(), uniformTupleList, uniformTupleList, uniformTupleList, uniformTupleList, }; for (int i = 0; i < bufferables.Length; i++) { GeometryModel key = keys[i]; IBufferable bufferable = bufferables[i]; ShaderCode[] shaders = shaderCodesGroup[i]; var propertyNameMap = propertyNameMaps[i]; string positionNameInIBufferable = positionNameInIBufferables[i]; var highlightRenderer = new HighlightRenderer( bufferable, positionNameInIBufferable); highlightRenderer.Name = string.Format("Highlight: [{0}]", key); highlightRenderer.Initialize(); var pickableRenderer = new PickableRenderer( bufferable, shaders, propertyNameMap, positionNameInIBufferable); pickableRenderer.Name = string.Format("Pickable: [{0}]", key); pickableRenderer.Initialize(); var uniformVariables = uniformVariablesList[i]; foreach (var item in uniformVariables) { pickableRenderer.SetUniform(item.Item1, item.Item2); } HighlightedPickableRenderer renderer = new HighlightedPickableRenderer( highlightRenderer, pickableRenderer); renderer.Initialize(); { GLSwitch lineWidthSwitch = new LineWidthSwitch(5); pickableRenderer.SwitchList.Add(lineWidthSwitch); GLSwitch pointSizeSwitch = new PointSizeSwitch(10); pickableRenderer.SwitchList.Add(pointSizeSwitch); GLSwitch polygonModeSwitch = new PolygonModeSwitch(PolygonModes.Filled); pickableRenderer.SwitchList.Add(polygonModeSwitch); //GLSwitch blendSwitch = new BlendSwitch(); //pickableRenderer.SwitchList.Add(blendSwitch); } this.rendererDict.Add(key, renderer); } this.SelectedModel = GeometryModel.Tetrahedron; } { // build the axis var bufferables = new IBufferable[] { new Axis(), }; var keys = new GeometryModel[] { GeometryModel.Axis, }; ShaderCode[] simpleShader = new ShaderCode[2]; simpleShader[0] = new ShaderCode(File.ReadAllText(@"01Renderer\Simple.vert"), ShaderType.VertexShader); simpleShader[1] = new ShaderCode(File.ReadAllText(@"01Renderer\Simple.frag"), ShaderType.FragmentShader); var shaderCodesGroup = new ShaderCode[][] { simpleShader, }; var simpleShaderPropertyNameMap = new PropertyNameMap(); simpleShaderPropertyNameMap.Add("in_Position", "position"); simpleShaderPropertyNameMap.Add("in_Color", "color"); var propertyNameMaps = new PropertyNameMap[] { simpleShaderPropertyNameMap, }; var positionNameInIBufferables = new string[] { "position", }; var uniformVariablesList = new List <List <Tuple <string, ValueType> > >() { new List <Tuple <string, ValueType> >(), }; for (int i = 0; i < bufferables.Length; i++) { GeometryModel key = keys[i]; IBufferable bufferable = bufferables[i]; ShaderCode[] shaders = shaderCodesGroup[i]; var propertyNameMap = propertyNameMaps[i]; string positionNameInIBufferable = positionNameInIBufferables[i]; var pickableRenderer = new PickableRenderer( bufferable, shaders, propertyNameMap, positionNameInIBufferable); pickableRenderer.Name = string.Format("Pickable: [{0}]", key); pickableRenderer.Initialize(); var uniformVariables = uniformVariablesList[i]; foreach (var item in uniformVariables) { pickableRenderer.SetUniform(item.Item1, item.Item2); } } } { var frmBulletinBoard = new FormBulletinBoard(); //frmBulletinBoard.Dump = true; frmBulletinBoard.Show(); this.pickedGeometryBoard = frmBulletinBoard; } { var UIRoot = new GLControl(this.glCanvas1.Size, -100, 100); UIRoot.Initialize(); this.uiRoot = UIRoot; var glAxis = new GLAxis(AnchorStyles.Right | AnchorStyles.Bottom, new Padding(3, 3, 3, 3), new Size(70, 70), -100, 100); glAxis.Initialize(); this.glAxis = glAxis; UIRoot.Controls.Add(glAxis); } { var frmPropertyGrid = new FormProperyGrid(); frmPropertyGrid.DisplayObject(this.rendererDict[this.SelectedModel].PickableRenderer); frmPropertyGrid.Show(); this.pickableRendererPropertyGrid = frmPropertyGrid; } { var frmPropertyGrid = new FormProperyGrid(); frmPropertyGrid.DisplayObject(this.rendererDict[this.SelectedModel].Highlighter); frmPropertyGrid.Show(); this.highlightRendererPropertyGrid = frmPropertyGrid; } { var frmPropertyGrid = new FormProperyGrid(); frmPropertyGrid.DisplayObject(this); frmPropertyGrid.Show(); this.formPropertyGrid = frmPropertyGrid; } { var frmIndexBufferPtrBoard = new FormIndexBufferPtrBoard(); frmIndexBufferPtrBoard.SetTarget(this.rendererDict[this.SelectedModel].PickableRenderer.IndexBufferPtr); frmIndexBufferPtrBoard.Show(); this.frmIndexBufferPtrBoard = frmIndexBufferPtrBoard; } }
private void Form_Load(object sender, EventArgs e) { { var camera = new Camera( new vec3(0, 0, 5), new vec3(), new vec3(0, 1, 0), CameraType.Perspecitive, this.glCanvas1.Width, this.glCanvas1.Height); var rotator = new SatelliteManipulater(); rotator.Bind(camera, this.glCanvas1); this.camera = camera; } { // build several models Random random = new Random(); var bufferables = new IBufferable[] { new Axis(), new BigDipper(), new Chain(new ChainModel(random.Next(7, 100), 5, 5)), new Tetrahedron(), new Cube(), new Sphere(), new Teapot(), }; var keys = new GeometryModel[] { GeometryModel.Axis, GeometryModel.BigDipper, GeometryModel.Chain, GeometryModel.Tetrahedron, GeometryModel.Cube, GeometryModel.Sphere, GeometryModel.Teapot, }; ShaderCode[] simpleShader = new ShaderCode[2]; simpleShader[0] = new ShaderCode(File.ReadAllText(@"shaders\Simple.vert"), ShaderType.VertexShader); simpleShader[1] = new ShaderCode(File.ReadAllText(@"shaders\Simple.frag"), ShaderType.FragmentShader); ShaderCode[] emitNormalLineShader = new ShaderCode[3]; emitNormalLineShader[0] = new ShaderCode(File.ReadAllText(@"shaders\EmitNormalLine.vert"), ShaderType.VertexShader); emitNormalLineShader[1] = new ShaderCode(File.ReadAllText(@"shaders\EmitNormalLine.geom"), ShaderType.GeometryShader); emitNormalLineShader[2] = new ShaderCode(File.ReadAllText(@"shaders\EmitNormalLine.frag"), ShaderType.FragmentShader); var shaderCodesGroup = new ShaderCode[][] { simpleShader, simpleShader, simpleShader, emitNormalLineShader, emitNormalLineShader, emitNormalLineShader, emitNormalLineShader, }; var simpleShaderPropertyNameMap = new PropertyNameMap(); simpleShaderPropertyNameMap.Add("in_Position", "position"); simpleShaderPropertyNameMap.Add("in_Color", "color"); var emitNormalLineShaderPropertyNameMap = new PropertyNameMap(); emitNormalLineShaderPropertyNameMap.Add("in_Position", "position"); emitNormalLineShaderPropertyNameMap.Add("in_Normal", "normal"); var propertyNameMaps = new PropertyNameMap[] { simpleShaderPropertyNameMap, simpleShaderPropertyNameMap, simpleShaderPropertyNameMap, emitNormalLineShaderPropertyNameMap, emitNormalLineShaderPropertyNameMap, emitNormalLineShaderPropertyNameMap, emitNormalLineShaderPropertyNameMap, }; var positionNameInIBufferables = new string[] { "position", "position", "position", "position", "position", "position", "position", }; for (int i = 0; i < bufferables.Length; i++) { GeometryModel key = keys[i]; IBufferable bufferable = bufferables[i]; ShaderCode[] shaders = shaderCodesGroup[i]; var propertyNameMap = propertyNameMaps[i]; string positionNameInIBufferable = positionNameInIBufferables[i]; var highlightRenderer = new HighlightRenderer( bufferable, positionNameInIBufferable); highlightRenderer.Name = string.Format("Highlight: [{0}]", key); highlightRenderer.Initialize(); var pickableRenderer = new PickableRenderer( bufferable, shaders, propertyNameMap, positionNameInIBufferable); pickableRenderer.Name = string.Format("Pickable: [{0}]", key); pickableRenderer.Initialize(); if (i > 2) { pickableRenderer.SetUniform("normalLength", 0.5f); pickableRenderer.SetUniform("showModel", true); pickableRenderer.SetUniform("showNormal", false); } HighlightedPickableRenderer renderer = new HighlightedPickableRenderer( highlightRenderer, pickableRenderer); renderer.Initialize(); { GLSwitch lineWidthSwitch = new LineWidthSwitch(5); pickableRenderer.SwitchList.Add(lineWidthSwitch); GLSwitch pointSizeSwitch = new PointSizeSwitch(10); pickableRenderer.SwitchList.Add(pointSizeSwitch); GLSwitch polygonModeSwitch = new PolygonModeSwitch(PolygonModes.Filled); pickableRenderer.SwitchList.Add(polygonModeSwitch); //GLSwitch blendSwitch = new BlendSwitch(); //pickableRenderer.SwitchList.Add(blendSwitch); } this.rendererDict.Add(key, renderer); } this.SelectedModel = GeometryModel.Tetrahedron; } { var frmBulletinBoard = new FormBulletinBoard(); //frmBulletinBoard.Dump = true; frmBulletinBoard.Show(); this.pickedGeometryBoard = frmBulletinBoard; } { var UIRoot = new UIRoot(); UIRoot.Initialize(); this.uiRoot = UIRoot; var uiAxis = new UIAxis(AnchorStyles.Left | AnchorStyles.Bottom, new Padding(3, 3, 3, 3), new Size(128, 128), -100, 100); uiAxis.Initialize(); UIRoot.Children.Add(uiAxis); this.uiAxis = uiAxis; var font = new Font("Courier New", 32); var uiText = new UIText(AnchorStyles.Left | AnchorStyles.Bottom, new Padding(0, 0, 0, 0), new Size(250, 20), -100, 100, font.GetFontBitmap("[index: 0123456789]").GetFontTexture()); uiText.Text = ""; uiRoot.Children.Add(uiText); this.uiText = uiText; } { var frmPropertyGrid = new FormProperyGrid(this.rendererDict[this.SelectedModel].PickableRenderer); frmPropertyGrid.Show(); this.pickableRendererPropertyGrid = frmPropertyGrid; } { var frmPropertyGrid = new FormProperyGrid(this.rendererDict[this.SelectedModel].Highlighter); frmPropertyGrid.Show(); this.highlightRendererPropertyGrid = frmPropertyGrid; } { var frmPropertyGrid = new FormProperyGrid(this); frmPropertyGrid.Show(); this.formPropertyGrid = frmPropertyGrid; } { var frmPropertyGrid = new FormProperyGrid(this.uiText); frmPropertyGrid.Show(); } { var frmIndexBufferPtrBoard = new FormIndexBufferPtrBoard(); frmIndexBufferPtrBoard.SetTarget(this.rendererDict[this.SelectedModel].PickableRenderer.IndexBufferPtr); frmIndexBufferPtrBoard.Show(); this.frmIndexBufferPtrBoard = frmIndexBufferPtrBoard; } }