最終更新: mikk_ni3_92 2007年09月03日(月) 17:29:48履歴
現在地 >> メニュー >> サンプルコード::OpenGL >> マウスと3次元位置
#include <cstdio>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
void display();
void reshape(int w, int h);
void timer(int value);
void mouse(int button, int state,int x, int y);
void DRAW_XYZ();
using namespace std;
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}
inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(10,timer,0);
glutMouseFunc(mouse);
}
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();
return 0;
}
GLfloat light_pos[] = { 0.0, 3.0, 5.0, 1.0 };
GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 };
GLfloat blue[] = { 0.2, 0.2, 0.8, 1.0 };
/********[ここからコールバック]****************************************/
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(3, 4, 5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red);
glBegin(GL_QUADS);
glNormal3d(0,1,1);
glVertex3f(0,0,0);
glVertex3f(2,0,0);
glVertex3f(2,2,0);
glVertex3f(0,2,0);
glEnd();
glPopMatrix();
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red);
glBegin(GL_QUADS);
glNormal3d(1,1,0);
glVertex3f(0,0,0);
glVertex3f(0,0,2);
glVertex3f(0,2,2);
glVertex3f(0,2,0);
glEnd();
glPopMatrix();
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blue);
DRAW_XYZ();
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
}
int window_h;
void reshape(int w, int h)
{
window_h = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1,timer,0); //タイマー関数
}
void DRAW_XYZ()
{
glBegin(GL_LINES);
glVertex2d(-100,0);
glVertex2d(100, 0);
glVertex2d(0,0);
glVertex2d(0,100);
glVertex3d(0,0,-100);
glVertex3d(0,0, 100);
glEnd();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
GLdouble model[16], proj[16];
GLint view[4];
GLfloat z;
GLdouble ox, oy, oz;
/*** 各種行列の取得 ***/
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);
/*****クリック位置(ピクセル)の奥行き値を取得*********/
glReadPixels(x, window_h - y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
/* ox, oy, oz にクリックしたところの3次元位置が入る */
gluUnProject(x, window_h - y, z, model, proj, view, &ox, &oy, &oz);
/**テスト用出力***/
printf("(x,y,z)=(%f,%f,%f)\n",ox,oy,oz);
}
}
#include <cstdio>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
void display();
void reshape(int w, int h);
void timer(int value);
void mouse(int button, int state,int x, int y);
void DRAW_XYZ();
using namespace std;
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}
inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(10,timer,0);
glutMouseFunc(mouse);
}
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();
return 0;
}
GLfloat light_pos[] = { 0.0, 3.0, 5.0, 1.0 };
GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 };
GLfloat blue[] = { 0.2, 0.2, 0.8, 1.0 };
/********[ここからコールバック]****************************************/
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(3, 4, 5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red);
glBegin(GL_QUADS);
glNormal3d(0,1,1);
glVertex3f(0,0,0);
glVertex3f(2,0,0);
glVertex3f(2,2,0);
glVertex3f(0,2,0);
glEnd();
glPopMatrix();
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red);
glBegin(GL_QUADS);
glNormal3d(1,1,0);
glVertex3f(0,0,0);
glVertex3f(0,0,2);
glVertex3f(0,2,2);
glVertex3f(0,2,0);
glEnd();
glPopMatrix();
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blue);
DRAW_XYZ();
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
}
int window_h;
void reshape(int w, int h)
{
window_h = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1,timer,0); //タイマー関数
}
void DRAW_XYZ()
{
glBegin(GL_LINES);
glVertex2d(-100,0);
glVertex2d(100, 0);
glVertex2d(0,0);
glVertex2d(0,100);
glVertex3d(0,0,-100);
glVertex3d(0,0, 100);
glEnd();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
GLdouble model[16], proj[16];
GLint view[4];
GLfloat z;
GLdouble ox, oy, oz;
/*** 各種行列の取得 ***/
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);
/*****クリック位置(ピクセル)の奥行き値を取得*********/
glReadPixels(x, window_h - y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
/* ox, oy, oz にクリックしたところの3次元位置が入る */
gluUnProject(x, window_h - y, z, model, proj, view, &ox, &oy, &oz);
/**テスト用出力***/
printf("(x,y,z)=(%f,%f,%f)\n",ox,oy,oz);
}
}