티스토리 뷰

C++

OpenGL 기반의 Walker 예제

zedo 2017. 3. 5. 17:15

#include <cmath>

#include <ctime>

#include <cstdlib>

#include <iostream>

#include <sstream>

#include <string>

#include <GL/glut.h>


using namespace std;


const int width = 640;

const int height = 480;


class Walker {

public : 

int x,y;

double timeFrame = 0.0;

double deltaTime = 0.01;


Walker() {

srand((unsigned)time(NULL)); // seed value

x = width/2;

y = height/2;

}


// Randomly move to any neighboring pixel (or stay in the same spot)

void step() {

timeFrame = timeFrame + deltaTime;

int stepx = rand() % 3 - 1; // generate random number range : (-1,0,1)

int stepy = rand() % 3 - 1; // generate random number range : (-1,0,1)

x += stepx;

y += stepy;

        if (x >=  width) x = width - 1;     // x limit(high)

        if (x < 0) x = 0;       // x limit(low)

if (y >= height) y = height - 1;         // y limit(high)

        if (y < 0) y = 0;       // y limit(low)

}

};


Walker *walker;

void create_walker() {

    walker = new Walker();

}


void drawText(const string &str, double x, double y) {

    glRasterPos2d(x, y);

    int len = str.find('\0');

    for (int i = 0; i < len; i++)

       glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, str[i]);

}


// Glut callback function to update the display

void display() {

    glClear(GL_COLOR_BUFFER_BIT);


    static GLubyte color_table[6][3] =

        {{255, 0, 0}, {0, 255, 0}, {0, 0, 255},

         {0, 255, 255}, {255, 0, 255}, {255, 255, 0}}; // Red, Green, Blue, Sky, Purple, Yellow

glColor3ubv(color_table[0]);

glPointSize(10);

glBegin(GL_POINTS);

glVertex2f(walker->x, walker->y);

glEnd();

    glColor3ub(0, 0, 0);

    ostringstream os;

    os << "x pos: " << walker->x << "   y pos " << walker->y << ends;

    drawText(os.str(), 12, 24);

    os.seekp(0);

    glutSwapBuffers();

}


// Glut callback function to reset the viewing transformation

void reshape(int w, int h) {

    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    glOrtho( 0.0, 640.0, 0.0, 480.0, 1.0, -1.0 );

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

}


void animation_step() {

double frames_per_second = 30;   // for animation in real time

    double start = walker->timeFrame;

    clock_t start_time = clock();

    walker->step();

    double tau = 1.0 / frames_per_second;

    while ((double(clock()) - start_time)/CLOCKS_PER_SEC < tau)

        ;

    glutPostRedisplay();

}


bool running = false;   // is the animation running


// Glut mouse callback function

void mouse(int button, int state, int x, int y) {

    switch (button) {

    case GLUT_LEFT_BUTTON:

        if (state == GLUT_DOWN) {

            if (running) {

                glutIdleFunc(NULL);

                running = false;

            } else {

                glutIdleFunc(animation_step);

                running = true;

            }

        }

    }

}


int main(int argc, char *argv[]) {

    glutInit(&argc, argv);

    create_walker();

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    glutInitWindowSize(width, height);

    glutInitWindowPosition(100, 100);

    glutCreateWindow("Walker");

    glClearColor(1.0, 1.0, 1.0, 0.0);

    glShadeModel(GL_FLAT);

    glutDisplayFunc(display);

    glutReshapeFunc(reshape);

    glutMouseFunc(mouse);

    glutMainLoop();

}


------------------------------------------------------------

예제를 원으로 표현한 경우

------------------------------------------------------------

#include <cmath>

#include <ctime>

#include <cstdlib>

#include <iostream>

#include <sstream>

#include <string>

#include <GL/glut.h>


using namespace std;


const double pi = 4 * atan(1.0);

const int width = 640;

const int height = 480;


class Walker {

public : 

int x,y;

double timeFrame = 0.0;

double deltaTime = 0.01;


Walker() {

srand((unsigned)time(NULL)); // seed value

x = width/2;

y = height/2;

}


// Randomly move to any neighboring pixel (or stay in the same spot)

void step() {

timeFrame = timeFrame + deltaTime;

int stepx = rand() % 3 - 1; // generate random number range : (-1,0,1)

int stepy = rand() % 3 - 1; // generate random number range : (-1,0,1)

x += stepx;

y += stepy;

        if (x >=  width) x = width - 1;     // x limit(high)

        if (x < 0) x = 0;       // x limit(low)

if (y >= height) y = height - 1;         // y limit(high)

        if (y < 0) y = 0;       // y limit(low)

}

};


Walker *walker;

void create_walker() {

    walker = new Walker();

}


void drawText(const string &str, double x, double y) {

    glRasterPos2d(x, y);

    int len = str.find('\0');

    for (int i = 0; i < len; i++)

       glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, str[i]);

}


// Glut callback function to update the display

void display() {

    glClear(GL_COLOR_BUFFER_BIT);


    static GLubyte color_table[6][3] =

        {{255, 0, 0}, {0, 255, 0}, {0, 0, 255},

         {0, 255, 255}, {255, 0, 255}, {255, 255, 0}}; // Red, Green, Blue, Sky, Purple, Yellow

glColor3ubv(color_table[0]);

//glPointSize(10);

glBegin(GL_TRIANGLE_FAN);

glVertex2d(walker->x, walker->y);

double phi = 2 * pi / 24;

        for (int j = 0; j < 25; j++)

                glVertex2d(walker->x + 10 * cos(phi*j),

                           walker->y + 10 * sin(phi*j));

glEnd();

    glColor3ub(0, 0, 0);

    ostringstream os;

    os << "x pos: " << walker->x << "   y pos " << walker->y << ends;

    drawText(os.str(), 12, 24);

    os.seekp(0);

    glutSwapBuffers();

}


// Glut callback function to reset the viewing transformation

void reshape(int w, int h) {

    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    glOrtho( 0.0, 640.0, 0.0, 480.0, 1.0, -1.0 );

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

}


void animation_step() {

double frames_per_second = 30;   // for animation in real time

    double start = walker->timeFrame;

    clock_t start_time = clock();

    walker->step();

    double tau = 1.0 / frames_per_second;

    while ((double(clock()) - start_time)/CLOCKS_PER_SEC < tau)

        ;

    glutPostRedisplay();

}


bool running = false;   // is the animation running


// Glut mouse callback function

void mouse(int button, int state, int x, int y) {

    switch (button) {

    case GLUT_LEFT_BUTTON:

        if (state == GLUT_DOWN) {

            if (running) {

                glutIdleFunc(NULL);

                running = false;

            } else {

                glutIdleFunc(animation_step);

                running = true;

            }

        }

    }

}


int main(int argc, char *argv[]) {

    glutInit(&argc, argv);

    create_walker();

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    glutInitWindowSize(width, height);

    glutInitWindowPosition(100, 100);

    glutCreateWindow("Walker");

    glClearColor(1.0, 1.0, 1.0, 0.0);

    glShadeModel(GL_FLAT);

    glutDisplayFunc(display);

    glutReshapeFunc(reshape);

    glutMouseFunc(mouse);

    glutMainLoop();

}

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2025/02   »
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
글 보관함