現在地 >> メニュー >> サンプルコード::OpenGL >> 旗の表現 >> 旗の表現とテクスチャ >> 旗とテクスチャとアニメーション


問題


旗の表現とテクスチャのプログラムを利用して、旗のなびくようなアニメーションをせよ。

画像

答え

#include <iostream>
#include <cmath>
#include <cv.h>
#include <highgui.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>

void display();
void reshape(int w, int h);
void timer(int value);

using namespace std;



/*****[テクスチャ関連]*****/
GLuint texture[1];


/*****[画像読み込み準備]******************/
inline void LoadGLTextures()
{

IplImage *image1=cvLoadImage("test2.jpg",CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if(image1==NULL)
{
printf("can't load image \n");
exit(0);
}

cvCvtColor(image1, image1, CV_BGR2RGB);
cvFlip(image1,NULL,0);

/******* テクスチャ作成 ****************/
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image1->width,image1->height, GL_RGB,GL_UNSIGNED_BYTE,image1->imageData);

cvReleaseImage( &image1 );
};



inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}

inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(10,timer,0);
}


/** 点の数(45×45) **/
double points[45][45][3];

inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);

LoadGLTextures();

for (int y = 0; y < 45; y++)
{
for(int x = 0; x < 45; x++)
{
points[y][x][0]=double((x/5.0)-4.5);
points[y][x][1]=double((y/5.0)-4.5);
points[y][x][2]=double(sin( ( ( (x/5.0)*40.0)/360.0) *3.141592654*2.0));
}
}
}


int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();

return 0;
}

/********[ここからコールバック]****************************************/

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
gluLookAt(15.0, 16.0, 17.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glEnable(GL_TEXTURE_2D);

glBegin(GL_QUADS);

int y,x;
for ( y = 0; y < 44; y++)
{
for ( x = 0; x < 44; x++)
{

double t_x = double (x)/44.0;
double t_y = double (y)/44.0;
double t_x_nex =double (x+1)/44.0;
double t_y_nex =double (y+1)/44.0;

glTexCoord2d(t_x, t_y);
glVertex3d(points[y][x][0], points[y][x][1], points[y][x][2]);

glTexCoord2d(t_x_nex, t_y);
glVertex3d(points[y][x+1][0], points[y][x+1][1], points[y][x+1][2]);


glTexCoord2d(t_x_nex, t_y_nex);
glVertex3d(points[y+1][x+1][0], points[y+1][x+1][1], points[y+1][x+1][2]);

glTexCoord2d(t_x, t_y_nex);
glVertex3d(points[y+1][x][0], points[y+1][x][1],points[y+1][x][2]);
}
}
glEnd();


/*Zの移動*/
double tmp;
for (y = 0; y < 45; y++)
{
tmp = points[y][0][2];
for ( x = 0; x < 44; x++)
{
points[y][x][2] = points[y][x+1][2];
}
points[y][44][2] = tmp;
}

glutSwapBuffers();
}


void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);

}


void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1,timer,0); //タイマー関数
}


メモ


以下の部分を変更すると、より複雑に...

points[y][x][2]=double(sin( ( ( (x/5.0)*40.0)/360.0) *3.141592654*2.0));

      ↓

points[y][x][2]=double(cos*1;



/*Zの移動*/
double tmp;
for (y = 0; y < 45; y++)
{
tmp = points[y][0][2];
for (int x = 0; x < 44; x++)
{
points[y][x][2] = points[y][x+1][2];
}
points[y][44][2] = tmp;
}

     ↓


/*Zの移動1*/
double tmp;
for (y = 0; y < 45; y++)
{
tmp = points[y][0][2];
for (int x = 0; x < 44; x++)
{
points[y][x][2] = points[y][x+1][2];
}
points[y][44][2] = tmp;
}


/*Zの移動2*/

for (int x = 0; x < 45; x++)
{
tmp = points[0][x][2];
for (int y = 0; y < 44; y++)
{
points[y][x][2] = points[y+1][x][2];
}
points[44][x][2] = tmp;
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






▲よろしければ広告のクリックもお願いします


▲ランキングに参加しました

管理人/副管理人のみ編集できます