// 提示:在合适的地方修改或添加代码#include <GL/freeglut.h>#include<stdio.h>#include <stdlib.h>#include // 评测代码所用头文件-开始#include<opencv2/core/core.hpp>#include<opencv2/highgui/highgui.hpp>#include<opencv2/imgproc/imgproc.hpp>// 评测代码所用头文件-结束using namespace std;struct Point{ int x, y;};Point bz[11], bspt[11]; //bz为Bezier曲线,bspt为B样条曲线int nInput;//Bezier曲线控制点Point pt[4] = { { 50, 100}, { 140,300},{250, 320}, {290, 120} };//B样条曲线控制点/请在此处添加你的代码/ /*********** Begin /Point pt2[4]={ { 450, 100}, { 540,300},{650, 320}, {690, 120} };/*** end **/void CalcBZPoints() //Bezier曲线算法{ float a0, a1, a2, a3, b0, b1, b2, b3; a0 = pt[0].x; a1 = -3 * pt[0].x + 3 * pt[1].x; a2 = 3 * pt[0].x - 6 * pt[1].x + 3 * pt[2].x; a3 = -pt[0].x + 3 * pt[1].x - 3 * pt[2].x + pt[3].x; b0 = pt[0].y; b1 = -3 * pt[0].y + 3 * pt[1].y; b2 = 3 * pt[0].y - 6 * pt[1].y + 3 * pt[2].y; b3 = -pt[0].y + 3 * pt[1].y - 3 * pt[2].y + pt[3].y; float t = 0; float dt = 0.01; for (int i = 0; t < 1.1; t += 0.1, i++) { bz[i].x = a0 + a1 * t + a2 * t * t + a3 * t * t * t; bz[i].y = b0 + b1 * t + b2 * t * t + b3 * t * t * t; }}void CalcBSPoints() //B样条曲线{/请在此处添加你的代码/ / Begin / float a0, a1, a2, a3, b0, b1, b2, b3; a0 = pt2[0].x + 4 * pt2[1].x + pt2[2].x; a1 = -3 * pt2[0].x + 3 * pt2[2].x; a2 = 3 * pt2[0].x - 6 * pt2[1].x + 3 * pt2[2].x; a3 = -pt2[0].x + 3 * pt2[1].x - 3 * pt2[2].x + pt2[3].x; b0 = pt2[0].y + 4 * pt2[1].y + pt2[2].y; b1 = -3 * pt2[0].y + 3 * pt2[2].y; b2 = 3 * pt2[0].y - 6 * pt2[1].y + 3 * pt2[2].y; b3 = -pt2[0].y + 3 * pt2[1].y - 3 * pt2[2].y + pt2[3].y; float t = 0; float dt = 0.01; for (int i = 0; t < 1.1; t += 0.1, i++) { bspt[i].x = ( a0 + a1 * t + a2 * t * t + a3 * t * t * t ) / 6; bspt[i].y = ( b0 + b1 * t + b2 * t * t + b3 * t * t * t ) / 6; }}/ end ***/void ControlPoint(){ glPointSize(2); for (int i = 0; i < 4; i++) { glBegin(GL_POINTS); glColor3f(1.0f, 0.0f, 0.0f); glVertex2i(pt[i].x, pt[i].y); glEnd(); }}void PolylineGL(Point pt, int num){ glBegin(GL_LINE_STRIP); for (int i = 0; i < num; i++) { glColor3f(0.0f, 1.0f, 0.0f); glVertex2i(pt[i].x, pt[i].y); } glEnd();}void PolylineGL1(Point pt, int num){ glBegin(GL_LINE_STRIP); for (int i = 0; i < num; i++) { glColor3f(1.0f, 1.0f, 1.0f); glVertex2i(pt[i].x, pt[i].y); } glEnd();}void display(){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 1.0f, 1.0f); ControlPoint(); //画4个控制点 PolylineGL1(pt, 4); //画4个点之间的线段 CalcBZPoints(); PolylineGL(bz, 11); PolylineGL1(pt2, 4); CalcBSPoints(); PolylineGL(bspt, 11); glFlush();}void init(){ glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_SMOOTH);}void reshape(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[]){ GLubyte pPixelData = (GLubyte)malloc(800 * 400 * 3);//分配内存 GLint viewport[4] = { 0 }; glutInit(&argc, argv); glutInitWindowPosition(100, 100); glutInitWindowSize( 800 , 400 ); //设置初始化窗口大小 glutCreateWindow(“三维观察”); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoopEvent(); /以下为评测代码,与本次实验内容无关,请勿修改/ 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, 800, 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 < 800; j++) { int k = 3 * (i * 800 + 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;}
计算机图形学三维造型 Bezier曲线和养条曲线
最新推荐文章于 2023-01-05 17:28:31 发布