public void Render(DrawingContext drawingContext) { if (_framebuffer == null) { return; } var curFrameStamp = _stopwatch.Elapsed; var deltaT = curFrameStamp - _lastFrameStamp; _lastFrameStamp = curFrameStamp; PreRender(); GLRender?.Invoke(deltaT); GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); GL.Flush(); GLAsyncRender?.Invoke(); PostRender(); // Transforms are applied in reverse order drawingContext.PushTransform(_framebuffer.TranslateTransform); // Apply translation to the image on the Y axis by the height. This assures that in the next step, where we apply a negative scale the image is still inside of the window drawingContext.PushTransform(_framebuffer.FlipYTransform); // Apply a scale where the Y axis is -1. This will rotate the image by 180 deg // dpi scaled rectangle from the image var rect = new Rect(0, 0, _framebuffer.D3dImage.Width, _framebuffer.D3dImage.Height); drawingContext.DrawImage(_framebuffer.D3dImage, rect); // Draw the image source drawingContext.Pop(); // Remove the scale transform drawingContext.Pop(); // Remove the translation transform }
private TimeSpan Render() { mRenderwatch.Restart(); mUpdateStrategy.PreRender(); GLRender?.Invoke(this, EventArgs.Empty); mUpdateStrategy?.PostRender(); Dispatcher.Invoke(() => mUpdateStrategy.Compute()); return(mRenderwatch.Elapsed); }
public override void OnRenderObject() { foreach (Polyline pl in queue) { if (pl.vertices.Length > 1) { GLRender.Polyline(pl.vertices, false, null, Color.black); } } //动态线 if (dynamic != null && drawing) { SGGeometry.GLRender.Polyline(dynamic.vertices, false, null, Color.yellow); } }
void DrawTexture(Rect rect, Texture texture) { RenderTexture.active = m_renderTexture; GLRender.Clear(true, true, Color.black); m_mat.SetFloat("_Frequency", 1); m_mat.SetFloat("_Factor", 0.0f); Graphics.DrawTexture(new Rect(0, 0, m_renderTexture.width, m_renderTexture.height), Texture2D.whiteTexture, m_mat); m_mat.SetFloat("_Frequency", 4); m_mat.SetFloat("_Factor", 0.3f); Graphics.DrawTexture(new Rect(0, 0, m_renderTexture.width, m_renderTexture.height), Texture2D.whiteTexture, m_mat); //m_readback.ReadPixels(new Rect(0, 0, 32, 32), 0, 0); RenderTexture.active = null; //Color32[] c = m_readback.GetPixels32(); //Color32 c0 = c[0]; //Color32 c1 = c[1]; //Color32 c2 = c[2]; Graphics.DrawTexture(rect, m_renderTexture, new Rect(rect.x / Screen.width, rect.y / Screen.height, (rect.x + rect.width) / Screen.width, (rect.y + rect.height) / Screen.height), 0, 0, 0, 0); }
public void Render(DrawingContext drawingContext) { if (_framebuffer == null) { return; } var curFrameStamp = _stopwatch.Elapsed; var deltaT = curFrameStamp - _lastFrameStamp; _lastFrameStamp = curFrameStamp; // Set up framebuffer GL.BindFramebuffer(FramebufferTarget.Framebuffer, _framebuffer.GLFramebufferHandle); GL.Viewport(0, 0, _framebuffer.FramebufferWidth, _framebuffer.FramebufferHeight); GLRender?.Invoke(deltaT); GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); GL.Flush(); GLAsyncRender?.Invoke(); // Copy dirty area to front buffer _framebuffer.D3dImage.Lock(); _framebuffer.D3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, _framebuffer.DxRenderTargetHandle); _framebuffer.D3dImage.AddDirtyRect(new Int32Rect(0, 0, _framebuffer.FramebufferWidth, _framebuffer.FramebufferHeight)); _framebuffer.D3dImage.Unlock(); // Transforms are applied in reverse order drawingContext.PushTransform(_framebuffer.TranslateTransform); // Apply translation to the image on the Y axis by the height. This assures that in the next step, where we apply a negative scale the image is still inside of the window drawingContext.PushTransform(_framebuffer.FlipYTransform); // Apply a scale where the Y axis is -1. This will rotate the image by 180 deg // dpi scaled rectangle from the image var rect = new Rect(0, 0, _framebuffer.D3dImage.Width, _framebuffer.D3dImage.Height); Wgl.DXUnlockObjectsNV(_context.GlDeviceHandle, 1, new[] { _framebuffer.DxInteropRegisteredHandle }); drawingContext.DrawImage(_framebuffer.D3dImage, rect); // Draw the image source Wgl.DXLockObjectsNV(_context.GlDeviceHandle, 1, new[] { _framebuffer.DxInteropRegisteredHandle }); // Enable GL access to the framebuffer drawingContext.Pop(); // Remove the scale transform drawingContext.Pop(); // Remove the translation transform }
public override void OnApplyTemplate() { var window = Window.GetWindow(this); _windowHandle = window is null ? IntPtr.Zero : new WindowInteropHelper(window).Handle; _hwnd = new HwndSource(0, 0, 0, 0, 0, "Offscreen Window", _windowHandle); _windowInfo = Utilities.CreateWindowsWindowInfo(_hwnd.Handle); _wpfImage = new Image() { RenderTransformOrigin = new Point(0.5, 0.5), RenderTransform = new ScaleTransform(1, -1) }; var grid = new Grid(); _framesTextBlock = new TextBlock() { HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Blue), FontWeight = FontWeights.Bold }; Panel.SetZIndex(_framesTextBlock, 1); grid.Children.Add(_framesTextBlock); grid.Children.Add(_wpfImage); AddChild(grid); switch (UpdateStrategy) { case UpdateStrategy.WriteableBitmapImage: _updateStrategy = new UpdateStrategyWriteableBitmap(); break; case UpdateStrategy.D3DSurface: _updateStrategy = new UpdateStrategyD3D(); break; } if (UseSeperateRenderThread) { _cts = new CancellationTokenSource(); _renderThread = new Thread((object boxedToken) => { InitOpenGLContext(); while (!_cts.IsCancellationRequested) { UpdateFramerate(); if (_wasResized) { _wasResized = false; _updateStrategy.Resize((int)Math.Round(ActualWidth), (int)Math.Round(ActualHeight)); Dispatcher.Invoke(() => _wpfImage.Source = _updateStrategy.CreateImageSource()); } GLRender?.Invoke(this, EventArgs.Empty); _updateStrategy?.Draw(); Dispatcher.Invoke(() => _updateStrategy.InvalidateImageSource()); } _renderThread.Join(); }) { IsBackground = true, Priority = ThreadPriority.Highest }; _renderThread.Start(_cts); _stopwatch.Start(); } else { _dt = new DispatcherTimer(DispatcherPriority.Send) { Interval = TimeSpan.FromMilliseconds(1) }; InitOpenGLContext(); _dt.Tick += (o, e) => { UpdateFramerate(); if (_wasResized) { _wasResized = false; _updateStrategy.Resize((int)Math.Round(ActualWidth), (int)Math.Round(ActualHeight)); _wpfImage.Source = _updateStrategy.CreateImageSource(); } GLRender?.Invoke(this, EventArgs.Empty); _updateStrategy.Draw(); _updateStrategy.InvalidateImageSource(); }; _dt.Start(); _stopwatch.Start(); } base.OnApplyTemplate(); }
private void Form1_Load(object sender, EventArgs e) { stopWatch.Start(); // Maximize Window this.WindowState = FormWindowState.Maximized; this.MinimumSize = this.Size; this.MaximumSize = this.Size; // Set Title textBox2.Font = new Font(textBox1.Font.FontFamily, 22); textBox2.TextAlign = HorizontalAlignment.Center; textBox2.Text = title; // Set guide tube label label1.Font = new Font(textBox1.Font.FontFamily, 10); // Set outer tube label label2.Font = new Font(textBox1.Font.FontFamily, 10); // Set inner tube label label3.Font = new Font(textBox1.Font.FontFamily, 10); CLGLWindow = new GLRender(this, true, -1); CLGLWindow.DrawAxes = false; //Integer values of width and height, defined statically //h = 400; //700; //w = 500; //750; //reduces values of width and height proportionally (division by 8) to fit them onto the screen // Guide Tube Arrow float[] vgta = new float[] { 0,(float)-27.5,-(float)2, 0,(float)-27.5, (float)2, 0,(float) 27.5, (float)2, 0,(float) 27.5,-(float)2 }; //Sets texture coordinates // Values set below are for plain appearance float[] texCoord = new float[] { 0,1, 0,0, 1,0, 1,1, }; // normal data indicates the orientation of the surface - here, we have used 2d so normal is set to the unused z-axis float[] n = new float[] { 0,0,1, 0,0,1, 0,0,1, 0,0,1 }; // Set colour float[] cgta = new float[] { 220/255.0f, 20/255.0f, 60/255.0f, 1.0f, 220/255.0f, 20/255.0f, 60/255.0f, 1.0f, 220/255.0f, 20/255.0f, 60/255.0f, 1.0f, 220/255.0f, 20/255.0f, 60/255.0f, 1.0f }; // uses points 0,1,2,3 as vertexes i.e it's Sequential only int[] elem = new int[] { 0, 1, 2, 3 }; GTA = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); GTA.SetVertexData(vgta); GTA.SetTexCoordData(texCoord); GTA.SetNormalData(n); GTA.SetElemData(elem); GTA.SetColorData(cgta); GTA.vetTransl = new Vector(0.0, 100.0, 0.0); // Guide Tube float[] vg1 = new float[] { 0,(float)-7.5,-(float)60, 0,(float)-7.5, (float)60, 0,(float) 7.5, (float)60, 0,(float) 7.5,-(float)60 }; //Sets texture coordinates // Values set below are for plain appearance texCoord = new float[] { 0,1, 0,0, 1,0, 1,1, }; // normal data indicates the orientation of the surface - here, we have used 2d so normal is set to the unused z-axis n = new float[] { 0,0,1, 0,0,1, 0,0,1, 0,0,1 }; // Set colour float[] cg = new float[] { 176/255.0f, 226/255.0f, 255/255.0f, 1.0f, 176/255.0f, 226/255.0f, 255/255.0f, 1.0f, 176/255.0f, 226/255.0f, 255/255.0f, 1.0f, 176/255.0f, 226/255.0f, 255/255.0f, 1.0f }; // uses points 0,1,2,3 as vertexes i.e it's Sequential only elem = new int[] { 0, 1, 2, 3 }; GT1 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); GT1.SetVertexData(vg1); GT1.SetTexCoordData(texCoord); GT1.SetNormalData(n); GT1.SetElemData(elem); GT1.SetColorData(cg); GT1.vetTransl = new Vector(0.0, 0.0, 80.0); // GT1.vetRot = new Vector(20.0, -10.0, -6.0); float [] vg2 = new float[] { 0,(float)-16.25,-(float)10, 0,(float)-16.25, (float)10, 0,(float) 16.25, (float)10, 0,(float) 16.25,-(float)10 }; GT2 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); GT2.SetVertexData(vg2); GT2.SetTexCoordData(texCoord); GT2.SetNormalData(n); GT2.SetElemData(elem); GT2.SetColorData(cg); GT2.vetTransl = new Vector(0.0, 23.0, 30.0); // Outer Container // OC1 float[] voc1 = new float[] { 0,(float)-45,-(float)50, 0,(float)-45, (float)50, 0,(float) 45, (float)50, 0,(float) 45,-(float)50 }; float[] coc1 = new float[] { 255/255.0f, 255/255.0f, 255/255.0f, 1.0f, 255/255.0f, 255/255.0f, 255/255.0f, 1.0f, 255/255.0f, 255/255.0f, 255/255.0f, 1.0f, 255/255.0f, 255/255.0f, 255/255.0f, 1.0f }; OC1 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OC1.SetVertexData(voc1); OC1.SetTexCoordData(texCoord); OC1.SetNormalData(n); OC1.SetElemData(elem); OC1.SetColorData(coc1); OC1.vetTransl = new Vector(0.0, 5.0, -12.5); CLGLWindow.Models.Add(OC1); // OC2 - rotated float[] voc2 = new float[] { 0,(float)-10,-(float)40, 0,(float)-10, (float)40, 0,(float) 10, (float)40, 0,(float) 10,-(float)40 }; OC2 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OC2.SetVertexData(voc2); OC2.SetTexCoordData(texCoord); OC2.SetNormalData(n); OC2.SetElemData(elem); OC2.SetColorData(coc1); // OC2.vetRot = new Vector(-10.0, 0.0, 0.0); OC2.vetRot = new Vector(0.0, 10.5, 0.0); OC2.vetTransl = new Vector(0.0, 64.0, 0.0); CLGLWindow.Models.Add(OC2); // OC3 float[] voc3 = new float[] { 0,(float)-45,-(float)3, 0,(float)-45, (float)3, 0,(float) 45, (float)3, 0,(float) 45,-(float)3 }; float[] coc2 = new float[] { 205/255.0f, 105/255.0f, 201/255.0f, 1.0f, 205/255.0f, 105/255.0f, 201/255.0f, 1.0f, 205/255.0f, 105/255.0f, 201/255.0f, 1.0f, 205/255.0f, 105/255.0f, 201/255.0f, 1.0f }; OC3 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OC3.SetVertexData(voc3); OC3.SetTexCoordData(texCoord); OC3.SetNormalData(n); OC3.SetElemData(elem); OC3.SetColorData(coc2); OC3.vetTransl = new Vector(0.0, 5.0, 40.5); CLGLWindow.Models.Add(OC3); // OC4 float[] voc4 = new float[] { 0,(float)-12,-(float)3, 0,(float)-12, (float)3, 0,(float) 12, (float)3, 0,(float) 12,-(float)3 }; OC4 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OC4.SetVertexData(voc4); OC4.SetTexCoordData(texCoord); OC4.SetNormalData(n); OC4.SetElemData(elem); OC4.SetColorData(coc2); OC4.vetTransl = new Vector(0.0, 76.7, 25.5); CLGLWindow.Models.Add(OC4); CLGLWindow.Models.Add(GT1); CLGLWindow.Models.Add(GT2); // Outer Shield // OS1 float[] vos1 = new float[] { 0,(float)-2.0,-(float)8, 0,(float)-2.0, (float)8, 0,(float) 2.0, (float)8, 0,(float) 2.0,-(float)8 }; float[] cos = new float[] { 0/255.0f, 255/255.0f, 0/255.0f, 1.0f, 0/255.0f, 255/255.0f, 0/255.0f, 1.0f, 0/255.0f, 255/255.0f, 0/255.0f, 1.0f, 0/255.0f, 255/255.0f, 0/255.0f, 1.0f }; OS1 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OS1.SetVertexData(vos1); OS1.SetTexCoordData(texCoord); OS1.SetNormalData(n); OS1.SetElemData(elem); OS1.SetColorData(cos); OS1.vetTransl = new Vector(0.0, 9.6, 51.6); CLGLWindow.Models.Add(OS1); // OS2 OS2 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OS2.SetVertexData(vos1); OS2.SetTexCoordData(texCoord); OS2.SetNormalData(n); OS2.SetElemData(elem); OS2.SetColorData(cos); OS2.vetTransl = new Vector(0.0, -9.6, 51.6); CLGLWindow.Models.Add(OS2); // OS3 float[] vos2 = new float[] { 0,(float)-8.0,-(float)1.5, 0,(float)-8.0, (float)1.5, 0,(float) 8.0, (float)1.5, 0,(float) 8.0,-(float)1.5 }; OS3 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OS3.SetVertexData(vos2); OS3.SetTexCoordData(texCoord); OS3.SetNormalData(n); OS3.SetElemData(elem); OS3.SetColorData(cos); OS3.vetTransl = new Vector(0.0, -15.6, 45.0); CLGLWindow.Models.Add(OS3); // OS4 OS4 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OS4.SetVertexData(vos2); OS4.SetTexCoordData(texCoord); OS4.SetNormalData(n); OS4.SetElemData(elem); OS4.SetColorData(cos); OS4.vetTransl = new Vector(0.0, 15.6, 45.0); CLGLWindow.Models.Add(OS4); // OS5 float[] vos3 = new float[] { 0,(float)-2.5,-(float)1.5, 0,(float)-2.5, (float)1.5, 0,(float) 2.5, (float)1.5, 0,(float) 2.5,-(float)1.5 }; OS5 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OS5.SetVertexData(vos3); OS5.SetTexCoordData(texCoord); OS5.SetNormalData(n); OS5.SetElemData(elem); OS5.SetColorData(cos); OS5.vetTransl = new Vector(0.0, -14.15, 48.1); CLGLWindow.Models.Add(OS5); // OS6 OS6 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OS6.SetVertexData(vos3); OS6.SetTexCoordData(texCoord); OS6.SetNormalData(n); OS6.SetElemData(elem); OS6.SetColorData(cos); OS6.vetTransl = new Vector(0.0, 14.15, 48.1); CLGLWindow.Models.Add(OS6); // OS7 float[] vos4 = new float[] { 0,(float)-1.5,-(float)1.0, 0,(float)-1.5, (float)1.0, 0,(float) 1.5, (float)1.0, 0,(float) 1.5,-(float)1.0 }; OS7 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OS7.SetVertexData(vos4); OS7.SetTexCoordData(texCoord); OS7.SetNormalData(n); OS7.SetElemData(elem); OS7.SetColorData(cos); OS7.vetTransl = new Vector(0.0, -13.15, 58.5); CLGLWindow.Models.Add(OS7); // OS8 OS8 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OS8.SetVertexData(vos4); OS8.SetTexCoordData(texCoord); OS8.SetNormalData(n); OS8.SetElemData(elem); OS8.SetColorData(cos); OS8.vetTransl = new Vector(0.0, 13.15, 58.5); CLGLWindow.Models.Add(OS8); //FSAs // FSA0 float[] vfsa = new float[] { 0,(float)-3,-(float)18, 0,(float)-3, (float)18, 0,(float) 3, (float)18, 0,(float) 3,-(float)18 }; float[] cfsa = new float[] { 255/255.0f, 0/255.0f, 0/255.0f, 1.0f, 255/255.0f, 0/255.0f, 0/255.0f, 1.0f, 255/255.0f, 0/255.0f, 0/255.0f, 1.0f, 255/255.0f, 0/255.0f, 0/255.0f, 1.0f }; FSA0 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSA0.SetVertexData(vfsa); FSA0.SetTexCoordData(texCoord); FSA0.SetNormalData(n); FSA0.SetElemData(elem); FSA0.SetColorData(cfsa); FSA0.vetTransl = new Vector(0.0, 29.0, -40.0); CLGLWindow.Models.Add(FSA0); // FSA1 FSA1 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSA1.SetVertexData(vfsa); FSA1.SetTexCoordData(texCoord); FSA1.SetNormalData(n); FSA1.SetElemData(elem); FSA1.SetColorData(cfsa); FSA1.vetTransl = new Vector(0.0, 22.5, -40.0); CLGLWindow.Models.Add(FSA1); // FSA2 FSA2 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSA2.SetVertexData(vfsa); FSA2.SetTexCoordData(texCoord); FSA2.SetNormalData(n); FSA2.SetElemData(elem); FSA2.SetColorData(cfsa); FSA2.vetTransl = new Vector(0.0, 16.0, -40.0); CLGLWindow.Models.Add(FSA2); // FSA3 FSA3 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSA3.SetVertexData(vfsa); FSA3.SetTexCoordData(texCoord); FSA3.SetNormalData(n); FSA3.SetElemData(elem); FSA3.SetColorData(cfsa); FSA3.vetTransl = new Vector(0.0, 9.5, -40.0); CLGLWindow.Models.Add(FSA3); // FSA4 FSA4 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSA4.SetVertexData(vfsa); FSA4.SetTexCoordData(texCoord); FSA4.SetNormalData(n); FSA4.SetElemData(elem); FSA4.SetColorData(cfsa); FSA4.vetTransl = new Vector(0.0, 3.0, -40.0); CLGLWindow.Models.Add(FSA4); // FSA5 FSA5 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSA5.SetVertexData(vfsa); FSA5.SetTexCoordData(texCoord); FSA5.SetNormalData(n); FSA5.SetElemData(elem); FSA5.SetColorData(cfsa); FSA5.vetTransl = new Vector(0.0, -3.5, -40.0); CLGLWindow.Models.Add(FSA5); // FSA6 FSA6 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSA6.SetVertexData(vfsa); FSA6.SetTexCoordData(texCoord); FSA6.SetNormalData(n); FSA6.SetElemData(elem); FSA6.SetColorData(cfsa); FSA6.vetTransl = new Vector(0.0, -10.0, -40.0); CLGLWindow.Models.Add(FSA6); // FSA7 FSA7 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSA7.SetVertexData(vfsa); FSA7.SetTexCoordData(texCoord); FSA7.SetNormalData(n); FSA7.SetElemData(elem); FSA7.SetColorData(cfsa); FSA7.vetTransl = new Vector(0.0, -16.5, -40.0); CLGLWindow.Models.Add(FSA7); // FSA8 FSA8 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSA8.SetVertexData(vfsa); FSA8.SetTexCoordData(texCoord); FSA8.SetNormalData(n); FSA8.SetElemData(elem); FSA8.SetColorData(cfsa); FSA8.vetTransl = new Vector(0.0, -54.5, -37.0); CLGLWindow.Models.Add(FSA8); // FSA CAPS // FSACAP0 float[] vfsacap = new float[] { 0,(float)-3,-(float)3, 0,(float)-3, (float)3, 0,(float) 3, (float)3, 0,(float) 3,-(float)3 }; float[] cfsacap = new float[] { 205/255.0f, 105/255.0f, 201/255.0f, 1.0f, 205/255.0f, 105/255.0f, 201/255.0f, 1.0f, 205/255.0f, 105/255.0f, 201/255.0f, 1.0f, 205/255.0f, 105/255.0f, 201/255.0f, 1.0f }; FSACAP0 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSACAP0.SetVertexData(vfsacap); FSACAP0.SetTexCoordData(texCoord); FSACAP0.SetNormalData(n); FSACAP0.SetElemData(elem); FSACAP0.SetColorData(cfsacap); FSACAP0.vetTransl = new Vector(0.0, 29.0, -19.0); CLGLWindow.Models.Add(FSACAP0); // FSACAP1 FSACAP1 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSACAP1.SetVertexData(vfsacap); FSACAP1.SetTexCoordData(texCoord); FSACAP1.SetNormalData(n); FSACAP1.SetElemData(elem); FSACAP1.SetColorData(cfsacap); FSACAP1.vetTransl = new Vector(0.0, 22.5, -19.0); CLGLWindow.Models.Add(FSACAP1); // FSACAP2 FSACAP2 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSACAP2.SetVertexData(vfsacap); FSACAP2.SetTexCoordData(texCoord); FSACAP2.SetNormalData(n); FSACAP2.SetElemData(elem); FSACAP2.SetColorData(cfsacap); FSACAP2.vetTransl = new Vector(0.0, 16.0, -19.0); CLGLWindow.Models.Add(FSACAP2); // FSACAP3 FSACAP3 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSACAP3.SetVertexData(vfsacap); FSACAP3.SetTexCoordData(texCoord); FSACAP3.SetNormalData(n); FSACAP3.SetElemData(elem); FSACAP3.SetColorData(cfsacap); FSACAP3.vetTransl = new Vector(0.0, 9.5, -19.0); CLGLWindow.Models.Add(FSACAP3); // FSACAP4 FSACAP4 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSACAP4.SetVertexData(vfsacap); FSACAP4.SetTexCoordData(texCoord); FSACAP4.SetNormalData(n); FSACAP4.SetElemData(elem); FSACAP4.SetColorData(cfsacap); FSACAP4.vetTransl = new Vector(0.0, 3.0, -19.0); CLGLWindow.Models.Add(FSACAP4); // FSACAP5 FSACAP5 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSACAP5.SetVertexData(vfsacap); FSACAP5.SetTexCoordData(texCoord); FSACAP5.SetNormalData(n); FSACAP5.SetElemData(elem); FSACAP5.SetColorData(cfsacap); FSACAP5.vetTransl = new Vector(0.0, -3.5, -19.0); CLGLWindow.Models.Add(FSACAP5); // FSACAP6 FSACAP6 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSACAP6.SetVertexData(vfsacap); FSACAP6.SetTexCoordData(texCoord); FSACAP6.SetNormalData(n); FSACAP6.SetElemData(elem); FSACAP6.SetColorData(cfsacap); FSACAP6.vetTransl = new Vector(0.0, -10.0, -19.0); CLGLWindow.Models.Add(FSACAP6); // FSACAP7 FSACAP7 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSACAP7.SetVertexData(vfsacap); FSACAP7.SetTexCoordData(texCoord); FSACAP7.SetNormalData(n); FSACAP7.SetElemData(elem); FSACAP7.SetColorData(cfsacap); FSACAP7.vetTransl = new Vector(0.0, -16.5, -19.0); CLGLWindow.Models.Add(FSACAP7); // FSACAP8 FSACAP8 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FSACAP8.SetVertexData(vfsacap); FSACAP8.SetTexCoordData(texCoord); FSACAP8.SetNormalData(n); FSACAP8.SetElemData(elem); FSACAP8.SetColorData(cfsacap); FSACAP8.vetTransl = new Vector(0.0, -54.5, -17.0); CLGLWindow.Models.Add(FSACAP8); // Outer tube code // OT1 float[] vo1 = new float[] { 0,(float)-4.2,-(float)47, 0,(float)-4.2, (float)47, 0,(float) 4.2, (float)47, 0,(float) 4.2,-(float)47 }; float[] co = new float[] { 255/255.0f, 255/255.0f, 0/255.0f, 1.0f, 255/255.0f, 255/255.0f, 0/255.0f, 1.0f, 255/255.0f, 255/255.0f, 0/255.0f, 1.0f, 255/255.0f, 255/255.0f, 0/255.0f, 1.0f }; OT1 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OT1.SetVertexData(vo1); OT1.SetTexCoordData(texCoord); OT1.SetNormalData(n); OT1.SetElemData(elem); OT1.SetColorData(co); OT1.vetTransl = new Vector(0.0, 0.0, 78.5); CLGLWindow.Models.Add(OT1); // OT2 float[] vo2 = new float[] { 0,(float)-15.25,-(float)3, 0,(float)-15.25, (float)3, 0,(float) 15.25, (float)3, 0,(float) 15.25,-(float)3 }; OT2 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OT2.SetVertexData(vo2); OT2.SetTexCoordData(texCoord); OT2.SetNormalData(n); OT2.SetElemData(elem); OT2.SetColorData(co); OT2.vetTransl = new Vector(0.0, 17.5, 34.5); CLGLWindow.Models.Add(OT2); // OT3 float[] vo3 = new float[] { 0,(float)-4,-(float)6, 0,(float)-4, (float)6, 0,(float) 4, (float)6, 0,(float) 4,-(float)6 }; OT3 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); OT3.SetVertexData(vo3); OT3.SetTexCoordData(texCoord); OT3.SetNormalData(n); OT3.SetElemData(elem); OT3.SetColorData(co); OT3.vetTransl = new Vector(0.0, 28.7, 27.0); CLGLWindow.Models.Add(OT3); // Inner tube code // IT1 float[] vi1 = new float[] { 0,(float)-1.9,-(float)50, 0,(float)-1.9, (float)50, 0,(float) 1.9, (float)50, 0,(float) 1.9,-(float)50 }; float[] ci = new float[] { 238/255.0f, 162/255.0f, 193/255.0f, 1.0f, 238/255.0f, 162/255.0f, 193/255.0f, 1.0f, 238/255.0f, 162/255.0f, 193/255.0f, 1.0f, 238/255.0f, 162/255.0f, 193/255.0f, 1.0f }; IT1 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); IT1.SetVertexData(vi1); IT1.SetTexCoordData(texCoord); IT1.SetNormalData(n); IT1.SetElemData(elem); IT1.SetColorData(ci); IT1.vetTransl = new Vector(0.0, 0.0, 76.5); CLGLWindow.Models.Add(IT1); // IT2 float[] vi2 = new float[] { 0,(float)-13.25,-(float)1.5, 0,(float)-13.25, (float)1.5, 0,(float) 13.25, (float)1.5, 0,(float) 13.25,-(float)1.5 }; IT2 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); IT2.SetVertexData(vi2); IT2.SetTexCoordData(texCoord); IT2.SetNormalData(n); IT2.SetElemData(elem); IT2.SetColorData(ci); IT2.vetTransl = new Vector(0.0, 14.5, 28); CLGLWindow.Models.Add(IT2); // IT3 float[] vi3 = new float[] { 0,(float)-2,-(float)4.2, 0,(float)-2, (float)4.2, 0,(float) 2, (float)4.2, 0,(float) 2,-(float)4.2 }; IT3 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); IT3.SetVertexData(vi3); IT3.SetTexCoordData(texCoord); IT3.SetNormalData(n); IT3.SetElemData(elem); IT3.SetColorData(ci); IT3.vetTransl = new Vector(0.0, 28.7, 25.25); CLGLWindow.Models.Add(IT3); // Finger open code // FO1 float[] fo1 = new float[] { 0,(float)-1.5,-(float)0.8, 0,(float)-1.5, (float)0.8, 0,(float) 1.5, (float)0.8, 0,(float) 1.5,-(float)0.8 }; float[] cfo = new float[] { 0/255.0f, 255/255.0f, 0/255.0f, 1.0f, 0/255.0f, 255/255.0f, 0/255.0f, 1.0f, 0/255.0f, 255/255.0f, 0/255.0f, 1.0f, 0/255.0f, 255/255.0f, 0/255.0f, 1.0f }; FO1 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); FO1.SetVertexData(fo1); FO1.SetTexCoordData(texCoord); FO1.SetNormalData(n); FO1.SetElemData(elem); FO1.SetColorData(cfo); //FO2 FO2 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.Quads); // FO2 = new GLRender.GLVBOModel(OpenTK.Graphics.OpenGL.BeginMode.); FO2.SetVertexData(fo1); FO2.SetTexCoordData(texCoord); FO2.SetNormalData(n); FO2.SetElemData(elem); FO2.SetColorData(cfo); TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); }
public void Init(Camera cam, EditorCommandManager commandManager) { //计算格子 List <Vector3> tempLines = new List <Vector3>(); for (int i = 0; i <= SpaceX * Const.ChunkSize; i++) { tempLines.Add(new Vector3(i, halfSpaceSize.y, 0) - halfSpaceSize); tempLines.Add(new Vector3(i, halfSpaceSize.y, SpaceY * Const.ChunkSize) - halfSpaceSize); } for (int j = 0; j <= SpaceY * Const.ChunkSize; j++) { tempLines.Add(new Vector3(0, halfSpaceSize.y, j) - halfSpaceSize); tempLines.Add(new Vector3(SpaceX * Const.ChunkSize, halfSpaceSize.y, j) - halfSpaceSize); } gridLines = tempLines.ToArray(); //预处理光线 rays = new Vector3[6][]; { for (int i = 0; i < 6; i++) { rays[i] = TerrainTool.getRandomRays(9, i); } } DebugTool.Log("预处理光线追踪"); camera = cam; this.commandManager = commandManager; blockManager = new BlockManager(); blockManager.create(SpaceX * Const.ChunkSize, SpaceY * Const.ChunkSize, SpaceZ * Const.ChunkSize, new BlockTypeFun()); //blockManager.forEachChunk((BlockChunk chunk, int i, int j, int k) => { // for (int x = 0; x < Const.ChunkSize; x++) { // for (int y = 0; y < Const.ChunkSize; y++) { // for (int z = 0; z < Const.ChunkSize; z++) { // int gx = i * Const.ChunkSize + x; // int gy = j * Const.ChunkSize + y; // int gz = k * Const.ChunkSize + z; // if ((new Vector3(gx, gy, gz) - new Vector3(SpaceX / 2.0f, SpaceY / 2.0f, SpaceZ / 2.0f) * Const.ChunkSize).magnitude < 10) { // chunk.setBlock(x, y, z, (int)BlockTypeEnum.Sand); // } // } // } // } //}); rtm = new RayCastManager(); rtm.create(blockManager.SizeX, blockManager.SizeY, blockManager.SizeZ); //rtm.moveTo(0, 0, 0, (int gx, int gy, int gz) => { // return blockManager.getBlock(gx, gy, gz) != 0 ? RayCastBlockType.All : RayCastBlockType.Nothing; //}); for (int x = 0; x < SpaceX * Const.ChunkSize; x++) { for (int y = 0; y < SpaceY * Const.ChunkSize; y++) { for (int z = 0; z < SpaceZ * Const.ChunkSize; z++) { rtm.setBlock(x, y, z, blockManager.getBlock(x, y, z) != 0 ? RayCastBlockType.All : RayCastBlockType.Nothing); } } } bDirty = true; previewMesh = GLRender.createCubeMesh(0.5f, Color.white); }
public void OnRenderObject() { //GLRender.DrawWireCube(-halfSpaceSize, halfSpaceSize, new Color(1,1,1,0.1f), GlobalResources.getAlphaBlend()); //foreach (MeshInfo mi in m_meshes) { // Graphics.DrawMesh(mi.mesh, mi.pos - halfSpaceSize, Quaternion.identity, GlobalResources.getBlockMaterial(),0); //} //if (bShowPreview && curOperator == Operator.DeleteBlock) { // Vector3 min = selectedBlockPosStart - Vector3.one * 0.51f - halfSpaceSize; // Vector3 max = selectedBlockPosStart + Vector3.one * 0.51f - halfSpaceSize; // GLRender.DrawWireCube(min,max ,new Color(1,0,0,0.3f),GlobalResources.getAlphaBlend()); //} GLRender.DrawLinePairs(gridLines, new Color(1, 1, 1, 0.1f), GlobalResources.getAlphaBlend()); GLRender.DrawLine(Vector3.Scale(new Vector3(0, 0, -0.5f), spaceSize), Vector3.Scale(new Vector3(0, 0, 0.5f), spaceSize), new Color(1, 1, 1, 0.2f), GlobalResources.getAlphaBlend()); GLRender.DrawLine(Vector3.Scale(new Vector3(-0.5f, 0, 0), spaceSize), Vector3.Scale(new Vector3(0.5f, 0, 0), spaceSize), new Color(1, 1, 1, 0.2f), GlobalResources.getAlphaBlend()); if (bShowPreview && curOperator == Operator.AddBlock) { Vector3 pos = previewBlockPosStart - halfSpaceSize - Vector3.one * 0.51f; Vector3 diff = previewBlockPosEnd3D - previewBlockPosStart; int nx = (int)(Mathf.Abs(diff.x + 0.01f)) + 1; int ny = (int)(Mathf.Abs(diff.y + 0.01f)) + 1; int nz = (int)(Mathf.Abs(diff.z + 0.01f)) + 1; nx = nx < 100 ? nx : 100; ny = ny < 100 ? ny : 100; nz = nz < 100 ? nz : 100; int stepX = diff.x > 0 ? 1 : -1; int stepY = diff.y > 0 ? 1 : -1; int stepZ = diff.z > 0 ? 1 : -1; GlobalResources.getBlue().SetPass(0); for (int x = 0; x < nx; x++) { for (int y = 0; y < ny; y++) { for (int z = 0; z < nz; z++) { Matrix4x4 mat = Matrix4x4.TRS(pos + new Vector3(x * stepX, y * stepY, z * stepZ), Quaternion.identity, Vector3.one * 1.02f); Graphics.DrawMeshNow(previewMesh, mat, 0); //GLRender.DrawCube(min, max, new Color(1, 0, 0, 0.2f), GlobalResources.getAlphaBlend()); } } } Vector3 min = selectedBlockPosStart - halfSpaceSize; Vector3 max = selectedBlockPosStart + Vector3.one - halfSpaceSize; GLRender.DrawWireCube(min, max, new Color(1, 0, 0, 0.2f), GlobalResources.getAlphaBlend()); } else if (bShowPreview && curOperator == Operator.DeleteBlock) { //Vector3 pos = selectedBlockPosStart - halfSpaceSize; Vector3 diff = selectedBlockPosEnd3D - selectedBlockPosStart; int nx = (int)(Mathf.Abs(diff.x + 0.01f)) + 1; int ny = (int)(Mathf.Abs(diff.y + 0.01f)) + 1; int nz = (int)(Mathf.Abs(diff.z + 0.01f)) + 1; nx = nx < 100 ? nx : 100; ny = ny < 100 ? ny : 100; nz = nz < 100 ? nz : 100; int stepX = diff.x > 0 ? 1 : -1; int stepY = diff.y > 0 ? 1 : -1; int stepZ = diff.z > 0 ? 1 : -1; for (int x = 0; x < nx; x++) { for (int y = 0; y < ny; y++) { for (int z = 0; z < nz; z++) { Vector3 center = selectedBlockPosStart - halfSpaceSize + new Vector3(x * stepX, y * stepY, z * stepZ); Vector3 min = center; Vector3 max = center + Vector3.one; GLRender.DrawWireCube(min, max, new Color(1, 0, 0, 0.3f), GlobalResources.getAlphaBlend()); } } } } //if (bShowPreview && curOperator == Operator.AddBlock) { // Gizmos.matrix = transform.localToWorldMatrix; // Gizmos.color = Color.white; // Gizmos.DrawWireCube(selectedBlockPos, Vector3.one * 1.05f); // Gizmos.DrawCube(previewBlockPos, Vector3.one); //} //else if (bShowPreview && curOperator == Operator.DeleteBlock) { // Gizmos.matrix = transform.localToWorldMatrix; // Gizmos.color = Color.white; // Gizmos.DrawWireCube(selectedBlockPos, Vector3.one * 1.05f); //} }