2019-02-09 02:35:00 +01:00
using System ;
using System.Windows.Forms ;
using SharpGL.Enumerations ;
using SharpGL.Version ;
using SharpGL ;
using System.ComponentModel ;
namespace JuicyGraphics {
class canvas : Control , ISupportInitialize {
protected OpenGL GL = new OpenGL ( ) ;
public canvas ( ) {
SetStyle ( ControlStyles . AllPaintingInWmPaint , true ) ;
SetStyle ( ControlStyles . UserPaint , true ) ;
InitializeOpenGL ( ) ;
}
protected void InitializeOpenGL ( ) {
2019-02-09 05:03:17 +01:00
GL . Create ( OpenGLVersion . OpenGL4_3 , RenderContextType . NativeWindow , Width , Height , 32 , this . Handle ) ;
2019-02-09 02:35:00 +01:00
GL . ShadeModel ( OpenGL . GL_SMOOTH ) ;
GL . ClearDepth ( 1.0f ) ;
GL . Enable ( OpenGL . GL_DEPTH_TEST ) ;
GL . DepthFunc ( OpenGL . GL_LEQUAL ) ;
GL . Hint ( OpenGL . GL_PERSPECTIVE_CORRECTION_HINT , OpenGL . GL_NICEST ) ;
GL . Viewport ( 0 , 0 , Width , Height ) ;
}
bool renderingForDesigner ( ) {
if ( this = = null )
return false ;
if ( LicenseManager . UsageMode = = LicenseUsageMode . Designtime )
return true ;
Control p = this . Parent ;
while ( p ! = null ) {
if ( p . GetType ( ) . FullName . Contains ( ".DesignerFrame" ) )
return true ;
p = p . Parent ;
}
return false ;
}
protected override void OnPaintBackground ( PaintEventArgs e ) {
if ( renderingForDesigner ( ) ) { base . OnPaintBackground ( e ) ; }
return ;
}
2019-02-09 05:03:17 +01:00
2019-02-09 02:35:00 +01:00
protected override void OnPaint ( PaintEventArgs e ) {
if ( renderingForDesigner ( ) ) {
SetStyle ( ControlStyles . AllPaintingInWmPaint , true ) ;
SetStyle ( ControlStyles . UserPaint , true ) ;
InitializeOpenGL ( ) ;
}
GL . MakeCurrent ( ) ;
GL . ClearColor ( 0f , 0.3f , 0.7f , 1f ) ;
GL . Clear ( OpenGL . GL_COLOR_BUFFER_BIT | OpenGL . GL_DEPTH_BUFFER_BIT ) ;
GL . Begin ( BeginMode . Triangles ) ;
GL . Color ( 0.8 , 0.1 , 0.1 ) ;
GL . Vertex ( 1.0 , 1.0 ) ;
GL . Vertex ( - 1.0 , - 1.0 ) ; GL . Vertex ( - 1.0 , 1.0 ) ;
GL . End ( ) ;
GL . Flush ( ) ;
var handleDeviceContext = e . Graphics . GetHdc ( ) ;
GL . Blit ( handleDeviceContext ) ;
e . Graphics . ReleaseHdc ( handleDeviceContext ) ;
}
protected override void OnSizeChanged ( EventArgs e ) {
if ( GL . RenderContextProvider = = null )
return ;
GL . SetDimensions ( Width , Height ) ;
GL . Viewport ( 0 , 0 , Width , Height ) ;
Invalidate ( ) ;
}
2019-02-09 05:03:17 +01:00
public void BeginInit ( ) { }
2019-02-09 02:35:00 +01:00
public void EndInit ( ) {
InitializeOpenGL ( ) ;
OnSizeChanged ( null ) ;
}
}
}