現在地 >> メニュー >> サンプルコード::OpenGL >> 投影テクスチャ

問題


以下の画像を読み込んで、テクスチャをポリゴンに投影せよ。

画像

その他条件:
 はじめの投影位置は、(0,0,1) → キーボードで投影位置を変更可能
 「↑」「↓」「→」「←」:x、y座標の移動
 「b」「v」:z座標の移動
 マウスで視点を変更できる

投影テクスチャ2

答え


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


using namespace std;

/******** データ構造の定義 ***********/
typedef struct
{
double x,y,z;

}PROJ_EYE;
PROJ_EYE proj_A={0,0,1};


/********** プロトタイプ宣言 ****************/
void SET_UP_TEX_GEN();

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


void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);


void keyboard(unsigned char key, int x, int y);
void SpecialKey(int key, int x, int y);


void DRAW_XYZ();
void DRAW_SQU();
void SET_UP_PROJ_TEX();



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

double genfunc[][4] = {
{ 1.0, 0.0, 0.0, 0.0 }, // s
{ 0.0, 1.0, 0.0, 0.0 }, // t
{ 0.0, 0.0, 1.0, 0.0 }, // r
{ 0.0, 0.0, 0.0, 1.0 }, // q
};



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

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

cvCvtColor(imgA, imgA, CV_BGR2RGB);
cvFlip(imgA,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, imgA->width,imgA->height, GL_RGB,GL_UNSIGNED_BYTE,imgA->imageData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

cvReleaseImage( &imgA );
};





/************ OpenGLの設定 ****************/
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
}


inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);


glutMouseFunc(myMouseFunc);
glutMotionFunc(myMouseMotion);

glutSpecialFunc(SpecialKey);

glutKeyboardFunc(keyboard);
}




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

SET_UP_TEX_GEN();


}

void SET_UP_TEX_GEN()
{
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGendv(GL_S, GL_OBJECT_PLANE, genfunc[0]);
glTexGendv(GL_T, GL_OBJECT_PLANE, genfunc[1]);
glTexGendv(GL_R, GL_OBJECT_PLANE, genfunc[2]);
glTexGendv(GL_Q, GL_OBJECT_PLANE, genfunc[3]);
}

/*********** main関数 **************/
int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();
return 0;
}



/********[ここからコールバック]****************************************/
double xAngle = 0.0, yAngle = 0.0;
double aspect;
void display()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

SET_UP_PROJ_TEX();

glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glRotated(xAngle,1,0,0);
glRotated(yAngle,0,1,0);

glEnable(GL_DEPTH_TEST);


DRAW_XYZ();


glColor3d(1,0,1);
glPointSize(5);
glBegin(GL_POINTS);
glVertex3d(proj_A.x, proj_A.y, proj_A.z);
glEnd();
glColor3d(1,1,1);


glPushMatrix();
DRAW_SQU();
glPopMatrix();


glDisable(GL_DEPTH_TEST);

glutSwapBuffers();


}


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

}



int xStart,yStart;
bool mouseFlag = GL_FALSE;
void myMouseFunc(int button,int state,int x,int y)
{
if(button == GLUT_LEFT_BUTTON &&state == GLUT_DOWN)
{
xStart = x;
yStart = y;
mouseFlag = GL_TRUE;
}
else
{
mouseFlag = GL_FALSE;

}

}

void myMouseMotion(int x,int y)
{
int xdis,ydis;
double a = 0.5;

if(mouseFlag == GL_FALSE)return;

xdis = x - xStart;
ydis = y - yStart;

xAngle += (double)ydis * a;
yAngle += (double)xdis * a;

xStart = x;
yStart = y;
glutPostRedisplay();
}



void SpecialKey(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
proj_A.y += 0.1;
break;

case GLUT_KEY_DOWN:
proj_A.y -= 0.1;
break;

case GLUT_KEY_RIGHT:
proj_A.x +=0.1;
break;

case GLUT_KEY_LEFT:
proj_A.x -=0.1;
break;

}

glutPostRedisplay();
}


void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'b':
proj_A.z += 0.1;
break;

case 'v':
proj_A.z -= 0.1;
break;
}
glutPostRedisplay();
}


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


void DRAW_XYZ()
{
glBegin(GL_LINES);

glColor3d(0,1,0);//x
glVertex2d(-100,0);
glVertex2d(100, 0);

glColor3d(1,0,0);//y
glVertex2d(0,0);
glVertex2d(0,100);

glColor3d(0,0,1);//z
glVertex3d(0,0,-100);
glVertex3d(0,0, 100);
glEnd();

glColor3d(1,1,1);
}


void DRAW_SQU()
{
glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);
glEnable(GL_TEXTURE_GEN_Q);


glRectd(-0.5,0.5,0.5,-0.5);


glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_GEN_Q);
glDisable(GL_TEXTURE_2D);
}


/*********** 投影位置の設定 ******************/
void SET_UP_PROJ_TEX()
{
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslated(0.5, 0.5, 0.0);
gluPerspective(30.0, aspect, 1.0, 100.0);
gluLookAt(proj_A.x, proj_A.y, proj_A.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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