在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. problem during schema create,statement create sequence act_evt_log_seq

    今天在调试程序的时候出现"problem during schema create,statement create sequence act_evt_log_seq"这个错误,跟 ...

  2. Android 通过广播获取网络状态

    Android系统网络连接状态的改变会发一个广播,注册一个广播接收者,实时动态的检测网络状态,及时提醒用户,优化用户体验.          本文仅提供WIFI 状态的检测作为参考,其他网络连接方式请 ...

  3. [转]基于AWS的自动化部署实践

    作者 徐桂林 发布于 2014年1月22日 -------------------------------------------------------------------- 1. 背景 在过去 ...

  4. flash的读写与擦除

    对于flash的认识,比较肤浅,在网上找了些资料,感谢 http://blog.csdn.net/lin364812726/article/details/18815395  的博主, 将其博文转载过 ...

  5. Ambari 不能配置 Kafka 监听host的问题

    问题:Ambari下Kafka多IP监听配置 环境:Ambari 1.7.0 , Hadoop 2.2 Kafka 0.8.1.2.2.0.0 现象: Ambari 中是不能配置Kafka的host. ...

  6. Stopwatch 类

    Stopwatch 为计时器的实现. 主要属性方法 属性和方法 说明 static GetTimestamp() 如果Stopwatch使用高分辨率的性能计数器,则返回该计数器的当前值:如果Stopw ...

  7. Tiny6410 LCD设置

    1.注意LCD的硬件连接 2.LCD初始化 2.1 初始化步骤 LCD时序设置 LCD芯片 2.2 引脚初始化 2.3 配置 MIFPCON 寄存器及SPCON 寄存器 2.4 配置VIDCONx 2 ...

  8. python 脚本中使用了第三方openpyxl 打包程序运行提示ImportError:cannot import name __version__

    最近写了一个脚本,脚本中使用了第三方openpyxl(openpyxl是使用 pip install openpyxl 下载的),先是使用py2exe打包程序,打包过程中提示很多文件没有包含,在没有仔 ...

  9. oracle基础语法大全

    -----创建序列create sequence book_idINCREMENT BY 1 -- 每次加几个 START WITH 001 -- 从1开始计数 NOMAXVALUE -- 不设置最大 ...

  10. 使用Asp.net WebAPI 快速构建后台数据接口

    现在的互联网应用,无论是web应用,还是移动APP,基本都需要实现非常多的数据访问接口.其实对一些轻应用来说Asp.net WebAPI是一个很快捷简单并且易于维护的后台数据接口框架.下面我们来快速构 ...