当我使用glutIdleFunc尝试将立方体放在不同的x轴上时,3d立方体在x轴上跳跃

时间:2013-10-12 01:31:20

标签: c++ opengl

我正在使用rand()函数将我的立方体放在屏幕上的不同位置。原因是我必须在屏幕上有一个随机数量的立方体,我不希望它们都在同一个地方开始。发生的事情就是当我运行程序时,由于glutPostRedisplay()我需要旋转立方体,因此立方体在整个屏幕上跳跃。我的问题是我怎么能拥有,例如,7个立方体从屏幕上的7个不同位置开始,而所有都在旋转?我最终需要他们从z轴上的起始位置移出屏幕,然后重新开始整个过程​​?

这是我的代码:

/* -- GLOBAL VARIABLES --------------------------------------------------------------------------    --------------- */

GLint screenWidth = 500;
GLint screenHeight = 500;
GLfloat cubeX = 0.0;
GLfloat cubeY = 5.0;
GLfloat cubeZ = -20.0;
GLfloat rotateY = 0.0;
//GLfloat rotCube = 0.0;
GLint cubeCount = 0;
//const GLint CUBE_LOW = 7;
//const GLint CUBE_HIGH = 15;
const GLint CUBE_HIGH = 15;
const GLint CUBE_LOW = 7;
const GLfloat X_HIGH = 10.0;
const GLfloat X_LOW = -10.0;
const GLfloat Y_HIGH = 10.0;
const GLfloat Y_LOW = -10.0;

/* -- NAMESPACE ---------------------------------------------------------------------------------    --------------- */

using namespace std;

/* ----------------------------------------------------------------------------------------------    -------------- */
/*
Function:   unsigned time_seed()
*/

unsigned time_seed()
{
    time_t now = time (0);
    unsigned char *p = (unsigned char *)&now;
    unsigned seed = 0;
    size_t i;

    for(i = 0; i < sizeof now; i++)
        seed  = seed * (UCHAR_MAX + 2U) + p[i];

    return seed;
}

/* ----------------------------------------------------------------------------------------------    --------------- */
/*
Function:   void rotateCube()
*/

 void rotateCube()
{
    rotateY += 0.050f;
    if(rotateY > 360.0f)
        rotateY -= 360.0f;
    glutPostRedisplay();
}

/* ----------------------------------------------------------------------------------------------    --------------- */
/*
Function:   void myDisplay()
*/

void myDisplay()
{
cubeCount = CUBE_LOW + rand() / RAND_MAX * (CUBE_HIGH - CUBE_LOW);
float cubeX = X_LOW + (float)rand() / (float)RAND_MAX * (X_HIGH - X_LOW);
float cubeY = Y_LOW + (float)rand() / (float)RAND_MAX * (Y_HIGH - Y_LOW);

for(int i = 0; i < cubeCount; i++)
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(cubeX, cubeY, cubeZ);
    glRotatef(rotateY, 0.0f, 1.0f, 0.0f);

    glutWireCube(2.0f);
    glutSwapBuffers();
    glFlush();


}
}

/* ----------------------------------------------------------------------------------------------    --------------- */
/*
Function:   int main( int argc, char **argv )
*/

void myReshape(int width, int height)
{
    glViewport(0, 0, (GLsizei)width, (GLsizei)height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100);
    glMatrixMode(GL_MODELVIEW);
}
/* ----------------------------------------------------------------------------------------------    --------------- */
/*
Function:   int main( int argc, char **argv )
*/

int main( int argc, char **argv )
{
    srand(time_seed());
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(screenWidth, screenHeight);
    glutInitWindowPosition(100, 40);
    glutCreateWindow("Boxes!");
    glutDisplayFunc(myDisplay);
    glutIdleFunc(rotateCube);
    glutReshapeFunc(myReshape);
    glutMainLoop();

   return 0;
}

2 个答案:

答案 0 :(得分:2)

