我的开发环境是VS2008——VC++
一、准备工作
1、下载OpenGl SDK ,CSDN中就有
2、释放后,在“工具|选项”中设置“项目和解决方案|VC++目录”
在“包含文件”与“库文件”中添加上OpenGl SDK的相关目录。
这样在你的程序中就可以用上#include<gl.h>,#include<glu.h>,#include<glaux.h>了
3、在你的stdafx.h中加上
#pragma comment( lib, "opengl32.lib")
#pragma comment( lib, "glu32.lib")
#pragma comment( lib, "glaux.lib")
然后就可以在自己的项目中调用OpenGl了。
二、创建一个支持OpenGL的窗口
必备的几个对象
HDC hDC=NULL;// 图形设备环境
HGLRC hRC=NULL;// OpenGl的展示环境
HWND hWnd=NULL;// Holds Our Window Handle
HINSTANCE hInstance;// Holds The Instance Of The Application
1、声明一个窗口类结构的对象,然后注册它,最后建立一个窗口
WNDCLASS wc;// Windows Class Structure
……//填写wc的过程略,如何一步步建立窗口的教程里都有。
RegisterClass(&wc); // To Register The Window Class
……//生成一个标准窗口的细节步骤,相关教程里很清楚
(hWnd=CreateWindowEx(……); //我实验的结果用CreateWindow();也不影响对OpenGL的支持
2、让生成的标准窗口支持OpenGL!
static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR),// Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW |// Format Must Support Window
PFD_SUPPORT_OPENGL |// Format Must Support OpenGL——就是这里了。
PFD_DOUBLEBUFFER,// Must Support Double Buffering
PFD_TYPE_RGBA,// Request An RGBA Format
bits, // Select Our Color Depth
0, 0, 0, 0, 0, 0,// Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE,// Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
hDC=GetDC(hWnd))) // To Get A Device Context(得到窗口的设备环境(入口)——一般说是设备上下文,我觉得“上下文”让人莫名其妙)
PixelFormat=ChoosePixelFormat(hDC,&pfd); // To Find A Matching Pixel Format
SetPixelFormat(hDC,PixelFormat,&pfd); // To Set The Pixel Format
hRC=wglCreateContext(hDC); // To Get A Rendering Context for OpenGL 得到一个OpenGL的展示环境(入口?权限)
wglMakeCurrent(hDC,hRC);// To Activate The Rendering Context
ShowWindow(hWnd,SW_SHOW);// Show The Window
.............
好一个OpenGL表演的窗口就完成了。