#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) { int a, b, dt1, dt2, d, x, y; a = y0 - y1; b = x1 - x0; d = a + a + b; //为了避免小数,这里取2倍 不写成2*a + b的原因是防止乘法 dt1 = a + b + a + b; dt2 = a + a; x = x1; y = y1; // 绘制起点 glColor3f(0.0f, 1.0f, 0.0f); glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); // 绘制整条线 while (x < x1) { if (d < 0) { x++; y++; d += dt1; } else { x++; d += dt2; } glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); } }

void myDisplay(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glPointSize(1);

MidPLine(10, 50, 300, 260);

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::imwrite('../img_step5/test.jpg', img);
cv::waitKey();
return 0;

原文地址: https://www.cveoy.top/t/topic/QFO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录