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


問題


ポリゴンを3枚描画し、クリック位置からRayを出し、
何枚のポリゴンにぶつかっているか描画せよ。

その他条件:
 セレクションバッファのサイズ 256
 ピックアップ範囲は5×5

マウスとピックアップ処理2

答え


#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 timer(int value);
void mouse(int button, int state,int x, int y);

void DRAW_QUADS();
void PICK_UP(int x,int y);
int SELECT_HITS(GLuint 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);
glutTimerFunc(1,timer,0);
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 timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1,timer,0); //タイマー関数
}


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

}


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

void DRAW_QUADS()
{

glPushName(1);
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 1.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);
glColor3f(0.0, 1.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);
glColor3f(0.0, 0.0, 1.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(GLuint hits,GLuint *buf)
{

/*ヒット数チェック*/
if(hits<=0)
{
cout << "0 hit ..."<< endl;
return -1;

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


return 1;
}

メモ


int SELECT_HITS(GLuint hits,GLuint *buf)
のbufは今回使っていない。

bufに様々な情報が書き込まれる。


PICK_UP関数内は、定石的な書き方?

視体積は一致させる。

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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