计算机图形学-二维变换及二维观察

基本概念

  • 齐次坐标:所谓齐次坐标就是用$n+1$维向量表示$n$维向量,$h=1$(h是任意不为零的比例系数)的齐次坐标叫做规范化齐次坐标

英语单词:

matrix:矩阵

  • opengl初探
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<GLUT/GLUT.h>
void Display(void){
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);//用当前背景色填充窗口
glColor3f(1.0f, 1.0f, 1.0f);//h设置当前的绘图颜色为红色
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();
glFlush();
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);//对GLUT库j初始化
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);//指定窗口使用单缓存和RGB颜色模型//初始化窗口的显示模式
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("1710121058李博");//创建一个名为矩形的窗口
glutDisplayFunc(Display);//设置当前窗口的显示回调函数
//Initial();//完成窗口初始化
glutMainLoop();//启动GLUT事件处理循环
}
  • Win xp锁屏动画
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// win xp
#include<GLUT/GLUT.h>
using namespace std;
GLfloat x1=100.0f;
GLfloat y1=150.0f;
GLsizei rsize=50;

GLfloat xstep=1.0f;
GLfloat ystep=1.0f;

GLfloat windowWidth;
GLfloat windowHeight;
void RenderScene(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glRectf(x1, y1, x1+rsize, y1+rsize);

glutSwapBuffers();
}
void ChangeSize(GLsizei w,GLsizei h){
if(h==0) h=1;
glViewport(0, 0, w, h);//s视区h
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h){
windowWidth=250.0f*h/w;
windowHeight=250.0f;
}else{
windowWidth=250.0f*w/h;
windowHeight=250.0f;
}
//修剪区
glOrtho(0.0f, windowWidth, 0.0f, windowHeight,1.0f, -1.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void TimerFunction(int value){
if(x1>windowWidth-rsize||x1<0) xstep=-xstep;
if(y1>windowHeight-rsize||y1<0) ystep=-ystep;
if(x1>windowWidth-rsize) x1=windowWidth-rsize-1;
if(y1>windowHeight-rsize) y1=windowHeight-rsize-1;

x1+=xstep;
y1+=ystep;

glutPostRedisplay();
glutTimerFunc(33, TimerFunction, 1);
}
void SetupRC(void){
//设置窗口清除色为蓝色
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
int main(void){
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow("Bounce");

glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33, TimerFunction, 1);

//SetupRC();
glutMainLoop();

}
  • 太阳地球月亮模型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//sun_earth_moon_model
#include<GLUT/GLUT.h>
#include<ctime>
#include<cstdio>
#include<time.h>
using namespace std;
static int day = 200;
double CalFrequency()
{
static int count;
static double save;
static clock_t last, current;
double timegap;

++count;
if( count <= 50 )
return save;
count = 0;
last = current;
current = clock();
timegap = (current - last ) / (double)CLOCKS_PER_SEC;
save = 50.0 / timegap;

return save;

}

void myDisplay(void)
{
double FPS = CalFrequency();
printf("FPF = %f\n", FPS);

glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75, 1, 1, 400000000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, -200000000, 200000000, 0, 0, 0, 0, 0, 1);

// 绘制红色的“太阳”
glColor3f(1.0f, 0.0f, 0.0f);
glutSolidSphere(69600000, 20, 20);

// 绘制蓝色的“地球”
glColor3f(0.0f, 0.0f, 1.0f);
glRotatef(day/360.0*360.0, 0.0f, 0.0f, -1.0f);
glTranslatef(150000000, 0.0f, 0.0f);
glutSolidSphere(15945000, 20, 20);

// 绘制黄色的“月亮”
glColor3f(1.0f, 1.0f, 0.0f);
glRotatef(day/30.0*360.0 - day/360.0*360.0, 0.0f, 0.0f, -1.0f);
glTranslatef(38000000, 0.0f, 0.0f);
glutSolidSphere(4345000, 20, 20);

glFlush();

glutSwapBuffers();
}

void myIdle(void)
{
++day;
if( day >= 360 )
day = 0;
myDisplay();

}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); // 修改了参数为GLUT_DOUBLE
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("太阳,地球和月亮");
glutDisplayFunc(&myDisplay);
glutIdleFunc(&myIdle); // 表示在CPU空闲的时间调用某一函数
glutMainLoop();

