if (dx || dy || dz) {
angle = 90.OF * sqrt(dx*dx + dy*dy + dz*dz); axis[0] = lastPos[l]*curPos[2] - lastPos[2]*curPos[l); axis[l] = lastPos[2]*curPos[0] - lastPos[0]*curPos[2]; axis[2] = lastPos[0]*curPos[l] - lastPos[l]*curPos[0];
lastPos[0] = curPos[0]; lastPos[l] = curPos[l]; lastPos[2] = curPos[2);

A. 10. Вращающийся куб, управляемый трекболом

}
}
glutPostRedisplay();
}
/*-----------.........-----------*/
void startMotion(int x, int y) {
trackingMouse = true; redrawContinue = false; startX = x; startY = y; curx = x; cury = y;
trackball_ptov(x, y, winWidth, winHeight, lastPos); trackballMove=true;
}
/*---------------------..........*/
void stopMotion(int x, int y) {
trackingMouse = false;
if (startX != x 11 startY != y) { redrawContinue = true;
}
else {
angle = 0.0F; redrawContinue = false; trackballMove = false;
}
}
/*-------......................-- */
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

/* Преобразование вида */ if (trackballMove) {

glRotatef(angle, axis[0], axis[l], axis[2]);
}
colorcube(); glutSwapBuffers();
}
/*-----------....................*/
void mouseButton(int button, int state, int x, int y) {
if (button==GLUT_RIGHT_BUTTON) exit(); if(button==GLUT LEFT BUTTON)

Приложение А. Демонстрационные программы

*/
switch(state) {
case GLUT_DOWN: y=winHeight-y;
startMotion( x,y);
break; case GLUT_UP:
stopMotion( x,y);
break;
)
void myReshape(int w, int h)
glviewport(0, 0, w, h); winWidth = w; winHeight = h;
...............................*/
void spinCube()
if (redrawContinue) glutPostRedisplay();
...........--------------------*/
void main(int argc, char **argv)
glutlnit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("colorcube");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutldleFunc(spinCube);
glutMouseFunc(mouseButton);
glutMotionFunc(mouseMotion);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PR0JECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0); glMatrixMode(GL_MODELVIEW); glutMainLoop();

⇐ Предыдущая| |Следующая ⇒