头歌计算机图形学CG2-v1.1-Line and Circle Rasterization

1 Task Description

Modify the code on the right side according to the following requirements to draw the expected output image.The system will test the code you write.

代码:

#include <GL/freeglut.h>

#include<stdio.h>

// The header file used to evaluate the code - begin

#include<opencv2/core/core.hpp>

#include<opencv2/highgui/highgui.hpp>

#include<opencv2/imgproc/imgproc.hpp>

// The header file used to evaluate the code - end


 

void LineDDA(int x1, int y1, int x2, int y2)

{

    // Please add your code here

    /********** Begin ********/

    int dx=x2-x1;

    int dy=y2-y1;

    int steps,k;

    float xIncrement,yIncrement;

    float x=x1,y=y1;

    if(abs(dx)>abs(dy))

    steps=abs(dx);

    else

    steps=abs(dy);

    xIncrement=dx/(float)steps;

    yIncrement=dy/(float)steps;

    glColor3f(1.0f,1.0f,1.0f);

    glPointSize(1);

    for(int i=0;i<=steps;i++){

        glBegin(GL_POINTS);

        glVertex2i((int)(x+0.5),(int)(y+0.5));

        glEnd();

        x+=xIncrement;

        y+=yIncrement;

    }

    /********** End **********/

}

void myDisplay(void)

{

    glClear(GL_COLOR_BUFFER_BIT);

   // Please add your code here

   /********** Begin ********/    

    LineDDA(0,0,200,300);

    LineDDA(0,0,300,200);

   /********** End **********/

    glFlush();

}

void Init()

{

    glClearColor(0.0, 0.0, 0.0, 0.0);

    glShadeModel(GL_SMOOTH);

}

void myReshape(int w, int h)

{

    glViewport(0, 0, (GLsizei)w, (GLsizei)h);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);

}

int main(int argc, char *argv[])

{

    glutInit(&argc, argv);

    glutInitWindowPosition(100, 100);

    glutInitWindowSize(400, 400);

    glutCreateWindow("Hello Point!");

    Init();

    glutDisplayFunc(myDisplay);

    glutReshapeFunc(myReshape);

    glutMainLoopEvent();    

     

     

     

    //The following is the evaluation code, which has nothing to do with the content of this experiment. Please do not modify it//

    GLubyte* pPixelData = (GLubyte*)malloc(400 * 400 * 3);

    GLint viewport[4] = {0};

    glReadBuffer(GL_FRONT);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

    glGetIntegerv(GL_VIEWPORT, viewport);

    glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);

     cv::Mat img;

    std::vector<cv::Mat> imgPlanes;

    img.create(400, 400, CV_8UC3);

    cv::split(img, imgPlanes);

        for(int i = 0; i < 400; i ++) {

            unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);

            unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);

            unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);

            for(int j = 0; j < 400; j ++) {

                int k = 3 * (i * 400 + j);

                plane2Ptr[j] = pPixelData[k];

                plane1Ptr[j] = pPixelData[k+1];

                plane0Ptr[j] = pPixelData[k+2];

            }

        }

        cv::merge(imgPlanes, img);

        cv::flip(img, img ,0);

        cv::namedWindow("openglGrab");

        cv::imshow("openglGrab", img);

        //cv::waitKey();

        cv::imwrite("../img_step4/test.jpg", img);

    return 0;

}

第二关:

