Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdlib.h>
- #if defined(__APPLE__)
- #include <OpenGL/gl.h>
- #include <OpenGL/glu.h>
- #include <GLUT/glut.h>
- #else
- #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
- #include <windows.h>
- #endif
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #endif
- struct Vector {
- float x, y, z;
- Vector(float v = 0) : x(v), y(v), z(v) { }
- Vector(float x, float y, float z) : x(x), y(y), z(z) { }
- Vector operator+(const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }
- Vector operator-(const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }
- Vector operator*(const Vector& v) const { return Vector(x * v.x, y * v.y, z * v.z); }
- Vector operator/(const Vector& v) const { return Vector(x / v.x, y / v.y, z / v.z); }
- float length() const { return sqrt(x*x + y*y + z*z); }
- void glTranslatef() { ::glTranslatef(x, y, z); }
- void glRotatef(float angle) { ::glRotatef(angle, x, y, z); }
- void glScalef() { ::glScalef(x, y, z); }
- };
- Vector x_axis(1, 0, 0), y_axis(0, 1, 0), z_axis(0, 0, 1);
- 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),
- scale_main_arm(1, 1, 4), pos_lower_arm(0, 0, -1.5f), scale_lower_arm(0.7f, 0.7f, 3.0f), scale_wrist(1, 1, 1),
- pos_left_finger(0.5f, -1.0f, 0.0f), pos_right_finger(-0.5f, -1.0f, 0.0f), scale_finger(0.2f, 1.0f, 0.2f);
- float rot_base = 0, rot_main_arm = 70, rot_lower_arm = -60, rot_finger = 20, rot_finger_relative = 20;
- void onDisplay() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix(); {
- y_axis.glRotatef(rot_base);
- // Jobb oldali alap
- glPushMatrix(); {
- pos_right_base.glTranslatef();
- scale_base.glScalef();
- glutSolidCube(1.0f);
- } glPopMatrix();
- // Bal oldali alap
- glPushMatrix(); {
- pos_left_base.glTranslatef();
- scale_base.glScalef();
- glutSolidCube(1.0f);
- } glPopMatrix();
- x_axis.glRotatef(rot_main_arm);
- pos_main_arm.glTranslatef();
- // Felkar
- glPushMatrix(); {
- scale_main_arm.glScalef();
- glutSolidCube(1.0f);
- } glPopMatrix();
- pos_main_arm.glTranslatef();
- x_axis.glRotatef(rot_lower_arm);
- pos_lower_arm.glTranslatef();
- // Alkar
- glPushMatrix(); {
- scale_lower_arm.glScalef();
- glutSolidCube(1.0f);
- } glPopMatrix();
- pos_lower_arm.glTranslatef();
- // Csukló
- glPushMatrix(); {
- scale_wrist.glScalef();
- glutSolidCube(1.0f);
- } glPopMatrix();
- // Jobb 'ujj'
- glPushMatrix(); {
- z_axis.glRotatef(-rot_finger);
- glTranslatef(0, pos_right_finger.y, 0);
- glPushMatrix(); {
- glTranslatef(pos_right_finger.x, 0, 0);
- z_axis.glRotatef(-rot_finger_relative);
- scale_finger.glScalef();
- glutSolidCube(1.0f);
- } glPopMatrix();
- pos_right_finger.glTranslatef();
- z_axis.glRotatef(rot_finger_relative);
- scale_finger.glScalef();
- glutSolidCube(1.0f);
- } glPopMatrix();
- // Bal 'ujj'
- glPushMatrix(); {
- z_axis.glRotatef(rot_finger);
- glTranslatef(0, pos_left_finger.y, 0);
- glPushMatrix(); {
- glTranslatef(pos_left_finger.x, 0, 0);
- z_axis.glRotatef(rot_finger_relative);
- scale_finger.glScalef();
- glutSolidCube(1.0f);
- } glPopMatrix();
- pos_left_finger.glTranslatef();
- z_axis.glRotatef(-rot_finger_relative);
- scale_finger.glScalef();
- glutSolidCube(1.0f);
- } glPopMatrix();
- } glPopMatrix();
- // Láda
- glPushMatrix(); {
- pos_crate.glTranslatef();
- glutSolidCube(1.0f);
- } glPopMatrix();
- glutSwapBuffers();
- }
- enum keys_enum {q, a, w, s, e, d, r, f, num_keys};
- bool is_pressed[num_keys];
- void updateCratePos(float dt);
- void onIdle() {
- // Itt nem szükséges a pontos diszkrét idő szimuláció
- static int last_time = glutGet(GLUT_ELAPSED_TIME);
- int curr_time = glutGet(GLUT_ELAPSED_TIME);
- float dt = (curr_time - last_time) / 1000.0f;
- last_time = curr_time;
- if(is_pressed[q] && !is_pressed[a])
- rot_finger += 32 * dt;
- else if(!is_pressed[q] && is_pressed[a])
- rot_finger -= 32 * dt;
- if(is_pressed[w] && !is_pressed[s])
- rot_lower_arm += 32 * dt;
- else if(!is_pressed[w] && is_pressed[s])
- rot_lower_arm -= 32 * dt;
- if(is_pressed[e] && !is_pressed[d])
- rot_main_arm += 32 * dt;
- else if(!is_pressed[e] && is_pressed[d])
- rot_main_arm -= 32 * dt;
- if(is_pressed[f] && !is_pressed[r])
- rot_base += 32 * dt;
- else if(!is_pressed[f] && is_pressed[r])
- rot_base -= 32 * dt;
- updateCratePos(dt);
- glutPostRedisplay();
- }
- void onKeyboard(unsigned char key, int, int) {
- switch (key) {
- case 'q': case 'Q':
- is_pressed[q] = true;
- break;
- case 'a': case 'A':
- is_pressed[a] = true;
- break;
- case 'w': case 'W':
- is_pressed[w] = true;
- break;
- case 's': case 'S':
- is_pressed[s] = true;
- break;
- case 'e': case 'E':
- is_pressed[e] = true;
- break;
- case 'd': case 'D':
- is_pressed[d] = true;
- break;
- case 'r': case 'R':
- is_pressed[r] = true;
- break;
- case 'f': case 'F':
- is_pressed[f] = true;
- break;
- }
- }
- void onKeyboardUp(unsigned char key, int, int) {
- switch (key) {
- case 'q': case 'Q':
- is_pressed[q] = false;
- break;
- case 'a': case 'A':
- is_pressed[a] = false;
- break;
- case 'w': case 'W':
- is_pressed[w] = false;
- break;
- case 's': case 'S':
- is_pressed[s] = false;
- break;
- case 'e': case 'E':
- is_pressed[e] = false;
- break;
- case 'd': case 'D':
- is_pressed[d] = false;
- break;
- case 'r': case 'R':
- is_pressed[r] = false;
- break;
- case 'f': case 'F':
- is_pressed[f] = false;
- break;
- }
- }
- void onInitialization();
- void onMouse(int, int, int, int) {}
- void onMouseMotion(int, int) {}
- int main(int argc, char **argv) {
- glutInit(&argc, argv);
- glutInitWindowSize(600, 600);
- glutInitWindowPosition(100, 100);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Grafika pelda program");
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- onInitialization();
- glutDisplayFunc(onDisplay);
- glutMouseFunc(onMouse);
- glutIdleFunc(onIdle);
- glutKeyboardFunc(onKeyboard);
- glutKeyboardUpFunc(onKeyboardUp);
- glutMotionFunc(onMouseMotion);
- glutMainLoop();
- return 0;
- }
- // Ez a rész szükséges ahhoz, hogy a kockát 3d-ben lásd,
- // de a példa szempontjából most lényegtelen, hogy
- // ténylegesen mit csinál.
- void onInitialization() {
- glEnable(GL_DEPTH_TEST);
- glMatrixMode(GL_PROJECTION);
- gluPerspective(60, 1, 0.1, 20);
- glMatrixMode(GL_MODELVIEW);
- gluLookAt(-9, 6, -6, 9, 0, 0, 0, 1, 0);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- float p[4] = {-2, 4, -1, 0};
- glLightfv(GL_LIGHT0, GL_POSITION, p);
- // float c[4] = {0.0f, 0.4f, 1.0f, 1.0f};
- // glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c);
- }
- Vector pos_left_finger_tip, pos_right_finger_tip, pos_grab;
- float crate_speed = 0.0f;
- // A példa program egy másik szépséghibája,
- // hogy a láda mefogásához hackelni kell egy kicsit.
- void updateCratePos(float dt) {
- glPushMatrix(); {
- glLoadIdentity();
- y_axis.glRotatef(rot_base);
- x_axis.glRotatef(rot_main_arm);
- pos_main_arm.glTranslatef();
- pos_main_arm.glTranslatef();
- x_axis.glRotatef(rot_lower_arm);
- pos_lower_arm.glTranslatef();
- pos_lower_arm.glTranslatef();
- glPushMatrix(); {
- z_axis.glRotatef(-rot_finger);
- glTranslatef(0, pos_right_finger.y, 0);
- pos_right_finger.glTranslatef();
- z_axis.glRotatef(rot_finger_relative);
- scale_finger.glScalef();
- glTranslatef(0, -0.5f, 0);
- float mx[16];
- glGetFloatv(GL_MODELVIEW_MATRIX, mx);
- pos_right_finger_tip.x = mx[12];
- pos_right_finger_tip.y = mx[13];
- pos_right_finger_tip.z = mx[14];
- } glPopMatrix();
- glPushMatrix(); {
- z_axis.glRotatef(rot_finger);
- glTranslatef(0, pos_left_finger.y, 0);
- pos_left_finger.glTranslatef();
- z_axis.glRotatef(-rot_finger_relative);
- scale_finger.glScalef();
- glTranslatef(0, -0.5f, 0);
- float mx[16];
- glGetFloatv(GL_MODELVIEW_MATRIX, mx);
- pos_left_finger_tip.x = mx[12];
- pos_left_finger_tip.y = mx[13];
- pos_left_finger_tip.z = mx[14];
- } glPopMatrix();
- } glPopMatrix();
- pos_grab = (pos_right_finger_tip + pos_left_finger_tip) / 2;
- if((pos_right_finger_tip - pos_left_finger_tip).length() < 1.2f
- && (pos_grab - pos_crate).length() < 0.6f) {
- pos_crate = pos_grab;
- } else {
- if(pos_crate.y > 0) {
- crate_speed -= 0.1 * dt;
- pos_crate.y += crate_speed;
- } else {
- crate_speed = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement