头哥计算机图形学CG1-v1.2-Line 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.

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.
1.2 Expected Output

实验图片

)

代码:

#include <GL/freeglut.h>

#include <stdio.h>

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

#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 myDisplay(void)

{

    // Please add your code here

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

    glPointSize(5);        

    glBegin(GL_POINTS);

    glColor3f(1.0, 0.0, 0.0);

    glVertex2f(200, 200.0);

    glColor3f(0.0, 1.0, 0.0);

    glVertex2f(400, 200.0);

    glColor3f(0.0, 0.0, 1.0);

    glVertex2f(300, 400);

    glEnd();

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

    glFlush();

}

void Init(int width, int height)

{

    glClearColor(0.0, 0.0, 0.0, 0.0);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluOrtho2D(0.0, width, 0.0, height);

}

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

{

    int width = 600, height = 600;

    glutInit(&argc, argv);

    glutInitWindowPosition(100, 100);

    glutInitWindowSize(width, height);

    glutCreateWindow("Hello OpenGL!");

    Init(width, height);

    glutDisplayFunc(&myDisplay);

    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(width * height * 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(width, height, CV_8UC3);

    cv::split(img, imgPlanes);

    for (int i = 0; i < width; 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 < height; j++) {

            int k = 3 * (i * width + 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_step1/test.jpg", img);

    return 0;

}

第二关:

代码:

#include <GL/freeglut.h>

#include <stdio.h>

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

#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 LineWithEquation(int x1, int y1, int x2, int y2)

{

    // Please add your code here

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

    glClearColor(0.0,0.0,0.0,0.0);

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

    dx=x2-x1;

    dy=y2-y1;

    d=dx-2*dy;          

    dt=2*dx-2*dy;      

    db=-2*dy;            

    x=x1;y=y1;

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

    glPointSize(1);

    glBegin(GL_POINTS);

    glVertex2i(x,y);

    glEnd();

    while (x<x2)

    {

        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 ********/    

    LineWithEquation(0,0,300,200);

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

    glFlush();

}

void Init(int width, int height)

{

    glClearColor(0.0, 0.0, 0.0, 0.0);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluOrtho2D(0.0, width, 0.0, height);

}

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

{

    int width = 600, height = 600;

    glutInit(&argc, argv);

    glutInitWindowPosition(100, 100);

    glutInitWindowSize(width, height);

    glutCreateWindow("Hello OpenGL!");

    Init(width, height);

    glutDisplayFunc(&myDisplay);

    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(width * height * 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(width, height, CV_8UC3);

    cv::split(img, imgPlanes);

    for (int i = 0; i < width; 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 < height; j++) {

            int k = 3 * (i * width + 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::imwrite("../img_step2/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


 

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,300,200);

    LineDDA(0,0,200,300);

   /********** 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;

}

第四关:

代码:

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

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值