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

代码


//头文件------------------------------------------------------------------------#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; }


相关文章:
完整M代码
M 代码 内容 M00 程序停止 M01 任选停止 M02 程序结束 M03 工作主轴起动 (正转) M04 工作主轴起动 (反转) M05 主轴停止 M06 刀具交换 M07 M08 冷却液开 ...
科目代码
科目代码_专业资料。企业常用科目 一、资产类 1001 库存现金 1002 银行存款 1121 应收票据 1122 应收账款 1123 预付账款 1131 应收股利 1132 应收利息 1231 其他...
源代码是什么
源代码是什么_计算机软件及应用_IT/计算机_专业资料。介绍源代码是什么,可供初学者解读源代码:一系列人类可读的计算机语言指令 源程序(source code)是指未编译的按...
质量审核员专业代码
质量审核员专业代码 15页 免费 ISO9001:2008质量管理体... 10页 1下载券 质量管理体系认证业务范... 35页 免费 质量管理体系审核员专业... 1页 免费 附表2...
生产过程危险和有害因素分类与代码(GB13861-2009)
生产过程危险和有害因素分类与代码(GB13861-2009) 1 范围 本标准规定了生产过程中各种主要危险和有害因素的分类和代码。 本标准适用于各行业在规划、设计和组织...
参考文献代码
参考文献类型、电子文献载体类型及其标志代码项目 1.参考文献类型及其标志代码 计学数算汇报期位报标专据机编纸刊论告准利库程文序 D G N J D R S P CP ...
PSC检查代码
PSC检查代码_专业资料。PSC 代码 表一: Code 代码 Actions to be Taken 需采取的措施 00 No Action Taken 不需采取措施 10 Deficiency Rectified 缺陷已PSC 代码...
5. 省份代码对照表
5. 省份代码对照表_计算机软件及应用_IT/计算机_专业资料。附件 5 省份代码对照表 代码 11 12 13 14 15 21 22 23 31 32 33 34 35 36 37 41 42 43 ...
SAP代码
创建公司代码中的主记录 Change Master Record in Company Code 更改公司代码中的主记录 Display Master Record in Comp.Code 显示公司代码中的主记录 G/L Account...
用地分类和代码_图文
1 城乡用地分类 用地分类 表 3.2.2 城乡用地分类和代码 类别代码 大类 H 中类 小类 类别名称 内容 包括城乡居民点建设用地、 区域交通设施用地、 区域公用...
更多相关标签: