C++ Java Python JavaScript Physics Robotics Electronics Astronomy Summer Courses Other Courses

Using Random Numbers with OpenGL

Random Numbers for Vertices

Random Numbers for Vertices and Colors

 

Random Numbers for Vertices

#include <GL/glut.h>                                      //GL indicates a directory on Windows systems - make sure it is there on code used

#include <stdlib.h>                                         //To create random numbers

#include <time.h>                                          //To use system clock as "seed" for random numbers

 

void init(void)                                                  //void is not necessary

{

   glClearColor (0.0, 0.0, 0.0, 0.0);                   //"clearing the canvas" - setting background color

   glShadeModel (GL_SMOOTH);                    //Smooth will cause interpolation between the vertices (alternative is flat)

}

void triangle(void)

{

   srand (time(0));                                         //seeding with system clock

   int vx1, vy1, vx2, vy2, vx3, vy3;                   //vertices, ints because of scaling procedure (ints)

   //setting low and high values for the vertices

   int lowv = -20;

   int highv = 20;

 

   for (int i = 1; i < 6; i++)

   {

        //getting random values for the vertices of the triangles

        vx1 = rand () % (highv - lowv + 1) + lowv;

        vy1 = rand () % (highv - lowv + 1) + lowv;

        vx2 = rand () % (highv - lowv + 1) + lowv;

        vy2 = rand () % (highv - lowv + 1) + lowv;

        vx3 = rand () % (highv - lowv + 1) + lowv;

        vy3 = rand () % (highv - lowv + 1) + lowv;

       glBegin (GL_TRIANGLES);

         glColor3f (1.0, 0.0, 0.0);

         glVertex2i (vx1, vy1);                          //changed vertex parameters to integers

         glColor3f (0.0, 1.0, 0.0);

         glVertex2i (vx2, vy2);

         glColor3f (0.0, 0.0, 1.0);

         glVertex2i (vx3, vy3);

       glEnd();

   }

}

void display(void)

{

   glClear (GL_COLOR_BUFFER_BIT);

   triangle();     //calling triangle function above

   glFlush ();

}

void reshape (int w, int h)

{

   glViewport (0, 0, (GLsizei) w, (GLsizei) h);

   glLoadIdentity ();

   //camera default (at origin, looking down negative z axis), left, right, top, bottom planes - note high and low above

   gluOrtho2D(-21,21, -21, 21); 

}

int main(int argc, char** argv)

{

   glutInit(&argc, argv);

   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);                           //Using RGB color

   glutInitWindowSize (500, 500);                                                         //Size of window (width, height in pixels)

   glutInitWindowPosition (100, 100);                                                    //Position of window

   glutCreateWindow ("Using Random Numbers");                                 //Create window and set title

   init ();                                        

   glutDisplayFunc(display);                       

   glutReshapeFunc(reshape);

   glutMainLoop();                                                                               //starts the activities - must be last item called

   return 0;

}

Random Numbers for Vertices and Colors

 

#include <GL/glut.h>                                      //GL indicates a directory on Windows systems - make sure it is there on code used

#include <stdlib.h>                                        //To create random numbers

#include <time.h>                                          //To use system clock as "seed" for random numbers

void init(void)                                                  //void is not necessary

{

   glClearColor (0.0, 0.0, 0.0, 0.0);                  //"clearing the canvas" - setting background color

   glShadeModel (GL_SMOOTH);                    //Smooth will cause interpolation between the vertices (alternative is flat)

}

void triangle(void)

{

   srand (time(0));                                         //seeding with system clock

   int vx1, vy1, vx2, vy2, vx3, vy3;                   //vertices, ints because of scaling procedure (ints)

   float R1, G1, B1, R2, G2, B2, R3, G3, B3;            //RGB values for the 3 vertices

   //setting low and high values for the vertices

   int lowv = -20;

   int highv = 20;

   //setting low and high values for the RGB colors (before dividing by 10)

   int lowc = 0;

   int highc = 9;

   for (int i = 1; i < 6; i++)

   {

        //getting random values for the vertices of the triangles

        vx1 = rand () % (highv - lowv + 1) + lowv;

        vy1 = rand () % (highv - lowv + 1) + lowv;

        vx2 = rand () % (highv - lowv + 1) + lowv;

        vy2 = rand () % (highv - lowv + 1) + lowv;

        vx3 = rand () % (highv - lowv + 1) + lowv;

        vy3 = rand () % (highv - lowv + 1) + lowv;

        //getting random values for the RGB colors

        R1 = (rand () % (highc - lowc + 1) + lowc)/10.0;      //10.0 necessary to avoid integer division;

        G1 = (rand () % (highc - lowc + 1) + lowc)/10.0;

        B1 = (rand () % (highc - lowc + 1) + lowc)/10.0;

        R2 = (rand () % (highc - lowc + 1) + lowc)/10.0;

        G2 = (rand () % (highc - lowc + 1) + lowc)/10.0;

        B2 = (rand () % (highc - lowc + 1) + lowc)/10.0;

        R3 = (rand () % (highc - lowc + 1) + lowc)/10.0;

        G3 = (rand () % (highc - lowc + 1) + lowc)/10.0;

        B3 = (rand () % (highc - lowc + 1) + lowc)/10.0;

         glBegin (GL_TRIANGLES);

             glColor3f (R1, G1, B1);

             glVertex2i (vx1, vy1);                          //changed vertex parameters to integers

             glColor3f (R2, G2, B2);

             glVertex2i (vx2, vy2);

             glColor3f (R3, G3, B3);

             glVertex2i (vx3, vy3);

       glEnd();

   }

}

void display(void)

{

   glClear (GL_COLOR_BUFFER_BIT);

   triangle();     //calling triangle function above

   glFlush ();

}

void reshape (int w, int h)

{

   glViewport (0, 0, (GLsizei) w, (GLsizei) h);

   glLoadIdentity ();

   //camera default (at origin, looking down negative z axis), left, right, top, bottom planes - note high and low above

   gluOrtho2D(-21,21, -21, 21); 

}

int main(int argc, char** argv)

{

   glutInit(&argc, argv);

   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);                           //Using RGB color

   glutInitWindowSize (500, 500);                                                         //Size of window (width, height in pixels)

   glutInitWindowPosition (100, 100);                                                    //Position of window

   glutCreateWindow ("Using Random Numbers");                                 //Create window and set title

   init ();                                        

   glutDisplayFunc(display);                       

   glutReshapeFunc(reshape);

   glutMainLoop();                                                                              //starts the activities - must be last item called

   return 0;

}