您的立方体似乎使用相同的坐标,因为您在循环之前设置了cubeX和cubeY。 您可以在for循环中设置迭代所有多维数据集的坐标,以确保所有多维数据集都有不同的坐标。另外,你可能不应该在循环中做glClear()(在外面做),除非你只想在7个不同的位置绘制一个立方体。 glClear()将清除整个屏幕。

答案 1 :(得分:1)

除了qternion所说的,您可能希望尝试以下代码。 我添加了一个用户定义的类型 - vec3。我硬编码了多维数据集的数量,我在程序启动时添加了一个调用来定位所有多维数据集。

#include <GL/glut.h>
#include <stdlib.h>
#include <time.h>


/* -- GLOBAL VARIABLES --------------------------------------------------------------------------    --------------- */

GLint screenWidth = 500;
GLint screenHeight = 500;
GLfloat cubeX = 0.0;
GLfloat cubeY = 5.0;
GLfloat cubeZ = -20.0;
GLfloat rotateY = 0.0;
//GLfloat rotCube = 0.0;
GLint cubeCount = 0;
//const GLint CUBE_LOW = 7;
//const GLint CUBE_HIGH = 15;
const GLint CUBE_HIGH = 15;
const GLint CUBE_LOW = 7;
const GLfloat X_HIGH = 10.0;
const GLfloat X_LOW = -10.0;
const GLfloat Y_HIGH = 10.0;
const GLfloat Y_LOW = -10.0;

const int maxCubes = 50;

typedef struct vec3
{
    double x, y, z;
};

vec3 cubeOrigins[maxCubes];

/* -- NAMESPACE ---------------------------------------------------------------------------------    --------------- */

using namespace std;


void initOrigins()
{
    int i;
    for (i=0; i<maxCubes; i++)
    {
        cubeOrigins[i].x = X_LOW + (float)rand() / (float)RAND_MAX * (X_HIGH - X_LOW);
        cubeOrigins[i].y = Y_LOW + (float)rand() / (float)RAND_MAX * (Y_HIGH - Y_LOW);
    }
}

/* ----------------------------------------------------------------------------------------------    -------------- */
/*
Function:   unsigned time_seed()
*/


unsigned time_seed()
{
    time_t now = time (0);
    unsigned char *p = (unsigned char *)&now;
    unsigned seed = 0;
    size_t i;

    for(i = 0; i < sizeof now; i++)
        seed  = seed * (255 + 2U) + p[i];

    return seed;
}


/* ----------------------------------------------------------------------------------------------    --------------- */
/*
Function:   void rotateCube()
*/

 void rotateCube()
{
    rotateY += 0.050f;
    if(rotateY > 360.0f)
        rotateY -= 360.0f;
    glutPostRedisplay();
}

/* ----------------------------------------------------------------------------------------------    --------------- */
/*
Function:   void myDisplay()
*/

void myDisplay()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    for(int i = 0; i < maxCubes; i++)
    {
        glLoadIdentity();

        glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, cubeZ);
        glRotatef(rotateY, 0.0f, 1.0f, 0.0f);

        glutWireCube(2.0f);
    }
    glutSwapBuffers();
    glFlush();
}

/* ----------------------------------------------------------------------------------------------    --------------- */
/*
Function:   int main( int argc, char **argv )
*/

void myReshape(int width, int height)
{
    glViewport(0, 0, (GLsizei)width, (GLsizei)height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100);
    glMatrixMode(GL_MODELVIEW);
}
/* ----------------------------------------------------------------------------------------------    --------------- */
/*
Function:   int main( int argc, char **argv )
*/

int main( int argc, char **argv )
{
    srand(time_seed());
    initOrigins();

    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(screenWidth, screenHeight);
    glutInitWindowPosition(100, 40);
    glutCreateWindow("Boxes!");
    glutDisplayFunc(myDisplay);
    glutIdleFunc(rotateCube);
    glutReshapeFunc(myReshape);
    glutMainLoop();

   return 0;
}