1、1) 改变控制点,观察曲线和曲面形状的变化,控制点起什么作用?2) 改写bezier.cpp,增加控制点数目,修改控制点位置,使之成为空间封闭曲线,写出修改的关键代码及注释TIPS:OpenGLBezier曲线绘制方法最多只能有8个控制点3) 根据bezier曲线的性质,改写程序,使之成为两段曲线光滑连接。每段曲线用不同颜色表示,并画出控制点。图7-2 Bezier曲线绘制效果五、函数参考一3D编程1 视点设置函数 void gluLookAt(GLdouble eyex, GLdouble eyey,GLdouble eyez,GLdouble atx,GLdouble aty,GLdoub
2、le atz,GLdouble upx,GLdouble upy,GLdouble upz) 给出矩阵作用于当前矩阵,定义相机位置和方向 视点:eyex, eyey, eyez 目的点:atx,aty,atz 相机向上方向:upx,upy,upz 假设不引用该函数,那么 eyex=0,eyey=0,eyez=0, atx=0,aty=0,atz=-1,Upx=0,upy=1,upz=0此函数放在display函数中调用 参考坐标系:世界坐标系2 正交投影变换设置函数 . void glOrtho(GLdouble left,GLdouble right,GLdouble bottom,GLdo
3、uble top,GLdouble near,GLdouble far),建立正交投影矩阵,定义一个正平行观察体。间隔 从相机位置处测量。rightleft,topbottom,farnearOpenGL中不提供对观察平面的选择功能。近裁减平面永远和观察平面重合。假设OpenGL不提供投影函数,默认调用为:glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0) 3 透视投影变换设置函数void gluPerspective(GLdouble fov,GLdouble aspect, GLdouble near,GLdouble far),定义一个透视矩阵作用于当前矩阵 fov-
4、近裁剪平面与远裁剪平面的连线与视点的角度,也称视场角(field-of-view angle) aspect-投影平面的宽与高之比 near,far-近裁剪平面和远裁剪平面离相机(视点)的间隔 4三维根本图形绘制函数1立方体绘制函数void glutWireCube(GLdouble size) /线框形式void glutSolidCube(GLdouble size) /实体形式 功能:绘制一个边长为size的线框的或实心立方体,立方体的中心位于原点2小球绘制函数void glutWireSphere(GLdouble Radius, Glint slices,Glint stacks)
5、void glutSolidSphere(GLdouble Radius, Glint slices,Glint stacks);绘制一个半径为Radius的线框的的或实心小球,小球的中心点位于原点, slices: 为小球的经线数目,stacks为小球的纬线数目3茶壶绘制函数void glutWireTeapot(GLdouble size); void glutSolidTeapot(GLdouble size);绘制一个半径为size的线框的或实心茶壶,茶壶的中心位于原点 参数说明:参数size为茶壶的近似半径,以size为半径的球体可完全包容这个茶壶。4圆环绘制函数void glutW
6、ireTorus(GLdouble innerRadius, GLdouble outerRadius,Glint slices,Glint stacks);void glutWireTorus(GLdouble innerRadius, GLdouble outerRadius,Glint slices,Glint stacks);绘制一个半径为size的线框的的或实心圆环体,圆环体的中心位于原点,圆环的内径和外径由参数innerRadius, outerRadius指定innerRadius: 圆环体的内径outerRadius: 圆环体的外径 slices: 圆环体的经线数目 stack
7、s: 圆环体的纬线数目二Bezier曲线绘制1Bezier曲线绘制步骤:1.Defining Bezier curvevoid glMap1fd (GLenum target, TYPE u1, TYPE u2, GLint stride, GLint order, const TYPE *points);target: 指定控制点所描绘的内容u1, u2:曲线的参数范围(t),一般u1=0,u2=1;stride: 控制点之间的浮点数或双精度的个数order: 次数+1, 即控制点的数目points: 指向控制点的指针 2.Enabling Bezier curve glEnable(GL_
8、MAP1_VERTEX_3);3.Calculating data points4.Linking and drawing求出Bezier曲线上的详细点 void glEvalCoord1fd (TYPE u) void glEvalCoord1fdv (TYPE *u)画出Bezier曲线 glBegin(GL_LINE_STRIP); for (i = 0; i = 100; i+) glEvalCoord1f(GLfloat) i/100.0); glEnd();或void glMapGridf,d (GLint n,TYPE u1,TYPE u2);void glEvalMesh1 (
9、GLenum mode,GLint p1, GLint p2);Mode的取值可以是GL_POINT或GL_LINE 相当于glBegin(GL_LINE_STRIP); for (i = p1;= p2; glEvalCoord1f(u1+i*(u2-u1)/n);六、附属程序1.3D Cube.cpp 静止立方体程序#include void display() glClear(GL_COLOR_BUFFER_BIT); /清屏 glMatrixMode(GL_MODELVIEW); /矩阵形式设置 glLoadIdentity(); /清空矩阵堆栈 gluLookAt(1.4,1.0,0
10、.8,0.0,0.0,0.0,0.0,1.0,0.0); /设置视点 / gluLookAt(1,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0); glColor3f(1,0,0); / glutWireCube(0.5); glutSolidCube(0.5); /绘制立方体,立方体中心在坐标原点 glColor3f(0,0,1); glutWireCube(0.5); /绘制线框立方体,表达边框效果 glutSwapBuffers(); void reshape(int w,int h) glViewport(0,0,w,h); glMatrixMode(GL_PROJE
11、CTION); glOrtho(-1,1,-1,1,2.5); /定义三维观察体void init() glClearColor(0.0,0.0,0.0,0.0); glLineWidth(3); /glColor3f(1.0,1.0,1.0); int main(int argc, char* argv) glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(800,800); glutInitWindowPosition(0,0); glutCreateWindow(cube)
12、; glutReshapeFunc(reshape); glutDisplayFunc(display); init(); glutMainLoop();2.3D Cube2.exe 旋转立方体参考程序#include math.hint flag=0;/GLfloat vertices3=-1.0,-1.0,1.0,-1.0,1.0,1.0,1.0,1.0,1.0,1.0,-1.0,1.0,-1.0,-1.0,-1.0,/-1.0,1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0,-1.0;GLfloat vertices3=-1.0,-1.0,-1.0,1.0,-1.0,-1.
13、0,1.0,1.0,-1.0,-1.0,1.0,-1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0,1.0,1.0,1.0,-1.0,1.0,1.0;GLfloat colors3=1.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0,1.0,0.0,1.0,0.0,1.0,1.0,1.0,1.0,1.0; glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glEnable(GL_DEPTH_TEST);void polygon(int a,int b,int c,int d) /*
14、 draw a polygon via list of vertices */ if (flag=0) glBegin(GL_POLYGON); glColor3fv(colorsa); glVertex3fv(verticesa); glColor3fv(colorsb); glVertex3fv(verticesb); glColor3fv(colorsc); glVertex3fv(verticesc); glColor3fv(colorsd); glVertex3fv(verticesd); else glColor3f(0,0,0); glBegin(GL_POLYGON);void
15、 colorcube(void) /* map vertices to faces */ polygon(0,3,2,1); polygon(2,3,7,6); polygon(0,4,7,3); polygon(1,2,6,5); polygon(4,5,6,7); polygon(0,1,5,4);static GLfloat theta=0.0,0.0,0.0;static GLint axis=2; / glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); gluLookAt(3,
16、3,3,0,0,0,0,1,0); glRotatef(theta0,1.0,0.0,0.0); glRotatef(theta1,0.0,1.0,0.0); glRotatef(theta2,0.0,0.0,1.0); flag=0; colorcube(); /绘制彩色立方体 glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); flag=1; void spinCube()1; if (thetaaxis360.0) thetaaxis-=360.0; glutPostRedisplay(); void mouse(int btn, int state,in
17、t x, int y) if (btn=GLUT_LEFT_BUTTON & state=GLUT_DOWN) axis=0; if (btn=GLUT_MIDDLE_BUTTON & state=GLUT_DOWN) axis=1; if (btn=GLUT_RIGHT_BUTTON & state=GLUT_DOWN) axis=2; glViewport(0,0,w,h); /定义正交投影观察体 if (w=h) glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,1.0,20.0); glOrtho
18、(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,1.0,20.0); /gluPerspective(120,w/h,1,60); /定义透视投影投影观察体 void main(int argc, char* argv) glutInit(& / glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DETH); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);colorcube glutIdleFunc(spinCube
19、); glutMouseFunc(mouse);bezierCurve.cpp /Bezier曲线绘制程序/ The number of control points for this curveGLint nNumPoints = 4; /control point group1GLfloat ctrlPoints43= -4.0f, 0.0f, 0.0f, / End Point -6.0f, 4.0f, 0.0f, / Control Point 6.0f, -4.0f, 0.0f, / Control Point 4.0f, 0.0f, 0.0f ; / End Pointvoid C
20、hangeSize(int w, int h);void DrawPoints(void);void RenderScene(void);void SetupRC();int main(int argc, char *argv) glutInit(&argc, argv); /初始化GLUT库; glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); /设置显示形式;缓冲,颜色类型 glutInitWindowSize(500, 500); glutInitWindowPosition(1024 / 2 - 250, 768 / 2 - 250);Bezier
21、 Curve /创立窗口,标题为“Rotating 3D World; glutReshapeFunc(ChangeSize); SetupRC(); glutDisplayFunc(RenderScene); /用于绘制当前窗口; /表示开始运行程序,用于程序的结尾; return 0;void DrawPoints(void) int i; / Counting variable / Set point size larger to make more visible glPointSize(5.0f); / Loop through all control points for this
22、 example glBegin(GL_POINTS); for(i = 0; nNumPoints; i+) glVertex2fv(ctrlPointsi);/ Called to draw scenevoid RenderScene(void) / Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT); / Sets up the bezier / This actually only needs to be called once and could go in / the setup fun
23、ction glMap1f(GL_MAP1_VERTEX_3, / Type of data generated 0.0f, / Lower u range 100.0f, / Upper u range 3, / Distance between points in the data nNumPoints, / number of control points &ctrlPoints00); / array of control points / Enable the evaluator glEnable(GL_MAP1_VERTEX_3); / Use a line strip to co
24、nnect-the-dots for(i = 0; / Evaluate the curve at this point glEvalCoord1f(GLfloat) i); / Use higher level functions to map to a grid, then evaluate the / entire thing. / Put these two functions in to replace above loop / Map a grid of 100 points from 0 to 100 /glMapGrid1d(100,0.0,100.0); / Evaluate the grid, using lines /glEvalMesh1(GL_LINE,0,100); / Draw the Control Points DrawPoints(); / Flush drawing commands/ This function does any needed initialization on the rendering/ context. void SetupRC() / Clear Window to white glClearColor(1.0f, 1.0f, 1.0f, 1.0f ); /
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1