计算机图形学——简单图形绘制

图元的绘制

1.在屏幕上绘制几何图元(如下图所示),自定义坐标和颜色。

2.采用中点圆算法在屏幕上画一个圆。

二、实验环境

dev-c IDE

MinGW32编译器

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;

}

实验二



#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;

}

你可能感兴趣的:(计算机图形学——简单图形绘制)