第1关:OpenGL点的绘制
// 提示:写完代码请保存之后再进行评测
#include <GL/freeglut.h>
#include<stdio.h>
// 评测代码所用头文件-开始
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
// 评测代码所用头文件-结束
void myDisplay(void)
{
// 请在此添加你的代码
/********** Begin ********/
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(3);
glBegin(GL_POINTS);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.4f, -0.4f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.4f, 0.4f);
glEnd();
/********** End **********/
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Hello Point!");
glutDisplayFunc(&myDisplay);
glutMainLoopEvent();
/*************以下为评测代码,与本次实验内容无关,请勿修改**************/
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_step1/test.jpg", img);
return 0;
}
第2关:OpenGL简单图形绘制
// 提示:写完代码请保存之后再进行评测
#include <GL/freeglut.h>
#include<stdio.h>
// 评测代码所用头文件-开始
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
// 评测代码所用头文件-结束
void myDisplay(void)
{
// 请在此添加你的代码
/********** Begin ********/
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.8f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(-0.8f, -0.5f);
glEnd();
glPointSize(3);
glBegin(GL_POINTS);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.4f, -0.4f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.4f, 0.4f);
glEnd();
/********** End **********/
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Hello Point!");
glutDisplayFunc(&myDisplay);
glutMainLoopEvent();
/*************以下为评测代码,与本次实验内容无关,请勿修改**************/
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_step2/test.jpg", img);
return 0;
}
第3关:OpenGL直线绘制
// 提示:写完代码请保存之后再进行评测
#include <GL/freeglut.h>
#include<stdio.h>
// 评测代码所用头文件-开始
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
// 评测代码所用头文件-结束
void myDisplay(void)
{
// 请在此添加你的代码
/********** Begin ********/
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glRectf(25.0, 25.0, 75.0, 75.0);
glPointSize(10);
glBegin(GL_POINTS);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glEnd();
glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(100.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(180.0f, 240.0f);
glEnd();
/********** 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();
/*************以下为评测代码,与本次实验内容无关,请勿修改**************/
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;
}
第4关:0<k<1直线绘制-DDA算法
// 提示:写完代码请保存之后再进行评测
#include <GL/freeglut.h>
#include<stdio.h>
// 评测代码所用头文件-开始
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
// 评测代码所用头文件-结束
void LineDDA(int x0, int y0, int x1, int y1)
{
// 请在此添加你的代码
/********** Begin ********/
int x, dy, dx, y;
float m;
dx = x1 - x0;
dy = y1 - y0;
m = dy / dx;
y = y0;
glColor3f(1.0f, 1.0f, 0.0f);
glPointSize(1);
for (x = x0; x <= x1; x++)
{
glBegin(GL_POINTS);
glVertex2i(x, (int)(y + 0.5));
glEnd();
y += m;
}
/********** End **********/
glFlush();
}
void myDisplay(void)
{
// 请在此添加你的代码
/********** Begin ********/
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();
/*************以下为评测代码,与本次实验内容无关,请勿修改**************/
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;
}
第5关:0<k<1直线绘制-中点算法
// 提示:写完代码请保存之后再进行评测
#include <GL/freeglut.h>
#include<stdio.h>
// 评测代码所用头文件-开始
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
// 评测代码所用头文件-结束
void MidPLine(int x0, int y0, int x1, int y1)
{
// 请在此添加你的代码
/********** Begin ********/
int dx, dy, dt, db, d, x, y;
dx = x1 - x0;
dy = y1 - y0;
d = dx - 2*dy; //即原初值d1
dt = 2*dx - 2*dy; //原1-k
db = -2*dy; //原-k
x = x0; y = y0;
glColor3f(0.0f, 1.0f, 0.0f);
glPointSize(1);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
while (x < x1)
{
if (d < 0)
{
x++;
y++;
d += dt;
}
else
{
x++;
d += db;
}
glColor3f(0.0f, 1.0f, 0.0f);
glPointSize(1);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
/********** End **********/
glFlush();
}
void myDisplay(void)
{
// 请在此添加你的代码
/********** Begin ********/
MidPLine( 10, 50,300 ,260 );
//glColor3f(0.0f, 1.0f, 0.0f);
//glLineWidth(1);
/********** 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();
/*************以下为评测代码,与本次实验内容无关,请勿修改**************/
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;
}
第6关:一般直线绘制
// 提示:写完代码请保存之后再进行评测
#include <GL/freeglut.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
// 评测代码所用头文件-开始
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
// 评测代码所用头文件-结束
void Line(int x0, int y0, int x1, int y1)
{
// 请在此添加你的代码
/********** Begin ********/
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POINTS);
glPointSize(1);
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
if (dx > dy) // x方向为步进方向
{
if (x0 > x1) //确保x0<x1,这样循环中x=x+1
{
swap(x0, x1);
swap(y0, y1);
}
int d = dx - 2 * dy;
int d1 = 2 * dx - 2 * dy;
int d2 = -2 * dy;
int x = x0;
int y = y0;
int yIncr = (y1 > y0) ? 1 : -1; // 比较两端点y值大小决定y的增量值
glVertex2i(x, y);
for (int x = x0 + 1; x <= x1; x++)
{
if (d < 0)
{
y = y + yIncr;
d = d + d1;
}
else
d = d + d2;
glVertex2i(x, y);
}
}
else
{
if (y0 > y1)
{
swap(x0, x1);
swap(y0, y1);
}
int d = dy - 2 * dx;
int d1 = 2 * dy - 2 * dx;
int d2 = -2 * dx;
int x = x0;
int y = y0;
int xIncr = (x1 > x0) ? 1 : -1;
glVertex2i(x, y);
for (int y = y0 + 1; y <= y1; y++)
{
if (d < 0)
{
x = x + xIncr;
d = d + d1;
}
else
d = d + d2;
glVertex2i(x, y);
}
}
glEnd();
/********** End **********/
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
// 请在此添加你的代码用来测试直线绘制代码
/********** Begin ********/
Line(261, 215, 344, 275);
Line(344, 275, 429, 213);
Line(429, 213, 398, 319);
Line(398, 319, 477, 384);
Line(477, 384, 378, 385);
Line(378, 385, 344, 491);
Line(344, 491, 310, 384);
Line(310, 384, 209, 382);
Line(209, 382, 292, 319);
Line(292, 319, 261, 215);
/********** 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[])
{
int width = 800;
int height = 600;
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(width, height);
glutCreateWindow("Hello Line!");
Init();
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
glutMainLoopEvent();
/*************以下为评测代码,与本次实验内容无关,请勿修改**************/
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(height, width, CV_8UC3);
cv::split(img, imgPlanes);
for(int i = 0; i < height; 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 < width; 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_step6/test.jpg", img);
return 0;
}