使用OpenGL

时间:2018-09-27 21:30:23

标签: c opengl

我希望每个立方体都有一个随机的颜色,但是有了这段代码,颜色一直在变化

int espacioX = -500;
int espacioY = -500;
for(int i=1; i<=100; i++){
    for(int j=1; j<=100; j++){
        randomR = (double)rand() / (RAND_MAX);
        randomG = (double)rand() / (RAND_MAX);
        randomB = (double)rand() / (RAND_MAX);
        glColor3f(randomR,randomG,randomB);
        glTranslatef(espacioX,espacioY,0);
        glutSolidCube(9);
        glTranslatef(-espacioX,-espacioY,0);
        espacioX+=10;
    }
    glTranslatef(-1000,0,0);
    espacioY+=10;
}

在程序运行时如何使它们保持相同的颜色?

编辑: 这是完整的代码:

#include <windows.h>

/***** OpenGL library imports. *****/
#include<gl\glut.h>                                 //basic header file for OpenGL 
//#include <gl\glaux.h>                             // Header File For The Glaux Library
/***** C library imports. *****/ 
#include <stdlib.h>
#include <stdio.h>
#include <math.h>



int ScreenWidth  = 0;
int ScreenHeight = 0; 
int last_x = 0, last_y = 0,left = 1,middle = 0,right = 0;
int zoom_y = 0;
int anglex=0, angley=0, anglez=0;
int lookfrom=450;
int ang=0;


void myGlutKeyboard(unsigned char Key, int x, int y)
{
    printf("%5d\n",Key);
    glutPostRedisplay();
}


void init_GL_params()
{
    static GLfloat light_ambient[] = { .250, .250, .250, .250 };
    static GLfloat light_diffuse[] = { .250, .250, .25, .250 };
    static GLfloat light_specular[] = { .250, .250, .250, .250 };

    static GLfloat light1_diffuse[] = { .250, .250, .250, .0 };
    static GLfloat light1_specular[] = { .250, .250, .250, .0 };

    glEnable (GL_LIGHTING);
    glEnable (GL_LIGHT0);
    glEnable (GL_LIGHT1);
    glDisable (GL_TEXTURE_2D);
    glDisable (GL_TEXTURE_GEN_S);
    glDisable (GL_TEXTURE_GEN_T);
    glShadeModel (GL_FLAT);
    glEnable (GL_DEPTH_TEST);
    glDepthFunc (GL_LESS);
    glEnable (GL_CULL_FACE);
    glCullFace (GL_BACK);
    glFrontFace (GL_CCW);

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();

    glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
    glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);

    glLightfv(GL_LIGHT1, GL_DIFFUSE,  light1_diffuse); 
    glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
    glLightfv (GL_LIGHT1, GL_AMBIENT, light_ambient);

    glColor3f (1.0, 1.0, 1.0);

}

void setLightPosition()
{
    static GLfloat light_position[] = { 100, 200.0, 10, 1.0 };
    static GLfloat light1_position[] = { -0.0, 300.0, 0.0, 1.0 };

    // set the light position (for some reason we have to do this in model view.
    glLightfv (GL_LIGHT0, GL_POSITION, light_position);
    glLightfv(GL_LIGHT1, GL_POSITION, light1_position);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable(GL_COLOR_MATERIAL); 
}

void myGlutDisplay(void)
{
    init_GL_params();

      // clear the window
    glClearColor (0,0,0,0); //Color de fondo R G B
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();
    setLightPosition();     //Posici{on de la fuente de luz
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective(60.0f, 1.0, 1.0f, 10000.0f);
    gluLookAt(                          //punto de vista
            0 ,100 ,lookfrom ,          // desde donde veo xyz
            0, 0, 0,                    // hacia donde veo xyz
            0,1,0 ); 
    glRotatef(anglex, 1.0, 0.0, 0.0);
    glRotatef(angley, 0.0, 1.0, 0.0);
    glRotatef(anglez, 0.0, 0.0, 1.0);

    //Empieza a dibujar
    int espacioX = 500;
    int espacioY = 500;
    double randomR, randomG, randomB;

    for(int i=1; i<=100; i++){
        for(int j=1; j<=100; j++){
            randomR = (double)rand() / (RAND_MAX);
            randomG = (double)rand() / (RAND_MAX);
            randomB = (double)rand() / (RAND_MAX);
            glColor3f(randomR,randomG,randomB);
            glTranslatef(espacioX,espacioY,0);
            glutSolidCube(9);
            glTranslatef(-espacioX,-espacioY,0);
            espacioX-=10;
        }
        glTranslatef(1000,0,0);       
        espacioY-=10;
    }

    glFlush();                                          // Flush The GL Pipeline
    glutSwapBuffers();  
}

