現在地 >> メニュー >> サンプルコード::OpenGL >> ドラッグ処理 >>ドラッグ処理2

問題


ウィンドウを作成し、マウス左ボタンでドラッグすることで絵がかけるようなプログラムを作成せよ。

その他条件:
はやく動かした時などの補間はしなくてよい。


OpenCV版→OpenCV::ドラッグ処理2

答え


#include <GL/glut.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);


/**********[OpenGLの初期設定]**********************/
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA);
}


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);
glutSetCursor(GLUT_CURSOR_CROSSHAIR);
}

int main(int argc, char *argv[])
{

glutInit(&argc,argv);
GLUT_INIT();

glutCreateWindow("window name");

GLUT_CALL_FUNC();
MY_INIT();

glutMainLoop();

return 0;
}


/********[ここから、各種コールバック]****************************************/
void display(void)
{
glFlush();
}

void reshape(int w, int h)
{
glClear(GL_COLOR_BUFFER_BIT);

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, h, 0 ,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


bool mouseFlag = GL_FALSE;
void myMouseFunc(int button, int state,int x, int y)
{
if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
mouseFlag = GL_TRUE;
glColor3d(1.0, 0.0, 0.0);
glPointSize(1);
glBegin(GL_POINTS);
glVertex2d(x,y);
glEnd();
glutPostRedisplay();
}
else
{
mouseFlag = GL_FALSE;
}


}



void myMouseMotion(int x,int y)
{
if(mouseFlag == GL_FALSE)return;

glColor3d(1.0, 0.0, 0.0);
glPointSize(1);
glBegin(GL_POINTS);
glVertex2d(x,y);
glEnd();
glutPostRedisplay();
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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