在VS2008的MSDN中有一个标准的OpenGL例子,记录如下:

 /*
* Example of a Win32 OpenGL program.
* The OpenGL code is the same as that used in
* the X Window System sample
*/
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h> /* Windows globals, defines, and prototypes */
CHAR szAppName[]="Win OpenGL";
HWND ghWnd;
HDC ghDC;
HGLRC ghRC; #define SWAPBUFFERS SwapBuffers(ghDC)
#define BLACK_INDEX 0
#define RED_INDEX 13
#define GREEN_INDEX 14
#define BLUE_INDEX 16
#define WIDTH 640
#define HEIGHT 480 LONG WINAPI MainWndProc (HWND, UINT, WPARAM, LPARAM);
BOOL bSetupPixelFormat(HDC); /* OpenGL globals, defines, and prototypes */
GLfloat latitude, longitude, latinc, longinc;
GLdouble radius; #define GLOBE 1
#define CYLINDER 2
#define CONE 3 GLvoid resize(GLsizei, GLsizei);
GLvoid initializeGL(GLsizei, GLsizei);
GLvoid drawScene(GLvoid);
void polarView( GLdouble, GLdouble, GLdouble, GLdouble); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wndclass; /* Register the frame class */
wndclass.style = ;
wndclass.lpfnWndProc = (WNDPROC)MainWndProc;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (hInstance, szAppName);
wndclass.hCursor = LoadCursor (NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName; if (!RegisterClass (&wndclass) )
return FALSE; /* Create the frame */
ghWnd = CreateWindow (szAppName,
"Generic OpenGL Sample",
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
WIDTH,
HEIGHT,
NULL,
NULL,
hInstance,
NULL); /* make sure window was created */
if (!ghWnd)
return FALSE; /* show and update main window */
ShowWindow (ghWnd, nCmdShow); UpdateWindow (ghWnd); /* animation loop */
while ()
{
/*
* Process all pending messages
*/ while (PeekMessage(&msg, NULL, , , PM_NOREMOVE) == TRUE)
{
if (GetMessage(&msg, NULL, , ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
return TRUE;
}
}
drawScene();
Sleep();
}
} /* main window procedure */
LONG WINAPI MainWndProc (
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
LONG lRet = ;
PAINTSTRUCT ps;
RECT rect; switch (uMsg)
{ case WM_CREATE:
ghDC = GetDC(hWnd);
if (!bSetupPixelFormat(ghDC))
PostQuitMessage (); ghRC = wglCreateContext(ghDC);
wglMakeCurrent(ghDC, ghRC);
GetClientRect(hWnd, &rect);
initializeGL(rect.right, rect.bottom);
break; case WM_PAINT:
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break; case WM_SIZE:
GetClientRect(hWnd, &rect);
resize(rect.right, rect.bottom);
break; case WM_CLOSE:
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(hWnd, ghDC);
ghRC = ;
ghDC = ; DestroyWindow (hWnd);
break; case WM_DESTROY:
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(hWnd, ghDC); PostQuitMessage ();
break; case WM_KEYDOWN:
switch (wParam)
{
case VK_LEFT:
longinc += 0.5F;
break;
case VK_RIGHT:
longinc -= 0.5F;
break;
case VK_UP:
latinc += 0.5F;
break;
case VK_DOWN:
latinc -= 0.5F;
break;
} default:
lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
break;
} return lRet;
} BOOL bSetupPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd, *ppfd;
int pixelformat; ppfd = &pfd; ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
ppfd->nVersion = ;
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
ppfd->dwLayerMask = PFD_MAIN_PLANE;
ppfd->iPixelType = PFD_TYPE_COLORINDEX;
ppfd->cColorBits = ;
ppfd->cDepthBits = ;
ppfd->cAccumBits = ;
ppfd->cStencilBits = ; pixelformat = ChoosePixelFormat(hdc, ppfd); if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == )
{
MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
return FALSE;
} if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)
{
MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
return FALSE;
} return TRUE;
} /* OpenGL code */ GLvoid resize( GLsizei width, GLsizei height )
{
GLfloat aspect; glViewport( , , width, height ); aspect = (GLfloat) width / height; glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, aspect, 3.0, 7.0 );
glMatrixMode( GL_MODELVIEW );
} GLvoid createObjects()
{
GLUquadricObj *quadObj; glNewList(GLOBE, GL_COMPILE);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_LINE);
gluSphere (quadObj, 1.5, , );
glEndList(); glNewList(CONE, GL_COMPILE);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_FILL);
gluQuadricNormals (quadObj, GLU_SMOOTH);
gluCylinder(quadObj, 0.3, 0.0, 0.6, , );
glEndList(); glNewList(CYLINDER, GL_COMPILE);
glPushMatrix ();
glRotatef ((GLfloat)90.0, (GLfloat)1.0, (GLfloat)0.0, (GLfloat)0.0);
glTranslatef ((GLfloat)0.0, (GLfloat)0.0, (GLfloat)-1.0);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_FILL);
gluQuadricNormals (quadObj, GLU_SMOOTH);
gluCylinder (quadObj, 0.3, 0.3, 0.6, , );
glPopMatrix ();
glEndList();
} GLvoid initializeGL(GLsizei width, GLsizei height)
{
GLfloat maxObjectSize, aspect;
GLdouble near_plane, far_plane; glClearIndex( (GLfloat)BLACK_INDEX);
glClearDepth( 1.0 ); glEnable(GL_DEPTH_TEST); glMatrixMode( GL_PROJECTION );
aspect = (GLfloat) width / height;
gluPerspective( 45.0, aspect, 3.0, 7.0 );
glMatrixMode( GL_MODELVIEW ); near_plane = 3.0;
far_plane = 7.0;
maxObjectSize = 3.0F;
radius = near_plane + maxObjectSize/2.0; latitude = 0.0F;
longitude = 0.0F;
latinc = 6.0F;
longinc = 2.5F; createObjects();
} void polarView(GLdouble radius, GLdouble twist, GLdouble latitude,
GLdouble longitude)
{
glTranslated(0.0, 0.0, -radius);
glRotated(-twist, 0.0, 0.0, 1.0);
glRotated(-latitude, 1.0, 0.0, 0.0);
glRotated(longitude, 0.0, 0.0, 1.0); } GLvoid drawScene(GLvoid)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); latitude += latinc;
longitude += longinc; polarView( radius, , latitude, longitude ); // glIndexi(RED_INDEX);
glColor3f(,,);
glCallList(CONE); // glIndexi(BLUE_INDEX);
glColor3f(,,);
glCallList(GLOBE); // glIndexi(GREEN_INDEX);
glColor3f(,,);
glPushMatrix();
glTranslatef(0.8F, -0.65F, 0.0F);
glRotatef(30.0F, 1.0F, 0.5F, 1.0F);
glCallList(CYLINDER);
glPopMatrix(); glPopMatrix(); SWAPBUFFERS;
}

