最終更新: mikk_ni3_92 2007年11月09日(金) 13:05:31履歴
現在地 >> メニュー >> サンプルコード::OpenGL >> 曲面描画2+テクスチャ
#include <iostream>
#include <GL/glut.h>
#include <cv.h>
#include <highgui.h>
void display();
void reshape(int w, int h);
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);
inline void SET_UP_MAP2();
using namespace std;
/*****[制御点関連]******/
GLfloat ctlpoints[3][4][3]
= {
{{0,0,0},{0.5,0,0},{1,0,0},{1.5,0,0}},
{{0,0,-0.5},{0.5,1,-0.5},{1,1,-0.5},{1.5,0,-0.5}},
{{0,0,-1},{0.5,0,-1},{1,0,-1},{1.5,0,-1}},
};
GLfloat texpts[2][2][2]
= {
{{0.0, 0.0}, {1.0, 0.0}},
{{0.0, 1.0}, {1.0, 1.0}}
};
/*****[テクスチャ関連]*****/
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");
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 );
};
/************[OpenGLの初期設定]***********************/
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}
inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(myMouseFunc);
glutMotionFunc(myMouseMotion);
}
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
LoadGLTextures();
SET_UP_MAP2();
glEnable(GL_TEXTURE_2D);
}
inline void SET_UP_MAP2()
{
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,0, 1, 12, 3, &ctlpoints[0][0][0]);
glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2, &texpts[0][0][0]);
glEnable(GL_MAP2_VERTEX_3);
glEnable(GL_MAP2_TEXTURE_COORD_2);
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
}
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;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(3.0, 4.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);
glEvalMesh2(GL_FILL, 0, 20, 0, 20);
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();
}
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();
}
#include <iostream>
#include <GL/glut.h>
#include <cv.h>
#include <highgui.h>
void display();
void reshape(int w, int h);
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);
inline void SET_UP_MAP2();
using namespace std;
/*****[制御点関連]******/
GLfloat ctlpoints[3][4][3]
= {
{{0,0,0},{0.5,0,0},{1,0,0},{1.5,0,0}},
{{0,0,-0.5},{0.5,1,-0.5},{1,1,-0.5},{1.5,0,-0.5}},
{{0,0,-1},{0.5,0,-1},{1,0,-1},{1.5,0,-1}},
};
GLfloat texpts[2][2][2]
= {
{{0.0, 0.0}, {1.0, 0.0}},
{{0.0, 1.0}, {1.0, 1.0}}
};
/*****[テクスチャ関連]*****/
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");
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 );
};
/************[OpenGLの初期設定]***********************/
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}
inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(myMouseFunc);
glutMotionFunc(myMouseMotion);
}
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
LoadGLTextures();
SET_UP_MAP2();
glEnable(GL_TEXTURE_2D);
}
inline void SET_UP_MAP2()
{
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,0, 1, 12, 3, &ctlpoints[0][0][0]);
glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2, &texpts[0][0][0]);
glEnable(GL_MAP2_VERTEX_3);
glEnable(GL_MAP2_TEXTURE_COORD_2);
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
}
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;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(3.0, 4.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);
glEvalMesh2(GL_FILL, 0, 20, 0, 20);
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();
}
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();
}