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

代码


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


相关文章:
G代码一览表
CNC 代码一览表 M 代码一览表 M 代码一览表 M 代码 M00 M01 M02 M03 M04 M05 M06 M08 M09 M12 M13 M15 M16 M19 M23 M24 M30 M35 程序停止 选择停止 ...
农行省市代码
农行省市代码_人文社科_专业资料。农行省市代码: 01 总行;02 07 吉林省;08 13 福建省;14 19 浙江省;20 25 西藏区;26 31 重庆市;34 44 广东省;45 83 ...
四川省行政区划代码_图文
四川省行政区划代码_计算机软件及应用_IT/计算机_专业资料。四川省行政区划代码表(一)行政区域代码:510000 成都 代码 510101 地区名称 29 市辖区 锦江区 青羊区 ...
完整M代码
M 代码 内容 M00 程序停止 M01 任选停止 M02 程序结束 M03 工作主轴起动 (正转) M04 工作主轴起动 (反转) M05 主轴停止 M06 刀具交换 M07 M08 冷却液开 ...
专利国省代码
专利国省代码省 市代码 11 北京 12 天津 13 河北 14 山西 35 福建 52 贵州 71 台湾 15 内蒙 21 辽宁 22 吉林 23 黑龙江 31 上海 41 河南 42 湖北 61...
科目代码
科目代码_专业资料。企业常用科目 一、资产类 1001 库存现金 1002 银行存款 1121 应收票据 1122 应收账款 1123 预付账款 1131 应收股利 1132 应收利息 1231 其他...
《国家学生体质健康标准》民族代码表_图文
《国家学生体质健康标准》民族代码表_表格类模板_表格/模板_实用文档。《国家学生体质健康标准》民族代码表 附表1 《国家学生体质健康标准》评价指标与分值 年级 ...
移动错误代码说明
移动错误代码 1 由 SMSC 返回的一般结果状态报告 DELIVRD EXPIRED 含义 消息发送成功 因为用户长时间关机或者不在服 务区等导致的短消息超时没有递 交到用户手机...
科目三考试项目及扣分项代码_图文
机动车驾驶人科目三 考试项目及扣分项代码 公安部交通管理科学研究所 2012 年 11 月目录 2 科目三考试 ... 1 2.1 科目三考试项目代码 ... 1 2.2 科目三...
2009版学科分类与代码表(GBT13745-2009)
65 67 68 71 72 73 76 76 81 83 84 85 87 88 89 90 91 91 93 II GB/T 13745—××× 前 言 本标准代替GB/T 13745-1992《学科分类与代码》。 本...
更多相关标签: