0

我是opengl和glm的新手,目前我正在做一个类项目,我上传了两个对象汽车如下

void CARMODEL:: drawmodel_box()
{
    glPushMatrix();
    glTranslatef(carx,cary ,carz);
    if (!pmodel1){
        pmodel1 = glmReadOBJ("Car.obj");
    }

    glmDraw(pmodel1, GLM_SMOOTH | GLM_TEXTURE | GLM_MATERIAL);
    glPopMatrix();
}

void OpponentCarModel::drawopponentmodel()
{
    glPushMatrix();
    srand(time(NULL));
    opcarx=rand() % 7-3+(double)rand()/(RAND_MAX+1)*(1-0)+0;

    glTranslatef(opcarx,0,-20);

    if (!pmodel2){
        pmodel2 = glmReadOBJ("car.obj");
    }

    glmDraw(pmodel2, GLM_SMOOTH | GLM_TEXTURE | GLM_MATERIAL);
    glPopMatrix();
}

现在,到目前为止一切都很好,现在我来到碰撞检测部分,我不知道如何在两辆车之间进行,因为我不知道它们的坐标或顶点,所以请帮忙..

4

1 回答 1

1

是的,但是您可以确切地知道模型在矩阵中的放置位置,因为您之前调用glTranslatef()glmDraw()将它们放置在那里。现在,由于您知道模型的x、y、z坐标,您可以开始检查简单的碰撞。

但是,如果您正在寻找更真实/更复杂的碰撞检测,您应该打开glm.h并检查GLMmodel结构的定义,因为它存储了在屏幕上绘制模型所需的所有内容,包括顶点信息、法线、纹理坐标等:

/* GLMmodel: Structure that defines a model.
 */
typedef struct _GLMmodel {
  char*    pathname;            /* path to this model */
  char*    mtllibname;          /* name of the material library */

  GLuint   numvertices;         /* number of vertices in model */
  GLfloat* vertices;            /* array of vertices  */

  GLuint   numnormals;          /* number of normals in model */
  GLfloat* normals;             /* array of normals */

  GLuint   numtexcoords;        /* number of texcoords in model */
  GLfloat* texcoords;           /* array of texture coordinates */

  GLuint   numfacetnorms;       /* number of facetnorms in model */
  GLfloat* facetnorms;          /* array of facetnorms */

  GLuint       numtriangles;    /* number of triangles in model */
  GLMtriangle* triangles;       /* array of triangles */

  GLuint       nummaterials;    /* number of materials in model */
  GLMmaterial* materials;       /* array of materials */

  GLuint       numgroups;       /* number of groups in model */
  GLMgroup*    groups;          /* linked list of groups */

  GLfloat position[3];          /* position of the model */

} GLMmodel;
于 2013-02-17T14:48:34.550 回答