First commit which includes support for drawing to a WinForms control.

This commit is contained in:
kanato 2008-12-09 19:44:18 +00:00
parent ec92b72469
commit 11430665f3
8 changed files with 86 additions and 75 deletions

View file

@ -1,4 +1,4 @@
namespace Examples namespace Examples
{ {
partial class ExampleLauncher partial class ExampleLauncher
{ {
@ -85,9 +85,9 @@
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266); this.ClientSize = new System.Drawing.Size(292, 366);
this.Controls.Add(this.splitContainer1); this.Controls.Add(this.splitContainer1);
this.MinimumSize = new System.Drawing.Size(300, 300); this.MinimumSize = new System.Drawing.Size(300, 400);
this.Name = "ExampleLauncher"; this.Name = "ExampleLauncher";
this.Text = "OpenTK Example Launcher"; this.Text = "OpenTK Example Launcher";
this.Load += new System.EventHandler(this.ExampleLauncher_Load); this.Load += new System.EventHandler(this.ExampleLauncher_Load);

View file

@ -1,4 +1,4 @@
#region --- License --- #region --- License ---
/* Licensed under the MIT/X11 license. /* Licensed under the MIT/X11 license.
* Copyright (c) 2006-2008 the OpenTK Team. * Copyright (c) 2006-2008 the OpenTK Team.
* This notice may not be removed from any source distribution. * This notice may not be removed from any source distribution.
@ -37,7 +37,7 @@ namespace Examples.Tutorial
#region --- Constructor --- #region --- Constructor ---
public T03_Immediate_Mode_Cube() : base(800, 600, new GraphicsMode(16, 16)) public T03_Immediate_Mode_Cube() : base(800, 600, new GraphicsMode(32, 16))
{ } { }
#endregion #endregion

View file

@ -1,4 +1,4 @@
namespace Examples.WinForms namespace Examples.WinForms
{ {
partial class W01_First_Window partial class W01_First_Window
{ {
@ -69,13 +69,13 @@
// //
// glControl1 // glControl1
// //
this.glControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) this.glControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top)
| System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.glControl1.BackColor = System.Drawing.SystemColors.ControlDarkDark; this.glControl1.BackColor = System.Drawing.SystemColors.ControlDarkDark;
this.glControl1.Location = new System.Drawing.Point(1, 0); this.glControl1.Location = new System.Drawing.Point(10, 10);
this.glControl1.Name = "glControl1"; this.glControl1.Name = "glControl1";
this.glControl1.Size = new System.Drawing.Size(629, 565); this.glControl1.Size = new System.Drawing.Size(629, 225);
this.glControl1.TabIndex = 0; this.glControl1.TabIndex = 0;
this.glControl1.VSync = false; this.glControl1.VSync = false;
this.glControl1.Resize += new System.EventHandler(this.glControl1_Resize); this.glControl1.Resize += new System.EventHandler(this.glControl1_Resize);

View file

@ -1,4 +1,4 @@
#region --- License --- #region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos /* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info * See license.txt for license info
*/ */
@ -59,6 +59,8 @@ namespace Examples.WinForms
private void glControl1_Paint(object sender, PaintEventArgs e) private void glControl1_Paint(object sender, PaintEventArgs e)
{ {
System.Diagnostics.Debug.Print("Repainting glControl.");
GL.Clear(ClearBufferMask.ColorBufferBit); GL.Clear(ClearBufferMask.ColorBufferBit);
glControl1.SwapBuffers(); glControl1.SwapBuffers();
} }

View file

@ -1,4 +1,4 @@
#region --- License --- #region --- License ---
/* This source file is released under the MIT license. See License.txt for more information. /* This source file is released under the MIT license. See License.txt for more information.
* Coded by Erik Ylvisaker and Stefanos Apostolopoulos. * Coded by Erik Ylvisaker and Stefanos Apostolopoulos.
*/ */
@ -155,6 +155,7 @@ namespace Examples.WinForms
DrawCube(); DrawCube();
glControl.SwapBuffers(); glControl.SwapBuffers();
} }
#endregion #endregion

View file

@ -27,13 +27,26 @@ namespace OpenTK.Platform.MacOS
IntPtr contextRef; IntPtr contextRef;
bool mVSync = false; bool mVSync = false;
IntPtr displayID; IntPtr displayID;
GraphicsMode mode;
CarbonWindowInfo carbonWindow;
IGraphicsContext shareContext;
static AglContext() static AglContext()
{ {
if (GraphicsContext.GetCurrentContext == null) if (GraphicsContext.GetCurrentContext == null)
GraphicsContext.GetCurrentContext = AglContext.GetCurrentContext; GraphicsContext.GetCurrentContext = AglContext.GetCurrentContext;
} }
public AglContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext) public AglContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext)
{
this.mode = mode;
this.carbonWindow = (CarbonWindowInfo)window;
this.shareContext = shareContext;
CreateContext(mode, carbonWindow, shareContext);
}
void CreateContext(GraphicsMode mode, CarbonWindowInfo carbonWindow, IGraphicsContext shareContext)
{ {
int[] attributes = int[] attributes =
{ {
@ -53,10 +66,8 @@ namespace OpenTK.Platform.MacOS
(int)Agl.PixelFormatAttribute.AGL_NONE, (int)Agl.PixelFormatAttribute.AGL_NONE,
}; };
AGLContext myAGLContext = IntPtr.Zero;
AGLPixelFormat myAGLPixelFormat; AGLPixelFormat myAGLPixelFormat;
IntPtr shareContextRef = IntPtr.Zero; IntPtr shareContextRef = IntPtr.Zero;
CarbonWindowInfo carbonWindow = (CarbonWindowInfo)window;
// Choose a pixel format with the attributes we specified. // Choose a pixel format with the attributes we specified.
myAGLPixelFormat = Agl.aglChoosePixelFormat(IntPtr.Zero, 0, attributes); myAGLPixelFormat = Agl.aglChoosePixelFormat(IntPtr.Zero, 0, attributes);
@ -71,33 +82,25 @@ namespace OpenTK.Platform.MacOS
shareContextRef = ((AglContext)shareContext).contextRef; shareContextRef = ((AglContext)shareContext).contextRef;
// create the context and share it with the share reference. // create the context and share it with the share reference.
myAGLContext = Agl.aglCreateContext(myAGLPixelFormat, shareContextRef); this.contextRef = Agl.aglCreateContext(myAGLPixelFormat, shareContextRef);
MyAGLReportError(); MyAGLReportError();
// Free the pixel format from memory. // Free the pixel format from memory.
Agl.aglDestroyPixelFormat(myAGLPixelFormat); Agl.aglDestroyPixelFormat(myAGLPixelFormat);
MyAGLReportError(); MyAGLReportError();
IntPtr windowPort;
Debug.Print("IsControl: {0}", carbonWindow.IsControl); Debug.Print("IsControl: {0}", carbonWindow.IsControl);
if (carbonWindow.IsControl)
{ SetDrawable(carbonWindow);
IntPtr controlOwner = API.GetControlOwner(carbonWindow.WindowRef); SetBufferRect(carbonWindow);
Update(carbonWindow);
Debug.Print("GetControlOwner: {0}", controlOwner);
MakeCurrent(carbonWindow);
windowPort = API.GetWindowPort(controlOwner);
}
else
windowPort = API.GetWindowPort(carbonWindow.WindowRef);
windowPort = API.GetWindowPort(carbonWindow.WindowRef);
Agl.aglSetDrawable(myAGLContext, windowPort);
MyAGLReportError();
Debug.Print("context: {0}", contextRef);
}
void SetBufferRect(CarbonWindowInfo carbonWindow)
{
if (carbonWindow.IsControl) if (carbonWindow.IsControl)
{ {
Rect rect = API.GetControlBounds(carbonWindow.WindowRef); Rect rect = API.GetControlBounds(carbonWindow.WindowRef);
@ -114,50 +117,41 @@ namespace OpenTK.Platform.MacOS
glrect[2] = rect.Width; glrect[2] = rect.Width;
glrect[3] = rect.Height; glrect[3] = rect.Height;
Agl.aglSetInteger(myAGLContext, Agl.ParameterNames.AGL_BUFFER_RECT, glrect);
MyAGLReportError();
Agl.aglEnable(myAGLContext, Agl.ParameterNames.AGL_BUFFER_RECT);
MyAGLReportError();
}
//
MyAGLReportError();
MakeCurrent(window);
this.contextRef = myAGLContext;
Debug.Print("context: {0}", contextRef);
}
public void Update(IWindowInfo window)
{
CarbonWindowInfo carbonWindow = (CarbonWindowInfo)window;
if (carbonWindow.IsControl)
{
Rect rect = API.GetControlBounds(carbonWindow.WindowRef);
HIRect hirect = API.HIViewGetFrame(carbonWindow.WindowRef);
Debug.Print("Setting buffer_rect for control.");
Debug.Print("Rect: {0}", rect);
Debug.Print("HIRect: {0}", hirect);
int[] glrect = new int[4];
glrect[0] = rect.Left;
glrect[1] = rect.Top;
glrect[2] = rect.Width;
glrect[3] = rect.Height;
Agl.aglSetInteger(contextRef, Agl.ParameterNames.AGL_BUFFER_RECT, glrect); Agl.aglSetInteger(contextRef, Agl.ParameterNames.AGL_BUFFER_RECT, glrect);
MyAGLReportError(); MyAGLReportError();
Agl.aglEnable(contextRef, Agl.ParameterNames.AGL_BUFFER_RECT);
MyAGLReportError();
}
}
void SetDrawable(CarbonWindowInfo carbonWindow)
{
IntPtr windowPort;
if (carbonWindow.IsControl)
{
IntPtr controlOwner = API.GetControlOwner(carbonWindow.WindowRef);
Debug.Print("GetControlOwner: {0}", controlOwner);
windowPort = API.GetWindowPort(controlOwner);
} }
else
windowPort = API.GetWindowPort(carbonWindow.WindowRef);
Agl.aglSetDrawable(contextRef, windowPort);
MyAGLReportError();
}
public void Update(IWindowInfo window)
{
CarbonWindowInfo carbonWindow = (CarbonWindowInfo)window;
SetBufferRect(carbonWindow);
//Agl.aglSetCurrentContext(contextRef); //Agl.aglSetCurrentContext(contextRef);
Agl.aglUpdateContext(contextRef); Agl.aglUpdateContext(contextRef);
//Agl.aglSetDrawable(contextRef, API.GetWindowPort(carbonWindow.WindowRef));
} }
void MyAGLReportError() void MyAGLReportError()
@ -174,13 +168,21 @@ namespace OpenTK.Platform.MacOS
} }
#region IGraphicsContext Members #region IGraphicsContext Members
bool first = false;
public void SwapBuffers() public void SwapBuffers()
{ {
if (first == false && carbonWindow.IsControl)
{
Debug.WriteLine("--> Resetting drawable. <--");
first = true;
SetDrawable(carbonWindow);
Update(carbonWindow);
}
Agl.aglSwapBuffers(contextRef); Agl.aglSwapBuffers(contextRef);
MyAGLReportError(); MyAGLReportError();
} }
public void MakeCurrent(IWindowInfo window) public void MakeCurrent(IWindowInfo window)
{ {
if (Agl.aglSetCurrentContext(contextRef) == false) if (Agl.aglSetCurrentContext(contextRef) == false)

View file

@ -29,7 +29,7 @@ namespace OpenTK.Platform.MacOS.Carbon
internal static void Initialize() internal static void Initialize()
{ {
if (mInitialized) return; if (mInitialized) return;
API.AcquireRootMenu(); API.AcquireRootMenu();
ConnectEvents(); ConnectEvents();

View file

@ -25,9 +25,15 @@ namespace OpenTK.Platform.MacOS
return new GraphicsContext(mode, WindowInfo); return new GraphicsContext(mode, WindowInfo);
} }
// TODO: Fix this
bool lastIsIdle = false;
public bool IsIdle public bool IsIdle
{ {
get { return true; } get
{
lastIsIdle = !lastIsIdle;
return lastIsIdle;
}
} }
public IWindowInfo WindowInfo public IWindowInfo WindowInfo