return 0;
}
  • 画线,画圆,填充区域的算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include<GLUT/GLUT.h>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;

int m_drawmode=1;
//m_drawmode是绘画模式
//1,DDA算法a画直线
//2,中点Bresenham算法画直线
//3,改进Bresenham算法画直线
//4,八分法绘制圆
//5,区域填充
int m_pointnumber=0;
struct point{
int x,y;
point(int x,int y):x(x),y(y){}
};
int vis[30][30];
//绘制坐标线
void DrawCordinateLine(void){
int i=0;
//坐标线为黑色
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
for(i=10;i<=250;i+=10){
glVertex2f((float)(i), 0.0f);
glVertex2f((float)(i), 250.0f);
glVertex2f(0.0f, (float)(i));
glVertex2f(250.0f, (float)(i));
}
glEnd();
}
//绘制一个点,这里用一个正方形代表一个点
void PutPixel(GLsizei x,GLsizei y){
glRectf(10*x, 10*y, 10*x+10, 10*y+10);
}
/**
x0,y0起点坐标
x1,y1终点坐标
num扫描转换时从起点开始输出的点的数目,用于动画
*/
void DDAcreateLine(GLsizei x0,GLsizei y0,GLsizei x1,GLsizei y1,GLsizei num){
//设置颜色
glColor3f(1.0f, 0.0f, 0.0f);
//对画线动画进行控制
if(num==1)
cout<<"DDA画线算法:各点坐标\n";
else if(num==0)
return ;
//画线算法的实现
GLsizei dx,dy,epsl,k;
GLfloat x,y,xincre,yincre;

dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
if(abs(dx)>abs(dy)) epsl=abs(dx);
else epsl=abs(dy);

xincre=(float)dx/epsl;
yincre=(float)dy/epsl;

for(k=0;k<=epsl;k++){
PutPixel((int)(x+0.5), (int)(y+0.5));
if(k>=num-1){
cout<<"x="<<x<<",y="<<y
<<";取整后"<<"x="<<(int)(x+0.5)
<<",y="<<(int)(y+0.5)<<endl;
break;
}
x+=xincre;
y+=yincre;
if(x>=25||y>=25) break;
}
}
void MidBresenhamLine(GLsizei x0,GLsizei y0,GLsizei x1,GLsizei y1,GLsizei num){
//设置颜色
glColor3f(1.0f, 0.0f, 0.0f);
if(num==1)
cout<<"中点Bresenham算法画直线,各点及判别式的值:\n";
else if(num==0)
return ;
//画线算法的实现
int dx,dy,d,UpIncre,DownIncre,x,y;
if(x0>x1){
x=x1;x1=x0;x0=x;
y=y1;y1=y0;y0=y;
}
x=x0;y=y0;
dx=x1-x0; dy=y1-y0; d=dx-2*dy;
UpIncre=2*dx-2*dy; DownIncre=-2*dy;
while(x<=x1){
PutPixel(x, y);
//随便加的,不知道原理
if(x>=num-1){
cout<<"x="<<x<<",y="<<y<<",d="<<d<<endl;
break;
}

x++;
if(d<0){
y++;
d+=UpIncre;
}else{
d+=DownIncre;
}
if(x>=25||y>=25) break;
}
}

void BresenhamLine(GLsizei x0,GLsizei y0,GLsizei x1,GLsizei y1,GLsizei num){
glColor3f(1.0f, 0.0f, 0.0f);
if(num==1) cout<<"改进Bresenham算法画直线,各点及判别式的值\n";
else if(num==0) return ;
int x,y,dx,dy,e;
dx=x1-x0;
dy=y1-y0;
e=-dx; x=x0; y=y0;
while(x<=x1){
if(x>=num-1){
cout<<"x="<<x<<",y="<<y<<",e="<<e<<endl;
break;
}
PutPixel(x, y);
x++;
e=e+2*dy;
if(e>0){
y++;
e=e-2*dx;
}
}
}

