Touge computer graphics experiment–two-dimensional geometric transformation

Translation and zooming of squares

// Tip: After writing the code, please save it and then evaluate it.
#include <GL/freeglut.h>
#include<stdio.h>

//Header files used for evaluation code-start
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
//Header files used for evaluation code-end

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0); //Set the background color
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-5.0, 5.0, -5.0, 5.0); //Set the display range to X:-5.0~5.0, Y:-5.0~5.0
glMatrixMode(GL_MODELVIEW);
}

void myDraw(void) //Two-dimensional geometric transformation
{
   // Please add your code here
   /********** Begin **********/

    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glRectf(-1.0,-1.0,1.0,1.0);
    glTranslatef(0.0f, 2.0f, 0.0f);
    glScalef(3.0, 0.5, 1.0);
    glColor3f(1.0, 1.0, 1.0);
    glRectf(-1.0,-1.0,1.0,1.0);
    glPopMatrix();



   /********** End **********/
glFlush();
}

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

glutInit( & argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Geometric Transformation Example");
    
    init();
glutDisplayFunc( & amp;myDraw);
    glutMainLoopEvent();
     
     
    /****************The following is the evaluation code and has nothing to do with the content of this experiment. Please do not modify it**************/
    GLubyte* pPixelData = (GLubyte*)malloc(400 * 400 * 3);//Allocate memory
    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_step1/test.jpg", img);
return 0;
}

Translation and rotation of square

// Tip: After writing the code, please save it and then evaluate it.
#include <GL/freeglut.h>
#include<stdio.h>

//Header files used for evaluation code-start
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
//Header files used for evaluation code-end

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0); //Set the background color
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-5.0, 5.0, -5.0, 5.0); //Set the display range to X:-5.0~5.0, Y:-5.0~5.0
glMatrixMode(GL_MODELVIEW);
}

void myDraw(void) //Two-dimensional geometric transformation
{
   // Please add your code here
   /********** Begin **********/

     glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glRectf(-1.0,-1.0,1.0,1.0);
    glPopMatrix();
    glTranslatef(-3.0, 0.0, 0.0);
    glPushMatrix();
    glRotatef(45.0, 0.0, 0.0, 3.0);
    glColor3f(0.0, 1.0, 0.0);
    glRectf(-1.0,-1.0,1.0,1.0);
    glPopMatrix();
    glTranslatef(6.0, 0.0, 0.0);
    glPushMatrix();
    glRotatef(45.0, 0.0, 0.0, 1.0);
    glColor3f(0.0, 0.7, 0.0);
    glRectf(-1.0,-1.0,1.0,1.0);
    glFlush();



   /********** End ************/
glFlush();
}

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

glutInit( & argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Geometric Transformation Example");
    
    init();
glutDisplayFunc( & amp;myDraw);
    glutMainLoopEvent();
     
     
    /****************The following is the evaluation code and has nothing to do with the content of this experiment. Please do not modify it**************/
    GLubyte* pPixelData = (GLubyte*)malloc(400 * 400 * 3);//Allocate memory
    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_step2/test.jpg", img);
return 0;
}

Transformation combination of squares

// Tip: After writing the code, please save it and then evaluate it.
#include <GL/freeglut.h>
#include<stdio.h>

//Header files used for evaluation code-start
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
//Header files used for evaluation code-end

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0); //Set the background color
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-5.0, 5.0, -5.0, 5.0); //Set the display range to X:-5.0~5.0, Y:-5.0~5.0
glMatrixMode(GL_MODELVIEW);
}

