南昌大学图形学实验报告完整版Word文档格式.docx
《南昌大学图形学实验报告完整版Word文档格式.docx》由会员分享,可在线阅读,更多相关《南昌大学图形学实验报告完整版Word文档格式.docx(32页珍藏版)》请在冰豆网上搜索。
for(i=0;
i<
n;
++i)
{
x=R*cos(2*Pi/n*i)*(1-cos(2*Pi/n*i));
y=R*sin(2*Pi/n*i)*(1-cos(2*Pi/n*i));
glVertex2f(y,x+0.2f);
}
glEnd();
glFlush();
}
voidmyDisplay()
glClear(GL_COLOR_BUFFER_BIT);
DrawCircle();
voidinit(void)
glClearColor(1.0f,1.0f,1.0f,0.0f);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-400.0,400.0,-400.0,400.0);
glMatrixMode(GL_MODELVIEW);
intmain(intargc,char*argv[])
glutInit(&
argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("
心形"
);
//init();
glutDisplayFunc(myDisplay);
glutMainLoop();
return0;
实验二
任意斜率直线和中点画圆
理解DDA直线算法和中点画圆算法
画线的核心是通过算法来调整直线的生成方式,这只是其中一种方式
八、参考资料:
附源码:
1:
任意直线斜率
gl/glut.h>
cmath>
staticfloatm=0;
floatx,y;
voidinit(void)
glClearColor(1.0,1.0,1.0,0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,200.0,0.0,150.0);
voidline()
glColor3f(1.0,0.0,0.0);
if(fabs(m)==0)
glBegin(GL_LINES);
glVertex2i(18,70);
glVertex2i(300,70);
elseif(fabs(m)<
=1)
glBegin(GL_POINTS);
x=70;
y=70;
for(inti=0;
i<
=1000;
i++)
{
glVertex2f(x,y);
x+=0.1;
y=y+m*0.1;
}
glEnd();
else
y+=0.1;
x=x+0.1/m;
voidmain(intargc,char**argv)
cout<
<
"
输入斜率:
;
cin>
>
m;
argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,100);
glutInitWindowSize(400,300);
任意斜率的直线"
init();
glutDisplayFunc(line);
2:
中点画圆
//以下为一般圆生成算法,不采纳
/*voidDrawCircle()
glVertex2f(R*cos(2*Pi/n*i),R*sin(2*Pi/n*i));
}*/
//以下代码为中点画圆算法
classscreenPt
private:
GLintx,y;
public:
sceenPt(){x=y=0;
voidsetCoords(GLintxcoordValue,GLintycoordValue)
x=xcoordValue;
y=ycoordValue;
GLintgetx()const{returnx;
GLintgety()const{returny;
voidincrementx(){x++;
voiddecrementy(){y--;
};
voidsetPixel(GLintxcoord,GLintycoord)
//glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(xcoord,ycoord);
//glRectf(-0.5f,-0.5f,0.5f,0.5f);
voidcircleMidpoint(GLintxc,GLintyc,GLintradius)
screenPtcircPt;
GLintp=1-radius;
circPt.setCoords(0,radius);
voidcirclePlotPoints(GLint,GLint,screenPt);
circlePlotPoints(xc,yc,circPt);
while(circPt.getx()<
circPt.gety())
circPt.incrementx();
if(p<
0)
p+=2*circPt.getx()+1;
else
circPt.decrementy();
p+=2*(circPt.getx()-circPt.gety())+1;
circlePlotPoints(xc,yc,circPt);
voidcirclePlotPoints(GLintxc,GLintyc,screenPtcircPt)
setPixel(xc+circPt.getx(),yc+circPt.gety());
//1
setPixel(xc-circPt.getx(),yc+circPt.gety());
setPixel(xc+circPt.getx(),yc-circPt.gety());
setPixel(xc-circPt.getx(),yc-circPt.gety());
setPixel(xc+circPt.gety(),yc+circPt.getx());
//5
setPixel(xc-circPt.gety(),yc+circPt.getx());
setPixel(xc+circPt.gety(),yc-circPt.getx());
setPixel(xc-circPt.gety(),yc-circPt.getx());
glColor3f(1.0f,0.0f,0.0f);
glViewport(0,0,400,400);
circleMidpoint(0,0,360);
画一个圆"
glutDisplayFunc(&
myDisplay);
实验三
直线裁剪算法
掌握Sutherland-Cohen或Liang-Barsky直线裁剪算法
3、实验基本原理和内容
4、四、主要仪器设备及耗材
剪裁的时候要注意视点的位置,以及剪裁点的设置
/*#include<
Gl\glut.h>
classwcPt2D
wcPt3D()
x=0.0;
y=0.0;
setCoords(GLfloatxCoord,GLfloatyCoord)
x=xCoord;
y=yCoord;
GLfloatgetx()const
{
returnx;
GLfloatgety()const
returny;
inlineGLintround(constGLfloata)
returnGLint(a+0.5);
GLintclipTest(GLfloatp,GLfloatq,GLfloat*u1,GLfloat*u2)
GLfloatr;
GLintreturnValue=true;
if(p<
0.0)
r=q/p;
if(r>
*u2)
returnValue=false;
if(r>
*u1)
*u1=r;
if(p>
r=q/p;
if(r<
returnValue=false;
elseif(r<
{
*u2=r;
}
if(q<
returnreturnValue;
voidlineClipLiangBarsk(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2)
GLfloatu1=0.0,u2=1.0,dx=p2.getx()-p1.getx(),dy;
if(clipTest(-dx,p1.getx()-winMin.getx(),&
u1,&
u2))
dy=p2.gety()-p1.gety();
if(clipTest(-dy,p1.gety()-winMin.gety(),&
if(clipTest(dy,winMax.gety()-p1.gety(),&
if(u2<
1.0)
p2.setCoords(p1.getx()+u2*dx,p1.gety()+u2*dy);
if(u1>
p1.setCoords(p1.getx()+u1*dx,p1.gety()+u1*dy);
lineBres(round(p1.getx()),round(p1.gety()),round(p2.getx()),round(p2.gety));
voidmyDisplay(void)
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2i(180,15);
glVertex2i(10,145);
glRectf(-0.5f,-0.5f,0.5f,0.5f);
glFlush();
voidinit(void)
glClearColor(1.0,1.0,1.0,0.0);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowPosition(50,100);
glutInitWindowSize(400,300);
glutCreateWindow("
OpenGL"
init();
glutDisplayFunc(&
glutMainLoop();
return0;
}*//*
利用VC+OpenGL实现直线的编码裁剪算法,在屏幕上用一个封闭矩形裁剪任意一条直线。
intflag;
voidsetPixel(GLintx,GLinty)
glVertex2i(x,y);
/*Bresenhamline-drawingprocedurefor|m|<
1.0.*/
voidlineBres(intx0,inty0,intxEnd,intyEnd)
intdx=fabs(xEnd-x0),dy=fabs(yEnd-y0);
intp=2*dy-dx;
inttwoDy=2*dy,twoDyMinusDx=2*(dy-dx);
intx,y;
/*Determinewhichendpointtouseasstartposition.*/
if(x0>
xEnd){
x=xEnd;
y=yEnd;
xEnd=x0;
else{
x=x0;
y=y0;
setPixel(x,y);
while(x<
x++;
if(p<
0)
p+=twoDy;
y++;
p+=twoDyMinusDx;
}//***********************************//以下未变,见课本P264
//***************************************
classwcPt2D{
public:
GLfloatx,y;
inlineGLintround(constGLfloata){returnGLint(a+0.5);
/*Defineafour-bitcodeforeachoftheoutsideregionsofa
*rectangularclippingwindow.
constGLintwinLeftBitCode=0x1;
constGLintwinRightBitCode=0x2;
constGLintwinBottomBitCode=0x4;
constGLintwinTopBitCode=0x8;
/*Abit-maskregioncodeisalsoassignedtoeachendpointofaninput
*linesegment,accordingtoitspositionrelativetothefouredgesof
*aninputrectangularclipwindow.
*
*Anendpointwitharegion-codevalueof0000isinsidetheclipping
*window,otherwiseitisoutsideatleastoneclippingboundary.If
*the'
or'
operationforthetwoendpointcodesproducesavalueof
*false,theentirelinedefinedbythesetwoendpointsissaved
*(accepted).Ifthe'
and'
operationbetweentwoendpointcodesis
*true,thelineiscompletelyoutsidetheclippingwindow,anditis
*eliminated(rejected)fromfurtherprocessing.
*/
inlineGLintinside(GLintcode){returnGLint(!
code);
inlineGLintreject(GLintcode1,GLintcode2)
{returnGLint(code1&
code2);
inlineGLintaccept(GLintcode1,GLintcode2)
{returnGLint(!
(code1|code2));
GLubyteencode(wcPt2Dpt,wcPt2DwinMin,wcPt2DwinMax)
GLubytecode=0x00;
if(pt.x<
winMin.x)
code=code|winLeftBitCode;
if(pt.x>
winMax.x)
code=code|winRightBitCode;
if(pt.y<
winMin.y)
code=code|winBottomBitCode;
if(pt.y>
winMax.y)
code=code|winTopBitCode;
return(code);
voidswapPts(wcPt2D*p1,wcPt2D*p2)
wcPt2Dtmp;
tmp=*p1;
*p1=*p2;
*p2=tmp;
voidswapCodes(GLubyte*c1,GLubyte*c2)
GLubytetmp;
tmp=*c1;
*c1=*c2;
*c2=tmp;
voidlineClipCohSuth(wcPt2DwinMin,wcPt2DwinMax,wcPt2Dp1,wcPt2Dp2)
GLubytecode1,code2;
GLintdone=false,plotLine=false;
GLfloatm;
while(!
done){
code1=encode(p1,winMin,winMax);
code2=encode(p2,winMin,winMax);
if(accept(code1,code2)){
done=tru