void MidBresenhamCircle(GLsizei r,GLsizei num){
int x,y,d;
glColor3f(1.0f, 0.0f, 0.0f);
x=0; y=r; d=1-r;
int dd=12;
while(x<=y){
if(x>=num-1){
cout<<"x="<<x<<",y="<<y<<endl;
break;
}
PutPixel(x+dd, y+dd);PutPixel(y+dd, x+dd);
PutPixel(-y+dd, x+dd);PutPixel(-x+dd, y+dd);
PutPixel(-x+dd, -y+dd);PutPixel(-y+dd, -x+dd);
PutPixel(y+dd, -x+dd);PutPixel(x+dd, -y+dd);
if(d<0) d+=2*x+3;
else{
d+=2*(x-y)+5;
y--;
}
x++;
}
}
void Circle(GLsizei r){
int x,y,d;
glColor3f(1.0f, 0.0f, 0.0f);
x=0; y=r; d=1-r;
int dd=12;
while(x<=y){
PutPixel(x+dd, y+dd);PutPixel(y+dd, x+dd);
PutPixel(-y+dd, x+dd);PutPixel(-x+dd, y+dd);
PutPixel(-x+dd, -y+dd);PutPixel(-y+dd, -x+dd);
PutPixel(y+dd, -x+dd);PutPixel(x+dd, -y+dd);
vis[x+dd][y+dd]=1;vis[y+dd][x+dd]=1;
vis[-y+dd][x+dd]=1;vis[-x+dd][y+dd]=1;
vis[-x+dd][-y+dd]=1;vis[-y+dd][-x+dd]=1;
vis[y+dd][-x+dd]=1;vis[x+dd][-y+dd]=1;
if(d<0) d+=2*x+3;
else{
d+=2*(x-y)+5;
y--;
}
x++;
}
}
void BFS(GLsizei x,GLsizei y,GLsizei num){
int cnt=0;
memset(vis,0,sizeof vis);
Circle(5);
queue<point>qu;
qu.push(point(x,y));
while(!qu.empty()){
if(cnt>=num-1) break;
cnt++;
point cur=qu.front();
qu.pop();
x=cur.x;y=cur.y;
vis[x][y]=1;
PutPixel(x,y);
if(!vis[x][y+1]) qu.push(point(x,y+1));
if(!vis[x][y-1]) qu.push(point(x,y-1));
if(!vis[x+1][y]) qu.push(point(x+1,y));
if(!vis[x-1][y]) qu.push(point(x-1,y));
}
}
//初始化窗口
void Initial(void){
//设置窗口颜色为白色
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
//窗口大小改变时调用的登记函数
void ChangeSize(GLsizei w,GLsizei h){
if(h==0){
h=1;
}
//设置视区尺寸
glViewport(0, 0, w, h);
//重置坐标系统
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//建立修剪空间的范围
if(w<=h){
glOrtho(0.0f, 250.0f, 0.0f, 250.0f*h/w, 1.0f, -1.0f);
}else{
glOrtho(0.0f, 250.0f*h/w, 0.0f, 250.0f, 1.0f, -1.0f);
}
}
//在窗口中绘制图形
void Redrow(void){
//用当前背景色填充窗口
glClear(GL_COLOR_BUFFER_BIT);

//画出坐标线
DrawCordinateLine();

switch (m_drawmode) {
case 1:
DDAcreateLine(0,0,24,20,m_pointnumber);
break;
case 2:
MidBresenhamLine(0,0,24,20,m_pointnumber);
break;
case 3:
BresenhamLine(0,0,24,20,m_pointnumber);
break;
case 4:
MidBresenhamCircle(10,m_pointnumber);
break;
case 5:
BFS(12,12,m_pointnumber);
break;
default:
break;
}



glutSwapBuffers();


}
//设置时间回调函数
void TimerFunc(int value){
if(m_pointnumber==0) value=1;
m_pointnumber=value;
glutPostRedisplay();
glutTimerFunc(500, TimerFunc, value+1);
}

//设置键盘回调函数
void Keyboard(unsigned char key,int x,int y){
if(key=='1') m_drawmode=1;
if(key=='2') m_drawmode=2;
if(key=='3') m_drawmode=3;
if(key=='4') m_drawmode=4;
if(key=='5') m_drawmode=5;
m_pointnumber=0;
glutPostRedisplay();
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);//对GLUT库j初始化
//初始化glut库opengl窗口的显示模式
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);//指定窗口使用单缓存和RGB颜色模型//初始化窗口的显示模式
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);

