窗口 视口 更深一层次的使用 平铺图案 之 Truchet平铺 源代码

来源:互联网 发布:网络机顶盒十大名牌 编辑:程序博客网 时间:2024/05/21 10:24
#include<windows.h>
#include<math.h>
#include <gl/Glut.h>
#include <iostream>

using namespace std;

const GLint screenWidth = 400;
const GLint screenHeight = 400;

const GLdouble PI = 3.1415926;

void myDisplay(void);
void myInit(void);

void setWindow(GLdouble left, GLdouble right , GLdouble botton, GLdouble top);
void setViewport(GLdouble left, GLdouble right , GLdouble botton, GLdouble top);
void drawArcs(GLint num);
void drawQuarterRound();
void drawQuarterRound1();    //左下右上两个1/4圆
void drawQuarterRound2();    //左上右下两个1/4圆

int main(int argc, char ** argv){

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(screenWidth,screenHeight);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Truchet平铺");

    setWindow(0,1,0,1);
    setViewport(0,screenWidth,0,screenHeight);

    glutDisplayFunc(myDisplay);

    myInit();
    glutMainLoop();

    return 0;
}

void setViewport(GLdouble left, GLdouble right , GLdouble botton, GLdouble top){
    glViewport(left,right,right-left,top-botton);
}


void setWindow(GLdouble left, GLdouble right , GLdouble botton, GLdouble top){
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(left,right,botton,top);
}

void drawQuarterRound1(){
    for (int j = 0 ; j< 2 ; j++)    
    {
        switch(j){
        case 0:setWindow(0,2,0,2);drawQuarterRound();break;    
        case 1:setWindow(2,0,2,0);drawQuarterRound();break;    
        }
    }
}

void drawQuarterRound2(){
    for (int j = 0 ; j< 2 ; j++)    
    {
        switch(j){
        case 0:setWindow(0,2,2,0);drawQuarterRound();break;    
        case 1:setWindow(2,0,0,2);drawQuarterRound();break;    
        }
    }
}

void drawArcs(GLint randNum){
    switch(randNum){
    case 0:drawQuarterRound1();    break;
    case 1:    drawQuarterRound2();break;
    }
}


void drawQuarterRound(){
    glBegin(GL_LINE_STRIP);
    for ( GLdouble i = 0; i < (GLdouble)PI /2.0 ;i+=0.01)
    {
        glVertex2d(cos(i),sin(i));
    }
    glEnd();
    glFlush();
}

void myDisplay(void){
    glClear(GL_COLOR_BUFFER_BIT);
    
    GLint randNum ;
    for ( GLint m = 0 ; m < 20; m++)
    {
        for ( GLint n = 0 ; n < 20; n++)
        {
            randNum = rand()%2;
            cout<<randNum<<endl;
            glViewport(m*20,n*20,20,20);

            drawArcs(randNum);
        }
    }
    
}

void myInit(void){
    glClearColor(1.0,1.0,1.0,0.0);
    glColor3f(0.0f, 0.0f, 0.0f);        

    glPointSize(1.0);
    glLineWidth(2.0);

}




原创粉丝点击