コンピュータグラフィックス-単純なグラフィック描画

53150 ワード

エンティティの描画
1.画面にジオメトリ要素(下図のように)を描画し、座標と色をカスタマイズします.
2.中点円アルゴリズムを用いて画面に円を描く.
二、実験環境
dev-c IDE
MinGW 32コンパイラ
Openglのいくつかの拡張パッケージ
三、実験一
画面にジオメトリを描画し、座標と色をカスタマイズします.
関数ライブラリの使用に熟練しています.
じっけんけっか
四、実験二
中点円アルゴリズムを使用して画面に円を描きます.
アルゴリズム:


  • MidPointCirclアルゴリズムは1/8個の円を描く


  • 円の八方対称性を利用して、他の7ブロックの座標値を配列に追加します.


  • 配列内の点を描く
    実験結果:
    五、コード実験一
    
    
    #include
    
    #include
    
    
    void
    myinit()
    
    {
    
        glClearColor(0.0,0.0,0.0,0.0);
    
    }
    
    void
    ChangeSize(GLsizei w,GLsizei h)
    
    {
    
        glViewport(0,0,w,h);
    
        glMatrixMode(GL_PROJECTION);
    
        glLoadIdentity();
    
        if(w<=h)
    
            glOrtho(-20.0,20.0,-20.0*(GLfloat)h/(GLfloat)w,20.0*(GLfloat)h/(GLfloat)w,-50.0,50.0);
    
        else
    
            glOrtho(-20.0*(GLfloat)h/(GLfloat)w,20.0*(GLfloat)h/(GLfloat)w,-20.0,20.0,-50.0,50.0);
    
        glMatrixMode(GL_MODELVIEW);
    
        glLoadIdentity();
    
    }
    
    void
    DrawMyObjects()
    
    {   
    
        
    
        //  
    
        glBegin(GL_POINTS);
    
            glColor3f(1.0,0.0,0.0);
    
            glVertex2f(-10.0,10.0);
    
            glColor3f(0.0,1.0,0.0);
    
            glVertex2f(10.0,10.0);
    
            glColor3f(0.0,0.0,1.0);
    
            glVertex2f(-10.0,-10.0);
    
        glEnd();
    
        //   
    
        glBegin(GL_LINES);
    
            glColor3f(1.0,1.0,1.0);
    
            glVertex2f(-11.0,11.0);
    
            glVertex2f(-6.0,2.0);
    
            glColor3f(1.0,1.5,0.0);
    
            glVertex2f(-9.0,10.0);
    
            glVertex2f(-8.0,3.0);
    
        glEnd();
    
        //    
    
        glBegin(GL_LINE_STRIP);
    
            glColor3f(1.0,1.0,1.0);
    
        //  glPointSize(5);
    
            glVertex2f(-3.0,9.0);
    
            glVertex2f(3.0,6.0);
    
            glVertex2f(2.0,3.0);
    
            glVertex2f(-2.5,6.5);
    
        glEnd();
    
        //    
    
        glBegin(GL_LINE_LOOP);
    
            glColor3f(1.0,1.0,1.0);
    
            glVertex2f(2.0,7.0);
    
            glVertex2f(5.0,6.0);
    
            glVertex2f(3.0,4.5);
    
            glVertex2f(10.3,7.5);
    
        glEnd();
    
        //      
    
        glBegin(GL_POLYGON);
    
            glColor3f(0.8,0.3,0.0);
    
            glVertex2f(-5.0,0.0);
    
            glVertex2f(-8.0,1.0);
    
            glVertex2f(-10.3,0.5);
    
            glVertex2f(-7.5,-2.0);
    
            glVertex2f(-6.0,-1.0);
    
        glEnd();
    
        //    
    
        glBegin(GL_QUADS);
    
            glColor3f(0.4,0.2,0.9);
    
            glVertex2f(1.0,2.0);
    
            glVertex2f(-1.0,3.0);
    
            glVertex2f(-3.3,0.5);
    
            glVertex2f(-0.5,-1.0);
    
        glEnd();
    
        //      
    
        glBegin(GL_QUAD_STRIP);
    
            glVertex2f(7.0,-2.0);
    
            glVertex2f(5.5,1.0);
    
            glVertex2f(8.0,-1.0);
    
            glColor3f(1.0,1.0,0.1);
    
            glVertex2f(9.0,2.0);
    
            glVertex2f(11.0,-2.0);
    
            glColor3f(1.0,0.3,1.0);
    
            glVertex2f(11.0,2.0);
    
            glVertex2f(13.0,-1.0);
    
            glColor3f(0.6,1.0,1.0);
    
            glVertex2f(14.0,1.0);
    
        glEnd();
    
        //    
    
        glBegin(GL_TRIANGLES);
    
            glColor3f(0.3,0.4,0.5);
    
            glVertex2f(10.0,5.0);
    
            glVertex2f(12.3,7.5);
    
            glVertex2f(8.5,6.0);
    
        glEnd();
    
        //      
    
        glBegin(GL_TRIANGLE_STRIP);
    
            glVertex2f(-1.0,-8.0);
    
            glVertex2f(-2.5,-5.0);
    
            glColor3f(0.3,0.8,0.0);
    
            glVertex2f(0.0,-2.0);
    
            glColor3f(0.0,0.4,0.2);
    
            glVertex2f(1.0,-4.0);
    
            glColor3f(0.7,0.1,0.4);
    
            glVertex2f(4.0,-6.0);
    
        glEnd();
    
        //      
    
        glBegin(GL_TRIANGLE_FAN);
    
            glVertex2f(8.0,-6.0);
    
            glVertex2f(10.0,-3.0);
    
            glColor3f(0.8,0.5,0.5);
    
            glVertex2f(12.5,-4.5);
    
            glColor3f(0.3,0.7,0.1);
    
            glVertex2f(13.0,-7.5);
    
            glColor3f(0.2,0.5,0.2);
    
            glVertex2f(10.5,-9.0);            
    
        glEnd();            
    
    }
    
    void
    RenderScene()
    
    {
    
        glClear(GL_COLOR_BUFFER_BIT);
    
        glColor3f(1.0,1.0,0.0);
    
        DrawMyObjects();
    
        glFlush();
    
    }
    
    int
    main()
    
    {
    
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    
        glutInitWindowSize(800,800);
    
        glutInitWindowPosition(0,0);
    
        glutCreateWindow("test1");
    
        myinit();
    
        glutDisplayFunc(RenderScene);
    
        glutReshapeFunc(ChangeSize);
    
        glutMainLoop();
    
        return 0;
    
    }
    
    

    実験2
    
    
    #include
    "windows.h"
    
    #include
    <glut.h>
    
     
    
    void
    myinit()
    
    {
    
        glClearColor(0.0,0.0,0.0,0.0);
    
     
    
    }
    
    void
    ChangeSize(GLsizei w,GLsizei h)
    
    {
    
        glViewport(0,0,w,h);
    
        glMatrixMode(GL_PROJECTION);
    
        glLoadIdentity();
    
     
    
        if(w<=h)
    
            glOrtho(-200.0,200.0,-200.0*(GLfloat)h/(GLfloat)w,200.0*(GLfloat)h/(GLfloat)w,-50.0,50.0);
    
        else
    
            glOrtho(-200.0*(GLfloat)h/(GLfloat)w,200.0*(GLfloat)h/(GLfloat)w,-20.0,20.0,-50.0,50.0);
    
        glMatrixMode(GL_MODELVIEW);
    
        glLoadIdentity();
    
    }
    
    void  pixel(int x,int y,int color)
    
    {    
    
           if(color==0) {
    
           
    glBegin(GL_POINTS);
    
         glColor3f(1.0,0.0,0.0);
    
            glVertex2f(x,y);
    
            glEnd();   }
    
            else if(color==1) {
    
           
    glBegin(GL_POINTS);
    
         glColor3f(0.0,1.0,0.0);
    
            glVertex2f(x,y);
    
            glEnd();       }
    
            else if(color==2) {
    
           
    glBegin(GL_POINTS);
    
         glColor3f(0.0,0.0,2.0);
    
            glVertex2f(x,y);
    
            glEnd() 
    ;     }
    
     
    
    }
    
    void  midcircle(int r,int color)
    
    {
    
        int x=0,y=r,d;
    
        d=1-r;
    
        pixel(x,y,color);
    
        pixel(y,x,color);
    
        pixel(x,-y,color);
    
        pixel(-y,x,color);
    
        for(x=0;x<y;++x)
    
        {
    
            if(d<0)
    
            {
    
                d+=2*x+3;
    
            }
    
            else
    
            {
    
                d+=2*x-2*y+5;
    
                y--;
    
            }
    
               
    pixel(x,y,color);
    
                pixel(y,x,color);
    
                pixel(y,-x,color);
    
                pixel(x,-y,color);
    
                pixel(-x,-y,color);
    
                pixel(-y,-x,color);
    
                pixel(-y,x,color);
    
                pixel(-x,y,color);
    
        }           
    
    }
    
    void
    RenderScene()
    
    {
    
        glClear(GL_COLOR_BUFFER_BIT);
    
        glColor3f(0.0,1.0,0.8);
    
        midcircle(100,1);
    
        glFlush();
    
    }
    
    int
    main()
    
    {
    
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    
        glutInitWindowSize(800,800);
    
        glutInitWindowPosition(0,0);
    
        glutCreateWindow("test2");
    
        myinit();
    
        glutDisplayFunc(RenderScene);
    
        glutReshapeFunc(ChangeSize);
    
        glutMainLoop();
    
        return 0;
    
    }