glutCreateWindow("1710121058李博");//创建一个名为矩形的窗口
glutDisplayFunc(Redrow);//设置当前窗口的显示回调函数
glutReshapeFunc(ChangeSize);
glutKeyboardFunc(Keyboard);//键盘响应回调函数
glutTimerFunc(500, TimerFunc, 1);
//窗口初始化
Initial();
glutMainLoop();//启动GLUT事件处理循环
}
  • 反走样等
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include<string.h>
#include<GLUT/GLUT.h>

int winWidth = 400, winHeight = 300;///窗口的宽度和高度

void myDisplay(void)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);///用当前背景色填充窗口
glColor3f(0.0f, 0.0f, 0.0f);///设置当前的绘图RGB颜色

///绘制不同大小的点
GLfloat sizes[2];///保存绘制点的尺寸范围
GLfloat step;///保存绘制点尺寸的步长
GLfloat curSize;///当前绘制的点的大小
glGetFloatv(GL_POINT_SIZE_RANGE, sizes);
glGetFloatv(GL_POINT_SIZE_GRANULARITY, &step);

curSize = sizes[0];

for (int i = 0; i < 25; i++)
{
glPointSize(curSize);
glBegin(GL_POINTS);
glVertex3f(25.0 + i * 8, 200.0, 0.0);
glEnd();
curSize += step * 2;
}

///绘制一条虚线
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0x00FF);
glBegin(GL_LINES);
glVertex3f(25.0, 120.0, 0.0);
glVertex3f(225.0, 120.0, 0.0);
glEnd();

///绘制一条宽度为3的点划线
glLineWidth(3);
glLineStipple(1, 0xFF0C);
glBegin(GL_LINES);
glVertex3f(25.0, 80.0, 0.0);
glVertex3f(225.0, 80.0, 0.0);
glEnd();

///增加重复因子绘制的点划线
glLineStipple(4, 0xFF0C);
glBegin(GL_LINES);
glVertex3f(25.0, 40.0, 0.0);
glVertex3f(225.0, 40.0, 0.0);
glEnd();
glDisable(GL_LINE_STIPPLE);

glFlush();///刷新OpenGL命令队列
}
void ChangeSize(int w, int h)
{
winWidth = w; winHeight = h;
glViewport(0, 0, w, h);///指定窗口显示区域
glMatrixMode(GL_PROJECTION);///设置投影参数
glLoadIdentity();
gluOrtho2D(0.0, winWidth, 0.0, winHeight);
}


///字符显示
void output(int x, int y, char s[])
{
int len, i;
glRasterPos2f(x, y);
len = strlen(s);
for (i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, s[i]);
}
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
char ss[] = "hello";
output(100, 100, ss);
glFlush();
}

///反走样技术
GLuint linelist;///指定显示列表ID
void Initial()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glLineWidth(12.0f);
glColor4f(0.0, 0.6, 1.0, 1.0);
linelist = glGenLists(1);
glNewList(linelist, GL_COMPILE);
glBegin(GL_LINE_LOOP);
glVertex2f(1.0f, 1.0f);
glVertex2f(4.0f, 2.0f);
glVertex2f(2.0f, 5.0f);
glEnd();
glEndList();
}
void ChangeSize1(GLsizei w, GLsizei h)
{
if (h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D(0.0, 5.0, 0.0, 6.0*(GLfloat)h / (GLfloat)w);
else
gluOrtho2D(0.0, 5.0*(GLfloat)w / (GLfloat)h, 0.0, 6.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Displayt(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glCallList(linelist);
glFlush();
}
void Displayw(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_LINE_SMOOTH);///使用反走样
glEnable(GL_BLEND);///启用混合函数
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);///指定混合函数
glCallList(linelist);
glFlush();
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300, 300);
glutInitWindowPosition(300, 300);
glutCreateWindow("1710121058李博");
glutDisplayFunc(myDisplay);
glutReshapeFunc(ChangeSize);
Initial();

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300, 300);
glutInitWindowPosition(300, 600);
glutCreateWindow("1710121058李博");
glutDisplayFunc(Display);
glutReshapeFunc(ChangeSize);
Initial();

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300, 300);
glutInitWindowPosition(600, 300);
glutCreateWindow("1710121058李博");
glutDisplayFunc(Displayt);
glutReshapeFunc(ChangeSize1);
Initial();

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(600, 600);
glutInitWindowSize(300, 300);
glutCreateWindow("1710121058李博 反走样");
glutDisplayFunc(Displayw);
glutReshapeFunc(ChangeSize1);
Initial();
glutMainLoop();
}
  • 橡皮筋技术
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<GLUT/GLUT.h>
#include<iostream>
#include<vector>
using namespace std;
int winWidth=400,winHeight=300;

