凯鲁嘎吉
用书写铭记日常,最迷人的不在远方

OpenGL实例:三角形

作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/

更多请查看:计算机图形学

1. 三角形的旋转

 1 #include <GL/glut.h>
 2 typedef GLfloat point2d[2]; // a point data type
 3 void triangle(point2d a, point2d b, point2d c) // display a triangle
 4 {
 5     glBegin(GL_TRIANGLES);
 6     glVertex2fv(a);
 7     glVertex2fv(b);
 8     glVertex2fv(c);
 9     glEnd();
10 }
11 void display(void)
12 {
13     point2d v[3] = { {-1.0, -0.58}, {1.0, -0.58}, {0.0, 1.15} }; //initial triangle vertices
14     glClear(GL_COLOR_BUFFER_BIT); // Clear display window
15     glColor3f(0.0, 0.0, 1.0); // Set fill color to blue
16     glLoadIdentity();//应该放在此处,保证每次显示正确
17     glViewport(0, 0, 300, 400); // Set left viewport
18     triangle(v[0], v[1], v[2]);
19     glColor3f(1.0, 0.0, 0.0); // Set fill color to red
20     glViewport(300, 0, 300, 400); // Set right viewport
21     glRotatef(90.0, 0.0, 0.0, 1.0); // Rotate about z axis
22     triangle(v[0], v[1], v[2]); // Display blue triangle
23     glFlush();
24 }
25 void init()
26 {
27     glMatrixMode(GL_PROJECTION);
28     //glLoadIdentity();//如果放在此处,会导致第一次显示正确,再次刷新时图形旋转
29     gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
30     glMatrixMode(GL_MODELVIEW);
31     glClearColor(1.0, 1.0, 1.0, 1.0);
32 }
33 void main(int argc, char **argv)
34 {
35     glutInit(&argc, argv);
36     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
37     glutInitWindowSize(600, 400);
38     glutCreateWindow("Triangle");
39     glutDisplayFunc(display);
40     init();
41     glutMainLoop();
42 }

参考网址:Opengl编程实例-红蓝三角形 - 图形学与可视化 - CSDN博客

2. Sierpinski gasket

方法1:非递归

 1 #include <cstdlib>
 2 #include <gl\glut.h>
 3 class GLintPoint
 4 {
 5 public:
 6     GLint x;
 7     GLint y;
 8     GLintPoint(GLint a, GLint b) {
 9         x = a;
10         y = b;
11     }
12 };
13 void myInit(void)
14 {
15     glClearColor(0.0, 0.0, 0.0, 0.0);
16     glColor3f(1.0, 0.0, 0.0);
17     glPointSize(2.0);
18     glMatrixMode(GL_PROJECTION);
19     glLoadIdentity();
20     gluOrtho2D(0.0, 600.0, 0.0, 600.0);
21 }
22 void drawDot(GLint x, GLint y) {
23     glBegin(GL_POINTS);
24     glVertex2i(x, y);
25     glEnd();
26 }
27 void myDisplay() {
28     glClear(GL_COLOR_BUFFER_BIT);
29     GLintPoint T[3] = { GLintPoint(10, 10), GLintPoint(600, 10), GLintPoint(300, 600) };
30     int index = rand() % 3;
31     GLintPoint point = T[index];
32     drawDot(point.x, point.y);
33     for (int i = 0; i < 5000; i++) {
34         index = rand() % 3;
35         point.x = (point.x + T[index].x) / 2;
36         point.y = (point.y + T[index].y) / 2;
37         drawDot(point.x, point.y);
38     }
39     glFlush();
40 }
41 int main(int argc, char *argv[])
42 {
43     glutInit(&argc, argv);
44     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
45     glutInitWindowPosition(100, 100);
46     glutInitWindowSize(500, 400);
47     glutCreateWindow("Sierphiski gasket");
48     glutDisplayFunc(&myDisplay);
49     myInit();
50     glutMainLoop();
51     return 0;
52 }

方法2:递归

 1 #include <GL/glut.h>
 2 #include<stdlib.h>
 3 /* initial triangle */
 4 GLfloat v[3][2] = { {10.0, 10.0}, {600.0, 10.0}, {300.0, 600.0} };
 5 int n;
 6 void triangle(GLfloat *a, GLfloat *b, GLfloat *c)
 7 /* display one triangle  */
 8 {
 9     glVertex2fv(a);
10     glVertex2fv(b);
11     glVertex2fv(c);
12 }
13 void divide_triangle(GLfloat  *a, GLfloat *b, GLfloat *c, int m)
14 {
15     /* triangle subdivision using vertex numbers */
16     GLfloat v0[2], v1[2], v2[2];
17     int j;
18     if (m > 0)
19     {
20         for (j = 0; j < 2; j++) v0[j] = (a[j] + b[j]) / 2;
21         for (j = 0; j < 2; j++) v1[j] = (a[j] + c[j]) / 2;
22         for (j = 0; j < 2; j++) v2[j] = (b[j] + c[j]) / 2;
23         divide_triangle(a, v0, v1, m - 1);
24         divide_triangle(c, v1, v2, m - 1);
25         divide_triangle(b, v2, v0, m - 1);
26     }
27     else triangle(a, b, c); /* draw triangle at end of recursion */
28 }
29 void display(void)
30 {
31     glClear(GL_COLOR_BUFFER_BIT);
32     glBegin(GL_TRIANGLES);
33     divide_triangle(v[0], v[1], v[2], n);
34     glEnd();
35     glFlush();
36 }
37 void myinit()
38 {
39     glMatrixMode(GL_PROJECTION);
40     glLoadIdentity();
41     gluOrtho2D(0.0, 600.0, 0.0, 600.0);
42     glMatrixMode(GL_MODELVIEW);
43     glClearColor(0.0, 0.0, 0.0, 0.0);
44     glColor3f(1.0, 0.0, 0.0);
45 }
46 void  main(int argc, char **argv)
47 {
48     n = 15;
49     glutInit(&argc, argv);
50     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
51     glutInitWindowSize(500, 400);
52     glutCreateWindow("Sierpinski Gasket");
53     glutDisplayFunc(display);
54     myinit();
55     glutMainLoop();
56 
57 }

posted on 2019-03-21 20:03  凯鲁嘎吉  阅读(223)  评论(0编辑  收藏