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

``````idefine true 1
int winWidth, winHeight;
float angle = 0.0, axis[3], trans[3];
bool trackingMouse = false;
bool redrawContinue = false;
bool trackballMove = false;
/*--------------------------------------*/``````

/* Вычерчивание куба */ GLfloat vertices[][3] = {

``````{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0},
{-1.0,-1.0,1.0}, {1.0,-1.0,1.0},
{1.0,1.0,1.0}, {-1.0,1.0,1.0}
};
GLfloat colors[][3] = {
{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}
};
/*-------------------------------*/``````

void polygon(int a, int b, int с , int d, int face) {

/* Вычерчивание многоугольника с помощью списка вершин */

``````glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glVertex3fv(vertices[d]); glEnd();
}
/*------------........._.........*/
void colorcube() {``````

/* Связь вершин с гранями */

``polygon(1,0,3,2,0); polygon!3,7,6,2,1); polygon!7,3,0,4,2)J polygon(2,6,5,1,3); polygon(4,5,6,7,4);``

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

``````polygon(5,4,0,1,5);
)
/*-----............-------------------------*/
/*``````

** Управление движением с помощью трекбола. */

``````float lastPos[3] = {0.0F, 0.0F, 0.0F}; int curx, cury; int startX, startY;
/*---------------......._........*/
void trackball_ptov(int x, int y, int width, int height, float v[3])
{
float d, a;``````

/* Проекция x,y на полусферу */

``````v[0] = (2.0F*x - width) / width;
v[l] = (height - 2.0F*y) / height;
d = (float) sqrt(v[0]*v[0] + v[l]*v[l]);
v[2] = (float) cos((M_PI/2.0F) * ((d < 1.0F) ? d : 1.0F));
a = 1.0F / (float) sqrt(v[0]*v[0] + v[l]*v[l] + v[2]*v[2]);
v[0] *= a;
v[l] *= a;
v[2] *= a;
}
/* -.........---------------------*/
void mouseMotion(int x, int y) {
float curPos[3], dx, dy, dz;
trackball_ptov(x, y, winWidth, winHeight, curPos);
if(trackingMouse)
{
dx = curPos[0] - lastPos[0]; dy = curPos[l] - lastPos[l]; dz = curPos[2] - lastPos[2];``````

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