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
- #ifndef M_PI
- #define M_PI 3.14159265359
- #endif
- struct Vector {
- float x, y;
- Vector(float x = 0.0f, float y = 0.0f) : x(x), y(y) {}
- };
- #define CIRCLE_RESOLUTION 16
- struct Stickman {
- const float period_time, walk_speed;
- bool left_leg_is_front;
- Vector pos;
- Stickman(float period_time)
- : period_time(period_time)
- , walk_speed(0.5f*sin(1.2f*period_time) + 0.5f*sin(1.2f*period_time-0.3f))
- , left_leg_is_front(false)
- { }
- void updatePos(float time) {
- pos.x = time*walk_speed;
- }
- void draw(float time) {
- float modulo_time = fmod(time, period_time) / period_time, mod = modulo_time - 0.5f;
- left_leg_is_front = fmod(time, 2*period_time) > period_time;
- const Vector head_pos(pos.x, 1.75f), neck_pos(pos.x, 1.5f), hip_pos(pos.x, 1.0f);
- // Egy kis trigonometria, a végtagok mozogjanak egy-egy körív mentén
- // (valóságban nagyjából parabola pályán mozognak, de egy egyszerű
- // közelítésnek a kör is megteszi). Az egyszerűség kedvéért a teljes
- // járás periódusnak csak a felét animáljuk le.
- Vector f_elbow_pos( // front_elblow
- neck_pos.x + 0.4f*sin(1.2f*mod),
- neck_pos.y - 0.4f*cos(1.2f*mod)
- );
- Vector f_hand_pos(
- f_elbow_pos.x + 0.4f*sin(M_PI/6 + fabs(mod)),
- f_elbow_pos.y - 0.4f*cos(M_PI/6 + fabs(mod))
- );
- Vector b_elbow_pos(
- neck_pos.x - 0.4f*sin(mod),
- neck_pos.y - 0.4f*cos(mod)
- );
- Vector b_hand_pos(
- b_elbow_pos.x + 0.4f*sin(M_PI/6 + fabs(mod)),
- b_elbow_pos.y - 0.4f*cos(M_PI/6 + fabs(mod))
- );
- Vector f_knee_pos(
- hip_pos.x - 0.5f*sin(1.2f*mod),
- hip_pos.y - 0.5f*cos(1.2f*mod)
- );
- Vector f_leg_pos(
- f_knee_pos.x - 0.5f*sin(1.2f*mod + 0.3f),
- f_knee_pos.y - 0.5f*cos(1.2f*mod)
- );
- Vector b_knee_pos(
- hip_pos.x + 0.5f*sin(1.2f*mod),
- hip_pos.y - 0.5f*cos(1.2f*mod)
- );
- Vector b_leg_pos(
- b_knee_pos.x + 0.5f*sin(1.2f*mod - 0.3f),
- b_knee_pos.y - 0.5f*cos(1.2f*mod)
- );
- glLineWidth(4.0f); // Ez a GL_LINE* primitívek vastagságát állítja
- glColor3f(1.0f, 1.0f, 1.0f);
- // Fej kirajzolása
- glBegin(GL_LINE_STRIP); {
- float radius = head_pos.y - neck_pos.y;
- for(int i = 0; i <= CIRCLE_RESOLUTION; i++) {
- float angle = float(i) / CIRCLE_RESOLUTION * 2.0f * M_PI;
- glVertex2f(head_pos.x + radius*cos(angle), head_pos.y + radius*sin(angle));
- }
- } glEnd();
- // Test kirajzolása
- glBegin(GL_LINES); {
- glVertex2f(neck_pos.x, neck_pos.y);
- glVertex2f(hip_pos.x, hip_pos.y);
- } glEnd();
- // A bal és jobb láb mérete különbözzön egy kicsit.
- if(left_leg_is_front) {
- glLineWidth(4.0f);
- } else {
- glLineWidth(3.0f);
- }
- glBegin(GL_LINES); {
- glVertex2f(neck_pos.x, neck_pos.y);
- glVertex2f(f_elbow_pos.x, f_elbow_pos.y);
- glVertex2f(f_elbow_pos.x, f_elbow_pos.y);
- glVertex2f(f_hand_pos.x, f_hand_pos.y);
- glVertex2f(hip_pos.x, hip_pos.y);
- glVertex2f(f_knee_pos.x, f_knee_pos.y);
- glVertex2f(f_knee_pos.x, f_knee_pos.y);
- glVertex2f(f_leg_pos.x, f_leg_pos.y);
- } glEnd();
- if(!left_leg_is_front) {
- glLineWidth(4.0f);
- } else {
- glLineWidth(3.0f);
- }
- glBegin(GL_LINES); {
- glVertex2f(neck_pos.x, neck_pos.y);
- glVertex2f(b_elbow_pos.x, b_elbow_pos.y);
- glVertex2f(b_elbow_pos.x, b_elbow_pos.y);
- glVertex2f(b_hand_pos.x, b_hand_pos.y);
- glVertex2f(hip_pos.x, hip_pos.y);
- glVertex2f(b_knee_pos.x, b_knee_pos.y);
- glVertex2f(b_knee_pos.x, b_knee_pos.y);
- glVertex2f(b_leg_pos.x, b_leg_pos.y);
- } glEnd();
- }
- } stickman(0.9f);
- struct Ground {
- static void Draw() {
- glLineWidth(5.0f);
- glColor3f(0.0f, 1.0f, 0.0f);
- // Talaj
- glBegin(GL_LINES); {
- glVertex2f(-50, 0);
- glVertex2f(50, 0);
- } glEnd();
- glColor3f(1.0f, 1.0f, 0.0f);
- // Házikó
- glBegin(GL_LINE_STRIP); {
- glVertex2f(2, 0);
- glVertex2f(2, 4);
- glVertex2f(6, 4);
- glVertex2f(6, 0);
- } glEnd();
- glColor3f(1.0f, 0.0f, 0.0f);
- // Tető
- glBegin(GL_LINE_STRIP); {
- glVertex2f(6, 4);
- glVertex2f(4, 6);
- glVertex2f(2, 4);
- } glEnd();
- glColor3f(1.0f, 1.0f, 1.0f);
- // Jobb Ablak
- glBegin(GL_LINE_STRIP); {
- glVertex2f(4.5f, 3.0f);
- glVertex2f(4.5f, 3.5f);
- glVertex2f(5.0f, 3.5f);
- glVertex2f(5.0f, 3.0f);
- glVertex2f(4.5f, 3.0f);
- } glEnd();
- // Bal ablak
- glBegin(GL_LINE_STRIP); {
- glVertex2f(3.5f, 3.0f);
- glVertex2f(3.5f, 3.5f);
- glVertex2f(3.0f, 3.5f);
- glVertex2f(3.0f, 3.0f);
- glVertex2f(3.5f, 3.0f);
- } glEnd();
- glColor3f(168.0f/255.0f, 84.0f/255.0f, 0.0f);
- // Ajtó
- glBegin(GL_LINE_STRIP); {
- glVertex2f(4.5f, 0);
- glVertex2f(4.5f, 2);
- glVertex2f(3.5f, 2);
- glVertex2f(3.5f, 0);
- } glEnd();
- // Fa törzs
- glBegin(GL_LINE_STRIP); {
- glVertex2f(10.0f, 0.0f);
- glVertex2f(10.2f, 2.0f);
- glVertex2f(10.0f, 4.0f);
- glVertex2f(12.0f, 4.0f);
- glVertex2f(11.8f, 2.0f);
- glVertex2f(12.0f, 0.0f);
- } glEnd();
- glColor3f(0.0f, 1.0f, 0.0f);
- // Fa lombkorona
- glBegin(GL_LINE_STRIP); {
- float radius = 2.0f;
- for(int i = 0; i <= CIRCLE_RESOLUTION; i++) {
- float angle = float(i) / CIRCLE_RESOLUTION * 2.0f * M_PI;
- glVertex2f(11.0f + radius*cos(angle), 6.0f + radius*sin(angle));
- }
- } glEnd();
- }
- };
- void onDisplay() {
- glClear(GL_COLOR_BUFFER_BIT);
- float time = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
- stickman.updatePos(time);
- // Megmondjuk a OpenGL-nek, hogy ezután a Projekciósmátrixot
- // akarjuk módosítani a transzformációs mátrix műveletekkel.
- glMatrixMode(GL_PROJECTION);
- // Egység mátrixot töltük be a jelenlegi vetítési mátrix helyére.
- glLoadIdentity();
- // Ez egy olyan merőleges (Orthogonális) vetítés, aminek eredménye képpen a karakter
- // az x tengely mentén középen lesz, és 10 egység (méter) széles részt látunk a világból
- // míg az y tengely mentén a képernyő alsó egy ötödében lesz, és itt is 10 egység magas
- // részt látunk.
- gluOrtho2D(
- stickman.pos.x - 5, stickman.pos.x + 5,
- stickman.pos.y - 2, stickman.pos.y + 8
- );
- Ground::Draw();
- stickman.draw(time);
- glutSwapBuffers();
- }
- void onIdle() {
- glutPostRedisplay();
- }
- void onInitialization() {}
- void onMouse(int, int, int, int) {}
- void onMouseMotion(int, int) {}
- void onKeyboard(unsigned char, int, int) {}
- void onKeyboardUp(unsigned char, 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment