January 16, 2014

Rainbow - part 3

Made some adjustments, looked online for some help and played with some trig.

#define PI 3.1415926535897932384626433832795

float colors[] =
{1.0, 0.0, 0.0,
1.0, 0.5, 0.0,
1.0, 1.0, 0.0,
 0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 1.0};

typedef struct
{
  float x;
  float y;
} CIRCLE;

int RED = 0, ORANGE = 3, YELLOW = 6, GREEN = 9, BLUE = 12, PURPLE = 15;

void createCircle(int r, int color, float yPos) {
  CIRCLE circle;
  glBegin(GL_LINES);
  glColor3f(colors[color], colors[color + 1], colors[color + 2]);

    for (float i = 0; i < PI; i+=.1) {
      circle.x = r * cos(i);
      circle.y = r * sin(i);
      glVertex3f(circle.x, circle.y + yPos, 0);

      circle.x = r * cos(i + 0.1);
      circle.y = r * sin(i + 0.1);
      glVertex3f(circle.x, circle.y + yPos, 0);
    }
    glEnd();
}

void display()
{
  glClearColor(1,1,1,1);
  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_LINES);

  createCircle(1, RED, 0);
  createCircle(1, ORANGE, -.05);
  createCircle(1, YELLOW, -.1);
  createCircle(1, GREEN, -.15);
  createCircle(1, BLUE, -.2);
  createCircle(1, PURPLE, -.25);

  glEnd();
  glutSwapBuffers();
}

int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
  glutInitWindowSize(600,600);

  glutCreateWindow("not a rainbow but i can still pretend");
  glutDisplayFunc(display);

  glutMainLoop();
}

And here is the end result:


It still looks a little weird but it's looking more like a rainbow!  Yay!