void myGlutReshape2(int mWidth, int mHeight)
{
    ScreenWidth  = mWidth;
    ScreenHeight = mHeight;
    glViewport(0, 0, (GLsizei) ScreenWidth, (GLsizei) ScreenHeight);
    glutPostRedisplay();
}


/****************************************************************/
void myGlutIdle(void)
{
    glutPostRedisplay();
}

/***************************************************************/

void myGlutMouse(int button, int button_state, int x, int y){
  if ( (button == GLUT_LEFT_BUTTON) && (button_state == GLUT_DOWN) ) 
  {
    last_x = x;
    last_y = y;
    left = 1;
    middle = 0;
    right = 0;
  }
  else if ((button == GLUT_RIGHT_BUTTON) && (button_state == GLUT_DOWN) ) 
  {
      zoom_y = y;
      right = 1;
      left = 0;
      middle = 0;
  }
}

void myGlutMotion(int x, int y){
        if (left){
            anglex+= (float) (y - last_y);
            angley+= (float) (x - last_x);

            last_x = x;
            last_y = y;
        }
        if (right)
        {
            lookfrom += (y - zoom_y);
            zoom_y = y;
        }
}

void GlutInit(int argc, char** argv)
{
    ScreenWidth  = 700;
    ScreenHeight = 700;
    /****************************************/
    /*   Initialize GLUT and create window  */
    /****************************************/
    glutInit(&argc, argv);

    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL );   //set up the display as a double buffer using RGB values
    glutInitWindowPosition(90, 50);                 //set the position of where you want the window to open
    glutInitWindowSize(ScreenWidth, ScreenHeight);  //set how big you want the initial window to be
    glutCreateWindow("Testing Virtual Environment" );   //what do you want the window to be called (titlebar text)

    //register callbacks
    glutDisplayFunc(myGlutDisplay);         //Register the display callback
    glutReshapeFunc(myGlutReshape2);        // -- Resize handler
    glutKeyboardFunc(myGlutKeyboard);       // -- Keyboard handler
    glutMouseFunc(myGlutMouse);             // -- Mouse Click handler
    glutMotionFunc(myGlutMotion);           // -- Mous motion handler
    glutIdleFunc(myGlutIdle);               // -- Idle handler

  glFlush();
}


int main( int argc, char **argv )
{
    GlutInit(argc, argv);
    glutMainLoop();
    return 0;
}

这是一个示例代码,抱歉,我只是发布了我编辑的部分。当我用特定的颜色制作立方体或球体时,它总是保持不变,因此我没有意识到这会一直刷新图像。我不太了解整个过程,所以不知道刷新图像的位置。

2 个答案:

答案 0 :(得分:1)

似乎您在另一个循环中运行此代码,这意味着您每次重新绘制场景时都在计算随机变量,这将导致颜色发生变化,并且显而易见的解决方案是一次填充多维数据集列表,然后然后遍历列表,在每个场景刷新时绘制多维数据集。

但是,看不到代码的其余部分并不清楚。请提供更多信息,以便我们为您提供更好的答案。

答案 1 :(得分:1)

为什么不在SEED的开头将myGlutDisplay(void)设置为某个常数。这样一来,您就可以在每一帧中获得相同的随机值,而无需在代码中进行任何大的更改,例如:

void myGlutDisplay(void)
{
    init_GL_params();

      // clear the window
    glClearColor (0,0,0,0); //Color de fondo R G B
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();
    setLightPosition();     //Posici{on de la fuente de luz
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective(60.0f, 1.0, 1.0f, 10000.0f);
    gluLookAt(                          //punto de vista
            0 ,100 ,lookfrom ,          // desde donde veo xyz
            0, 0, 0,                    // hacia donde veo xyz
            0,1,0 ); 
    glRotatef(anglex, 1.0, 0.0, 0.0);
    glRotatef(angley, 0.0, 1.0, 0.0);
    glRotatef(anglez, 0.0, 0.0, 1.0);

    //Empieza a dibujar
    int espacioX = 500;
    int espacioY = 500;
    double randomR, randomG, randomB;

    srand(0x98765432); // this is the SEED

    for(int i=1; i<=100; i++){
        for(int j=1; j<=100; j++){
            randomR = (double)rand() / (RAND_MAX);
            randomG = (double)rand() / (RAND_MAX);
            randomB = (double)rand() / (RAND_MAX);
            glColor3f(randomR,randomG,randomB);
            glTranslatef(espacioX,espacioY,0);
            glutSolidCube(9);
            glTranslatef(-espacioX,-espacioY,0);
            espacioX-=10;
        }
        glTranslatef(1000,0,0);       
        espacioY-=10;
    }

    glFlush();                                          // Flush The GL Pipeline
    glutSwapBuffers();  
}

SEED的值将为您提供模式。因此,更改常数将更改颜色。注意错误的常数(例如0)会破坏rand()函数的随机性。