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

代码


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


相关文章:
广州数控指令代码大全
广州数控指令代码大全_机械/仪表_工程科技_专业资料。广州数控指令代码大全 2011-01-31 02:13 GSK980TA/D GSK980TA/D 编程教材 980TA 《一》编程的基本概念 ...
民族代码表_图文
民族代码表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 汉族 蒙古族 回族 藏族 维吾尔族 苗族 ...
Discuz代码大全
Discuz代码大全_IT/计算机_专业资料。Discuz代码大全,经过整理编辑Discuz!代码大全 代码大全 代码大家在论坛发表主题或回帖时,经常要用到 Discuz!代码,本文就常用的 ...
中国天气预报代码大全
中国天气预报代码大全阿巴嘎旗 CHXX0243 阿城 CHXX0001 爱辉 CHXX0174 阿合奇 CHXX0210 阿拉尔 CHXX0212 阿勒泰 CHXX0196 安达 CHXX0187 敖汉旗 CHXX0002 安康 CHXX...
科目代码
科目代码_专业资料。企业常用科目 一、资产类 1001 库存现金 1002 银行存款 1121 应收票据 1122 应收账款 1123 预付账款 1131 应收股利 1132 应收利息 1231 其他...
源码买卖协议
代码买卖协议 甲方: 乙方: 身份证号码: 经甲乙双方友好协商,乙方准备向甲方买下《xxxx》的完整代码,内容包括如下: 1、 完整前端代码 2、 完整后端代码 3、 ...
参考文献类型及代码
参考文献类型及代码: 参考文献类型及代码:专著[M] ,论文集[C] ,报纸文章[N] ,期刊文章[J] ,学位论文[D] ,报告[R] ,标准[S] ,专利[P] ,论文集中的...
代码
世界港口 世界港口代码 .欧洲地区(EUROPE) 1.意大利(ITALY) 目的港 比萨 波伦那 杜林 佛洛伦萨 罗马 米兰 那不勒斯 热那瓦 威尼斯 三字代码 PSA BLQ TRN FLR ...
北京市行政区划代码
北京市行政区划代码_电力/水利_工程科技_专业资料。本标准是为适应信息技术需要而制订。本标准所给出的行政区划以北京市民政局所提供的北京市人民政府关于行政区划...
废气排放规律代码表
废气排放规律代码表_畜牧兽医_农林牧渔_专业资料。排污申报代码表 废气排放规律代码代码 废气排放规律 稳定连续排放 周期性连续排放 1 2 3 不规律连续排放 有...
更多相关标签:
代码大全 | 代码编辑器 | 源代码 | 网页代码 | 淘宝代码 | 代码生成器 | html代码 | 神秘代码 |