JuicyGraphics/src/canvas.cs

86 lines
2.7 KiB
C#

using System;
using System.Windows.Forms;
using System.Diagnostics;
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() {
GL.Create(OpenGLVersion.OpenGL2_1, RenderContextType.NativeWindow, Width, Height, 32, this.Handle);
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;
}
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();
}
public void BeginInit() {
}
public void EndInit() {
InitializeOpenGL();
OnSizeChanged(null);
}
}
}