如何在2个球体之间进行碰撞以及如何使用函数gluLookAt从顶部查看我的场景。 还有一件事,我已经在键盘弹GLUT中使用来检测我的按键,但是我如何使用AUX来检测它,因为在GLUT中我没有像A,D,C等那样的键。
这是我的代码:
#include<glut.h>
#include"glut\glut.h"
#include<GL.H>
#include<GLAux.h>
#include<stdio.h>
float x1=-1, x2=1, y1, y2, z1, z2;
void myinit()
{
GLfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat mat_diffuse[] = { 0.6, 0.6, 0.6, 1.0 };
GLfloat mat_specular[] = { 0.9, 0.9, 0.9, 1.0 };
GLfloat mat_shininess[] = { 100.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
GLfloat light0_ambient[] = { 0.5, 0.5, 0.5, 1.0 };
GLfloat light0_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light0_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light0_position[] = { 5.0, 5.0, 5.0, 0.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, light0_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
}
void sphere()
{
glPushMatrix();
glutSolidSphere(0.3, 100, 100);
glPopMatrix();
}
//static GLfloat theta[] = { 0.0, 0.0, 0.0 };
//static GLint axis = 2.0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/*glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);*/
glTranslated(x1, y1, z1);
sphere();
glLoadIdentity();
glTranslated(x2, y2, z2);
sphere();
glFlush();
glutSwapBuffers();
}
//void spinsphere()
//{
// theta[axis] += 10.0;
// if (theta[axis]>360.0)
// theta[axis] -= 360.0;
// glutPostRedisplay();
//}
void myreshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h){
//glOrtho(-2.0, 2.0, -2.0*(GLfloat)h / (GLfloat)w, 2.0*(GLfloat)h / (GLfloat)w, -10.0, 10.0);
gluPerspective(0, 10, 1, 10000);
//gluLookAt(0, 10, 1, 0, 0, 0, 0, 0, 0);
}
else{
//glOrtho(-2.0*(GLfloat)h / (GLfloat)w, 2.0*(GLfloat)h / (GLfloat)w, -2.0, 2.0, -10.0, 10.0);
gluPerspective(0, 10, 1, 10000);
//gluLookAt(0, 10, 1, 0, 0, 0, 0, 0, 0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboardown(int key, int x, int y)
{
switch (key){
case GLUT_KEY_F1:
x1 += 0.1;
break;
case GLUT_KEY_F2:
x1 -= 0.1;
break;
case GLUT_KEY_F3:
y1 += 0.1;
break;
case GLUT_KEY_F4:
y1 -= 0.1;
break;
case GLUT_KEY_F5:
z1 += 0.1;
break;
case GLUT_KEY_F6:
z1 -= 0.1;
break;
case GLUT_KEY_RIGHT:
x2 += 0.1;
break;
case GLUT_KEY_LEFT:
x2 -= 0.1;
break;
case GLUT_KEY_PAGE_UP:
y2 += 0.1;
break;
case GLUT_KEY_PAGE_DOWN:
y2 -= 0.1;
break;
case GLUT_KEY_UP:
z2 += 0.1;
break;
case GLUT_KEY_DOWN:
z2 -= 0.1;
break;
default:
break;
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
//glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1024, 1024);
glutInitWindowPosition(0, 0);
glutCreateWindow("TEST");
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
//glutIdleFunc(spinsphere);
myinit();
glutSpecialFunc(keyboardown);
glutMainLoop();
return 0;
}
寻求帮助:)
答案 0 :(得分:0)
球体 - 球体碰撞非常容易。只需取两个球体中心之间的距离,然后添加球体的两个半径,并将它们与距离进行比较。如果d&lt; (r1 + r2)然后他们相撞了。
要使用gluLookAt获得自上而下的外观,如果我没有弄错的话,你可以这样做
glutKeyboardFunc(void (*func)(unsigned char key, int x, int y));
基本上将眼睛设置为5个单位的高度,俯视vec3(0,0,0)处的场景原点。
我并不完全清楚你对关键输入的意思。请澄清。
但无论如何,请使用
$ basename /home/jsmith/base.wiki .wiki
base