当前位置:首页 >> 高中作文 >>

代码


//头文件------------------------------------------------------------------------#include <windows.h> //Windows 头文件 #include "resource.h" //资源头文件 #include <gl/gl.h> //OpenGL

核心库头文件 #include <gl/glu.h> //OpenGL 实用库头文件 int bAnimate=0; GLfloat xRotation=0.0,yRotation=0.0,zRotation=0.0;// ----------------------------------------------------------------------全 局 定 义

HINSTANCE hInst; //应用程序实例句柄 HWND hMainWnd; //主窗口句柄 char szWindowTitle[]="头晕的泡泡(点击新建开始动画)"; //主窗口标题 char szWindowClass[]="SDKWIN32APP"; //主窗口类名 struct WIN_MSGMAP_ENTRY { UINT nMessage; LRESULT (*pfn)(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); }; #define BEGIN_MESSAGE_MAP() const struct WIN_MSGMAP_ENTRY _messageEntries[]={ #define ON_MSG(nMessage,pfn) { nMessage, pfn}, #define END_MESSAGE_MAP() {0, NULL} }; //消息处理函数原型--------------------------------------------------------------void OnIdle(void); LRESULT OnDestroy(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); LRESULT OnCreate(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); LRESULT OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); LRESULT OnSize(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); LRESULT OnCommand(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); //消息映射表宏定义--------------------------------------------------------------BEGIN_MESSAGE_MAP() ON_MSG(WM_DESTROY, OnDestroy) //窗口销毁消息映射 ON_MSG(WM_CREATE, OnCreate) //窗口创建消息映射 ON_MSG(WM_PAINT, OnPaint) //重绘消息映射 ON_MSG(WM_SIZE, OnSize) //窗口大小变化消息映射 ON_MSG(WM_COMMAND, OnCommand) //命令消息映射 END_MESSAGE_MAP() //窗口过程----------------------------------------------------------------------LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { WORD i; for(i=0;_messageEntries[i].nMessage; i++) { //nMessage 为 0 结束循环 //如果消息映射表中有该消息,则调用对应的消息处理函数 if (message==_messageEntries[i].nMessage) { if (message!=WM_DESTROY) //非 WM_DESTROY 消息处理后立即返回 return (*_messageEntries[i].pfn)(hWnd,message,wParam,lParam); //如果是 WM_DESTROY 消息处理但不返回,继续后处理

