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

基本概念

  • 齐次坐标:所谓齐次坐标就是用$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
#include<GLUT/GLUT.h>
#include<iostream>
using namespace std;

int m_drawmode=1;
//m_drawmode是绘画模式
//1,DDA算法a画直线
//2,中点Bresenham算法画直线
//3,改进Bresenham算法画直线
//4,八分法绘制圆
//5,四分法绘制椭圆(选做)
int m_pointnumber=0;
//绘制坐标线
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){
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++;
}
x++;
}

//初始化窗口
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;
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;

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
#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;
}