Advertisement

头歌图形学CG1-v1.1-Simple Graphics Drawing with OpenGL

阅读量:

第一关

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 - 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(-0.6,0.0);

glColor3f(0.0,1.0,0.0);

glVertex2f(-0.2,0.0);

glColor3f(0.0,0.0,1.0);

glVertex2f(-0.4,0.4);

glEnd();

glBegin(GL_LINES);

glColor3f(1.0,1.0,1.0);

glVertex2f(0.2,0.0);

glVertex2f(0.6,0.4);

glEnd();

glBegin(GL_LINES);

glColor3f(1.0,1.0,1.0);

glVertex2f(0.2,0.4);

glVertex2f(0.6,0.0);

glEnd();

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

glFlush();

}

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

{

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

GLint viewport[4] = { 0 };

glutInit(&argc, argv);

glutInitWindowPosition(100, 100);

glutInitWindowSize(400, 400);

glutCreateWindow("Hello OpenGL!");

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

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::vectorcv::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(i);

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

unsigned char* plane2Ptr = imgPlanes[2].ptr(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;

}

1.1 Task Requirements
  • Familiar with programming environment;
  • Understand the characteristics of raster graphic display;
  • Take OpenGL as a development platform design procedures, in order to be able to generate three polygons.

第二关

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

{

// Please add your code here

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

glClearColor(0.0,0.0,0.0,0.0);

glColor3f(0.0,1.0f,0.0f);

glRectf(-0.2,0.0,0.2,0.4);

glBegin(GL_TRIANGLES);

glColor3f(1.0,0.0,0.0);

glVertex2f(-0.8,0.0);

glColor3f(1.0,0.0,0.0);

glVertex2f(-0.4,0.0);

glColor3f(1.0,0.0,0.0);

glVertex2f(-0.6,0.4);

glEnd();

glBegin(GL_TRIANGLE_FAN);

glColor3f(0.0,0.0,1.0);

glVertex2f(0.5,0.0);

glColor3f(0.0,0.0,1.0);

glVertex2f(0.7,0.0);

glColor3f(0.0,0.0,1.0);

glVertex2f(0.8,0.2);

glColor3f(0.0,0.0,1.0);

glVertex2f(0.6,0.4);

glColor3f(0.0,0.0,1.0);

glVertex2f(0.4,0.2);

glEnd();

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

glFlush();

}

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

{

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

GLint viewport[4] = { 0 };

glutInit(&argc, argv);

glutInitWindowPosition(100, 100);

glutInitWindowSize(400, 400);

glutCreateWindow("Hello OpenGL!");

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

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::vectorcv::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(i);

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

unsigned char* plane2Ptr = imgPlanes[2].ptr(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;

}

第三关:

1.3 Specific Requirements
  • The background color is black (0.0,0.0,0.0,0.0) and done with "glClearColor";
  • The vertex coordinates of the red(1.0f,0.0f,0.0f) triangle are (-0.8f, 0.0f),(-0.4f, 0.0f) and (-0.6f, 0.4f);
  • The green(0.0f,1.0f,0.0f) rectangle has two diagonal point which are (-0.2f,0.0f) and (0.2f,0.4f). Be careful that when drawing rectangle using GL_QUADS it need four points;
  • The point coordinates of the blue(0.0f,0.0f,1.0f) pentagon are (0.5f, 0.0f),(0.7f, 0.0f),(0.8f, 0.2f),(0.6f, 0.4f) and (0.4f, 0.2f).

#include <GL/freeglut.h>

#include<stdio.h>

#include<math.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

struct point {

double x, y;

};

float PI = 3.1415926f;

float R = 0.4f;

void myDisplay(void)

{

//The point coordinates of the pentagram

point A, B, C, D, E, A1, B1, C1, D1, E1, F;

A.x = R * cos(90 * 2 * PI / 360) - 0.5; A.y = R * sin(90 * 2 * PI / 360);

B.x = R * cos(306 * 2 * PI / 360) - 0.5; B.y = R * sin(306 * 2 * PI / 360);

C.x = R * cos(162 * 2 * PI / 360) - 0.5; C.y = R * sin(162 * 2 * PI / 360);

D.x = R * cos(18 * 2 * PI / 360) - 0.5; D.y = R * sin(18 * 2 * PI / 360);

E.x = R * cos(234 * 2 * PI / 360) - 0.5; E.y = R * sin(234 * 2 * PI / 360);

A1.x = R * cos(90 * 2 * PI / 360) + 0.5; A1.y = R * sin(90 * 2 * PI / 360);

B1.x = R * cos(306 * 2 * PI / 360) + 0.5; B1.y = R * sin(306 * 2 * PI / 360);

C1.x = R * cos(162 * 2 * PI / 360) + 0.5; C1.y = R * sin(162 * 2 * PI / 360);

D1.x = R * cos(18 * 2 * PI / 360) + 0.5; D1.y = R * sin(18 * 2 * PI / 360);

E1.x = R * cos(234 * 2 * PI / 360) + 0.5; E1.y = R * sin(234 * 2 * PI / 360);

F.x = R * cos(90 * 2 * PI / 360) + 0.5; F.y = ((D1.y - E1.y) / (D1.x - E1.x)) * (A1.x - D1.x) + D1.y;

// Please add your code here

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

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

glBegin(GL_LINE_LOOP);

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

glVertex2f(A.x,A.y);

glVertex2f(B.x,B.y);

glVertex2f(C.x,C.y);

glVertex2f(D.x,D.y);

glVertex2f(E.x,E.y);

glEnd();

glBegin(GL_TRIANGLES);

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

glVertex2f(A1.x,A1.y);

glVertex2f(E1.x,E1.y);

glVertex2f(F.x,F.y);

glEnd();

glBegin(GL_TRIANGLES);

glVertex2f(D1.x,D1.y);

glVertex2f(F.x,F.y);

glVertex2f(C1.x,C1.y);

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

glEnd();

glBegin(GL_TRIANGLES);

glVertex2f(B1.x,B1.y);

glVertex2f(F.x,F.y);

glVertex2f(A1.x,A1.y);

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

glEnd();

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

glFlush();

}

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

{

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

GLint viewport[4] = { 0 };

glutInit(&argc, argv);

glutInitWindowPosition(100, 100);

glutInitWindowSize(400, 400);

glutCreateWindow("Hello OpenGL!");

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

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::vectorcv::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(i);

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

unsigned char* plane2Ptr = imgPlanes[2].ptr(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)

还没有任何评论哟~