View difference between Paste ID: a3XtuCVh and GFh3iyLA
SHOW: | | - or go back to the newest paste.
1
#include <math.h>
2
#include <stdlib.h>
3
4
#if defined(__APPLE__)
5
  #include <OpenGL/gl.h>
6
  #include <OpenGL/glu.h>
7
  #include <GLUT/glut.h>
8
#else
9
  #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
10
    #include <windows.h>
11
  #endif
12
  #include <GL/gl.h>
13
  #include <GL/glu.h>
14
  #include <GL/glut.h>
15
#endif
16
17
struct Vector {
18
  float x, y, z;
19
20
  Vector(float v = 0) : x(v), y(v), z(v) { }
21
  Vector(float x, float y, float z) : x(x), y(y), z(z) { }
22-
  Vector operator+(const Vector& v) const {
22+
  Vector operator+(const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }
23-
    return Vector(x + v.x, y + v.y, z + v.z);
23+
  Vector operator-(const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }
24
  Vector operator*(const Vector& v) const { return Vector(x * v.x, y * v.y, z * v.z); }
25-
  Vector operator-(const Vector& v) const {
25+
  Vector operator/(const Vector& v) const { return Vector(x / v.x, y / v.y, z / v.z); }
26-
    return Vector(x - v.x, y - v.y, z - v.z);
26+
  float length() const { return sqrt(x*x + y*y + z*z); }
27
  void glTranslatef() { ::glTranslatef(x, y, z); }
28-
  Vector operator*(const Vector& v) const { 
28+
  void glRotatef(float angle) { ::glRotatef(angle, x, y, z); }
29-
    return Vector(x * v.x, y * v.y, z * v.z);
29+
  void glScalef() { ::glScalef(x, y, z); }
30
};
31-
  Vector operator/(const Vector& v) const { 
31+
32-
    return Vector(x / v.x, y / v.y, z / v.z);
32+
33
Vector pos_crate(0, 0, -5), pos_left_base(1, 0, 0), pos_right_base(-1, 0, 0), scale_base(1, 1, 3), pos_main_arm(0, 0, -2), 
34-
  float length() const {
34+
35-
    return sqrt(x*x + y*y + z*z);
35+
36
37
float rot_base = 0, rot_main_arm = 70, rot_lower_arm = -60, rot_finger = 20, rot_finger_relative = 20;
38-
  void glTranslatef() {
38+
39-
    ::glTranslatef(x, y, z);
39+
40
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
41
42-
  void glRotatef(float angle) {
42+
43-
    ::glRotatef(angle, x, y, z);
43+
44
45
    // Jobb oldali alap
46-
  void glScalef() {
46+
47-
    ::glScalef(x, y, z);
47+
48
      scale_base.glScalef();
49
      glutSolidCube(1.0f);
50
    } glPopMatrix();
51
    
52
    // Bal oldali alap
53
    glPushMatrix(); {
54
      pos_left_base.glTranslatef();
55
      scale_base.glScalef();
56
      glutSolidCube(1.0f);
57
    } glPopMatrix();
58
59
    x_axis.glRotatef(rot_main_arm);
60
    pos_main_arm.glTranslatef();
61
62
    // Felkar
63
    glPushMatrix(); {
64
      scale_main_arm.glScalef();
65
      glutSolidCube(1.0f);
66
    } glPopMatrix();
67
68
    pos_main_arm.glTranslatef();
69
70
    x_axis.glRotatef(rot_lower_arm);
71
    pos_lower_arm.glTranslatef();
72
73
    // Alkar
74
    glPushMatrix(); {
75
      scale_lower_arm.glScalef();
76
      glutSolidCube(1.0f);
77
    } glPopMatrix();
78
79
    pos_lower_arm.glTranslatef();
80
81
    // Csukló
82
    glPushMatrix(); {
83
      scale_wrist.glScalef();
84
      glutSolidCube(1.0f);
85
    } glPopMatrix();
86
87
    // Jobb 'ujj'
88
    glPushMatrix(); {
89
      z_axis.glRotatef(-rot_finger);
90
91
      glTranslatef(0, pos_right_finger.y, 0);
92
93
      glPushMatrix(); {
94
        glTranslatef(pos_right_finger.x, 0, 0);
95
        z_axis.glRotatef(-rot_finger_relative);
96
        scale_finger.glScalef();
97
        glutSolidCube(1.0f);
98
      } glPopMatrix();
99
100
      pos_right_finger.glTranslatef();
101
      z_axis.glRotatef(rot_finger_relative);
102
      scale_finger.glScalef();
103
      glutSolidCube(1.0f);
104
    } glPopMatrix();
105
106
    // Bal 'ujj'
107
    glPushMatrix(); {
108
      z_axis.glRotatef(rot_finger);
109
      
110
      glTranslatef(0, pos_left_finger.y, 0);
111
112
      glPushMatrix(); {
113
        glTranslatef(pos_left_finger.x, 0, 0);
114
        z_axis.glRotatef(rot_finger_relative);
115
        scale_finger.glScalef();
116
        glutSolidCube(1.0f);
117
      } glPopMatrix();
118
119
      pos_left_finger.glTranslatef();
120
      z_axis.glRotatef(-rot_finger_relative);
121
      scale_finger.glScalef();
122
      glutSolidCube(1.0f);
123
    } glPopMatrix();
124
125
  } glPopMatrix();
126
127
  // Láda
128
  glPushMatrix(); {
129
    pos_crate.glTranslatef();
130
    glutSolidCube(1.0f);
131
  } glPopMatrix();
132
133
  glutSwapBuffers();
134
}
135
136
enum keys_enum {q, a, w, s, e, d, r, f, num_keys};
137
bool is_pressed[num_keys];
138
void updateCratePos(float dt);
139
140
void onIdle() {
141
  // Itt nem szükséges a pontos diszkrét idő szimuláció
142
  static int last_time = glutGet(GLUT_ELAPSED_TIME);
143
  int curr_time = glutGet(GLUT_ELAPSED_TIME);
144
  float dt = (curr_time - last_time) / 1000.0f; 
145
  last_time = curr_time;
146
147
  if(is_pressed[q] && !is_pressed[a])
148
    rot_finger += 32 * dt;
149
  else if(!is_pressed[q] && is_pressed[a])
150
    rot_finger -= 32 * dt;
151
152
  if(is_pressed[w] && !is_pressed[s])
153
    rot_lower_arm += 32 * dt;
154
  else if(!is_pressed[w] && is_pressed[s])
155
    rot_lower_arm -= 32 * dt;
156
157-
int sgn(float f) {
157+
158-
  if(fabs(f) > 0.5f) {
158+
159-
    return f / fabs(f);
159+
160
    rot_main_arm -= 32 * dt;
161-
    return 0;
161+
162
  if(is_pressed[f] && !is_pressed[r])
163
    rot_base += 32 * dt;
164
  else if(!is_pressed[f] && is_pressed[r])
165
    rot_base -= 32 * dt;
166
167
  updateCratePos(dt);
168
  glutPostRedisplay();
169
}
170
171
void onKeyboard(unsigned char key, int, int) {
172
  switch (key) {
173
    case 'q': case 'Q':
174
      is_pressed[q] = true;
175
      break;
176
    case 'a': case 'A':
177
      is_pressed[a] = true;
178
      break;
179
    case 'w': case 'W':
180
      is_pressed[w] = true;
181
      break;
182
    case 's': case 'S':
183
      is_pressed[s] = true;
184
      break;
185
    case 'e': case 'E':
186
      is_pressed[e] = true;
187
      break;
188
    case 'd': case 'D':
189
      is_pressed[d] = true;
190
      break;
191
    case 'r': case 'R':
192
      is_pressed[r] = true;
193
      break;
194
    case 'f': case 'F':
195
      is_pressed[f] = true;
196
      break;   
197
  }
198
}
199
200
void onKeyboardUp(unsigned char key, int, int) {
201
  switch (key) {
202
    case 'q': case 'Q':
203
      is_pressed[q] = false;
204
      break;
205
    case 'a': case 'A':
206
      is_pressed[a] = false;
207
      break;
208
    case 'w': case 'W':
209
      is_pressed[w] = false;
210
      break;
211
    case 's': case 'S':
212
      is_pressed[s] = false;
213
      break;
214
    case 'e': case 'E':
215
      is_pressed[e] = false;
216
      break;
217
    case 'd': case 'D':
218
      is_pressed[d] = false;
219
      break;
220
    case 'r': case 'R':
221
      is_pressed[r] = false;
222
      break;
223
    case 'f': case 'F':
224
      is_pressed[f] = false;
225
      break;   
226
  }
227
}
228
229
void onInitialization();
230
231
void onMouse(int, int, int, int) {}
232
233
void onMouseMotion(int, int) {}
234
235
int main(int argc, char **argv) {
236
  glutInit(&argc, argv);
237
  glutInitWindowSize(600, 600);
238
  glutInitWindowPosition(100, 100);
239
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
240
241
  glutCreateWindow("Grafika pelda program");
242
243
  glMatrixMode(GL_MODELVIEW);
244
  glLoadIdentity();
245
  glMatrixMode(GL_PROJECTION);
246
  glLoadIdentity();
247
248
  onInitialization();
249
250
  glutDisplayFunc(onDisplay);
251
  glutMouseFunc(onMouse);
252
  glutIdleFunc(onIdle);
253
  glutKeyboardFunc(onKeyboard);
254
  glutKeyboardUpFunc(onKeyboardUp);
255
  glutMotionFunc(onMouseMotion);
256
257
  glutMainLoop();
258
259
  return 0;
260
}
261
262
// Ez a rész szükséges ahhoz, hogy a kockát 3d-ben lásd, 
263
// de a példa szempontjából most lényegtelen, hogy 
264
// ténylegesen mit csinál.
265
void onInitialization() {
266
  glEnable(GL_DEPTH_TEST);
267
  glMatrixMode(GL_PROJECTION);
268
  gluPerspective(60, 1, 0.1, 20);
269
  glMatrixMode(GL_MODELVIEW);
270
  gluLookAt(-9, 6, -6, 9, 0, 0, 0, 1, 0);
271
  glEnable(GL_LIGHTING);
272
  glEnable(GL_LIGHT0);
273
  float p[4] = {-2, 4, -1, 0};
274
  glLightfv(GL_LIGHT0, GL_POSITION, p);
275
  // float c[4] = {0.0f, 0.4f, 1.0f, 1.0f};
276
  // glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c);
277
}
278
279
Vector pos_left_finger_tip, pos_right_finger_tip, pos_grab;
280
281
float crate_speed = 0.0f;
282
283
// A példa program egy másik szépséghibája, 
284
// hogy a láda mefogásához hackelni kell egy kicsit.
285
void updateCratePos(float dt) {
286
  glPushMatrix(); {
287
    glLoadIdentity();
288
    y_axis.glRotatef(rot_base);
289
    x_axis.glRotatef(rot_main_arm);
290
    pos_main_arm.glTranslatef();
291
    pos_main_arm.glTranslatef();
292
    x_axis.glRotatef(rot_lower_arm);
293
    pos_lower_arm.glTranslatef();
294
    pos_lower_arm.glTranslatef();
295
296
    glPushMatrix(); {
297
      z_axis.glRotatef(-rot_finger);
298
      glTranslatef(0, pos_right_finger.y, 0);
299
      pos_right_finger.glTranslatef();
300
      z_axis.glRotatef(rot_finger_relative);
301
      scale_finger.glScalef();
302
      glTranslatef(0, -0.5f, 0);
303
304
      float mx[16];
305
      glGetFloatv(GL_MODELVIEW_MATRIX, mx);
306
      pos_right_finger_tip.x = mx[12];
307
      pos_right_finger_tip.y = mx[13];
308
      pos_right_finger_tip.z = mx[14];
309
310
    } glPopMatrix();
311
312
    glPushMatrix(); {
313
      z_axis.glRotatef(rot_finger);
314
      glTranslatef(0, pos_left_finger.y, 0);
315
      pos_left_finger.glTranslatef();
316
      z_axis.glRotatef(-rot_finger_relative);
317
      scale_finger.glScalef();
318
      glTranslatef(0, -0.5f, 0);
319
320
      float mx[16];
321
      glGetFloatv(GL_MODELVIEW_MATRIX, mx);
322
      pos_left_finger_tip.x = mx[12];
323
      pos_left_finger_tip.y = mx[13];
324
      pos_left_finger_tip.z = mx[14];
325
    } glPopMatrix();
326
327
  } glPopMatrix();
328
329
  pos_grab = (pos_right_finger_tip + pos_left_finger_tip) / 2;
330
331
  if((pos_right_finger_tip - pos_left_finger_tip).length() < 1.2f 
332
      && (pos_grab - pos_crate).length() < 0.6f) {
333
    pos_crate = pos_grab;
334
  } else {
335
    if(pos_crate.y > 0) {
336
      crate_speed -= 0.1 * dt;
337
      pos_crate.y += crate_speed;
338
    } else {
339
      crate_speed = 0;
340
    }
341
  }
342
}