C++ 创建三维立方体和反弹球

C++ 创建三维立方体和反弹球,c++,opengl,C++,Opengl,我正在用OpenGL编写一个程序,我正在绘制一个简单的3D立方体,我想添加弹跳球,使它们在立方体的墙壁内弹跳,我尝试添加一个ball类和以下代码: GLfloat WHITE[] = {1, 1, 1}; GLfloat RED[] = {1, 0, 0}; GLfloat GREEN[] = {0, 1, 0}; GLfloat MAGENTA[] = {0, 0, 1}; class Ball { double radius; GLfloat* color; double

我正在用OpenGL编写一个程序,我正在绘制一个简单的3D立方体,我想添加弹跳球,使它们在立方体的墙壁内弹跳,我尝试添加一个ball类和以下代码:

 GLfloat WHITE[] = {1, 1, 1};
GLfloat RED[] = {1, 0, 0};
GLfloat GREEN[] = {0, 1, 0};
GLfloat MAGENTA[] = {0, 0, 1};


class Ball {
  double radius;
  GLfloat* color;
  double maximumHeight;
  double x;
  double y;
  double z;
  int direction;
public:
  Ball(double r, GLfloat* c, double h, double x, double z):
      radius(r), color(c), maximumHeight(h), direction(-1),
      y(h), x(x), z(z) {
  }
  void update() {
    y += direction * 0.05;
    if (y > maximumHeight) {
      y = maximumHeight; direction = -1;
    } else if (y < radius) {
      y = radius; direction = 1;
    }
    glPushMatrix();
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
    glTranslated(x, y, z);
    glutSolidSphere(radius, 30, 30);
    glPopMatrix();
  }
};

Ball balls[] = {
  Ball(0.5, GREEN, 7, 6, 1),
  Ball(0.5, MAGENTA, 6, 3, 4),
  Ball(0.5, WHITE, 5, 1, 7)
};
:

GL_AMBIENT
:参数包含四个整数值或浮点值

GL_DIFFUSE
:参数包含四个整数值或浮点值

GL\u AMBIENT\u和\u DIFFUSE
:相当于使用相同的参数值调用
glMaterial
两次,一次调用
GL\u AMBIENT
,一次调用
GL\u DIFFUSE

编辑

#include <GL/glut.h>

// http://eigen.tuxfamily.org
#include <Eigen/Core>
using namespace Eigen;

struct PointMass
{
    PointMass() : pos(0,0,0), vel(0,0,0) {}
    PointMass( const Vector3f& pos )
        : pos( pos )
        , vel( 0, 0, 0 )
    {}

    void Integrate( float dt )
    {
        // "gravity" force vector
        Vector3f g( 0, 0, -2 );

        // semi-implicit euler
        vel = vel + g * dt;
        pos = pos + vel * dt;

        // collision detection/response
        if( pos.z() < 0 )
        {
            pos.z() = -pos.z();
            vel.z() = -vel.z();
        }
    }

    Vector3f pos;
    Vector3f vel;
};

GLfloat WHITE[] = {1, 1, 1, 1};
GLfloat RED[] = {1, 0, 0, 1};
GLfloat GREEN[] = {0, 1, 0, 1};
GLfloat MAGENTA[] = {0, 0, 1, 1};

class Ball 
{
    double radius;
    GLfloat* color;
    PointMass pm;
public:
    Ball(double r, GLfloat* c, const Vector3f& pos )
        : radius(r)
        , color(c)
        , pm( pos )
    { }

    void Integrate( float dt )
    {
        pm.Integrate( dt );
    }

    void Draw()
    {
        glPushMatrix();
        glColor4fv( color );
        glTranslatef( pm.pos.x(), pm.pos.y(), pm.pos.z() );
        glutSolidSphere(radius, 30, 30);
        glPopMatrix();
    }
};

Ball balls[] = 
{
    Ball( 0.1,   GREEN,      Vector3f( 1, 1, 2 )    ),
    Ball( 0.1,   MAGENTA,    Vector3f( -1, 1, 1 )   ),
    Ball( 0.1,   WHITE,      Vector3f( 0, -1, 1.5 ) ),
};

double rotate_x = 55; 
double rotate_z = 25;   
void display()
{
    static int last = glutGet(GLUT_ELAPSED_TIME);
    int cur = glutGet(GLUT_ELAPSED_TIME);
    float dt = ( cur - last ) / 1000.0f;
    last = cur;

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    gluPerspective( 60, w / h, 0.1, 100 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glTranslatef( 0, 0, -5 );
    glRotatef( -rotate_x, 1.0, 0.0, 0.0 );
    glRotatef( -rotate_z, 0.0, 0.0, 1.0 );

    // ground
    glBegin(GL_QUADS);
    glColor3f( 1.0, 0.0, 0.0 );     
    glVertex2f( -2, -2 );
    glColor3f( 0.0, 1.0, 0.0 );     
    glVertex2f(  2, -2 );
    glColor3f( 0.0, 0.0, 1.0 );     
    glVertex2f(  2,  2 );
    glColor3f( 1.0, 0.0, 1.0 );     
    glVertex2f( -2,  2 );
    glEnd();

    for (int i = 0; i < sizeof balls / sizeof(Ball); i++) 
    {
        balls[i].Integrate( dt );
        balls[i].Draw();
    }

    glutSwapBuffers();
}

void specialKeys( int key, int x, int y )
{
    if (key == GLUT_KEY_RIGHT)
        rotate_z += 5;
    if (key == GLUT_KEY_LEFT)
        rotate_z -= 5;
    if (key == GLUT_KEY_UP)
        rotate_x += 5;
    if (key == GLUT_KEY_DOWN)
        rotate_x -= 5;
}

void timer( int extra )
{
    // run display() every 16ms or so
    glutTimerFunc( 16, timer, 0 );
    glutPostRedisplay();
}

int main(int argc, char* argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutCreateWindow("Awesome Cube");
    glEnable(GL_DEPTH_TEST);
    glutDisplayFunc(display);
    glutSpecialFunc(specialKeys);
    glutTimerFunc( 0, timer, 0 );
    glutMainLoop();
    return 0;
}
#包括
// http://eigen.tuxfamily.org
#包括
使用名称空间特征;
结构点质量
{
PointMass():pos(0,0,0),vel(0,0,0){
点质量(常数矢量3F和位置)
:pos(pos)
,水平(0,0,0)
{}
虚积分(浮点dt)
{
//“重力”力矢量
向量3g(0,0,-2);
//半隐式欧拉
vel=vel+g*dt;
pos=pos+vel*dt;
//碰撞检测/响应
如果(位置z()<0)
{
位置z()=-位置z();
vel.z()=-vel.z();
}
}
向量3f-pos;
向量3f级;
};
GLfloat-WHITE[]={1,1,1,1};
GLfloat RED[]={1,0,0,1};
GLfloat绿色[]={0,1,0,1};
GLfloat洋红[]={0,0,1,1};
班级舞会
{
双半径;
GLfloat*颜色;
点质量pm;
公众:
球(双r、GLfloat*c、常数矢量3F和位置)
:半径(r)
,颜色(c)
,下午(pos)
{ }
虚积分(浮点dt)
{
积分(dt);
}
作废提款()
{
glPushMatrix();
glColor4fv(颜色);
glTranslatef(pm.pos.x(),pm.pos.y(),pm.pos.z());
实心球体(半径30,30);
glPopMatrix();
}
};
球[]=
{
球(0.1,绿色,矢量3F(1,1,2)),
球(0.1,品红色,矢量3F(-1,1,1)),
球(0.1,白色,向量3F(0,-1,1.5)),
};
双旋转_x=55;
双旋转_z=25;
无效显示()
{
静态int last=glutGet(GLUT\u经过的时间);
int cur=glutGet(GLUT经过的时间);
浮动dt=(当前-最后)/1000.0f;
last=cur;
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
glMatrixMode(GL_投影);
glLoadIdentity();
双w=glutGet(GLUT\U窗口\U宽度);
双h=glutGet(GLUT\U窗口\U高度);
谷氨酰胺(60,w/h,0.1100);
glMatrixMode(GLU模型视图);
glLoadIdentity();
glTranslatef(0,0,-5);
glRotatef(-rotate_x,1.0,0.0,0.0);
glRotatef(-rotate_z,0.0,0.0,1.0);
//地面
glBegin(GL_QUADS);
GL3F(1.0,0.0,0.0);
glVertex2f(-2,-2);
GL3F(0.0,1.0,0.0);
glVertex2f(2,-2);
GL3F(0.0,0.0,1.0);
glVertex2f(2,2);
GL3F(1.0,0.0,1.0);
glVertex2f(-2,2);
格伦德();
对于(int i=0;i
我想你错过了你的问题:)对于使用(3d)运动学,nothings甚至不显示弹跳球+1,如果在碰撞检测/响应功能中添加速度衰减,球最终将停止弹跳,如irl:)
void display();
void specialKeys();
double rotate_y=0; 
double rotate_x=0;   
void display(){

  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

  glLoadIdentity();

  glRotatef( rotate_x, 1.0, 0.0, 0.0 );
  glRotatef( rotate_y, 0.0, 1.0, 0.0 );

  glBegin(GL_POLYGON);

  glColor3f( 1.0, 0.0, 0.0 );     glVertex3f(  0.5, -0.5, -0.5 );      // P1 is red
  glColor3f( 0.0, 1.0, 0.0 );     glVertex3f(  0.5,  0.5, -0.5 );      // P2 is green
  glColor3f( 0.0, 0.0, 1.0 );     glVertex3f( -0.5,  0.5, -0.5 );      // P3 is blue
  glColor3f( 1.0, 0.0, 1.0 );     glVertex3f( -0.5, -0.5, -0.5 );      // P4 is purple

  glEnd();
  glBegin(GL_POLYGON);
  glColor3f(   1.0,  1.0, 1.0 );
  glVertex3f(  0.5, -0.5, 0.5 );
  glVertex3f(  0.5,  0.5, 0.5 );
  glVertex3f( -0.5,  0.5, 0.5 );
  glVertex3f( -0.5, -0.5, 0.5 );
  glEnd();

  glBegin(GL_POLYGON);
  glColor3f(  1.0,  0.0,  1.0 );
  glVertex3f( 0.5, -0.5, -0.5 );
  glVertex3f( 0.5,  0.5, -0.5 );
  glVertex3f( 0.5,  0.5,  0.5 );
  glVertex3f( 0.5, -0.5,  0.5 );
  glEnd();

  glBegin(GL_POLYGON);
  glColor3f(   0.0,  1.0,  0.0 );
  glVertex3f( -0.5, -0.5,  0.5 );
  glVertex3f( -0.5,  0.5,  0.5 );
  glVertex3f( -0.5,  0.5, -0.5 );
  glVertex3f( -0.5, -0.5, -0.5 );
  glEnd();

  glBegin(GL_POLYGON);
  glColor3f(   0.0,  0.0,  1.0 );
  glVertex3f(  0.5,  0.5,  0.5 );
  glVertex3f(  0.5,  0.5, -0.5 );
  glVertex3f( -0.5,  0.5, -0.5 );
  glVertex3f( -0.5,  0.5,  0.5 );
  glEnd();*/

  glBegin(GL_POLYGON);
  glColor3f(   1.0,  0.0,  0.0 );
  glVertex3f(  0.5, -0.5, -0.5 );
  glVertex3f(  0.5, -0.5,  0.5 );
  glVertex3f( -0.5, -0.5,  0.5 );
  glVertex3f( -0.5, -0.5, -0.5 );
  glEnd();

  glFlush();
  glutSwapBuffers();

}

void specialKeys( int key, int x, int y ) {

  if (key == GLUT_KEY_RIGHT)
    rotate_y += 5;
  else if (key == GLUT_KEY_LEFT)
    rotate_y -= 5;

  else if (key == GLUT_KEY_UP)
    rotate_x += 5;

  else if (key == GLUT_KEY_DOWN)
    rotate_x -= 5;
  glutPostRedisplay();

}
int main(int argc, char* argv[]){
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutCreateWindow("Awesome Cube");
       glEnable(GL_DEPTH_TEST);
  glutDisplayFunc(display);
  glutSpecialFunc(specialKeys);
      glutMainLoop();
  return 0;

}
GLfloat WHITE[] = {1, 1, 1};
...
Ball(0.5, WHITE, 5, 1, 7)
...
color(c)
....
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
#include <GL/glut.h>

// http://eigen.tuxfamily.org
#include <Eigen/Core>
using namespace Eigen;

struct PointMass
{
    PointMass() : pos(0,0,0), vel(0,0,0) {}
    PointMass( const Vector3f& pos )
        : pos( pos )
        , vel( 0, 0, 0 )
    {}

    void Integrate( float dt )
    {
        // "gravity" force vector
        Vector3f g( 0, 0, -2 );

        // semi-implicit euler
        vel = vel + g * dt;
        pos = pos + vel * dt;

        // collision detection/response
        if( pos.z() < 0 )
        {
            pos.z() = -pos.z();
            vel.z() = -vel.z();
        }
    }

    Vector3f pos;
    Vector3f vel;
};

GLfloat WHITE[] = {1, 1, 1, 1};
GLfloat RED[] = {1, 0, 0, 1};
GLfloat GREEN[] = {0, 1, 0, 1};
GLfloat MAGENTA[] = {0, 0, 1, 1};

class Ball 
{
    double radius;
    GLfloat* color;
    PointMass pm;
public:
    Ball(double r, GLfloat* c, const Vector3f& pos )
        : radius(r)
        , color(c)
        , pm( pos )
    { }

    void Integrate( float dt )
    {
        pm.Integrate( dt );
    }

    void Draw()
    {
        glPushMatrix();
        glColor4fv( color );
        glTranslatef( pm.pos.x(), pm.pos.y(), pm.pos.z() );
        glutSolidSphere(radius, 30, 30);
        glPopMatrix();
    }
};

Ball balls[] = 
{
    Ball( 0.1,   GREEN,      Vector3f( 1, 1, 2 )    ),
    Ball( 0.1,   MAGENTA,    Vector3f( -1, 1, 1 )   ),
    Ball( 0.1,   WHITE,      Vector3f( 0, -1, 1.5 ) ),
};

double rotate_x = 55; 
double rotate_z = 25;   
void display()
{
    static int last = glutGet(GLUT_ELAPSED_TIME);
    int cur = glutGet(GLUT_ELAPSED_TIME);
    float dt = ( cur - last ) / 1000.0f;
    last = cur;

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    gluPerspective( 60, w / h, 0.1, 100 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glTranslatef( 0, 0, -5 );
    glRotatef( -rotate_x, 1.0, 0.0, 0.0 );
    glRotatef( -rotate_z, 0.0, 0.0, 1.0 );

    // ground
    glBegin(GL_QUADS);
    glColor3f( 1.0, 0.0, 0.0 );     
    glVertex2f( -2, -2 );
    glColor3f( 0.0, 1.0, 0.0 );     
    glVertex2f(  2, -2 );
    glColor3f( 0.0, 0.0, 1.0 );     
    glVertex2f(  2,  2 );
    glColor3f( 1.0, 0.0, 1.0 );     
    glVertex2f( -2,  2 );
    glEnd();

    for (int i = 0; i < sizeof balls / sizeof(Ball); i++) 
    {
        balls[i].Integrate( dt );
        balls[i].Draw();
    }

    glutSwapBuffers();
}

void specialKeys( int key, int x, int y )
{
    if (key == GLUT_KEY_RIGHT)
        rotate_z += 5;
    if (key == GLUT_KEY_LEFT)
        rotate_z -= 5;
    if (key == GLUT_KEY_UP)
        rotate_x += 5;
    if (key == GLUT_KEY_DOWN)
        rotate_x -= 5;
}

void timer( int extra )
{
    // run display() every 16ms or so
    glutTimerFunc( 16, timer, 0 );
    glutPostRedisplay();
}

int main(int argc, char* argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutCreateWindow("Awesome Cube");
    glEnable(GL_DEPTH_TEST);
    glutDisplayFunc(display);
    glutSpecialFunc(specialKeys);
    glutTimerFunc( 0, timer, 0 );
    glutMainLoop();
    return 0;
}