I start a project and make a form with a single panel in it and try to make it work with the below code. All it should do is create a form with a panel in it and attach a rendering context to the panel and blank it. I tried to convert it from a working c++ nehe project although for the life of me i can not get the "m_uint_RC = OpenGL.Win32.wgl.CreateContext(m_uint_DC);" to create the rendering context. What am i doing wrong?
Thanks in advance,
Peet
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace AttemptAtGettingOpenGLGoing {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AttemptAtGettingOpenGLGoing {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.Load += new EventHandler(InitializeGL);
this.SizeChanged += new EventHandler(ResizeGL);
}
Questions?
I start a project and make a form with a single panel in it and try to make it work with the below code. All it should do is create a form with a panel in it and attach a rendering context to the panel and blank it. I tried to convert it from a working c++ nehe project although for the life of me i can not get the "m_uint_RC = OpenGL.Win32.wgl.CreateContext(m_uint_DC);" to create the rendering context. What am i doing wrong?
Thanks in advance,
Peet
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace AttemptAtGettingOpenGLGoing {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AttemptAtGettingOpenGLGoing {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.Load += new EventHandler(InitializeGL);
this.SizeChanged += new EventHandler(ResizeGL);
}
IntPtr m_uint_DC = (IntPtr)0;
IntPtr m_uint_HWND = (IntPtr)0;
IntPtr m_uint_RC = (IntPtr)0;
private void panel1_Paint(object sender, PaintEventArgs e) {
// To go in
if (m_uint_DC == (IntPtr)0 || m_uint_RC == (IntPtr)0)
return;
OpenGL.gl.Clear(OpenGL.ClearBufferMask.ColorBufferBit | OpenGL.ClearBufferMask.DepthBufferBit);
OpenGL.gl.LoadIdentity();
OpenGL.gl.Flush();
OpenGL.Win32.Gdi.SwapBuffers(m_uint_DC);
}
protected virtual void InitializeGL(object sender, EventArgs e) {
unsafe {
m_uint_HWND = (System.IntPtr)this.Handle.ToPointer();
m_uint_DC = OpenGL.Win32.Native.GetDC(m_uint_HWND);
OpenGL.Win32.Gdi.SwapBuffers(m_uint_DC);
OpenGL.Win32.Gdi.PixelFormatDescriptor pfd = new OpenGL.Win32.Gdi.PixelFormatDescriptor();
// WGL.ZeroPixelDescriptor(ref pfd);
pfd.Version = 1;
pfd.Flags = OpenGL.Win32.Gdi.PixelFormatDescriptorFlags.DrawToWindow | OpenGL.Win32.Gdi.PixelFormatDescriptorFlags.SupportOpenGL | OpenGL.Win32.Gdi.PixelFormatDescriptorFlags.DoubleBuffer;
pfd.PixelType = OpenGL.Win32.Gdi.PixelType.Rgba;
pfd.ColorBits = 32;
pfd.DepthBits = 32;
pfd.LayerType = (byte)OpenGL.Win32.wgl.Layer.Main;
int pixelFormatIndex = 0;
pixelFormatIndex = OpenGL.Win32.Gdi.ChoosePixelFormat(m_uint_DC, ref pfd);
if (pixelFormatIndex == 0) {
MessageBox.Show("Unable to retrieve pixel format");
return;
}
if(OpenGL.Win32.Gdi.SetPixelFormat(m_uint_DC, pixelFormatIndex, ref pfd)== false) {
MessageBox.Show("Unable to set pixel format");
return;
}
//Create rendering context
m_uint_RC = OpenGL.Win32.wgl.CreateContext(m_uint_DC);
if (m_uint_RC == (IntPtr)0) {
MessageBox.Show("Unable to get rendering context");
return;
}
if (OpenGL.Win32.wgl.MakeCurrent(m_uint_DC, m_uint_RC) == false) {
MessageBox.Show("Unable to make rendering context current");
return;
}
ResizeGL(null,null);
OpenGL.gl.ClearColor(0,0,0,0.5f);
OpenGL.gl.Enable(OpenGL.Enable.DepthTest);
OpenGL.gl.DepthFunc(OpenGL.DepthFunction.Lequal);
OpenGL.gl.CullFace(OpenGL.CullFaceMode.Back);
}
}
protected virtual void ResizeGL(object sender, EventArgs e){
if (m_uint_DC == (IntPtr)0 || m_uint_RC == (IntPtr)0)
return;
if (panel1.Width == 0 || panel1.Height == 0)
return;
OpenGL.Win32.wgl.MakeCurrent(m_uint_DC, m_uint_RC);
OpenGL.gl.Viewport(0, 0, panel1.Width, panel1.Height);
OpenGL.gl.MatrixMode(OpenGL.MatrixMode.Projection);
OpenGL.gl.LoadIdentity();
OpenGL.glu.Perspective(60.0, ((double)(panel1.Width) / (double)(panel1.Height)), 1.0, 1000.0);
OpenGL.gl.MatrixMode(OpenGL.MatrixMode.Modelview);
OpenGL.gl.LoadIdentity();
}
}
}
This should be addressed in the sample =) Please let me know if it is not.