//定义变量
int cur_x,cur_y;
struct NodeLine{
int x,y;
NodeLine(int _x,int _y):x(_x),y(_y){}
};
vector<NodeLine>ve;

void initial(void){
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
void ChangeSize(int w,int h){
winWidth=w;winHeight=h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, winWidth, 0.0,winHeight);
}
void Display(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
//绘制折线
glBegin(GL_LINE_STRIP);
for(int i=0;i<ve.size();i++){
glVertex2d(ve[i].x, ve[i].y);
}
glVertex2d(cur_x, cur_y);
glEnd();
glutSwapBuffers();
}
void MousePlot(GLint button,GLint action,GLint xMouse,GLint yMouse){//鼠标函数
//按下鼠标左键绘制折线
if(button==GLUT_LEFT_BUTTON&&action==GLUT_DOWN){
//cout<<"x:"<<xMouse<<"y:"<<yMouse<<"size:"<<ve.size()<<endl;
ve.push_back({xMouse,winHeight-yMouse});
}
//按下鼠标右键结束绘制
if(button==GLUT_RIGHT_BUTTON&&action==GLUT_DOWN){
ve.clear();
}
glutPostRedisplay();
}
void PassiveMouseMove(GLint xMouse,GLint yMouse){//鼠标移动函数
//记录鼠标指针的当前位置
cur_x=xMouse;
cur_y=winHeight-yMouse;
glutPostRedisplay();
}
int main(int argc,char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(400, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("橡皮筋技术");
glutDisplayFunc(Display);

glutReshapeFunc(ChangeSize);
glutMouseFunc(MousePlot);
glutPassiveMotionFunc(PassiveMouseMove);

initial();
glutMainLoop();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<GLUT/GLUT.h>
int winwidth = 400, winheight = 300;
int iPointNum = 0;
int x1 = 0, x2 = 0, x3 = 0, x4 = 0, y1 = 0, y2 = 0, y3 = 0, y4 = 0;

void Initial(void){
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}

void ChangeSize(int w, int h){
winwidth = w;
winheight = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, winwidth, 0.0, winheight);
}
int bb = 0;
void Display(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
if (bb>0) {
glBegin(GL_QUADS);
glVertex2i(x2, y2);
glVertex2i(x1, y1);
glVertex2i(x3, y3);
glVertex2i(x4, y4);
glEnd();
}

glutSwapBuffers();
}

void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse)//鼠标函数
{
//按下鼠标左键绘制折线
if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
{
if (bb==0) {
bb = 1;
x1 = xMouse;
y1 = winheight- yMouse;
}
else {
bb = 2;
}
}
//按下鼠标右键结束绘制
if (button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN)
{
bb = 0;
x1 = x2 = x3 = x4 = y1 = y2 = y3 = y4 = 0;
glutPostRedisplay();
}
}

void PassiveMouseMove(GLint xMouse, GLint yMouse)//鼠标移动函数
{
//记录鼠标指针的当前位置
if (bb==1) {
x4 = xMouse;
y4 =winheight - yMouse;
x2 = x4;
x3 = x1;
y2 = y1;
y3 = y4;
glutPostRedisplay();
}

}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("1710121058李博");
glutDisplayFunc(Display);

glutReshapeFunc(ChangeSize); //指定窗口在整形回调函数
glutMouseFunc(MousePlot); //指定鼠标响应函数
glutPassiveMotionFunc(PassiveMouseMove);//指定鼠标移动响应函数

Initial();
glutMainLoop();
return 0;
}