Win32 OpenGL标准例子的更多相关文章

  1. Win32 OpenGL 编程( 1 ) Win32 下的 OpenGL 编程必须步骤

    http://blog.csdn.net/vagrxie/article/details/4602961 Win32 OpenGL 编程( 1 ) Win32 下的 OpenGL 编程必须步骤 wri ...

  2. VC++ 多线程编程,win32,MFC 例子(转)

    一.问题的提出 编写一个耗时的单线程程序: 新建一个基于对话框的应用程序SingleThread,在主对话框IDD_SINGLETHREAD_DIALOG添加一个按钮,ID为IDC_SLEEP_SIX ...

  3. 【转】Backbone标准例子——通讯录

    参考:http://z2009zxiaolong.iteye.com/blog/1847833 感觉不错的例子,模型.视图.路由等知识点都用到了:),将此文中的源码转载如下: http://dmyz. ...

  4. OpenGL编程指南第版本学习笔记 --- OpenGL程序实现过程(win32 + OpenGL)

    1. 先上代码 头文件glCommon.h #include <GL/glew.h> #include <GL/GL.h> #include <GL/GLU.h> ...

  5. C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 树形选择项目的标准例子

    用成套的现成的方法引导大家开发程序,整个团队的开发效率会很高.例如我们现在有30多个开发人员,若有300个开发人员,这开发工作很容易乱套,我们需要有效的管理维护所有团队的开发工作.把数据结构.通用的组 ...

  6. 隔行换色(WPF DataGrid 标准例子)

     <DataGrid AlternationCount="2">             <DataGrid.RowStyle>               ...

  7. OpenGL “太阳、地球和月亮”天体运动动画 例子

    http://oulehui.blog.163.com/blog/static/7961469820119186616743/ OpenGL “太阳.地球和月亮”天体运动动画 例子 2011-10-1 ...

  8. opengl入门学习

    OpenGL入门学习 说起编程作图,大概还有很多人想起TC的#include <graphics.h>吧? 但是各位是否想过,那些画面绚丽的PC游戏是如何编写出来的?就靠TC那可怜的640 ...

  9. OpenGL入门学习(转)

    OpenGL入门学习 http://www.cppblog.com/doing5552/archive/2009/01/08/71532.html 说起编程作图,大概还有很多人想起TC的#includ ...

随机推荐

  1. EASYUI 表单(FORM)用法

    提交表单 $('#addform').form('submit', { url: '/Admin/AdminUser/AddAdminUser', onSubmit: function () { re ...

  2. Mybatis 批量更新 ORA-00911: 无效字符的错误

    使用<foreach></foreach> 批量insert时报错 ORA-00911: 无效字符的错误 <foreach collection="list&q ...

  3. ORACLE10gRAC数据库迁移至10gRAC

    1.数据库备份RUN {ALLOCATE CHANNEL ch00 DEVICE TYPE disk;ALLOCATE CHANNEL ch01 DEVICE TYPE disk;ALLOCATE C ...

  4. Linux学习笔记之——安装虚拟机后,如何启用网卡

    版本:CentOS-6.5-i386-minimal 虚拟机:vmware 11.1.2   安装完之后是看不到网卡信息的,如下:         我们编辑网卡etho的配置信息:         将 ...

  5. .net MVC3 页面和 action 传值问题

    一.ViewData ViewData ViewBag 的特点和使用场景比较 1.  TempData:类型是字典的键值对结构 特点:值只能取一次.保存在Session中,Controller每次执行 ...

  6. RIO-SEIO水泵流量表

    http://rio-seio.com/TW/products_pumps/Rio_plus.html 流量表:

  7. 如何使用 aspnetpager

    <%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix=" ...

  8. dubbo+zookeeper集群配置

    集群服务注册到多台zookeeper配置: <dubbo:registry protocol="zookeeper" address="10.20.153.10:2 ...

  9. eclipse 引用项目(转)

    1. 项目右键 -->Properties 2. Java Build Path  a) Projects --> Add  b) Required Project Selection - ...

  10. java路径问题

    使用了java这么久一直对java获取路径存在困惑,将一些常用的获取路径方式记录如下: val property = System.getProperty("user.dir")) ...