最終更新: mikk_ni3_92 2007年10月01日(月) 10:29:16履歴
現在地 >> メニュー >> サンプルコード::OpenGL >> テクスチャ作成 >> OpenCVとテクスチャ作成
関連:OpenCVで読み込む
関連:OpenCVで読み込む
#include <iostream> #include <cv.h> #include <highgui.h> #include <GL/glut.h> //------------- プロトタイプ宣言 --------------------// void display(); void reshape(int w, int h); void timer(int value); void DRAW_SQU(); //----- テクスチャID -----// GLuint texture[1]; //---------- 画像を読み込んでコンバート ----------------// bool LoadGLTextures() { IplImage *image1=cvLoadImage("test.jpg",CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); if(image1==NULL) { return false; } 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 ); return true; }; //-------------OpenGLの初期設定------------------// void GLUT_INIT() { glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("window name"); } void GLUT_CALL_FUNC() { glutDisplayFunc(display); glutReshapeFunc(reshape); glutTimerFunc(0,timer,17); } void MY_INIT() { glClearColor(1.0, 1.0, 1.0, 1.0); if( LoadGLTextures() == false) { std::cerr << "Can't Load Image\n"; } } int main(int argc, char **argv) { glutInit(&argc,argv); GLUT_INIT(); GLUT_CALL_FUNC(); MY_INIT(); glutMainLoop(); return 0; } /********[ここからコールバック]****************************************/ void display() { static int r = 0; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glPushMatrix(); glRotated((double)r, 0.0, 1.0, 0.0); DRAW_SQU(); glPopMatrix(); glDisable(GL_DEPTH_TEST); glutSwapBuffers(); if(++r > 360){ r= 0; } } 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(); 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(value,timer,17); //タイマー関数 } /**********[ここから各種関数]***********************/ void DRAW_SQU() { glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2d(0.0, 1.0); glVertex2d(-1,1); glTexCoord2d(0.0, 0.0); glVertex2d(-1,-1); glTexCoord2d(1.0, 0.0); glVertex2d(1,-1); glTexCoord2d(1.0, 1.0); glVertex2d(1,1); glEnd(); glDisable(GL_TEXTURE_2D); }