void myDraw(void) //Two-dimensional geometric transformation
{
   // Please add your code here
   /********** Begin **********/

    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glRectf(-1.0,-1.0,1.0,1.0);
    glPopMatrix();
    glPushMatrix();
    glTranslatef(0.0f, 2.0f, 0.0f);
    glScalef(3.0, 0.5, 1.0);
    glColor3f(1.0, 1.0, 1.0);
    glRectf(-1.0,-1.0,1.0,1.0);
    glPopMatrix();
    glPushMatrix();
    glTranslatef(-3.0, 0.0, 0.0);
    glPushMatrix();
    glRotatef(45.0, 0.0, 0.0, 1.0);
    glColor3f(0.0, 1.0, 0.0);
    glRectf(-1.0,-1.0,1.0,1.0);
    glPopMatrix();
    glTranslatef(6.0, 0.0, 0.0);
    glPushMatrix();
    glRotatef(45.0, 0.0, 0.0, 1.0);
    glColor3f(0.0, 0.7, 0.0);
    glRectf(-1.0,-1.0,1.0,1.0);
    glPopMatrix();
    glPopMatrix();
    glTranslatef(0.0, -3.0, 0.0);
    glScalef(4.0, 1.5, 1.0);
    glColor3f(0.0, 0.0, 1.0);
    glRectf(-1.0,-1.0,1.0,1.0);



   /********** End ************/
glFlush();
}

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

glutInit( & argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Geometric Transformation Example");
    
    init();
glutDisplayFunc( & amp;myDraw);
    glutMainLoopEvent();
     
     
    /****************The following is the evaluation code and has nothing to do with the content of this experiment. Please do not modify it**************/
    GLubyte* pPixelData = (GLubyte*)malloc(400 * 400 * 3);//Allocate memory
    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;
}

Mitsubishi shape

// Tip: After writing the code, please save it and then evaluate it.
#include <GL/freeglut.h>
#include<stdio.h>

//Header files used for evaluation code-start
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
//Header files used for evaluation code-end

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0); //Set the background color
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-5.0, 5.0, -5.0, 5.0); //Set the display range to X:-5.0~5.0, Y:-5.0~5.0
glMatrixMode(GL_MODELVIEW);
}
void drawDiamond(void) //Draw a diamond
{
   // Please add your code here
   /********** Begin **********/
 
    glBegin(GL_POLYGON); //Vertex designation needs to be in the counterclockwise direction
    glVertex2f(0.0f, -1.0f); //Lower point
    glVertex2f(2.0f, 0.0f); //right point
    glVertex2f(0.0f, 1.0f); //upper point
    glVertex2f(-2.0f, 0.0f); //Left point
    glEnd();

   /********** End ************/
}

void myDraw(void) //Two-dimensional geometric transformation
{
   // Please add your code here
   /********** Begin **********/
    glClear(GL_COLOR_BUFFER_BIT); //Clear
    glLoadIdentity(); //Set the current matrix as the identity matrix
    glPushMatrix();
    glRotatef(30.0, 0.0, 0.0, 1.0);
    glTranslatef(-2.0, 0.0, 0.0);
    glColor3f(0.0, 1.0, 0.0);
    drawDiamond(); //green diamond on the left
    glPopMatrix();
    glPushMatrix();
    glRotatef(150.0, 0.0, 0.0, 1.0);
    glTranslatef(-2.0, 0.0, 0.0);
    glColor3f(0.0, 0.0, 1.0);
    drawDiamond(); //Blue diamond on the right
    glPopMatrix();
    glPushMatrix();
    glRotatef(270.0, 0.0, 0.0, 1.0);
    glTranslatef(-2.0, 0.0, 0.0);
    glColor3f(1.0, 0.0, 0.0);
    drawDiamond(); //The red diamond on the top
    glPopMatrix();
    glFlush();




   /********** End **********/
glFlush();
}

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

glutInit( & argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Geometric Transformation Example");
    
    init();
glutDisplayFunc( & amp;myDraw);
    glutMainLoopEvent();
     
     
    /****************The following is the evaluation code and has nothing to do with the content of this experiment. Please do not modify it**************/
    GLubyte* pPixelData = (GLubyte*)malloc(400 * 400 * 3);//Allocate memory
    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;
}