也许是我用google的水平太菜,到现在为止也没有找到用于Win32SDK开发的基于对话框的OpenGL程序,google的搜索结果基本都是关于 用MFC做OpenGL程序,或者什么C++的,GLUT的!对以我这等MFC盲、C++盲,难道不用glut等库、仅C开发不了OpenGL程序?非 也,参考wxdevcpp的sample下的opengl代码,我写了如下的程序(若有抄袭之嫌,请通知我),貌似很有意思。
//----------------------------------------------------------------------------------------------------------------
//主题风格文件xpmanifest.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="*"
name="Company.Product.Name"
type="win32"
/>
<description></description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
//----------------------------------------------------------------------------------------------------------------
//资源文件glWin.rc
#define IDC_GRP1 1003
#define IDC_BTN1 1004
#define IDC_BTN2 1005
#define IDC_BTN3 1006
#define IDD_DLG1 1000
#define IDC_IMG1 1001
#define IDC_SBR1 1002
#define IDC_GRP2 1003
#define IDC_BTN4 1004
#define IDC_BTN5 1005
#define IDC_BTN6 1006
#define IDI_ICON1 100
#include <windows.h>
IDD_DLG1 DIALOGEX 3,18,360,261
CAPTION "glWin"
FONT 8,"Tahoma",0,0,0
STYLE WS_VISIBLE|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|DS_CENTER
BEGIN
CONTROL "",IDC_IMG1,"Static",WS_CHILDWINDOW|WS_VISIBLE|SS_SUNKEN|SS_CENTERIMAGE|SS_BITMAP,3,3,255,243
CONTROL "message: 绘制三角形",IDC_SBR1,"msctls_statusbar32",WS_CHILDWINDOW|WS_VISIBLE|CCS_BOTTOM,51,245,54,15
CONTROL "",IDC_GRP2,"Button",WS_CHILDWINDOW|WS_VISIBLE|BS_GROUPBOX,261,-1,96,247
CONTROL "绘制三角形",IDC_BTN4,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,267,9,82,18
CONTROL "绘制四棱锥",IDC_BTN5,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,267,31,82,18
CONTROL "绘制立方体",IDC_BTN6,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,267,54,82,18
END
IDI_ICON1 ICON DISCARDABLE "icon1.ico"
#ifdef __GNUC__
#define MANIFEST 24
#define IDR_XPMANIFEST1 1
IDR_XPMANIFEST1 MANIFEST "xpmanifest.xml"
#endif
//----------------------------------------------------------------------------------------------------------------
//源程序glwin.rc
//基于Win32 SDK对话框的OpenGL程序
//21:38 2011-3-15
//notepad++,msys(gcc4.5.2)
#include <windows.h>
#include <Commctrl.h>
#include <gl/gl.h>
#define IDC_GRP1 1003
#define IDC_BTN1 1004
#define IDC_BTN2 1005
#define IDC_BTN3 1006
#define IDD_DLG1 1000
#define IDC_IMG1 1001
#define IDC_SBR1 1002
#define IDC_GRP2 1003
#define IDC_BTN4 1004
#define IDC_BTN5 1005
#define IDC_BTN6 1006
#define IDI_ICON1 100
VOID EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC )
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
// get the device context (DC)
*hDC = GetDC( hWnd );
// set the pixel format for the DC
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;
iFormat = ChoosePixelFormat( *hDC, &pfd );
SetPixelFormat( *hDC, iFormat, &pfd );
// create and enable the render context (RC)
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
// Disable OpenGL
VOID DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC )
{
wglMakeCurrent( NULL, NULL );
wglDeleteContext( hRC );
ReleaseDC( hWnd, hDC );
}
// Initialize OpenGL
VOID InitOpenGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST|GL_POINT_SMOOTH);
glShadeModel(GL_SMOOTH|GL_FLAT );
glMatrixMode(GL_PROJECTION|GL_MODELVIEW);
}
//drawing the pyramid
VOID DrawFunc1()
{
glBegin(GL_TRIANGLES); // Draw using triangles
glColor3f(0.5f, 0.0f, 0.0f); // Red
glVertex3f( 0.0f, 0.5f, 0.0f); // Top of triangle (Front)
glColor3f(0.0f, 0.5f, 0.0f); // Green
glVertex3f(-0.5f, -0.5f, 0.5f); // Left of triangle (Front)
glColor3f(0.0f, 0.0f, 0.5f); // Blue
glVertex3f(0.5f, -0.5f, 0.5f); // Right of triangle (Front)
glColor3f(0.5f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 0.5f, 0.0f); // Top of triangle (Right)
glColor3f(0.0f, 0.0f, 0.5f); // Blue
glVertex3f(0.5f, -0.5f, 0.5f); // Left of triangle (Right)
glColor3f(0.0f, 0.5f, 0.0f); // Green
glVertex3f(0.5f, -0.5f, -0.5f); // Right of triangle (Right)
glColor3f(0.5f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 0.5f, 0.0f); // Top of triangle (Back)
glColor3f(0.0f, 0.5f, 0.0f); // Green
glVertex3f(0.5f, -0.5f, -0.5f); // Left of triangle (Back)
glColor3f(0.0f, 0.0f, 0.5f); // Blue
glVertex3f(-0.5f, -0.5f, -0.5f); // Right of triangle (Back)
glColor3f(0.5f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 0.5f, 0.0f); // Top of triangle (Left)
glColor3f(0.0f, 0.0f, 0.5f); // Blue
glVertex3f(-0.5f, -0.5f, -0.5f); // Left of triangle (Left)
glColor3f(0.0f, 0.5f, 0.0f); // Green
glVertex3f(-0.5f, -0.5f, 0.5f); // Right of triangle (Left)
glEnd(); // Done drawing the pyramid
glFlush();
}
//drawing the color cube
VOID DrawFunc2()
{
double
xmin =-0.5,
xmax = 0.5,
ymin = -0.5,
ymax = 0.5,
zmin =-0.5,
zmax =0.5;
glBegin (GL_POLYGON);
glColor3d(0.5,0.8,0.2);
glVertex3d(xmin,ymin,zmin);
glVertex3d(xmin,ymin,zmax);
glVertex3d(xmin,ymin,zmax);
glVertex3d(xmin,ymax,zmax);
glVertex3d(xmin,ymax,zmax);
glVertex3d(xmin,ymax,zmin);
glVertex3d(xmin,ymax,zmin);
glVertex3d(xmin,ymin,zmin);
glColor3d(0.2,0.5,0.8);
glVertex3d(xmin,ymin,zmin);
glVertex3d(xmin,ymin,zmax);
glVertex3d(xmin,ymin,zmax);
glVertex3d(xmax,ymin,zmax);
glVertex3d(xmax,ymin,zmax);
glVertex3d(xmax,ymin,zmin);
glVertex3d(xmax,ymin,zmin);
glVertex3d(xmin,ymin,zmin);
glColor3d(0.8,0.2,0.5);
glVertex3d(xmin,ymin,zmin);
glVertex3d(xmin,ymax,zmin);
glVertex3d(xmin,ymax,zmin);
glVertex3d(xmax,ymax,zmin);
glVertex3d(xmax,ymax,zmin);
glVertex3d(xmax,ymin,zmin);
glVertex3d(xmax,ymin,zmin);
glVertex3d(xmin,ymin,zmin);
glEnd();
glBegin (GL_LINES);
glColor3d(0.8,0.8,0.8);
glVertex3d(xmax,ymax,zmax);
glVertex3d(xmin,ymax,zmax);
glVertex3d(xmax,ymax,zmax);
glVertex3d(xmax,ymin,zmax);
glVertex3d(xmax,ymax,zmax);
glVertex3d(xmax,ymax,zmin);
glEnd();
glFlush();
}
VOID DrawFunc0()
{
glBegin(GL_TRIANGLES);
glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.0f, 1.0f );
glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 0.87f, -0.5f );
glColor3f( 0.0f, 0.0f, 1.0f ); glVertex2f( -0.87f, -0.5f );
glEnd();
glFlush();
}
typedef VOID (*PDRAWFUNC)();
// OpenGL animation code goes here
VOID GLDrawFunc(PDRAWFUNC func)
{
static float fRotAngle = 0.0f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(fRotAngle, 1.0f, 1.0f, 1.0f);
{
func();
}
fRotAngle += 0.5f;
}
//窗口事件处理程序
BOOL WINAPI DlgProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
static HDC hDC;
static HGLRC hRC;
static PDRAWFUNC ptrDrawFunc=NULL;
switch(uMsg)
{
case WM_INITDIALOG://窗口初始化事件
//设置窗口图标
SendMessageA(hDlg,WM_SETICON,ICON_SMALL,(LPARAM)LoadIcon(*(HINSTANCE*)lParam,MAKEINTRESOURCE(IDI_ICON1)));
EnableOpenGL( GetDlgItem(hDlg,IDC_IMG1), &hDC, &hRC );
InitOpenGL() ;
ptrDrawFunc=DrawFunc0;
SetTimer(hDlg,0x1234,10,NULL);
break;
case WM_CLOSE://窗口关闭事件
DisableOpenGL( GetDlgItem(hDlg,IDC_IMG1), hDC, hRC );
hDC=NULL;
EndDialog(hDlg,0);
break;
case WM_TIMER:
{
GLDrawFunc(ptrDrawFunc);
SwapBuffers( hDC );
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BTN1:
ptrDrawFunc=DrawFunc0;
SendDlgItemMessageA(hDlg,IDC_SBR1,WM_SETTEXT,0,(LPARAM)"message: 绘制三角形");
break;
case IDC_BTN2:
ptrDrawFunc=DrawFunc1;
SendDlgItemMessageA(hDlg,IDC_SBR1,WM_SETTEXT,0,(LPARAM)"message: 绘制四棱锥");
break;
case IDC_BTN3:
ptrDrawFunc=DrawFunc2;
SendDlgItemMessageA(hDlg,IDC_SBR1,WM_SETTEXT,0,(LPARAM)"message: 绘制立方体");
break;
}
break;
default:
break;
}
return FALSE;
}
INT APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpCmdLine,INT nShowCmd)
{
InitCommonControls();//WinXP必须要有,Win2003SP2DC则不需要
//启动对话框,并进入窗口事件处理循环
DialogBoxParamA(hInstance,MAKEINTRESOURCE(IDD_DLG1),NULL,(DLGPROC)DlgProc,(LPARAM)&hInstance);
return 0;
}
//----------------------------------------------------------------------------------------------------------------
这个程序的相似程序,我刚在codeproject上看到(20110318)
//----------------------------------------------------------------------------------------------------------------
msys(gcc4.5.2)编译命令行:
windres -coff -i glWin.rc -o glwin.o
gcc glwin.c glwin.o -o glwin -lcomctl32 -lopengl32 -lglu32 -mwindows
strip glwin.exe