zoukankan      html  css  js  c++  java
  • OpenGL编程 基础篇(二)Sierpinski垫片

    虽然网上有很多博客讲解OpenGL,但是为了打好基础,有一个好的知识体系结构,阅读经典的书籍是首选,博客适合用来快速了解某些知识。

    这几天在阅读Computer Graphics with OpenGL,本篇文章实现的是书中第二章的程序——Sierpinski垫片

    简单介绍:

    谢尔宾斯基三角形(英语:Sierpinski triangle)是一种分形,由波兰数学家谢尔宾斯基在1915年提出。它是自相似集的例子。

    代码:

    #include "stdafx.h"
    #include <cstdlib>
    #include <glglut.h>
    class GLintPoint
    {
    public:
        GLint x;
        GLint y;
        GLintPoint(GLint a, GLint b){
            x = a;
            y = b;
        }
    };
    void myInit(void)
    {
        glClearColor(1.0, 1.0, 1.0, 0.0);
        glColor3f(0.0f, 0.0f, 0.0f);
        glPointSize(2.0);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0.0, 800.0, 0.0, 600.0);
    }
    void drawDot(GLint x, GLint y){
        glBegin(GL_POINTS);
        glVertex2i(x, y);
        glEnd();
    }
    void myDisplay(){
        glClear(GL_COLOR_BUFFER_BIT);
        GLintPoint T[3] = { GLintPoint(10, 10), GLintPoint(600, 10), GLintPoint(300, 600) };
        int index = rand() % 3;
        GLintPoint point = T[index];
        drawDot(point.x, point.y);
        for (int i = 0; i < 55000; i++){
            index = rand() % 3;
            point.x = (point.x + T[index].x) / 2;
            point.y = (point.y + T[index].y) / 2;
            printf("%d %d
    ", point.x, point.y);
            drawDot(point.x, point.y);
        }
        glFlush();
    }
    
    int main(int argc, char *argv[])
    
    {
    
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
        glutInitWindowPosition(100, 100);
        glutInitWindowSize(800, 600);
        glutCreateWindow("Sierpinski");
    
        glutDisplayFunc(&myDisplay);
        myInit();
        glutMainLoop();
    
        return 0;
    
    }

    运行效果:

    左侧为循环5000次的,右侧为循环为55000次的

  • 相关阅读:
    JDBC操作MySQL
    MySQL-SQL语句
    MySQL-环境安装、配置、卸载、系统介绍
    JavaSE-单元测试、反射、注解
    JavaSE-Stream、方法引用
    JavaSE-函数式编程接口
    JavaSE-网络编程
    Document
    Document
    Document
  • 原文地址:https://www.cnblogs.com/starryxsky/p/7129228.html
Copyright © 2011-2022 走看看