-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest.c
More file actions
147 lines (122 loc) · 2.62 KB
/
test.c
File metadata and controls
147 lines (122 loc) · 2.62 KB
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
#include "miniglut.h"
void idle(void);
void display(void);
void reshape(int x, int y);
void keypress(unsigned char key, int x, int y);
void mouse(int bn, int st, int x, int y);
void motion(int x, int y);
float cam_theta, cam_phi = 25, cam_dist = 8;
int mouse_x, mouse_y;
int bnstate[8];
int anim;
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(1024, 768);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("miniglut test");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keypress);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glutMainLoop();
return 0;
}
void idle(void)
{
glutPostRedisplay();
}
void display(void)
{
long tm;
float lpos[] = {-1, 2, 3, 0};
tm = glutGet(GLUT_ELAPSED_TIME);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -cam_dist);
glRotatef(cam_phi, 1, 0, 0);
glRotatef(cam_theta, 0, 1, 0);
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glPushMatrix();
if(anim) {
glRotatef(tm / 10.0f, 1, 0, 0);
glRotatef(tm / 10.0f, 0, 1, 0);
}
glutSolidTorus(0.3, 1, 16, 24);
glPopMatrix();
glutSolidSphere(0.4, 16, 8);
glPushMatrix();
glTranslatef(-2.5, 0, 0);
glutSolidCube(1.5);
glPopMatrix();
glPushMatrix();
glTranslatef(2.5, -1, 0);
glRotatef(-90, 1, 0, 0);
glutSolidCone(1.1, 2, 16, 2);
glPopMatrix();
glBegin(GL_QUADS);
glNormal3f(0, 1, 0);
glVertex3f(-5, -1.3, 5);
glVertex3f(5, -1.3, 5);
glVertex3f(5, -1.3, -5);
glVertex3f(-5, -1.3, -5);
glEnd();
glutSwapBuffers();
}
#define ZNEAR 0.5f
void reshape(int x, int y)
{
float vsz, aspect = (float)x / (float)y;
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
vsz = 0.4663f * ZNEAR;
glFrustum(-aspect * vsz, aspect * vsz, -vsz, vsz, 0.5, 500.0);
}
void keypress(unsigned char key, int x, int y)
{
switch(key) {
case 27:
case 'q':
glutExit();
break;
case ' ':
anim ^= 1;
glutIdleFunc(anim ? idle : 0);
glutPostRedisplay();
break;
}
}
void mouse(int bn, int st, int x, int y)
{
int bidx = bn - GLUT_LEFT_BUTTON;
bnstate[bidx] = st == GLUT_DOWN;
mouse_x = x;
mouse_y = y;
}
void motion(int x, int y)
{
int dx = x - mouse_x;
int dy = y - mouse_y;
mouse_x = x;
mouse_y = y;
if(!(dx | dy)) return;
if(bnstate[0]) {
cam_theta += dx * 0.5;
cam_phi += dy * 0.5;
if(cam_phi < -90) cam_phi = -90;
if(cam_phi > 90) cam_phi = 90;
glutPostRedisplay();
}
if(bnstate[2]) {
cam_dist += dy * 0.1;
if(cam_dist < 0) cam_dist = 0;
glutPostRedisplay();
}
}