我正在C#中調用一個C++ dll的基本程序。我將pannel的句柄交給dll,以便OpenGL知道在哪裏繪製。OpenGL不顯示/繪製我的C#pannel
[DllImport(@"../../../Debug/Model.DLL")]
public static extern void startOpenGL(IntPtr hWindow);
I mashall IntPtr hWindow to HWND hWindow。
我打電話後,借鑑C#
在C++中我有一個類接口的作品(測試過)調用我的OpenGLManager的方法。
#pragma once
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "singleton.h"
class OpenGLManager
{
public:
static OpenGLManager* instance()
{
if (p_theInstance == 0)
p_theInstance = new OpenGLManager;
return p_theInstance;
}
void init(HWND hWnd);
void purge();
void reset();
void showSomething();
private:
HWND mhWnd;
HDC mhDC;
HGLRC mhRC;
static OpenGLManager* p_theInstance;
OpenGLManager(void);
~OpenGLManager(void);
};
在.cpp
#include "OpenGLManager.h"
#include <stdio.h>
OpenGLManager* OpenGLManager::p_theInstance = 0;
OpenGLManager::OpenGLManager(void)
{
reset();
}
OpenGLManager::~OpenGLManager(void)
{
purge();
}
void OpenGLManager::showSomething()
{
//MessageBox(NULL,"DRAW","DRAW", MB_OK);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f, -0.5f, -4.0f); // lower left vertex
glVertex3f(1.0f, -0.5f, -4.0f); // lower right vertex
glVertex3f(0.0f, 0.5f, -4.0f); // upper vertex
glEnd();
glFlush();
}
void OpenGLManager::init(HWND hWnd)
{
// remember the window handle (HWND)
OpenGLManager::mhWnd = hWnd;
// get the device context (DC)
OpenGLManager::mhDC = GetDC(OpenGLManager::mhWnd);
// set the pixel format for the DC
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
int format = ChoosePixelFormat(OpenGLManager::mhDC, &pfd);
SetPixelFormat(OpenGLManager::mhDC, format, &pfd);
// create the render context (RC)
OpenGLManager::mhRC = wglCreateContext(OpenGLManager::mhDC);
// make it the current render context
wglMakeCurrent(OpenGLManager::mhDC, OpenGLManager::mhRC);
}
void OpenGLManager::purge()
{
if (OpenGLManager::mhRC)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(OpenGLManager::mhRC);
}
if (OpenGLManager::mhWnd && OpenGLManager::mhDC)
{
ReleaseDC(OpenGLManager::mhWnd, OpenGLManager::mhDC);
}
reset();
}
void OpenGLManager::reset()
{
OpenGLManager::mhWnd = NULL;
OpenGLManager::mhDC = NULL;
OpenGLManager::mhRC = NULL;
}
我看到ShowSomething方法與提示消息框通常被稱爲但是我看到我的C#潘內爾什麼。 (對不起,縮進!)
我同意。使用Tao框架讓你的生活變得如此簡單。對於C#/ WPF,您可能需要查看http://www.codeproject.com/KB/WPF/WPFOpenGL.aspx,瞭解基於Tao和非Tao的解決方案。 – mnemosyn 2010-11-23 12:00:18