1.1 Task Requirements
  • Familiar with programming environment;
  • Understand the characteristics of raster graphic display;
  • Take OpenGL as a development platform design procedures, understand and master "Midpoint Method"in order to be able to draw lines.
  • 代码:
  • #include <GL/freeglut.h>

    #include<stdio.h>

    // The header file used to evaluate the code - begin

    #include<opencv2/core/core.hpp>

    #include<opencv2/highgui/highgui.hpp>

    #include<opencv2/imgproc/imgproc.hpp>

    // The header file used to evaluate the code - end


     

    void MidPLine(int x1, int y1, int xn, int yn)

    {

       // Please add your code here

       /********** Begin ********/

        int dx,dy,dt,db,d,x,y;

        dx=xn-x1;

        dy=yn-y1;

        d=dx-2*dy;

        dt=2*dx-2*dy;

        db=-2*dy;

        x=x1;y=y1;

        glColor3f(0.0f,3.0f,0.0f);

        glPointSize(1);

        glBegin(GL_POINTS);

        glVertex2i(x,y);

        glEnd();

        while(x<xn){

            if(d<0){

                x++;

                y++;

                d+=dt;

            }

            else{

                x++;

                d+=db;

            }

            glBegin(GL_POINTS);

            glVertex2i(x,y);

            glEnd();

        }

       /********** End **********/

    }

    void myDisplay(void)

    {

       glClear(GL_COLOR_BUFFER_BIT);

       // Please add your code here

       /********** Begin ********/

       MidPLine( 10,50 , 300,260 );

       /********** End **********/

        glFlush();

    }

    void Init()

    {

        glClearColor(0.0, 0.0, 0.0, 0.0);

        glShadeModel(GL_SMOOTH);

    }

    void myReshape(int w, int h)

    {

        glViewport(0, 0, (GLsizei)w, (GLsizei)h);

        glMatrixMode(GL_PROJECTION);

        glLoadIdentity();

        gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);

    }

    int main(int argc, char *argv[])

    {

        glutInit(&argc, argv);

        glutInitWindowPosition(100, 100);

        glutInitWindowSize(400, 400);

        glutCreateWindow("Hello Point!");

        Init();

        glutDisplayFunc(myDisplay);

        glutReshapeFunc(myReshape);

        glutMainLoopEvent();    

         

         

         

        //The following is the evaluation code, which has nothing to do with the content of this experiment. Please do not modify it//

        GLubyte* pPixelData = (GLubyte*)malloc(400 * 400 * 3);

        GLint viewport[4] = {0};

        glReadBuffer(GL_FRONT);

        glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

        glGetIntegerv(GL_VIEWPORT, viewport);

        glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);

         cv::Mat img;

        std::vector<cv::Mat> imgPlanes;

        img.create(400, 400, CV_8UC3);

        cv::split(img, imgPlanes);

            for(int i = 0; i < 400; i ++) {

                unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);

                unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);

                unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);

                for(int j = 0; j < 400; j ++) {

                    int k = 3 * (i * 400 + j);

                    plane2Ptr[j] = pPixelData[k];

                    plane1Ptr[j] = pPixelData[k+1];

                    plane0Ptr[j] = pPixelData[k+2];

                }

            }

            cv::merge(imgPlanes, img);

            cv::flip(img, img ,0);

            cv::namedWindow("openglGrab");

            cv::imshow("openglGrab", img);

            //cv::waitKey();//

            cv::imwrite("../img_step5/test.jpg", img);

        return 0;

    }

    第三关:

  • #include <GL/freeglut.h>

    #include<stdio.h>

    // The header file used to evaluate the code - begin

    #include<opencv2/core/core.hpp>

    #include<opencv2/highgui/highgui.hpp>

    #include<opencv2/imgproc/imgproc.hpp>

    // The header file used to evaluate the code - end


     

    //Eight points of symmetry are formed on the circumference of the circle

    void CirclePoints(int x0, int y0, int x, int y)

    {

        // Please add your code here

        /********** Begin ********/

        glColor3f(0.0f,1.0f,1.0f);

        glPointSize(2);

        glBegin(GL_POINTS);

        glVertex2f(x0+x, y0+y);

        glVertex2f(x0+y, y0+x);

        glVertex2f(x0-y, y0+x);

        glVertex2f (x0-x, y0+y);

        glVertex2f (x0-x, y0-y);

        glVertex2f (x0-y, y0-x);

        glVertex2f (x0+y, y0-x);

        glVertex2f(x0+x, y0-y);

        glEnd();

        /********** End **********/

    }

    void MidPoint_Circle(int x0, int y0, int r)

    {

        // Please add your code here

        /********** Begin ********/

        int x,y,d;

        x=0;y=r;d=1-r;

        CirclePoints(x0,y0,x,y);

        while(x<y)

        {

            if(d<0)

                d+=2*x+3;

            else

            {

                d+=2*(x-y)+5;

                y--;

            }

            x++;

            CirclePoints(x0,y0,x,y);

        }

        /********** End **********/

    }

    void myDisplay(void)

    {

        glClear(GL_COLOR_BUFFER_BIT);

        // Please add your code here

        /********** Begin ********/

        //Write down the center coordinates and the radius of the circle in turn

        MidPoint_Circle(200,200,150);    

        /********** End **********/

        glFlush();

    }

    void Init()

    {

        glClearColor(0.0, 0.0, 0.0, 0.0);

        glShadeModel(GL_SMOOTH);

    }

    void myReshape(int w, int h)

    {

        glViewport(0, 0, (GLsizei)w, (GLsizei)h);

        glMatrixMode(GL_PROJECTION);

        glLoadIdentity();

        gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);

    }

    int main(int argc, char* argv[])

    {

        glutInit(&argc, argv);

        glutInitWindowPosition(100, 100);

        glutInitWindowSize(400, 400);

        glutCreateWindow("Hello circle!");

        Init();

        glutDisplayFunc(myDisplay);

        glutReshapeFunc(myReshape);

        glutMainLoopEvent();

        //The following is the evaluation code, which has nothing to do with the content of this experiment. Please do not modify it//

        GLubyte* pPixelData = (GLubyte*)malloc(400 * 400 * 3);

        GLint viewport[4] = { 0 };

        glReadBuffer(GL_FRONT);

        glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

        glGetIntegerv(GL_VIEWPORT, viewport);

        glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);

        cv::Mat img;

        std::vector<cv::Mat> imgPlanes;

        img.create(400, 400, CV_8UC3);

        cv::split(img, imgPlanes);

        for (int i = 0; i < 400; i++) {

            unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);

            unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);

            unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);

            for (int j = 0; j < 400; j++) {

                int k = 3 * (i * 400 + j);

                plane2Ptr[j] = pPixelData[k];

                plane1Ptr[j] = pPixelData[k + 1];

                plane0Ptr[j] = pPixelData[k + 2];

            }

        }

        cv::merge(imgPlanes, img);

        cv::flip(img, img, 0);

        cv::namedWindow("openglGrab");

        cv::imshow("openglGrab", img);

        //cv::waitKey();//

        cv::imwrite("../img_step3/test.jpg", img);

        return 0;

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值