現在地 >> メニュー >> サンプルコード::OpenGL >> マウスとピックアップ処理1 >> マウスとピックアップ処理2 >> マウスとピックアップ処理3

問題


黒いポリゴンを描画し、マウスでクリックしピックアップ処理をせよ。
この時、一番手前のポリゴンにのみ色をつけよ。

その他条件:
 0ヒットで全て黒くなる。

答え




#include <cstdio>
#include <iostream>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>

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

void mouse(int button, int state,int x, int y);

void DRAW_QUADS();
void PICK_UP(int x,int y);
int SELECT_HITS(int hits,GLuint *buf);

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);
glutMouseFunc(mouse);
}

inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.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;

}

/********[ここからコールバック]****************************************/
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(1.0, 2.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glEnable(GL_DEPTH_TEST);

glPushMatrix();
DRAW_QUADS();
glPopMatrix();

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 );
gluLookAt(1.0, 2.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}



void mouse(int button, int state,int x, int y)
{
if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
PICK_UP(x,y);
}
glutPostRedisplay();
}


/**********[ここから各種関数]***********************/
bool FLAG[3] ={false,false,false};
void DRAW_QUADS()
{

glPushName(1);
glBegin(GL_QUADS);
FLAG[0] == true ? glColor3f(1.0, 0.0, 1.0):glColor3f(0.0, 0.0, 0.0);
glVertex2d(-0.5,0.5);
glVertex2d(-0.5,-0.5);
glVertex2d(0.5,-0.5);
glVertex2d(0.5,0.5);
glEnd();
glPopName();

glPushName(2);
glBegin(GL_QUADS);
FLAG[1] == true ? glColor3f(0.0, 1.0, 0.0):glColor3f(0.0, 0.0, 0.0);
glVertex3d(0,0.8,-0.5);
glVertex3d(0,-0.8,-0.5);
glVertex3d(0.6,-0.8,-0.5);
glVertex3d(0.6,0.8,-0.5);
glEnd();
glPopName();

glPushName(3);
glBegin(GL_QUADS);
FLAG[2] == true ? glColor3f(0.0, 0.0, 1.0):glColor3f(0.0, 0.0, 0.0);
glVertex3d(-1,0.4,-1);
glVertex3d(-1,-0.4,-1);
glVertex3d(1,-0.4,-1);
glVertex3d(1,0.4,-1);
glEnd();
glPopName();

}


void PICK_UP(int x, int y)
{

/*ビューポート取得*/
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

/*セレクションバッファ用意*/
#define BUFSIZE 256
GLuint selectBuf[BUFSIZE];
glSelectBuffer(BUFSIZE, selectBuf);

/*レンダーモード変更*/
glRenderMode(GL_SELECT);

glInitNames();


glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix(x,viewport[3]-y,5.0,5.0, viewport);

/*視体積*/
gluPerspective( 30.0,(double)viewport[2]/(double)viewport[3] , 1.0, 100.0 );

glMatrixMode( GL_MODELVIEW );
DRAW_QUADS();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

glMatrixMode( GL_MODELVIEW );


int hits;
hits = glRenderMode(GL_RENDER);

SELECT_HITS(hits, selectBuf);

}

int SELECT_HITS(int hits,GLuint *buf)
{

/*ヒット数チェック*/
if(hits<=0)
{
cout << "0 hit ..."<< endl;
for(int i = 0;i<3;i++) FLAG[i] = false;
return -1;

}else
{
cout << hits << " hits !!" << endl;
}



int name = buf[3];
int depth = buf[1];

for(int loop = 1; loop < hits;loop++)
{
if(buf[loop*4+1] < GLuint(depth)) //より手前のものが見つかったとき
{
name = buf[loop*4+3];
depth = buf[loop*4+1];
}

}

FLAG[name-1] = true;
cout << "min name is " << name << endl;

return 1;
}

メモ


マウス右ボタンドラッグで回転できるようにしても正しく動作できる。

視点変更もできる場合


#include <cstdio>
#include <iostream>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>

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

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

void DRAW_QUADS();
void PICK_UP(int x,int y);
int SELECT_HITS(int hits,GLuint *buf);

using namespace std;


/************ 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(mouse);
glutMotionFunc(myMouseMotion);
}

inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.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()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(1.0, 2.0, 4.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);

glPushMatrix();
DRAW_QUADS();
glPopMatrix();

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 );
gluLookAt(1.0, 2.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}


int xStart,yStart;
bool mouseFlag = GL_FALSE;
void mouse(int button, int state,int x, int y)
{
if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
PICK_UP(x,y);
glutPostRedisplay();
}

if(button == GLUT_RIGHT_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();
}


/**********[ここから各種関数]***********************/
bool FLAG[3] ={false,false,false};
void DRAW_QUADS()
{

glPushName(1);
glBegin(GL_QUADS);
FLAG[0] == true ? glColor3f(1.0, 0.0, 1.0):glColor3f(0.0, 0.0, 0.0);
glVertex2d(-0.5,0.5);
glVertex2d(-0.5,-0.5);
glVertex2d(0.5,-0.5);
glVertex2d(0.5,0.5);
glEnd();
glPopName();

glPushName(2);
glBegin(GL_QUADS);
FLAG[1] == true ? glColor3f(0.0, 1.0, 0.0):glColor3f(0.0, 0.0, 0.0);
glVertex3d(0,0.8,-0.5);
glVertex3d(0,-0.8,-0.5);
glVertex3d(0.6,-0.8,-0.5);
glVertex3d(0.6,0.8,-0.5);
glEnd();
glPopName();

glPushName(3);
glBegin(GL_QUADS);
FLAG[2] == true ? glColor3f(0.0, 0.0, 1.0):glColor3f(0.0, 0.0, 0.0);
glVertex3d(-1,0.4,-1);
glVertex3d(-1,-0.4,-1);
glVertex3d(1,-0.4,-1);
glVertex3d(1,0.4,-1);
glEnd();
glPopName();

}


void PICK_UP(int x, int y)
{

/*ビューポート取得*/
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

/*セレクションバッファ用意*/
#define BUFSIZE 256
GLuint selectBuf[BUFSIZE];
glSelectBuffer(BUFSIZE, selectBuf);

/*レンダーモード変更*/
glRenderMode(GL_SELECT);

glInitNames();

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix(x,viewport[3]-y,5.0,5.0, viewport);

/*視体積*/
gluPerspective( 30.0,(double)viewport[2]/(double)viewport[3] , 1.0, 100.0 );

glMatrixMode( GL_MODELVIEW );
DRAW_QUADS();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

glMatrixMode( GL_MODELVIEW );

int hits;
hits = glRenderMode(GL_RENDER);

SELECT_HITS(hits, selectBuf);

}

int SELECT_HITS(int hits,GLuint *buf)
{

/*ヒット数チェック*/
if(hits<=0)
{
cout << "0 hit ..."<< endl;
for(int i = 0;i<3;i++) FLAG[i] = false;
return -1;

}else
{
cout << hits << " hits !!" << endl;
}

int name = buf[3];
int depth = buf[1];

for(int loop = 1; loop < hits;loop++)
{
if(buf[loop*4+1] < GLuint(depth)) //より手前のものが見つかったとき
{
name = buf[loop*4+3];
depth = buf[loop*4+1];
}

}

FLAG[name-1] = true;
cout << "min name is " << name << endl;

return 1;
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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