現在地 >> メニュー >> サンプルコード::OpenGL >> テクスチャ座標による移動

問題


1×1の四角いポリゴンの左下に以下の画像をはり、
その後、テクスチャを移動することで、ポリゴンの中心付近に画像を動かせ。

こたえ


#include <cstdio>
#include <cstdlib>
#include <cv.h>
#include <highgui.h>

#include <GL/glut.h>
#include <GL/gl.h>


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

void DRAW_SQU();


using namespace std;

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

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

image1=cvLoadImage("mask2.bmp",CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if(image1==NULL)
{
printf("can't load image \n");
std::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);
}

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

inline void MY_INIT()
{
glClearColor(0.2, 0.8, 0.2, 1.0);
LoadGLTextures();


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

}

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(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glEnable(GL_DEPTH_TEST);




glPushMatrix();

glMatrixMode(GL_TEXTURE); //モード切替え
glLoadIdentity();

glTranslated(-0.5, -0.5, 0.0);
DRAW_SQU();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPopMatrix();


glDisable(GL_DEPTH_TEST);

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);
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}


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

/**********[ここから各種関数]***********************/

void DRAW_SQU()
{
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);

glTexCoord2d(0.0, 2.0);
glVertex2d(-1,1);

glTexCoord2d(0.0, 0.0);
glVertex2d(-1,-1);

glTexCoord2d(2.0, 0.0);
glVertex2d(1,-1);

glTexCoord2d(2.0, 2.0);
glVertex2d(1,1);

glEnd();

glDisable(GL_TEXTURE_2D);
}

メモ


テクスチャの移動はポリゴンが移動していると、とらえる。

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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