(*_messageEntries[i].pfn)(hWnd,message,wParam,lParam); break; } } if (message==WM_DESTROY) { //WM_DESTROY 后处理 PostQuitMessage(0); //发出 WM_QUIT 消息,结束应用程序 return 0; //处理了这条消息必须返回 0 } return DefWindowProc(hWnd,message,wParam,lParam); //其他消息调用默认处理 } //注册窗口类--------------------------------------------------------------------ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS; wcex.lpfnWndProc = (WNDPROC)WndProc; //设置主窗口过程 wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; //应用程序实例 wcex.hIcon = LoadIcon(hInstance,(LPCTSTR)IDI_APP); //主窗口图标 wcex.hCursor = LoadCursor(NULL,IDC_ARROW); //主窗口光标 wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //主窗口背景色 wcex.lpszMenuName = (LPCSTR)IDR_APP; //主窗口主菜单 wcex.lpszClassName = szWindowClass; //主窗口类名 wcex.hIconSm = LoadIcon(hInstance,(LPCTSTR)IDI_APPSMALL); //小图标 return RegisterClassEx(&wcex); //注册窗口类 } //初始化窗口--------------------------------------------------------------------BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { DWORD ds=WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN; //主框架风格,剪切子控件 hInst=hInstance; //保存应用程序实例 hMainWnd=CreateWindow(szWindowClass,szWindowTitle,ds, CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL); //创建主窗口 if (!hMainWnd) return FALSE; ShowWindow(hMainWnd, nCmdShow); //显示主窗口 UpdateWindow(hMainWnd); //更新主窗口 return TRUE; } //消息循环----------------------------------------------------------------------int Run() //消息循环 { MSG msg;

while(TRUE) { //消息循环处理 if (PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) { //检测队列有无消息 if (!GetMessage(&msg,NULL,0,0)) break; //WM_QUIT 结束消息循环 TranslateMessage(&msg); //将键盘事件转化成字符消息 DispatchMessage(&msg); //分发消息到窗口的消息处理函数去执行 } else OnIdle(); //无消息时处理空闲过程 } return msg.wParam; //结束消息循环 } //主函数------------------------------------------------------------------------int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) { MyRegisterClass(hInstance); //注册窗口类 if (!InitInstance(hInstance,nCmdShow)) return 0; //初始化窗口失败退出程序 return Run(); //进入消息循环 } //消息处理函数实现--------------------------------------------------------------HGLRC hRC; //OpenGL 渲染场境 void InitScene(); //初始化 OpenGL 显示场景 void ChangeSize(int w,int h); //OpenGL 视区缩放 void RenderScene(); //显示 OpenGL 场景 void CleanupScene(); //清理 OpenGL 场景 LRESULT OnDestroy(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { //窗口销毁消息处理 HDC hdc; hdc=GetDC(hWnd); //客户区 DC wglMakeCurrent(hdc,hRC); //设置当前态 CleanupScene(); //清理释放场景 wglMakeCurrent(0,0); //取消当前态 wglDeleteContext(hRC); //删除渲染场境 ReleaseDC(hWnd,hdc); //释放 DC return 0; //处理了这条消息必须返回 0 } LRESULT OnCreate(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { //窗口创建消息处理 PIXELFORMATDESCRIPTOR pfd; int nPixelFormat; HDC hdc; //定义像素格式 ZeroMemory(&pfd,sizeof(pfd));//全部数据设为 0 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; hdc=GetDC(hWnd); //客户区 DC nPixelFormat=ChoosePixelFormat(hdc,&pfd); //检索 DC 匹配的像素格式 SetPixelFormat(hdc,nPixelFormat,&pfd); //设置像素格式 hRC=wglCreateContext(hdc); //创建 OpenGL 渲染场境 wglMakeCurrent(hdc,hRC); //设置当前态 InitScene(); //初始化 OpenGL 显示场景 wglMakeCurrent(0,0); //取消当前态 ReleaseDC(hWnd,hdc); //释放 DC return 0; //处理了这条消息必须返回 0 } LRESULT OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { //重绘消息处理 PAINTSTRUCT ps; HDC hdc; hdc=BeginPaint(hWnd,&ps); //客户区准备绘图 wglMakeCurrent(hdc,hRC); //设置当前态 RenderScene(); //显示 OpenGL 场景 SwapBuffers(hdc); //交换缓冲区 wglMakeCurrent(0,0); //取消当前态 EndPaint(hWnd,&ps); //客户区绘图结束 return 0; //处理了这条消息必须返回 0 } LRESULT OnSize(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { //窗口大小变化消息处理 HDC hdc; int cx, cy; cx = LOWORD(lParam), cy = HIWORD(lParam); //宽度值和高度值 if(cx==0 || cy==0) return 0; hdc=GetDC(hWnd); //客户区 DC wglMakeCurrent(hdc,hRC); //设置当前态 ChangeSize(cx,cy); //OpenGL 视区缩放 wglMakeCurrent(0,0); //取消当前态 ReleaseDC(hWnd,hdc); //释放 DC return 0; //处理了这条消息必须返回 0 } void InitScene() { //初始化 OpenGL 显示场景 GLfloat ambientProperties[]={0.7f,0.7f,0.7f,1.0f};

GLfloat diffuseProperties[]={0.8f,0.8f,0.8f,1.0f}; GLfloat specularProperties[]={1.0f,1.0f,1.0f,1.0f}; glPolygonMode(GL_FRONT,GL_LINE); glPolygonMode(GL_BACK,GL_LINE); glShadeModel(GL_FLAT); glEnable(GL_NORMALIZE); glClearDepth(1.0); glLightfv(GL_LIGHT0,GL_AMBIENT,ambientProperties); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseProperties); glLightfv(GL_LIGHT0,GL_SPECULAR,specularProperties); glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,1.0); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); } void ChangeSize(int w,int h) { //OpenGL 视区缩放 GLfloat fAspect; fAspect=(h==0)?1.0f:(GLfloat)w/h; glViewport(0,0,w,h);//定义视区 glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30,fAspect,1,15.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDrawBuffer(GL_BACK); } void RenderScene() { { //显示 OpenGL 场景 GLUquadricObj* q; glClearColor(0.0f,1.0f,1.0f,1.0f); //设置清除缓冲区的颜色为白色 glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glTranslated(0.0,0.0,-5.5);//调整小球绕坐标系 z 轴转动半径 glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(0,255,0); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0);

glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,0.3,50000,5000);//各个小球场景 glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,0.1,500000,500000); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,0.4,50000,5000); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,0.6,50000,5000); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(0,0,200);

