1 / 20

计算机图形学

Computer Graphics. 计算机图形学. 徐文鹏 wpxu@hpu.edu.cn 教学博客 http:// Opengl.cnblogs.com 个人博客 http://wpxu21st.blog.163.com. Opengl 三维图形. 提供一个更复杂的三维图形示例 分形: Sierpinski gasket 垫 介绍隐藏面消除. 三维图形应用程序. 在 OpenGL 中二维应用程序是三维应用程序的特殊情形 要得到三维图形应用程序 只需对前面程序进行很小的修改 使用 glVertex3*( ) 必须考虑多边形绘制的顺序或者启用隐藏面消除功能

Download Presentation

计算机图形学

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Graphics 计算机图形学 徐文鹏 wpxu@hpu.edu.cn 教学博客http:// Opengl.cnblogs.com 个人博客http://wpxu21st.blog.163.com

  2. Opengl三维图形 • 提供一个更复杂的三维图形示例 • 分形:Sierpinski gasket垫 • 介绍隐藏面消除

  3. 三维图形应用程序 • 在OpenGL中二维应用程序是三维应用程序的特殊情形 • 要得到三维图形应用程序 • 只需对前面程序进行很小的修改 • 使用glVertex3*( ) • 必须考虑多边形绘制的顺序或者启用隐藏面消除功能 • 只考虑平面的简单凸多边形

  4. Sierpinski Gasket (2D) • 从一个三角形开始 • 连接三边的中点并去掉中间的三角形 • 重复上述过程

  5. Example • 五次细分后的结果

  6. The gasket as a fractal分形 • 考虑黑色填充区域的面积与周长(即包含填充 • 区域的所有线段总长) • 当持续细分时 • 面积趋向于零 • 但周长趋向于无穷 • 因此无穷细分后的结果不是通常的几何形状 • 它的维数既不是一维的,也不是二维的 • 我们称之为分形

  7. Gasket Program #include <GL/glut.h> // a point data type typedef GLfloat point2d[2]; /* initial triangle */ GLfloat v[3][2]={{-1.0, -0.58}, {1.0, -0.58}, {0.0, 1.15}}; int n; /* number of recursive steps */

  8. Draw one triangle绘制三角形 // display one triangle void triangle( point2d a, point2d b, point2d c) { glBegin(GL_TRIANGLES); glVertex2fv(a); glVertex2fv(b); glVertex2fv(c); glEnd(); }

  9. Triangle Subdivision三角形细分 // triangle subdivision using vertex numbers void divide_triangle(point2d a, point2d b, point2d c, int m) { point2d v0, v1, v2; int j; if(m>0) { for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2; for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2; for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2; divide_triangle(a, v0, v1, m-1); divide_triangle(c, v1, v2, m-1); divide_triangle(b, v2, v0, m-1); } // draw triangle at end of recursion else(triangle(a,b,c)); }

  10. 显示与初始化函数 void display(void) { glClear(GL_COLOR_BUFFER_BIT); divide_triangle(v[0], v[1], v[2], n); glFlush(); } void myinit() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-2.0, 2.0, -2.0, 2.0); glMatrixMode(GL_MODELVIEW); glClearColor(1.0, 1.0, 1.0,1.0); glColor3f(0.0,0.0,0.0); }

  11. main Function函数 int main(int argc, char **argv) { n=2; glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow(“2D Gasket"); glutDisplayFunc(display); myinit(); glutMainLoop(); }

  12. Moving to 3D改变到三维的情形 • 通过下述修改,可以很容易地使程序生成三维图形 Typedef Glfloat point3d[3] glVertex3f glOrtho • 但这并不没有多大实质性改变 • 下面从四面体开始迭代

  13. 3D Gasket • 细分四面体的四个面 • Appears as if we remove a solid tetrahedron from the center leaving four smaller tetrahedron

  14. tetrahedron code生成四面体的代码 void tetra(point3d a, point3d b, point3d c, point3d d) { glColor3f(1.0, 0.0, 0.0); triangle(a,b,c); glColor3f(0.0, 1.0, 0.0); triangle(a,c,d); glColor3f(0.0, 0.0, 1.0); triangle(a,d,b); glColor3f(0.0, 0.0, 0.0); triangle(b,d,c); }

  15. triangle code生成三角形的代码 void triangle( point3d a, point3d b, point3d c) { glVertex3fv(a); glVertex3fv(b); glVertex3fv(c); }

  16. subdivision code细分代码 // triangle subdivision using vertex numbers void divide_triangle(point2d a, point2d b, point2d c, int m) { point2d v0, v1, v2; int j; if(m>0) { for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2; for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2; for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2; divide_triangle(a, v0, v1, m-1); divide_triangle(c, v1, v2, m-1); divide_triangle(b, v2, v0, m-1); } else(triangle(a,b,c));// draw triangle at end of recursion }

  17. Example结果 • 五次迭代后

  18. 问题 • 由于三角形是按照在程序中定义的顺序画出的,本来在前面的三角形并不是显示在位于它后面的三角形的前面 get this want this

  19. 隐藏面的消除 • 只想见到那些位于其它面前面的曲面或平面片 • OpenGL采用称为z缓冲区的算法进行隐藏面消除,在z缓冲区中存贮着对象的深度信息,从而只有前面的对象出现在图像中

  20. Z缓冲区算法 • 在该算法中创建专门的缓冲区(称为z缓冲区),当几何体经过流水线各步骤时,存贮着该几何体的深度信息 • 启用该算法的要素 • 在main()中 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); • 在init()中启用 glEnable(GL_DEPTH_TEST); • 在显示回调函数中 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

More Related