OpenGL 练习10 Approximation of Sphere
来源:互联网 发布:信息过滤软件 编辑:程序博客网 时间:2024/06/05 21:14
Nice!
#include<gl/glut.h>#include<math.h>#include<stdlib.h> //a point data typetypedef GLfloat point3[3]; point3 v[] = {{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.333333}, {-0.816497, -0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}}; static int k = 5; void triangle(point3 a, point3 b, point3 c){ glVertex3fv(a); glVertex3fv(b); glVertex3fv(c);} void normalize(point3 v1, point3 v2, point3 &v3){ float len = sqrt((v1[0]+v2[0])*(v1[0]+v2[0])+(v1[1]+v2[1])*(v1[1]+v2[1])+(v1[2]+v2[2])*(v1[2]+v2[2])); v3[0] = (v1[0]+v2[0])/len; v3[1] = (v1[1]+v2[1])/len; v3[2] = (v1[2]+v2[2])/len;} void divide_triangle(point3 a, point3 b, point3 c, int k){ point3 ab, ac, bc; normalize(a, b, ab); normalize(a, c, ac); normalize(b, c, bc); if (k > 0) { divide_triangle(a, ac, ab, k-1); divide_triangle(c, bc, ac, k-1); divide_triangle(b, ab, bc, k-1); divide_triangle(ab, ac, bc, k-1); } else { triangle(a, b, c); }}void tetra(){ divide_triangle(v[0], v[1], v[2], k); divide_triangle(v[3], v[2], v[1], k); divide_triangle(v[0], v[3], v[1], k); divide_triangle(v[0], v[2], v[3], k);} void renderScene(void){ glClearColor(1.0, 1.0, 1.0, 0.0);//注意事项:要在glClear之前设置color! glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// 清除屏幕及深度缓存 glMatrixMode(GL_PROJECTION);//投影矩阵 glLoadIdentity(); gluPerspective(45, 1, 1, 1000); glMatrixMode(GL_MODELVIEW);//模型矩阵 glLoadIdentity(); gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0); glEnable(GL_DEPTH_TEST); // 设置正反面为填充模式 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBegin(GL_TRIANGLES); glColor3f(0.0f, 1.0f, 0.0f);//设置顶点颜色 //tetra(v[0], v[1], v[2], v[3], 6); tetra(); glEnd(); glutSwapBuffers();//当窗口模式为双缓存时,此函数的功能就是把后台缓存的内容交换到前台显示。当然,只有单缓存时,使用它的功能跟用glFlush()一样。而使用双缓存是为了把完整图画一次性显示在窗口上,或者是为了实现动画。} int main(int argc, char *argv[]){ glutInit(&argc, argv);//初始化GLUT glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);//设置图形显示模式。GLUT_DEPTH:使用深度缓存;GLUT_DOUBLE:使用双缓存; glutInitWindowPosition(100, 100);//设置窗口显示位置 glutInitWindowSize(600,600);//设置窗口大小 glutCreateWindow("Approximation of Sphere");//创建带标题的窗口 glutDisplayFunc(renderScene);//为当前窗口设置显示回调函数 glutMainLoop();//进入GLUT事件处理循环 return 0;}
0 0
- OpenGL 练习10 Approximation of Sphere
- OpenGL 练习02 Sphere
- Shader assembly of fresnel approximation
- OpenGL显示旋转的sphere
- 读书笔记 day1:The design of approximation algorithms
- The Approximation of One Matrix by Another of Lower Rank
- Sphere
- Tessellation of sphere by a recursive method
- fractal approximation of surfaces based on projected IFS attractors
- Fast implementation/approximation of pow() function in C/C++
- Generalized Low Rank Approximation of Matrices
- opengl 练习
- 【OpenGL】opengl练习
- OpenGL实现的一个wire-sphere的小程序
- Approximation Algorithms
- (笔记)神经网络压缩,Ristretto: Hardware-Oriented Approximation of Convolutional Neural Networks(一)
- OpenGL小练习一
- opengl之综合练习
- OpenGL 练习09 Walking through a scene
- UML几种类图关系的总结
- java中的NaN
- 非root用户用bluefish保存文件时报permission denied错误解决办法(之一)
- linux 下释放cache、swap
- OpenGL 练习10 Approximation of Sphere
- 2013级C++第12周(春)项目——成员的访问属性、多重继承
- 第四周作业
- QGIS编译
- 多益机试题目--字符串匹配的变形
- [LeetCode]Reverse Words in a String
- ContentProvider 实例
- 《漫画线性代数》读书笔记 序
- 昨天是母亲节,祝母亲天天开心,永远健康