/*p5-3*/ 1 /*c5-2 にreshape 機能を加える*/ 2 #include 3

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
/*p5-3*/
/*c5-2 に reshape 機能を加える*/
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#define KEY_ESC 27
void polarview(void);
void resetview(void);
unsigned char wireFlag = GL_TRUE;
unsigned char revolveFlag = GL_FALSE;/*回転の ON/OFF 切り替え*/
int xBegin,yBegin;/*ドラッグ開始時点のマウスポインタの座標*/
int mButton;/*ドラッグ時に押されているマウスボタンの判別*/
float distance,twist,elevation,azimuth;
float theta =15.0;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
}
glPushMatrix();
polarview();
glRotatef(theta,0.0,1.0,0.0);
glColor3f(1.0,1.0,1.0);
if(wireFlag ==GL_TRUE)
glutWireCube(1.0);
else
glutSolidCube(1.0);
glPopMatrix();
glutSwapBuffers();
void idle(void)
{
theta = fmod(theta+0.5,360.);
glutPostRedisplay();
}
void myKbd(unsigned char key,int x, int y)
{
switch(key){
case 'w':
wireFlag = !wireFlag;
break;
case 'R':
resetview();
break;
case KEY_ESC:
exit(0);
}
glutPostRedisplay();
}
void myMouse(int button,int state,int x,int y)
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
{
}
if(state == GLUT_DOWN){
switch(button){
case GLUT_LEFT_BUTTON:
mButton = button;
break;
case GLUT_MIDDLE_BUTTON:
revolveFlag = !revolveFlag;
if(revolveFlag == GL_TRUE)
glutIdleFunc(idle); /*idle を繰り返し実行するように設定*/
else
glutIdleFunc(NULL);/*アニメーションをとめる*/
break;
case GLUT_RIGHT_BUTTON:
mButton = button;
break;
}
xBegin = x; /*ドラッグ開始点の x,y 座標値を取得*/
yBegin = y;
}
void myMotion( int x, int y)
{
int xDisp, yDisp;
xDisp = x- xBegin; /*マウス移動距離の計算*/
yDisp = y -yBegin;
}
switch(mButton){
case GLUT_LEFT_BUTTON:/*左ボタンのドラッグで物体の姿勢を変える*/
azimuth += (float)xDisp/2.0;
elevation -=(float)yDisp/2.0;
break;
case GLUT_RIGHT_BUTTON:/*右ボタンのドラッグでズーム*/
distance +=(float)yDisp/40.0;
break;
}
xBegin = x;/*次のステップのマウスの出発点*/
yBegin = y;
glutPostRedisplay();/*1 ステップ分のドラッグの結果を描画に反映*/
void myInit(char *progname)
{
/*
int width=600,height=600;
float aspect =(float)width/(float)height;
*/
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutCreateWindow(progname);
glClearColor(0.0,0.0,0.0,1.0);
glutKeyboardFunc(myKbd);
glutMouseFunc(myMouse);/*マウスクリックに対するコールバック関数の登録*/
glutMotionFunc(myMotion);/*マウスドラッグに対するコールバック関数の登録*/
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
resetview();/*ビューイング変換 polarview に初期パラメータ値を設定*/
}
void myReshape(int width,int height)/*リシェープコールバック関数*/
{
float aspect =(float)width/(float)height;
glViewport(0,0,width,height);/*ビューポートの再設定*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,aspect,1.0,10.0);/*ビューボリュームの再定義*/
glMatrixMode(GL_MODELVIEW);
}
void resetview(void)
{
distance =5.0;
twist = 0.0;
elevation = 0.0;
azimuth = 0.0;
}
void polarview(void)
{
glTranslatef(0.0,0.0,-distance);
glRotatef(-twist,0.0,0.0,1.0);
glRotatef(-elevation,1.0,0.0,0.0);
glRotatef(-azimuth,0.0,1.0,0.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
myInit(argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(myReshape);/*リシェープコールバック関数の登録*/
glutIdleFunc(NULL);
glutMainLoop();
return(0);
}