前言
记录一次三维计算机图形学的作业,用于以后复习。也方便各位有相同需求的同学们。程序仍有问题待解决,欢迎大佬在评论区指点。以后会尽量更新新的学习记录贴,在优快云与广大码农共进步。
可使用鼠标交互的多项式曲线
一、功能简介
通过点击鼠标左键确定四个型值点位置,再次点击左键完成多项式曲线曲线的绘制
二、代码片段
代码如下,一共353行,还包括注释,请慢慢阅读,理解整体架构以后再尝试运行:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct Point {
//Point结构体
int x, y;
};
Point pt[4]; //存放点信息的数组,计算曲线时用到
vector<Point> vpt; //存放点的向量,输入数据时用到
bool bDraw; //是否绘制曲线,false则确定没有开始绘制曲线
int nInput; //点的数量
bool mouseLeftIsDown = false; //是否点击鼠标左键
bool mouseRightIsDown = false; //是否点击鼠标右键
int caculateSquareDistance(int x,int y, Point b) //计算鼠标与点的位置
{
return (x - b.x) * (x - b.x) + (y - b.y);
}
void ControlPoint(vector<Point> vpt) //绘制目标点
{
glPointSize(5); //点的大小
for (int i = 0; i < vpt.size(); i++)
{
glBegin(GL_POINTS);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2i(vpt[i].x, vpt[i].y); //点为红色
glEnd();
}
}
void ReControlPoint(int x, int y) //绘制修改的目标点
{
glPointSize(5); //点的大小
glBegin(GL_POINTS);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2i(x, y); //点为红色
glEnd();
}
void PolylineGL(Point* pt, int num) //绘制多项式曲线
{
//glBegin(GL_LINE_STRIP);
glBegin(GL_POINTS);
//glColor3f(1.0f, 0, 0);
glPointSize(15.0f);
/*glVertex2i(points[0].x, points[0].y);
glVertex2i(points[1].x, points[1].y);
glVertex2i(points[2].x, points[2].y);
glVertex2i(points[3].x, points[3].y);*/
glVertex2i(pt[0].x, pt[0].y);
glVertex2i(pt[1].x, pt[1].y);</