虚拟跟踪球

#include <math.h>
#include <stdlib.h>
#include <GL/glut.h>
#define bool int
#define false 0
#define true 1
#define M_PI 3.14
int  winWidth, winHeight;
float  angle = 0.0, axis[3], trans[3] = { 0.0 };

// 跟踪鼠标划动
bool trackingMouse = false;

// 旋转
bool trackballMove = false;


// 平移
bool trackballTranslate = false;

// 鼠标坐标重新定位 (第二次点击鼠标时)
bool relocateBall = true;

//绘制多面体或者线框图
bool draw = false; 


// 记录累计旋转的角度
float angleSum = 0.0;

// 记录累计旋转后的坐标轴向量
static GLfloat theta[] = { 0.0, 0.0, 0.0 };

// 视点位置
static GLdouble viewer[] = { 0.0, 0.0, 5.0 };

// 摄像头焦点的方向
static GLdouble center[] = { 0.0, 0.0, 0.0 };

// 记录鼠标前一次位置向量
float lastPos[3] = { 0.0F, 0.0F, 0.0F };

// 记录当前鼠标坐标
int curx, cury;

// 正十二面体点的坐标
GLfloat vertices[][3] = {
{ 1.214124, 0.000000, 1.589309 },
{ 0.375185, 1.154701, 1.589309 },
{ -0.982247, 0.713644, 1.589309 },
{ -0.982247, -0.713644, 1.589309 },
{ 0.375185, -1.154701, 1.589309 },
{ 1.964494, 0.000000, 0.375185 },
{ 0.607062, 1.868345, 0.375185 },
{ -1.589309, 1.154701, 0.375185 },
{ -1.589309, -1.154701, 0.375185 },
{ 0.607062, -1.868345, 0.375185 },
{ 1.589309, 1.154701, -0.375185 },
{ -0.607062, 1.868345, -0.375185 },
{ -1.964494, 0.000000, -0.375185 },
{ -0.607062, -1.868345, -0.375185 },
{ 1.589309, -1.154701, -0.375185 },
{ 0.982247, 0.713644, -1.589309 },
{ -0.375185, 1.154701, -1.589309 },
{ -1.214124, 0.000000, -1.589309 },
{ -0.375185, -1.154701, -1.589309 },
{ 0.982247, -0.713644, -1.589309 } };

// 正十二面体的颜色
GLfloat colors[][3] = {
{0.8,0.2,0.2}, {1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0},
    {0.0,0.0,0.5}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0},
{0.0,0.0,1.0}, {1.0,0.5,1.0}, {0.5,0.5,0.5}, {0.0,0.5,0.5}
};

// 画出多边形面
void polygon(int a, int b, int c, int d, int e, int f)
{
/* draw a polygon via list of vertices */
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[b]);
glVertex3fv(vertices[c]);
glVertex3fv(vertices[d]);
glVertex3fv(vertices[e]);
glVertex3fv(vertices[f]);
glEnd();
}

// 画出正十二面体
void colorcube(void)
{
if (draw)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);  //线框
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);  //多面体
/* map vertices to faces */
polygon(0, 0, 1, 2, 3, 4);
polygon(1, 0, 5, 10, 6, 1);
polygon(2, 1, 6, 11, 7, 2);
polygon(3, 2, 7, 12, 8, 3);
polygon(4, 3, 8, 13, 9, 4);
polygon(5, 4, 9, 14, 5, 0);
polygon(6, 15, 10, 5, 14, 19);
polygon(7, 16, 11, 6, 10, 15);
polygon(8, 17, 12, 7, 11, 16);
polygon(9, 18, 13, 8, 12, 17);
polygon(10, 19, 14, 9, 13, 18);
polygon(11, 19, 18, 17, 16, 15);
}

void trackball_ptov(int x, int y, int width, int height, float v[3])
{
float d, a;
/* project x,y onto a hemisphere centered within width, height */
v[0] = (2.0F*x - width) / width;
v[1] = (height - 2.0F*y) / height;
d = (float)sqrt(v[0] * v[0] + v[1] * v[1]);
v[2] = (float)cos((M_PI / 2.0F) * ((d < 1.0F) ? d : 1.0F));
a = 1.0F / (float)sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
v[0] *= a;
v[1] *= a;
v[2] *= a;
}

