两个Sphere和gluLookAt函数之间的OpenGL冲突

时间:2021-08-16 06:01:17

How I can make collision between 2 sphere and how I use function gluLookAt to look at my scene from top. And one more thing, I've use in keyboardown GLUT to detect my press key, but how I can use AUX to detect that, cause in GLUT I don't have key like A,D,C,etc..

如何在2个球体之间进行碰撞以及如何使用函数gluLookAt从顶部查看我的场景。还有一件事,我用键盘GLUT来检测我的按键,但是如何使用AUX来检测它,因为在GLUT中我没有像A,D,C等键。

Here is my code:

这是我的代码:

#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;
}

Thx for help :)

求助于帮助:)

1 个解决方案

#1


sphere-sphere collision is quite easy. Just take the distance between the two sphere centers and then add the two radii of the spheres and compare them with the distance. if d < (r1+r2) then they collided.

球 - 球碰撞很容易。只需取两个球体中心之间的距离,然后添加球体的两个半径,并将它们与距离进行比较。如果d <(r1 + r2)则它们相撞。

To get a top-down look with gluLookAt you can do this if I'm not mistaken

要使用gluLookAt获得自上而下的外观,如果我没有弄错的话,你可以这样做

gluLookAt(0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);

basically sets the eye at height of 5 units, looking down to the scene origin at vec3(0, 0, 0).

基本上将眼睛设置为5个单位的高度,向下看vec3(0,0,0)处的场景原点。

And I don't really know what you mean with the key inputs exactly. Please clarify.

我并不完全知道你对关键输入的意思。请澄清。

But anyway, use

但无论如何,使用

glutKeyboardFunc(void (*func)(unsigned char key, int x, int y));

#1


sphere-sphere collision is quite easy. Just take the distance between the two sphere centers and then add the two radii of the spheres and compare them with the distance. if d < (r1+r2) then they collided.

球 - 球碰撞很容易。只需取两个球体中心之间的距离,然后添加球体的两个半径,并将它们与距离进行比较。如果d <(r1 + r2)则它们相撞。

To get a top-down look with gluLookAt you can do this if I'm not mistaken

要使用gluLookAt获得自上而下的外观,如果我没有弄错的话,你可以这样做

gluLookAt(0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);

basically sets the eye at height of 5 units, looking down to the scene origin at vec3(0, 0, 0).

基本上将眼睛设置为5个单位的高度,向下看vec3(0,0,0)处的场景原点。

And I don't really know what you mean with the key inputs exactly. Please clarify.

我并不完全知道你对关键输入的意思。请澄清。

But anyway, use

但无论如何,使用

glutKeyboardFunc(void (*func)(unsigned char key, int x, int y));