计算机图形学1DOC.docx
《计算机图形学1DOC.docx》由会员分享,可在线阅读,更多相关《计算机图形学1DOC.docx(14页珍藏版)》请在冰豆网上搜索。
计算机图形学1DOC
电子科技大学
实验报告
学生姓名:
王兆东学号:
2014220901008
指导教师:
匡平实验地点:
科A实验时间:
2015.06.08
一、实验室名称:
软件实验室
二、实验项目名称:
bresenham算法模拟演示
三、实验学时:
3
四、实验原理:
利用bresenham算法模拟演示
五、实验目的:
了解图元生成意义,掌握bresenham算法过程。
掌握整数坐标概念以及线段在整数坐标系统中的生成过程
分析所产生线段的走样问题
利用opengl实现一个模拟演示程序
六、实验内容:
基于glut的opengl绘制框架实现
编写背景绘制函数voiddrawbackground()
✓绘制整数坐标网格,利用opengl模拟产生一个网格,以一个格子代表一个像素。
产生一个300×300的网格,在视点观察方向,垂直于视点方向。
编写bresenham算法函数,voiddrawline(point1,point2),能够实时动态产生整数坐标点,并绘制一个球替代该像素
七、实验器材(设备、元器件):
PC机一台,装有C/C++语言集成开发环境。
八、实验步骤:
1.搭建c++程序语言开发环境;
2.编写程序
3.运行程序,得出结果
九、实验程序:
#include//HeaderFileForWindows
#include//HeaderFileForTheOpenGL32Library
#include//HeaderFileForTheGLu32Library
#include
#include
HDChDC=NULL;//PrivateGDIDeviceContext
HGLRChRC=NULL;//PermanentRenderingContext
HWNDhWnd=NULL;//HoldsOurWindowHandle
HINSTANCEhInstance;//HoldsTheInstanceOfTheApplication
boolkeys[256];//ArrayUsedForTheKeyboardRoutine
boolactive=TRUE;//WindowActiveFlagSetToTRUEByDefault
boolfullscreen=TRUE;//FullscreenFlagSetToFullscreenModeByDefault
structobject//记录游戏中的对象
{
intfx,fy;//使移动变得平滑
intx,y;//当前游戏者的位置
floatspin;//旋转方向
};
structobjectplayer;//玩家信息
boolvline[11][10];//保存垂直方向的11根线条中,每根线条中的10段是否被走过
boolhline[10][11];//保存水平方向的11根线条中,每根线条中的10段是否被走
LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);//DeclarationForWndProc
GLvoidReSizeGLScene(GLsizeiwidth,GLsizeiheight)//ResizeAndInitializeTheGLWindow
{
if(height==0)//PreventADivideByZeroBy
{
height=1;//MakingHeightEqualOne
}
glViewport(0,0,width,height);//ResetTheCurrentViewport
glMatrixMode(GL_PROJECTION);//SelectTheProjectionMatrix
glLoadIdentity();//ResetTheProjectionMatrix
//CalculateTheAspectRatioOfTheWindow
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);//SelectTheModelviewMatrix
glLoadIdentity();//ResetTheModelviewMatrix
}
LRESULTCALLBACKWndProc(HWNDhWnd,//HandleForThisWindow
UINTuMsg,//MessageForThisWindow
WPARAMwParam,//AdditionalMessageInformation
LPARAMlParam)//AdditionalMessageInformation
{
switch(uMsg)//CheckForWindowsMessages
{
caseWM_ACTIVATE:
//WatchForWindowActivateMessage
{
if(!
HIWORD(wParam))//CheckMinimizationState
{
active=TRUE;//ProgramIsActive
}
else
{
active=FALSE;//ProgramIsNoLongerActive
}
return0;//ReturnToTheMessageLoop
}
caseWM_SYSCOMMAND:
//InterceptSystemCommands
{
switch(wParam)//CheckSystemCalls
{
caseSC_SCREENSAVE:
//ScreensaverTryingToStart?
caseSC_MONITORPOWER:
//MonitorTryingToEnterPowersave?
return0;//PreventFromHappening
}
break;//Exit
}
caseWM_CLOSE:
//DidWeReceiveACloseMessage?
{
PostQuitMessage(0);//SendAQuitMessage
return0;//JumpBack
}
caseWM_KEYDOWN:
//IsAKeyBeingHeldDown?
{
keys[wParam]=TRUE;//IfSo,MarkItAsTRUE
return0;//JumpBack
}
caseWM_KEYUP:
//HasAKeyBeenReleased?
{
keys[wParam]=FALSE;//IfSo,MarkItAsFALSE
return0;//JumpBack
}
caseWM_SIZE:
//ResizeTheOpenGLWindow
{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));//LoWord=Width,HiWord=Height
return0;//JumpBack
}
}
//PassAllUnhandledMessagesToDefWindowProc
returnDefWindowProc(hWnd,uMsg,wParam,lParam);
}
intInitGL(GLvoid)//AllSetupFor
voidpoint(floatx,floaty)
{
//glColor3f(0.0f,0.0f,1.0f);
glPointSize(7.0f);
glBegin(GL_POINTS);
glVertex2f(x+0.5,y+0.5);
glEnd();
}
voidbresenham(intx0,inty0,intx1,inty1)
{
intdx=fabs((float)(x1-x0)),dy=fabs((float)(y1-y0));
intp=2*dy-dx;
inttwoDx=2*dy,twoDxDy=2*(dy-dx);
intx,y;
if(x0>x1)
{
x=x1;
y=y1;
x1=x0;
}
else
{
x=x0;
y=y0;
}
point(x,y);
while(x{
x++;
if(p<0)
{
p+=twoDx;
}
else
{
y++;
p+=twoDxDy;
}
point(x,y);
}
}
booll,r;
intxx=-20,yy=-10;
intxx1=30,yy1=18;
intDrawGLScene(GLvoid)//Here'sWhereWeDoAllTheDrawing
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//ClearScreenAndDepthBuffer
glLoadIdentity();
glTranslatef(0.0f,0.0f,-100.0f);
for(inti=-50;i<=50;i++)
{
glBegin(GL_LINES);//StartDrawingHorizontalCellBorders
glVertex2f(-50,i);//LeftSideOfHorizontalLine
glVertex2f(50,i);//RightSideOfHorizontalLine
glEnd();
glBegin(GL_LINES);//StartDrawingHorizontalCellBorders
glVertex2f(i,50);//LeftSideOfHorizontalLine
glVertex2f(i,-50);//RightSideOfHorizontalLine
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);
returnTRUE;//EverythingWentOK
}
}
intWINAPIWinMain(HINSTANCEhInstance,//Instance
HINSTANCEhPrevInstance,//PreviousInstance
LPSTRlpCmdLine,//CommandLineParameters
intnCmdShow)//WindowShowState
{
MSGmsg;//WindowsMessageStructure
BOOLdone=FALSE;//BoolVariableToExitLoop
//AskTheUserWhichScreenModeTheyPrefer
if(MessageBox(NULL,"WouldYouLikeToRunInFullscreenMode?
","StartFullScreen?
",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
fullscreen=FALSE;//WindowedMode
}
//CreateOurOpenGLWindow
if(!
CreateGLWindow("NeHe'sOpenGLFramework",640,480,16,fullscreen))
{
return0;//QuitIfWindowWasNotCreated
}
if(keys[VK_RIGHT]&&(player.x<10)&&(player.fx==player.x*60)&&(player.fy==player.y*40))
{
hline[player.x][player.y]=TRUE;
player.x++;
}
if(keys[VK_LEFT]&&(player.x>0)&&(player.fx==player.x*60)&&(player.fy==player.y*40))
{
player.x--;
hline[player.x][player.y]=TRUE;
}
if(keys[VK_DOWN]&&(player.y<10)&&(player.fx==player.x*60)&&(player.fy==player.y*40))
{
vline[player.x][player.y]=TRUE;
player.y++;
}
if(keys[VK_UP]&&(player.y>0)&&(player.fx==player.x*60)&&(player.fy==player.y*40))
{
player.y--;
vline[player.x][player.y]=TRUE;
}
while(!
done)//LoopThatRunsWhiledone=FALSE
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))//IsThereAMessageWaiting?
{
if(msg.message==WM_QUIT)//HaveWeReceivedAQuitMessage?
{
done=TRUE;//IfSodone=TRUE
}
else//IfNot,DealWithWindowMessages
{
TranslateMessage(&msg);//TranslateTheMessage
DispatchMessage(&msg);//DispatchTheMessage
}
}
else//IfThereAreNoMessages
{
//DrawTheScene.WatchForESCKeyAndQuitMessagesFromDrawGLScene()
if(active)//ProgramActive?
{
if(keys[VK_ESCAPE])//WasESCPressed?
{
done=TRUE;//ESCSignalledAQuit
}
else//NotTimeToQuit,UpdateScreen
{
DrawGLScene();//DrawTheScene
SwapBuffers(hDC);//SwapBuffers(DoubleBuffering)
}
}
if(keys[VK_F1])//IsF1BeingPressed?
{
keys[VK_F1]=FALSE;//IfSoMakeKeyFALSE
KillGLWindow();//KillOurCurrentWindow
fullscreen=!
fullscreen;//ToggleFullscreen/WindowedMode
//RecreateOurOpenGLWindow
if(!
CreateGLWindow("NeHe'sOpenGLFramework",640,480,16,fullscreen))
{
return0;//QuitIfWindowWasNotCreated
}
}
if(keys[VK_UP])
{
yy++;
keys[VK_UP]=false;
}
if(keys[VK_DOWN])
{
yy--;
keys[VK_DOWN]=false;
}
if(keys[VK_RIGHT])
{
xx++;
keys[VK_RIGHT]=false;
}
if(keys[VK_LEFT])
{
xx--;
keys[VK_LEFT]=false;
}
if(keys[VK_F8])
{
yy1++;
keys[VK_F8]=false;
}
if(keys[VK_F2])
{
yy1--;
keys[VK_F2]=false;
}
if(keys[VK_F3])
{
xx1++;
keys[VK_F3]=false;
}
if(keys[VK_F4])
{
xx1--;
keys[VK_F4]=false;
}
}
}
//Shutdown
KillGLWindow();//KillTheWindow
return(msg.wParam);//ExitTheProgram
}
十、实验结果: