現在地 >> メニュー >> サンプルコード::OpenGL >> 1次元テクスチャ >> 1次元テクスチャと画像

問題


以下の画像を読み込んで、各線分に一次元テクスチャをはって、画像を表示せよ。

画像


その他条件:
 線分の長さは2
 アスペクト比も考慮する。

答え


#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 DRAW_XYZ();
void DRAW_LINE();


using namespace std;


/*****[テクスチャ関連]*****/
GLuint *texture;
int height;
int width;
IplImage *image1;

/*****[画像読み込み準備]******************/
inline void LoadGLTextures()
{
image1=cvLoadImage("file4.jpg",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);

/******* テクスチャ作成 ****************/

texture = new GLuint[image1->width];

glGenTextures(image1->height, &texture[0]);
for(int j = 0;j<image1->height;j++)
{
glBindTexture(GL_TEXTURE_1D, texture[j]);
glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
gluBuild1DMipmaps(GL_TEXTURE_1D, 3, image1->width,GL_RGB,GL_UNSIGNED_BYTE,&image1->imageData[j*image1->widthStep]);
}

height = image1->height;
width = image1->width;
cvReleaseImage( &image1 );

};


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

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

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

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(5.0, 6.0, 7.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glEnable(GL_DEPTH_TEST);

glLineWidth(1);
DRAW_XYZ();
glColor3d(1,1,1);

double h = 0;
for(int j = 0;j < height ;j++,h += 2.0/width)
{
glBindTexture(GL_TEXTURE_1D, texture[j]);
glPushMatrix();
glTranslated(0,h,0);
DRAW_LINE();
glPopMatrix();
glBindTexture(GL_TEXTURE_1D, 0);

}

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);
glLoadIdentity();

}




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

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();

}

void DRAW_LINE()
{

glEnable(GL_TEXTURE_1D);
glLineWidth(1);
glBegin(GL_LINES);

glTexCoord1d(0.0);
glVertex2d(0,0.1);

glTexCoord1d(1.0);
glVertex2d(2,0.1);
glEnd();

glDisable(GL_TEXTURE_1D);
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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