1、计算机图形学1DOC电子科技大学实验报告学生姓名:王兆东 学号:2014220901008指导教师:匡平 实验地点:科A 实验时间:2015.06.08一、实验室名称:软件实验室二、实验项目名称:bresenham算法模拟演示三、实验学时:3四、实验原理:利用bresenham算法模拟演示五、实验目的: 了解图元生成意义,掌握bresenham算法过程。 掌握整数坐标概念以及线段在整数坐标系统中的生成过程 分析所产生线段的走样问题 利用opengl实现一个模拟演示程序 六、实验内容: 基于glut的opengl绘制框架实现 编写背景绘制函数void drawbackground() 绘制整数坐
2、标网格,利用opengl模拟产生一个网格,以一个格子代表一个像素。产生一个300300的网格,在视点观察方向,垂直于视点方向。 编写bresenham算法函数,void drawline(point1, point2),能够实时动态产生整数坐标点,并绘制一个球替代该像素 七、实验器材(设备、元器件): PC机一台,装有C/C+语言集成开发环境。八、实验步骤: 1.搭建c+程序语言开发环境; 2.编写程序 3.运行程序,得出结果九、实验程序:#include / Header File For Windows#include / Header File For The OpenGL32 Libr
3、ary#include / Header File For The GLu32 Library#include #include HDC hDC=NULL; / Private GDI Device ContextHGLRC hRC=NULL; / Permanent Rendering ContextHWND hWnd=NULL; / Holds Our Window HandleHINSTANCE hInstance; / Holds The Instance Of The Applicationbool keys256; / Array Used For The Keyboard Rou
4、tinebool active=TRUE; / Window Active Flag Set To TRUE By Defaultbool fullscreen=TRUE; / Fullscreen Flag Set To Fullscreen Mode By Defaultstruct object / 记录游戏中的对象 int fx, fy; / 使移动变得平滑 int x, y; / 当前游戏者的位置 float spin; / 旋转方向;struct object player; / 玩家信息bool vline1110; / 保存垂直方向的11根线条中,每根线条中的10段是否被走过b
5、ool hline1011; /保存水平方向的11根线条中,每根线条中的10段是否被走LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); / Declaration For WndProcGLvoid ReSizeGLScene(GLsizei width, GLsizei height) / Resize And Initialize The GL Window if (height=0) / Prevent A Divide By Zero By height=1; / Making Height Equal One glViewpo
6、rt(0,0,width,height); / Reset The Current Viewport glMatrixMode(GL_PROJECTION); / Select The Projection Matrix glLoadIdentity(); / Reset The Projection Matrix / Calculate The Aspect Ratio Of The Window gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); / Se
7、lect The Modelview Matrix glLoadIdentity(); / Reset The Modelview Matrix LRESULT CALLBACK WndProc( HWND hWnd, / Handle For This Window UINT uMsg, / Message For This Window WPARAM wParam, / Additional Message Information LPARAM lParam) / Additional Message Information switch (uMsg) / Check For Window
8、s Messages case WM_ACTIVATE: / Watch For Window Activate Message if (!HIWORD(wParam) / Check Minimization State active=TRUE; / Program Is Active else active=FALSE; / Program Is No Longer Active return 0; / Return To The Message Loop case WM_SYSCOMMAND: / Intercept System Commands switch (wParam) / C
9、heck System Calls case SC_SCREENSAVE: / Screensaver Trying To Start? case SC_MONITORPOWER: / Monitor Trying To Enter Powersave? return 0; / Prevent From Happening break; / Exit case WM_CLOSE: / Did We Receive A Close Message? PostQuitMessage(0); / Send A Quit Message return 0; / Jump Back case WM_KE
10、YDOWN: / Is A Key Being Held Down? keyswParam = TRUE; / If So, Mark It As TRUE return 0; / Jump Back case WM_KEYUP: / Has A Key Been Released? keyswParam = FALSE; / If So, Mark It As FALSE return 0; / Jump Back case WM_SIZE: / Resize The OpenGL Window ReSizeGLScene(LOWORD(lParam),HIWORD(lParam); / L
11、oWord=Width, HiWord=Height return 0; / Jump Back / Pass All Unhandled Messages To DefWindowProc return DefWindowProc(hWnd,uMsg,wParam,lParam);int InitGL(GLvoid) / All Setup For void point(float x,float y)/ glColor3f(0.0f,0.0f, 1.0f); glPointSize(7.0f); glBegin(GL_POINTS); glVertex2f(x + 0.5,y + 0.5)
12、; glEnd();void bresenham(int x0,int y0,int x1,int y1) int dx=fabs(float)(x1-x0),dy=fabs(float)(y1-y0); int p = 2 * dy - dx; int twoDx=2 * dy,twoDxDy=2 * (dy -dx); int x,y; if(x0 x1) x = x1; y = y1; x1 = x0; else x = x0; y = y0; point(x,y); while(x x1) x+; if(p 0) p += twoDx; else y+; p +=twoDxDy; po
13、int(x,y); bool l,r;int xx = -20,yy= -10;int xx1 = 30,yy1= 18;int DrawGLScene(GLvoid) / Heres Where We Do All The Drawing glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); / Clear Screen And Depth Buffer glLoadIdentity(); glTranslatef(0.0f,0.0f,-100.0f); for(int i=-50;i=50;i+) glBegin(GL_LINES); /
14、Start Drawing Horizontal Cell Borders glVertex2f(-50,i);/ Left Side Of Horizontal Line glVertex2f(50,i);/ Right Side Of Horizontal Line glEnd(); glBegin(GL_LINES); / Start Drawing Horizontal Cell Borders glVertex2f(i,50);/ Left Side Of Horizontal Line glVertex2f(i,-50);/ Right Side Of Horizontal Lin
15、e glEnd(); glBegin(GL_LINES); glVertex2f(0,200); glVertex2f( 0,-200); glEnd(); glBegin(GL_LINES); glVertex2f(200,0); glVertex2f(-200,0); glEnd(); point(xx,yy); bresenham(xx,yy,xx1,yy1); return TRUE; / Everything Went OKint WINAPI WinMain( HINSTANCE hInstance, / Instance HINSTANCE hPrevInstance, / Pr
16、evious Instance LPSTR lpCmdLine, / Command Line Parameters int nCmdShow) / Window Show State MSG msg; / Windows Message Structure BOOL done=FALSE; / Bool Variable To Exit Loop / Ask The User Which Screen Mode They Prefer if (MessageBox(NULL,Would You Like To Run In Fullscreen Mode?, Start FullScreen
17、?,MB_YESNO|MB_ICONQUESTION)=IDNO) fullscreen=FALSE; / Windowed Mode / Create Our OpenGL Window if (!CreateGLWindow(NeHes OpenGL Framework,640,480,16,fullscreen) return 0; / Quit If Window Was Not Created if (keysVK_RIGHT & (player.x0) & (player.fx=player.x*60) & (player.fy=player.y*40) player.x-; hl
18、ineplayer.xplayer.y=TRUE; if (keysVK_DOWN & (player.y0) & (player.fx=player.x*60) & (player.fy=player.y*40) player.y-; vlineplayer.xplayer.y=TRUE; while(!done) / Loop That Runs While done=FALSE if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) / Is There A Message Waiting? if (msg.message=WM_QUIT) / Have We
19、Received A Quit Message? done=TRUE; / If So done=TRUE else / If Not, Deal With Window Messages TranslateMessage(&msg); / Translate The Message DispatchMessage(&msg); / Dispatch The Message else / If There Are No Messages / Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene() if (ac
20、tive) / Program Active? if (keysVK_ESCAPE) / Was ESC Pressed? done=TRUE; / ESC Signalled A Quit else / Not Time To Quit, Update Screen DrawGLScene(); / Draw The Scene SwapBuffers(hDC); / Swap Buffers (Double Buffering) if (keysVK_F1) / Is F1 Being Pressed? keysVK_F1=FALSE; / If So Make Key FALSE Kil
21、lGLWindow(); / Kill Our Current Window fullscreen=!fullscreen; / Toggle Fullscreen / Windowed Mode / Recreate Our OpenGL Window if (!CreateGLWindow(NeHes OpenGL Framework,640,480,16,fullscreen) return 0; / Quit If Window Was Not Created if (keysVK_UP) yy +; keysVK_UP = false; if (keysVK_DOWN) yy -;
22、keysVK_DOWN = false; if (keysVK_RIGHT) xx +; keysVK_RIGHT = false; if (keysVK_LEFT) xx -; keysVK_LEFT = false; if (keysVK_F8) yy1 +; keysVK_F8 = false; if (keysVK_F2) yy1 -; keysVK_F2 = false; if (keysVK_F3) xx1 +; keysVK_F3 = false; if (keysVK_F4) xx1 -; keysVK_F4 = false; / Shutdown KillGLWindow(); / Kill The Window return (msg.wParam); / Exit The Program十、实验结果:
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2