q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,0.5,50000,5000); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,1.3,50000,5000); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,1.2,50000,5000); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,1.0,50000,5000); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,0.9,50000,5000);

glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,1.5,50000,5000); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,1.4,50000,5000); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,1.7,50000,5000); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,0.2,50000,5000); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0);

glScalef(1.0,1.0,1.0); glColor3ub(255,255,255); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,0.4,50000,5000); glTranslated(0.0,0.0,-2.0); glRotatef(xRotation,1.0,0.0,0.0); glRotatef(yRotation,0.0,1.0,0.0); glRotatef(zRotation,0.0,0.0,1.0); glScalef(1.0,1.0,1.0); glColor3ub(0,0,0); q=gluNewQuadric(); gluQuadricDrawStyle(q,GLU_FILL); gluQuadricNormals(q,GLU_SMOOTH); gluSphere(q,0.3,50000,5000); gluDeleteQuadric(q); glPopMatrix(); } } void CleanupScene() { //清理 OpenGL 场景 } LRESULT OnMouseMove(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { static int lx,ly,x,y; if(!bAnimate) { x=LOWORD(lParam),y=HIWORD(lParam); yRotation-=(float)(lx-x)/3.0f; xRotation-=(float)(ly-y)/3.0f; lx=x,ly=y; InvalidateRect(hWnd,NULL,FALSE); } return 0; } void OnIdle(void) { HDC hdc; if(!bAnimate)return; yRotation+=1.0; hdc=GetDC(hMainWnd); if(hdc)

{ wglMakeCurrent(hdc,hRC); RenderScene(); SwapBuffers(hdc); wglMakeCurrent(0,0); } ReleaseDC(hMainWnd,hdc); } LRESULT OnCommand(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { WORD wID; wID=LOWORD(wParam); switch(wID) { case IDM_FILE_NEW: bAnimate=!bAnimate; break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd,message,wParam,lParam); } return 0; }


相关文章:
5. 省份代码对照表
5. 省份代码对照表_计算机软件及应用_IT/计算机_专业资料。附件 5 省份代码对照表 代码 11 12 13 14 15 21 22 23 31 32 33 34 35 36 37 41 42 43 ...
氨基酸缩写代码
氨基酸缩写代码_物理_自然科学_专业资料。附录 C 氨基酸缩写代码 以下是氨基酸的单字母记号和三字母记号 单字母记号 三字母记号 英文名称 A Ala Alanine C Cys ...
HTML 常用代码大全
HTML 常用代码大全_互联网_IT/计算机_专业资料。HTML 常用代码大全。益阳网站设计,益阳益阳网站设计,益阳VIS设计,益阳包装设计,益阳画册设计,益阳SEO ...
随意修改网页内容的代码
随意修改网页内容的代码先把这段代码输入到地址栏: javascript:document.body.contentEditable='true'; document.designMode='on'; void 0 然后按 enter,之后就...
中华人民共和国行政区划代码标准
中华人民共和国行政区划代码标准_军事/政治_人文社科_专业资料。中华人民共和国行政区划代码标准 GB/T 2260—1999 前 言 GB/T 2260《中华人民共和国行政区划代码...
规划数据库类型代码
规划数据库类型代码_建筑/土木_工程科技_专业资料。土地规划 乡级规划数据库主要图层命名及类型代码表 一、 主要图层命名对照表: CZJSKZQ JSYDGZQ TDYTQ JQDLTB ...
学生来源代码完整版本
学生来源代码完整版本_英语考试_外语学习_教育专区。附件二 民族代码代码 01 02 03 04 05 06 07 08 09 10 11 12 13 14 民族 汉族 蒙古族 回族 藏族 ...
行业代码
行业代码_经济/市场_经管营销_专业资料。行业代 码 C 13 制造业 农副食品加工业 1461 1462 1469 1491 1492 味精制造 酱油、食醋及类似制品制造 其他调味品、...
农行省市代码
农行省市代码_人文社科_专业资料。农行省市代码: 01 总行;02 07 吉林省;08 13 福建省;14 19 浙江省;20 25 西藏区;26 31 重庆市;34 44 广东省;45 83 ...
四川省行政区划代码
四川省行政区划代码_计算机软件及应用_IT/计算机_专业资料。四川省行政区划代码表(一)行政区域代码:510000 成都 代码 510101 地区名称 29 市辖区 锦江区 青羊区 ...
更多相关标签:
代码大全 | 代码编辑器 | 源代码 | 网页代码 | 淘宝代码 | 代码生成器 | html代码 | 神秘代码 |