diff --git a/README.md b/README.md index 2222c038..af2a561d 100644 --- a/README.md +++ b/README.md @@ -77,13 +77,13 @@ AppVeyor will automatically push the release to NuGet and GitHub. ## Sample Applications -There are a large number of sample applications that show how to use SharpGL. Check out the 'Samples' solution to see the samples that are available - they'll be documented soon. +There are a large number of sample applications that show how to use SharpGL. Check out the 'Samples' solution to see the samples that are available. -### Ducky Sample +### WinForms - Ducky Sample -This WinForms sample shows how to load an object file with materials, using the Serialization library. It also has great support for internationalization (thanks [`odalet`](https://github.com/odalet)! +This sample shows how to load an object file with materials, using the Serialization library. It also has great support for internationalization (thanks [`odalet`](https://github.com/odalet))! -![Ducky Sample](assets/samples/DuckySample.png) +Ducky Sample This sample demonstrates: @@ -91,6 +91,138 @@ This sample demonstrates: - Building polygons from `*.obj` files - Arcball rotation +Note that this sample uses _immediate mode_ OpenGL, which is officially deprecated. + +### WinForms - Extensions Sample + +This sample shows how to use OpenGL extensions. It demonstrates this by using the 'bump map' extensions. + +Note that this sample uses _immediate mode_ OpenGL, which is officially deprecated. + +Extensions Sample + +### WinForms - Hit Test Sample + +This sample shows how to use to perform hit testing with SharpGL. It uses the _Scene Graph_ to support this. + +Note that this sample uses _immediate mode_ OpenGL, which is officially deprecated. + +Extensions Sample + +### WinForms - Modern OpenGL Sample + +This sample shows how to use modern OpenGL capabilities which are Shader based, by showing a vertex and fragment shader. + +Extensions Sample + +### WinForms - Native Textures Sample + +This sample shows how to load textures into OpenGL using pure OpenGL functions. However, the `Texture` object from the `SceneGraph` will be much easier to use! + +Note that this sample uses _immediate mode_ OpenGL, which is officially deprecated. + +Native Textures Sample + +### WinForms - Particle Systems Sample + +This sample shows how to build a simple particle system with OpenGL. + +Note that this sample uses _immediate mode_ OpenGL, which is officially deprecated. + +Native Textures Sample + +### WinForms - Polygon Loading + +This sample shows how to load polygon data with the Scene Graph and Serialization libraries. + +Note that this sample uses _immediate mode_ OpenGL, which is officially deprecated. + +Native Textures Sample + +### WinForms - Radial Blue + +This sample shows how to use a Radial Blur effect in OpenGL. + +Note that this sample uses _immediate mode_ OpenGL, which is officially deprecated. + +Native Textures Sample + +### WinForms - Render Contexts Sample + +This sample demonstrates the different types of render contexts which are available, and how they affect performance and the extensions available. + +Native Textures Sample + +### WinForms - Render Trigger Sample + +This sample shows different ways to render; either on a timer or on demand. + +Native Textures Sample + +### WinForms - Scene Sample + +This sample demonstrates the _Scene Graph_ which can be used to manage and render geometry. + +Note that this sample uses _immediate mode_ OpenGL, which is officially deprecated. + +Native Textures Sample + +### WinForms - SharpGL Textures Sample + +This sample demonstrates how textures can be loaded using the SharpGL `Textures` object, which greatly simplifies texture management. + +Note that this sample uses _immediate mode_ OpenGL, which is officially deprecated. + +Native Textures Sample + +### WinForms - Simple Drawing Sample + +This sample demonstrates the most basic form of simple drawing in OpenGL. + +Note that this sample uses _immediate mode_ OpenGL, which is officially deprecated. + +Native Textures Sample + +### WPF - Cel Shading Sample + +This sample demonstrates how to use shaders to crete a cel-shaing effect. + +Native Textures Sample + +### WPF - Drawing Mechanisms Sample + +This sample demonstrates how to use shaders to crete a cel-shaing effect. + +Native Textures Sample + +### WPF - Object Loading Sample + +This sample demonstrates how to load objects in a WPF OpenGL project. + +### WPF - Simple Shader Sample + +This sample shows how to use a simple shader. + +Simple Shader Sample + +### WPF - Tea Pot Sample + +This sample shows how to quickly and easily render geometry. + +Simple Shader Sample + +### WPF - Text Rendering Sample + +This sample shows how to render 3D and 2D text. + +Text Rendering Sample + +### WPF - Two Dimensional Rendering Sample + +This sample shows how to do simple 2D render, with a visual like an old Windows Screen-Saver. + +Text Rendering Sample + ## Documentation All documentation is available on [the Wiki](https://github.com/dwmkerr/sharpgl/wiki). diff --git a/assets/samples/Screenshot 2020-06-12 at 1.27.01 PM.png b/assets/samples/Screenshot 2020-06-12 at 1.27.01 PM.png new file mode 100644 index 00000000..f0df4b51 Binary files /dev/null and b/assets/samples/Screenshot 2020-06-12 at 1.27.01 PM.png differ diff --git a/assets/samples/SimpleDrawingSample.png b/assets/samples/SimpleDrawingSample.png new file mode 100644 index 00000000..0dc5fd3b Binary files /dev/null and b/assets/samples/SimpleDrawingSample.png differ diff --git a/assets/samples/WinFormsExtensionsSample.png b/assets/samples/WinFormsExtensionsSample.png new file mode 100644 index 00000000..4e19fc4c Binary files /dev/null and b/assets/samples/WinFormsExtensionsSample.png differ diff --git a/assets/samples/WinFormsHitTestSample.png b/assets/samples/WinFormsHitTestSample.png new file mode 100644 index 00000000..9ea5c8f1 Binary files /dev/null and b/assets/samples/WinFormsHitTestSample.png differ diff --git a/assets/samples/WinFormsSceneSample.png b/assets/samples/WinFormsSceneSample.png new file mode 100644 index 00000000..cc8d1fce Binary files /dev/null and b/assets/samples/WinFormsSceneSample.png differ diff --git a/assets/samples/WinFormsSharpGLTexturesSample.png b/assets/samples/WinFormsSharpGLTexturesSample.png new file mode 100644 index 00000000..447e7305 Binary files /dev/null and b/assets/samples/WinFormsSharpGLTexturesSample.png differ diff --git a/assets/samples/WinformsNativeTexturesSample.png b/assets/samples/WinformsNativeTexturesSample.png index e45d0fe7..b5bafd78 100644 Binary files a/assets/samples/WinformsNativeTexturesSample.png and b/assets/samples/WinformsNativeTexturesSample.png differ diff --git a/assets/samples/WinformsParticleSystemsSample.png b/assets/samples/WinformsParticleSystemsSample.png new file mode 100644 index 00000000..320c3818 Binary files /dev/null and b/assets/samples/WinformsParticleSystemsSample.png differ diff --git a/assets/samples/WinformsPolygonLoadingSample.png b/assets/samples/WinformsPolygonLoadingSample.png new file mode 100644 index 00000000..194d7894 Binary files /dev/null and b/assets/samples/WinformsPolygonLoadingSample.png differ diff --git a/assets/samples/WinformsRadialBlueSample.png b/assets/samples/WinformsRadialBlueSample.png new file mode 100644 index 00000000..50abd7ae Binary files /dev/null and b/assets/samples/WinformsRadialBlueSample.png differ diff --git a/assets/samples/WinformsRenderContextsSample.png b/assets/samples/WinformsRenderContextsSample.png new file mode 100644 index 00000000..f76d933c Binary files /dev/null and b/assets/samples/WinformsRenderContextsSample.png differ diff --git a/assets/samples/WinformsRenderTriggerSample.png b/assets/samples/WinformsRenderTriggerSample.png new file mode 100644 index 00000000..226d8c4d Binary files /dev/null and b/assets/samples/WinformsRenderTriggerSample.png differ diff --git a/assets/samples/Wpf2DDrawingSample.png b/assets/samples/Wpf2DDrawingSample.png new file mode 100644 index 00000000..c6efe41a Binary files /dev/null and b/assets/samples/Wpf2DDrawingSample.png differ diff --git a/assets/samples/WpfDrawingMechanismsSample.png b/assets/samples/WpfDrawingMechanismsSample.png new file mode 100644 index 00000000..8852bb6a Binary files /dev/null and b/assets/samples/WpfDrawingMechanismsSample.png differ diff --git a/assets/samples/WpfSimpleShaderSample.png b/assets/samples/WpfSimpleShaderSample.png new file mode 100644 index 00000000..260a3184 Binary files /dev/null and b/assets/samples/WpfSimpleShaderSample.png differ diff --git a/assets/samples/WpfTeapotSample.png b/assets/samples/WpfTeapotSample.png new file mode 100644 index 00000000..5b168f25 Binary files /dev/null and b/assets/samples/WpfTeapotSample.png differ diff --git a/assets/samples/WpfTextRenderingSample.png b/assets/samples/WpfTextRenderingSample.png index 8b615c8a..3e81fb03 100644 Binary files a/assets/samples/WpfTextRenderingSample.png and b/assets/samples/WpfTextRenderingSample.png differ diff --git a/source/SharpGL/Core/SharpGL.SceneGraph/Core/SceneElement.cs b/source/SharpGL/Core/SharpGL.SceneGraph/Core/SceneElement.cs index 5f2b6fb3..8df71012 100644 --- a/source/SharpGL/Core/SharpGL.SceneGraph/Core/SceneElement.cs +++ b/source/SharpGL/Core/SharpGL.SceneGraph/Core/SceneElement.cs @@ -61,7 +61,7 @@ public void AddChild(SceneElement child) IHasOpenGLContext contextChild = child as IHasOpenGLContext; // Get the parent OpenGL. - OpenGL gl = TraverseToRootElement().ParentScene.OpenGL; + OpenGL gl = TraverseToRootElement().ParentScene.CurrentOpenGLContext; // If we don't exist in this context, create in this context. if(contextChild.CurrentOpenGLContext != gl) @@ -77,8 +77,8 @@ public void RemoveChild(SceneElement child) { // If the child has OpenGL context which is not the current one, we must destroy it. if (child is IHasOpenGLContext) - if(((IHasOpenGLContext)child).CurrentOpenGLContext != TraverseToRootElement().ParentScene.OpenGL) - ((IHasOpenGLContext)child).DestroyInContext(TraverseToRootElement().ParentScene.OpenGL); + if(((IHasOpenGLContext)child).CurrentOpenGLContext != TraverseToRootElement().ParentScene.CurrentOpenGLContext) + ((IHasOpenGLContext)child).DestroyInContext(TraverseToRootElement().ParentScene.CurrentOpenGLContext); // Throw an exception if the child is not a child of this element.. if (child.Parent != this) diff --git a/source/SharpGL/Core/SharpGL.SceneGraph/Raytracing/RayTracer.cs b/source/SharpGL/Core/SharpGL.SceneGraph/Raytracing/RayTracer.cs index b0abcf20..da1e63a0 100644 --- a/source/SharpGL/Core/SharpGL.SceneGraph/Raytracing/RayTracer.cs +++ b/source/SharpGL/Core/SharpGL.SceneGraph/Raytracing/RayTracer.cs @@ -24,7 +24,7 @@ public class RayTracer public Image Render(Scene scene, Camera camera) { // Useful references. - OpenGL gl = scene.OpenGL; + OpenGL gl = scene.CurrentOpenGLContext; // First, we need the matricies and viewport. double[] modelview = new double[16]; diff --git a/source/SharpGL/Core/SharpGL.SceneGraph/Scene.cs b/source/SharpGL/Core/SharpGL.SceneGraph/Scene.cs index 1e0f2bfb..256ef2b1 100644 --- a/source/SharpGL/Core/SharpGL.SceneGraph/Scene.cs +++ b/source/SharpGL/Core/SharpGL.SceneGraph/Scene.cs @@ -1,34 +1,19 @@ -using System; using System.ComponentModel; -using System.ComponentModel.Design; -using System.Drawing.Design; -using System.Collections; -using System.Diagnostics; -using System.Drawing; -using System.Linq; using System.Collections.Generic; - -using SharpGL; -using SharpGL.SceneGraph.Quadrics; using SharpGL.SceneGraph.Cameras; -using SharpGL.SceneGraph.Evaluators; -using SharpGL.SceneGraph.Collections; using SharpGL.SceneGraph.Core; -using SharpGL.SceneGraph.Lighting; -using SharpGL.SceneGraph.Effects; using SharpGL.SceneGraph.Assets; using System.Collections.ObjectModel; using System.Xml.Serialization; namespace SharpGL.SceneGraph { - [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))] + [TypeConverter(typeof(ExpandableObjectConverter))] [XmlInclude(typeof(PerspectiveCamera))] [XmlInclude(typeof(OrthographicCamera))] [XmlInclude(typeof(FrustumCamera))] [XmlInclude(typeof(LookAtCamera))] - [XmlInclude(typeof(ArcBallCamera))] - public class Scene : IHasOpenGLContext + public class Scene : IHasOpenGLContext { /// /// Initializes a new instance of the class. @@ -38,7 +23,7 @@ public Scene() RenderBoundingVolumes = true; // The SceneContainer must have it's parent scene set. - sceneContainer.ParentScene = this; + SceneContainer.ParentScene = this; } /// @@ -57,7 +42,7 @@ public virtual IEnumerable DoHitTest(int x, int y) Dictionary hitMap = new Dictionary(); // If we don't have a current camera, we cannot hit test. - if (currentCamera == null) + if (CurrentCamera == null) return resultSet; // Create an array that will be the viewport. @@ -83,7 +68,7 @@ public virtual IEnumerable DoHitTest(int x, int y) gl.PushMatrix(); gl.LoadIdentity(); gl.PickMatrix(x, y, 4, 4, viewport); - currentCamera.TransformProjectionMatrix(gl); + CurrentCamera.TransformProjectionMatrix(gl); gl.MatrixMode(OpenGL.GL_MODELVIEW); gl.LoadIdentity(); @@ -107,8 +92,8 @@ public virtual IEnumerable DoHitTest(int x, int y) for (int hit = 0; hit < hits; hit++) { uint nameCount = selectBuffer[posinarray++]; - uint z1 = selectBuffer[posinarray++]; - uint z2 = selectBuffer[posinarray++]; + uint unused = selectBuffer[posinarray++]; + uint unused1 = selectBuffer[posinarray++]; if (nameCount == 0) continue; @@ -135,10 +120,10 @@ public virtual void Draw(Camera camera = null) // cameras completely outside of the responsibility of the scene? // If no camera has been provided, use the current one. if (camera == null) - camera = currentCamera; + camera = CurrentCamera; // Set the clear color. - float[] clear = clearColour; + float[] clear = _clearColour; gl.ClearColor(clear[0], clear[1], clear[2], clear[3]); // Reproject. @@ -153,7 +138,7 @@ public virtual void Draw(Camera camera = null) // Render the root element, this will then render the whole // of the scene tree. - RenderElement(sceneContainer, RenderMode.Design); + RenderElement(SceneContainer, RenderMode.Design); // TODO: Adding this code here re-enables textures- it should work without it but it // doesn't, look into this. @@ -163,12 +148,12 @@ public virtual void Draw(Camera camera = null) gl.Flush(); } - /// - /// Renders the element. - /// - /// The gl. - /// The render mode. - public void RenderElement(SceneElement sceneElement, RenderMode renderMode) + /// + /// Renders the element. + /// + /// The secene element to render. + /// The render mode. + public void RenderElement(SceneElement sceneElement, RenderMode renderMode) { // If the element is disabled, we're done. if (sceneElement.IsEnabled == false) @@ -228,7 +213,7 @@ private void RenderElementForHitTest(SceneElement sceneElement, { // If the element is disabled, we're done. // Also, never hit test the current camera. - if (sceneElement.IsEnabled == false || sceneElement == currentCamera) + if (sceneElement.IsEnabled == false || sceneElement == CurrentCamera) return; // Push each effect. @@ -276,132 +261,59 @@ private void RenderElementForHitTest(SceneElement sceneElement, /// Height of the screen. public virtual void Resize(int width, int height) { - if(width != -1 && height != -1) + if(width != -1 && height != -1 && gl != null) { // Resize. gl.Viewport(0, 0, width, height); - if (currentCamera != null) + if (CurrentCamera != null) { // Set aspect ratio. - currentCamera.AspectRatio = (float)width / (float)height; + CurrentCamera.AspectRatio = width / (float)height; // Then project. - currentCamera.Project(gl); + CurrentCamera.Project(gl); } } } - - /// - /// Create in the context of the supplied OpenGL instance. - /// - /// The OpenGL instance. - public void CreateInContext(OpenGL gl) - { - // Create every scene element. - var openGLContextElements = SceneContainer.Traverse( - se => se is IHasOpenGLContext); - foreach (var openGLContextElement in openGLContextElements) - ((IHasOpenGLContext)openGLContextElement).CreateInContext(gl); - this.gl = gl; - } - - /// - /// Destroy in the context of the supplied OpenGL instance. - /// - /// The OpenGL instance. - public void DestroyInContext(OpenGL gl) - { - } - + + /// /// This is the OpenGL class, use it to call OpenGL functions. + /// Only set when the context is set. /// - private OpenGL gl = new OpenGL(); + private OpenGL gl; - /// - /// The main scene container - this is the top level element of the Scene Tree. - /// - private SceneContainer sceneContainer = new SceneContainer(); - - /// - /// The set of scene assets. - /// - private ObservableCollection assets = new ObservableCollection(); - - /// - /// This is the camera that is currently being used to view the scene. - /// - private Camera currentCamera; - - /// + /// /// This is the colour of the background of the scene. /// - private GLColor clearColour = new GLColor(0, 0, 0, 0); - - /// - /// Gets or sets the open GL. - /// - /// - /// The open GL. - /// - [XmlIgnore] - [Description("OpenGL API Wrapper Class"), Category("OpenGL/External")] - public OpenGL OpenGL - { - get {return gl;} - set {gl = value;} - } + private GLColor _clearColour = new GLColor(0, 0, 0, 0); - /// + /// /// Gets or sets the scene container. /// /// /// The scene container. /// [Description("The top-level object in the Scene Tree"), Category("Scene")] - public SceneContainer SceneContainer - { - get { return sceneContainer; } - set { sceneContainer = value; } - } + public SceneContainer SceneContainer { get; set; } = new SceneContainer(); - /// + /// /// Gets the assets. /// [Description("The scene assets."), Category("Scene")] - public ObservableCollection Assets - { - get { return assets; } - } + public ObservableCollection Assets { get; } = new ObservableCollection(); - /// + /// /// Gets or sets the current camera. /// /// /// The current camera. /// [Description("The current camera being used to view the scene."), Category("Scene")] - public Camera CurrentCamera - { - get {return currentCamera;} - set {currentCamera = value;} - } + public Camera CurrentCamera { get; set; } - /// - /// Gets or sets the color of the clear. - /// - /// - /// The color of the clear. - /// - [Description("The background colour."), Category("Scene")] - public Color ClearColor - { - get {return clearColour;} - set {clearColour = value;} - } - - /// + /// /// Gets or sets a value indicating whether [render bounding volumes]. /// /// @@ -414,14 +326,28 @@ public bool RenderBoundingVolumes set; } - /// - /// Gets the current OpenGL that the object exists in context. - /// - [XmlIgnore] - [Browsable(false)] - public OpenGL CurrentOpenGLContext - { - get { return gl; } + public void CreateInContext(OpenGL gl) + { + // Set our current context. + this.gl = gl; + + // Create every scene element. + var openGLContextElements = SceneContainer.Traverse( + se => se is IHasOpenGLContext); + foreach (var openGLContextElement in openGLContextElements) + ((IHasOpenGLContext)openGLContextElement).CreateInContext(gl); + } - } + + public void DestroyInContext(OpenGL gl) + { + // Create every scene element. + var openGLContextElements = SceneContainer.Traverse( + se => se is IHasOpenGLContext); + foreach (var openGLContextElement in openGLContextElements) + ((IHasOpenGLContext)openGLContextElement).CreateInContext(gl); + } + + public OpenGL CurrentOpenGLContext => gl; + } } diff --git a/source/SharpGL/Core/SharpGL.Serialization/Wavefront/ObjFileFormat.cs b/source/SharpGL/Core/SharpGL.Serialization/Wavefront/ObjFileFormat.cs index f0e7c64f..60fcaf41 100644 --- a/source/SharpGL/Core/SharpGL.Serialization/Wavefront/ObjFileFormat.cs +++ b/source/SharpGL/Core/SharpGL.Serialization/Wavefront/ObjFileFormat.cs @@ -252,7 +252,7 @@ private void LoadMaterials(string path, Scene scene) // Create/load texture. theTexture = new Texture(); - theTexture.Create(scene.OpenGL, textureFile); + theTexture.Create(scene.CurrentOpenGLContext, textureFile); } // Set texture for material. diff --git a/source/SharpGL/Core/SharpGL.WPF/SceneView.xaml.cs b/source/SharpGL/Core/SharpGL.WPF/SceneView.xaml.cs index 47fcd53d..09a20577 100644 --- a/source/SharpGL/Core/SharpGL.WPF/SceneView.xaml.cs +++ b/source/SharpGL/Core/SharpGL.WPF/SceneView.xaml.cs @@ -54,7 +54,7 @@ void SceneView_SizeChanged(object sender, SizeChangedEventArgs e) int height = (int)e.NewSize.Height; // Resize the scene. - Scene.OpenGL.SetDimensions(width, height); + Scene.CurrentOpenGLContext.SetDimensions(width, height); Scene.Resize(width, height); } } @@ -95,13 +95,13 @@ void timer_Tick(object sender, EventArgs e) // Draw the FPS. if (DrawFPS) { - Scene.OpenGL.DrawText(5, 5, 1.0f, 0.0f, 0.0f, "Courier New", 12.0f, string.Format("Draw Time: {0:0.0000} ms ~ {1:0.0} FPS", frameTime, 1000.0 / frameTime)); - Scene.OpenGL.Flush(); + Scene.CurrentOpenGLContext.DrawText(5, 5, 1.0f, 0.0f, 0.0f, "Courier New", 12.0f, string.Format("Draw Time: {0:0.0000} ms ~ {1:0.0} FPS", frameTime, 1000.0 / frameTime)); + Scene.CurrentOpenGLContext.Flush(); } - if (Scene.OpenGL.RenderContextProvider is RenderContextProviders.DIBSectionRenderContextProvider) + if (Scene.CurrentOpenGLContext.RenderContextProvider is RenderContextProviders.DIBSectionRenderContextProvider) { - RenderContextProviders.DIBSectionRenderContextProvider provider = Scene.OpenGL.RenderContextProvider as RenderContextProviders.DIBSectionRenderContextProvider; + RenderContextProviders.DIBSectionRenderContextProvider provider = Scene.CurrentOpenGLContext.RenderContextProvider as RenderContextProviders.DIBSectionRenderContextProvider; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. @@ -114,9 +114,9 @@ void timer_Tick(object sender, EventArgs e) // Copy the pixels over. image.Source = newFormatedBitmapSource; } - else if (Scene.OpenGL.RenderContextProvider is RenderContextProviders.FBORenderContextProvider) + else if (Scene.CurrentOpenGLContext.RenderContextProvider is RenderContextProviders.FBORenderContextProvider) { - RenderContextProviders.FBORenderContextProvider provider = Scene.OpenGL.RenderContextProvider as RenderContextProviders.FBORenderContextProvider; + RenderContextProviders.FBORenderContextProvider provider = Scene.CurrentOpenGLContext.RenderContextProvider as RenderContextProviders.FBORenderContextProvider; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. diff --git a/source/SharpGL/Core/SharpGL.WinForms/OpenGLControl.cs b/source/SharpGL/Core/SharpGL.WinForms/OpenGLControl.cs index 1e2da7fa..414cdf52 100644 --- a/source/SharpGL/Core/SharpGL.WinForms/OpenGLControl.cs +++ b/source/SharpGL/Core/SharpGL.WinForms/OpenGLControl.cs @@ -197,9 +197,7 @@ protected override void OnSizeChanged(EventArgs e) /// protected virtual void DoOpenGLInitialized() { - var handler = OpenGLInitialized; - if (handler != null) - handler(this, null); + OpenGLInitialized?.Invoke(this, null); } /// @@ -207,9 +205,7 @@ protected virtual void DoOpenGLInitialized() /// protected virtual void DoOpenGLDraw(RenderEventArgs e) { - var handler = OpenGLDraw; - if (handler != null) - handler(this, e); + OpenGLDraw?.Invoke(this, e); } /// @@ -217,9 +213,7 @@ protected virtual void DoOpenGLDraw(RenderEventArgs e) /// protected virtual void DoGDIDraw(RenderEventArgs e) { - var handler = GDIDraw; - if (handler != null) - handler(this, e); + GDIDraw?.Invoke(this, e); } /// @@ -292,11 +286,6 @@ void ISupportInitialize.EndInit() /// protected Stopwatch stopwatch = new Stopwatch(); - /// - /// The render context type. - /// - protected RenderContextType renderContextType = RenderContextType.DIBSection; - /// /// The last frame time in milliseconds. /// @@ -307,10 +296,7 @@ void ISupportInitialize.EndInit() /// /// The OpenGL. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] - public OpenGL OpenGL - { - get { return gl; } - } + public OpenGL OpenGL => gl; /// /// Gets or sets a value indicating whether to draw FPS information. @@ -352,11 +338,7 @@ public int FrameRate /// The type of the render context. /// [Description("The render context type."), Category("SharpGL")] - public RenderContextType RenderContextType - { - get { return renderContextType; } - set { renderContextType = value; } - } + public RenderContextType RenderContextType { get; set; } = RenderContextType.DIBSection; /// /// Gets or sets the desired OpenGL version. @@ -365,16 +347,7 @@ public RenderContextType RenderContextType /// The desired OpenGL version. /// [Description("The desired OpenGL version for the control."), Category("SharpGL")] - public OpenGLVersion OpenGLVersion - { - get { return openGLVersion; } - set { openGLVersion = value; } - } - - /// - /// The default desired OpenGL version. - /// - private OpenGLVersion openGLVersion = OpenGLVersion.OpenGL2_1; + public OpenGLVersion OpenGLVersion { get; set; } = OpenGLVersion.OpenGL2_1; /// /// Gets or sets the render trigger. diff --git a/source/SharpGL/Core/SharpGL.WinForms/SceneControl.cs b/source/SharpGL/Core/SharpGL.WinForms/SceneControl.cs index 7f81a661..e7fea0cf 100644 --- a/source/SharpGL/Core/SharpGL.WinForms/SceneControl.cs +++ b/source/SharpGL/Core/SharpGL.WinForms/SceneControl.cs @@ -23,6 +23,9 @@ public SceneControl() // Initialise the scene. SceneGraph.Helpers.SceneHelper.InitialiseModelingScene(Scene); + + // When the underlying OpenGL instance is initialised provide it to the scene. + OpenGLInitialized += (sender, args) => Scene.CreateInContext(OpenGL); } /// @@ -40,7 +43,8 @@ protected override void Dispose( bool disposing ) base.Dispose( disposing ); } - #region Component Designer generated code + #region Component Designer generated + /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. @@ -53,6 +57,7 @@ private void InitializeComponent() this.Name = "OpenGLCtrl"; } + #endregion protected override void OnPaint(PaintEventArgs e) @@ -64,7 +69,7 @@ protected override void OnPaint(PaintEventArgs e) OpenGL.MakeCurrent(); // Do the scene drawing. - scene.Draw(); + Scene.Draw(); // If there is a draw handler, then call it. DoOpenGLDraw(new RenderEventArgs(e.Graphics)); @@ -92,7 +97,6 @@ protected override void OnPaint(PaintEventArgs e) frameTime = stopwatch.Elapsed.TotalMilliseconds; } - /// /// Raises the event. /// @@ -113,21 +117,12 @@ protected override void OnSizeChanged(EventArgs e) // OpenGL needs to resize the viewport. OpenGL.SetDimensions(Width, Height); - scene.Resize(Width, Height); + Scene.Resize(Width, Height); Invalidate(); } - - /// - /// This is the scene itself. - /// - private Scene scene = new Scene(); - - [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] - public Scene Scene - { - get {return scene;} - set {scene = value;} - } + + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public Scene Scene { get; set; } = new Scene(); } } diff --git a/source/SharpGL/Samples/WinForms/ModernOpenGLSample/FormModernOpenGLSample.Designer.cs b/source/SharpGL/Samples/WinForms/ModernOpenGLSample/FormModernOpenGLSample.Designer.cs index e5fed3ed..662d8ff3 100644 --- a/source/SharpGL/Samples/WinForms/ModernOpenGLSample/FormModernOpenGLSample.Designer.cs +++ b/source/SharpGL/Samples/WinForms/ModernOpenGLSample/FormModernOpenGLSample.Designer.cs @@ -28,35 +28,38 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - this.openGLControl1 = new SharpGL.OpenGLControl(); - ((System.ComponentModel.ISupportInitialize)(this.openGLControl1)).BeginInit(); - this.SuspendLayout(); - // - // openGLControl1 - // - this.openGLControl1.Dock = System.Windows.Forms.DockStyle.Fill; - this.openGLControl1.DrawFPS = false; - this.openGLControl1.Location = new System.Drawing.Point(0, 0); - this.openGLControl1.Name = "openGLControl1"; - this.openGLControl1.OpenGLVersion = SharpGL.Version.OpenGLVersion.OpenGL4_4; - this.openGLControl1.RenderContextType = SharpGL.RenderContextType.NativeWindow; - this.openGLControl1.RenderTrigger = SharpGL.RenderTrigger.TimerBased; - this.openGLControl1.Size = new System.Drawing.Size(784, 561); - this.openGLControl1.TabIndex = 0; - this.openGLControl1.OpenGLInitialized += new System.EventHandler(this.openGLControl1_OpenGLInitialized); - this.openGLControl1.OpenGLDraw += new SharpGL.RenderEventHandler(this.openGLControl1_OpenGLDraw); - // - // FormModernOpenGLSample - // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(784, 561); - this.Controls.Add(this.openGLControl1); - this.Name = "FormModernOpenGLSample"; - this.Text = "Modern OpenGL Sample"; - ((System.ComponentModel.ISupportInitialize)(this.openGLControl1)).EndInit(); - this.ResumeLayout(false); - + this.openGLControl1 = new SharpGL.OpenGLControl(); + ((System.ComponentModel.ISupportInitialize)(this.openGLControl1)).BeginInit(); + this.SuspendLayout(); + // + // openGLControl1 + // + this.openGLControl1.Dock = System.Windows.Forms.DockStyle.Fill; + this.openGLControl1.DrawFPS = false; + this.openGLControl1.Location = new System.Drawing.Point(0, 0); + this.openGLControl1.Margin = new System.Windows.Forms.Padding(12, 12, 12, 12); + this.openGLControl1.Name = "openGLControl1"; + this.openGLControl1.OpenGLVersion = SharpGL.Version.OpenGLVersion.OpenGL4_4; + this.openGLControl1.RenderContextType = SharpGL.RenderContextType.NativeWindow; + this.openGLControl1.RenderTrigger = SharpGL.RenderTrigger.TimerBased; + this.openGLControl1.Size = new System.Drawing.Size(1568, 1079); + this.openGLControl1.TabIndex = 0; + this.openGLControl1.OpenGLInitialized += new System.EventHandler(this.openGLControl1_OpenGLInitialized); + this.openGLControl1.OpenGLDraw += new SharpGL.RenderEventHandler(this.openGLControl1_OpenGLDraw); + this.openGLControl1.Load += new System.EventHandler(this.openGLControl1_Load); + // + // FormModernOpenGLSample + // + this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1568, 1079); + this.Controls.Add(this.openGLControl1); + this.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6); + this.Name = "FormModernOpenGLSample"; + this.Text = "Modern OpenGL Sample"; + ((System.ComponentModel.ISupportInitialize)(this.openGLControl1)).EndInit(); + this.ResumeLayout(false); + } #endregion diff --git a/source/SharpGL/Samples/WinForms/ModernOpenGLSample/FormModernOpenGLSample.cs b/source/SharpGL/Samples/WinForms/ModernOpenGLSample/FormModernOpenGLSample.cs index 59ca59d7..d61d1255 100644 --- a/source/SharpGL/Samples/WinForms/ModernOpenGLSample/FormModernOpenGLSample.cs +++ b/source/SharpGL/Samples/WinForms/ModernOpenGLSample/FormModernOpenGLSample.cs @@ -42,6 +42,11 @@ private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs args) /// /// The scene that we are rendering. /// - private readonly Scene scene = new Scene(); + private readonly Scene scene = new Scene(); + + private void openGLControl1_Load(object sender, EventArgs e) + { + + } } } diff --git a/source/SharpGL/Samples/WinForms/ModernOpenGLSample/FormModernOpenGLSample.resx b/source/SharpGL/Samples/WinForms/ModernOpenGLSample/FormModernOpenGLSample.resx index 1af7de15..29dcb1b3 100644 --- a/source/SharpGL/Samples/WinForms/ModernOpenGLSample/FormModernOpenGLSample.resx +++ b/source/SharpGL/Samples/WinForms/ModernOpenGLSample/FormModernOpenGLSample.resx @@ -1,120 +1,120 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + \ No newline at end of file