// 鼠标划动时回调函数
void mouseMotion(int x, int y)
{
float curPos[3], dx, dy, dz;
trackball_ptov(x, y, winWidth, winHeight, curPos);


// 如果是新的一次鼠标点击,需要重新定位初始坐标
if (relocateBall) {
lastPos[0] = curPos[0];
lastPos[1] = curPos[1];
lastPos[2] = curPos[2];
curx = x; cury = y;


relocateBall = false;
}


// 跟踪鼠标
if (trackingMouse)
{
// 如果是触动平移变换
if (trackballTranslate) {
float xTheta = 12.0 * (x - curx) / winWidth;
float yTheta = 12.0 * (cury - y) / winHeight;


// 累计平移量
trans[0] += xTheta;
trans[1] += yTheta;


curx = x;
cury = y;
} else if (trackballMove) {
// 如果是触发旋转变换
dx = curPos[0] - lastPos[0];
dy = curPos[1] - lastPos[1];
dz = curPos[2] - lastPos[2];


if (dx || dy || dz) {
angle = 90.0F * sqrt(dx*dx + dy*dy + dz*dz);
axis[0] = lastPos[1] * curPos[2] - lastPos[2] * curPos[1];
axis[1] = lastPos[2] * curPos[0] - lastPos[0] * curPos[2];
axis[2] = lastPos[0] * curPos[1] - lastPos[1] * curPos[0];


lastPos[0] = curPos[0];
lastPos[1] = curPos[1];
lastPos[2] = curPos[2];


angleSum += angle;
theta[0] += axis[0];
theta[1] += axis[1];
theta[2] += axis[2];
}
}
}
glutPostRedisplay();
}


// 开始跟踪旋转
void startMotion(int x, int y)
{
trackingMouse = true;
trackball_ptov(x, y, winWidth, winHeight, lastPos);
trackballMove = true;
relocateBall = true;
}


// 停止跟踪旋转
void stopMotion(int x, int y)
{
trackingMouse = false;
}


// 开始跟踪平移
void startTranslation(int x, int y)
{
trackingMouse = true;
trackball_ptov(x, y, winWidth, winHeight, lastPos);
trackballTranslate = true;
relocateBall = true;
}


// 停止跟踪平移
void stopTranslation(int x, int y)
{
trackingMouse = false;
trackballTranslate = false;
}


void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// 重新加载原矩阵
glLoadIdentity();


// 先平移
glTranslatef(trans[0], trans[1], trans[2]);


// 再旋转
if (trackballMove) {
glRotatef(angleSum, theta[0], theta[1], theta[2]);
}


colorcube();
glFlush();
glutSwapBuffers();
}
void mouseButton(int btn, int state, int x, int y)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


// 如果是右键,则跟踪平移
if (btn == GLUT_RIGHT_BUTTON) switch (state)
{
case GLUT_DOWN:
y = winHeight - y;
startTranslation(x, y);
break;
case GLUT_UP:
stopTranslation(x, y);
break;
};


// 如果是左键,则跟踪旋转
if (btn == GLUT_LEFT_BUTTON) switch (state)
{
case GLUT_DOWN:
y = winHeight - y;
startMotion(x, y);
break;
case GLUT_UP:
stopMotion(x, y);
break;
}


colorcube();
glutSwapBuffers();
}


void keys(unsigned char key, int x, int y)
{
// 键盘控制平移
switch (key) {
case 'w':
case 'W':
trans[1] += .5;
break;
case 's':
case 'S':
trans[1] -= .5;
break;
case 'a':
case 'A':
trans[0] -= .5;
break;
case 'd':
case 'D':
trans[0] += .5;
break;
}
display();
}


void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
/* Use a perspective view */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (w <= h)
glFrustum(-2.0, 2.0, -2.0 * (GLfloat)h / (GLfloat)w,
2.0* (GLfloat)h / (GLfloat)w, 2.0, 20.0);
else glFrustum(-2.0, 2.0, -2.0 * (GLfloat)w / (GLfloat)h,
2.0* (GLfloat)w / (GLfloat)h, 2.0, 20.0);
/* Or we can use gluPerspective */
/* gluPerspective(45.0, w/h, -10.0, 10.0); */
winWidth = w;
winHeight = h;
glMatrixMode(GL_MODELVIEW);
}


void processMenuEvents(bool option) {
switch (option)
{
case true :
draw = true;
break;
case false :
draw = false;
break;
}
}


void createGLUTMenus() {
bool menu;
menu = glutCreateMenu(processMenuEvents);
glutAddMenuEntry("Polyhedral", false);
glutAddMenuEntry("Wireframe", true);
glutAttachMenu(GLUT_MIDDLE_BUTTON);
}


void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("colorcube");


glutMouseFunc(mouseButton);
glutMotionFunc(mouseMotion);


glutReshapeFunc(myReshape);
createGLUTMenus();
glutDisplayFunc(display);
glutKeyboardFunc(keys);


glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-6.0, 6.0, -6.0, 6.0, -6.0, 6.0);
    glMatrixMode(GL_MODELVIEW);
glutMainLoop();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值