Lab3-GeometryPositionColorMouse (using mouse & motion & geometry animation)
geometryPositionColor class restructured (adding center position & animation routine)
lab3-GeometryPositionColorMouse
std::vector<GeometryPositionColor*> geoList;
void mouse(int button, int state, int mx, int my)
{
if (button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
exit(0);
if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
g_mousemove = true;
else if (button==GLUT_LEFT_BUTTON && state==GLUT_UP)
g_mousemove = false;
}
void motion(int mx, int my)
{
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);
// 0~600(x+ right) x 0~600(y+ down) => -5~5(x+ right) x -5~5(y+ up)
float x = (float) 10 * (mx – w*0.5) / w;
float y = (float) 10 * (h*0.5 – my) / h;
if (g_mousemove)
{
//printf(“mx=%d my=%d x=%f y=%f\n”, mx, my, x, y);
geoList[g_geometrymode]->setPosition(glm::vec3(x, y, 0));
}
glutPostRedisplay();
}
void keyboard( unsigned char key, int x, int y )
{
switch ( key ) {
case 27:
exit( EXIT_SUCCESS );
break;
case ‘c’:
geoList[g_geometrymode]->setColor(glm::vec3(1, 0, 0));
break;
case ‘w’:
g_wiremode = !g_wiremode;
break;
case ‘a’:
g_animationmode = !g_animationmode;
break;
case ‘g’:
g_geometrymode = (g_geometrymode + 1) % 8;
printf(“g_geometrymode=%d\n”, g_geometrymode);
break;
case ‘m’:
startTime = glutGet(GLUT_ELAPSED_TIME);
geoList[g_geometrymode]->activate();
printf(“object animation active?=%d\n”, (int)geoList[g_geometrymode]->isActive());
break;
}
glutPostRedisplay();
}
void update()
{
…
// each geometry update (triggered by ‘m’-key)
geoList[g_geometrymode]->update(elapsedTime);
glutPostRedisplay();
}
void display( void )
{
…
geoList[g_geometrymode]->draw();
…
}
void setData()
{
geoList.push_back(new Quad());
geoList.push_back(new Triangle());
geoList.push_back(new Circle());
…
}
void init( void )
{
…
setData();
geoList[0]->setAnimation(glm::vec3(-1, 0, 0), glm::vec3(1, 0, 0), 5000); // start, end, duration
geoList[1]->setAnimation(glm::vec3(-2, -2, 0), glm::vec3(2, 2, 0), 5000);
geoList[2]->setAnimation(glm::vec3(2, -2, 0), glm::vec3(-2, 2, 0), 5000);
…
}