rendered paste body#include <iostream>
#include <ctime>
#include <GL/glut.h>
#include <string>
using namespace std;
int points = 3, noShapes = 2, noPoints;
double * red, * green, * blue;
int * x, * y, * state;
string shape;
double WIDTH = 400, HEIGHT = 400;
double HMIN = -WIDTH/2, HMAX = WIDTH/2, KMIN = -HEIGHT/2, KMAX = HEIGHT/2;
double startTime = 0;
void drawString (GLfloat x, GLfloat y, void *font, string s)
{
glRasterPos3f (x, y, 0);
for (int i = 0; i < (signed int)s.length(); i++)
{
glutBitmapCharacter (font, s[i]);
}
}
void display ()
{
glClear(GL_COLOR_BUFFER_BIT);
int n = noShapes;
for (int i = 0; i < n * points; i = i + points)
{
glPushMatrix();
glTranslatef(HMIN + i * (HMAX - HMIN) / n, KMAX - i * (KMAX - KMIN) / n, 0);
glRotatef (i * 360.0 / n, 0, 0, 1);
if(points == 3)
{
glBegin (GL_TRIANGLES);
glColor3f (red[0], green[0], blue[0]);
glVertex3f (x[0], y[0], 0);
glColor3f (red[1], green[1], blue[1]);
glVertex3f (x[1], y[1], 0);
glColor3f (red[2], green[2], blue[2]);
glVertex3f (x[2], y[2], 0);
glEnd ();
glPopMatrix ();
}
else
{
glBegin (GL_QUADS);
glColor3f (red[0], green[0], blue[0]);
glVertex3f (x[0], y[0], 0);
glColor3f (red[1], green[1], blue[1]);
glVertex3f (x[1], y[1], 0);
glColor3f (red[2], green[2], blue[2]);
glVertex3f (x[2], y[2], 0);
glColor3f (red[3], green[3], blue[3]);
glVertex3f (x[3], y[3], 0);
glEnd ();
glPopMatrix ();
}
}
glColor3f(red[0], green[1], blue[2]);
string s = "wow, the colors!";
drawString(0, 60, GLUT_BITMAP_HELVETICA_18, s);
glutSwapBuffers ();
glutPostRedisplay ();
}
//void motion(int x, int y)
//{
//}
void idle()
{
if (clock () - startTime >= 10)
{
startTime = clock();
for(int i = 0; i < points; i++)
{
switch(state[i])
{
case 0:
y[i]++;
if(y[i] == 50)
{
state[i] = 1;
}
cout<<"point "<<i+1<<" is in state 0.\n";
break;
case 1:
x[i]++;
if(x[i] == 50)
{
state[i] = 2;
}
cout<<"point "<<i+1<<" is in state 1.\n";
break;
case 2:
y[i]--;
if(y[i] == 0)
{
state[i] = 3;
}
cout<<"point "<<i+1<<" is in state 2.\n";
break;
case 3:
x[i]--;
if(x[i] == 0)
{
state[i] = 0;
}
cout<<"point "<<i+1<<" is in state 3.\n";
break;
}
cout<<"x["<<i<<"] is at "<<x[i]<<".\n";
cout<<"y["<<i<<"] is at "<<y[i]<<".\n";
}
cout<<endl;
}
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
double X = x * (HMAX - HMIN) / (WIDTH + HMIN);
double Y = (HEIGHT - y) * (KMAX - KMIN) / (HEIGHT + KMIN);
switch(button)
{
case GLUT_LEFT_BUTTON:
switch (state)
{
case GLUT_DOWN:
glRotatef(5, 0, 0, 1);
break;
case GLUT_UP:
break;
}
break;
case GLUT_MIDDLE_BUTTON:
switch (state)
{
case GLUT_DOWN:
glScalef(2, .5, 1);
break;
case GLUT_UP:
break;
}
break;
case GLUT_RIGHT_BUTTON:
switch (state)
{
case GLUT_DOWN:
glRotatef(-5, 0, 0, 1);
break;
case GLUT_UP:
break;
}
break;
}
glutPostRedisplay();
}
void special (int key, int x, int y)
{
if (key == GLUT_KEY_UP)
{
glTranslatef(0, 10, 0);
}
else if (key == GLUT_KEY_DOWN)
{
glTranslatef(0, -10, 0);
}
else if (key == GLUT_KEY_RIGHT)
{
glTranslatef(10, 0, 0);
}
else if (key == GLUT_KEY_LEFT)
{
glTranslatef(-10, 0, 0);
}
glutPostRedisplay();
}
void keyboard (unsigned char key, int x, int y)
{
if (key == 27) exit (0);
double X = x * (HMAX - HMIN) / WIDTH + HMIN;
double Y = (HEIGHT - y) * (KMAX - KMIN) / HEIGHT + KMIN;
string s = " ";
s[0] = key;
drawString (X, Y, GLUT_BITMAP_HELVETICA_18, s);
glutPostRedisplay ();
}
void main (int argc, char** argv)
{
startTime = clock();
cout<<"will you be drawing triangles or squares?\n";
do
{
cin>>shape;
if(shape == "triangles")
{
points = 3;
x = new int[3];
y = new int[3];
x[0] = 0;
y[0] = 0;
x[1] = 0;
y[1] = 50;
x[2] = 50;
y[2] = 0;
state = new int [3];
state[0] = 0;
state[1] = 1;
state[2] = 3;
}
else if(shape == "squares")
{
points = 4;
x = new int[4];
y = new int[4];
x[0] = 0;
x[1] = 50;
x[2] = 50;
x[3] = 0;
y[0] = 0;
y[1] = 0;
y[2] = 50;
y[3] = 50;
/*state = new int [4];
for (int i = 0; i < 4; i++)
{
state[i] = i;
}*/
}
else
{
cout<<"invalid. Please re-enter\n";
}
}while(shape != "triangles" && shape != "squares");
cout<<"how many shapes will you draw?\n";
cin>>noShapes;
noPoints = noShapes * points;
red = new double [points];
green = new double [points];
blue = new double [points];
for (int i = 0; i < points; i++)
{
}
for (int i = 0; i < points; i++)
{
cout<<"enter the red value for point "<<i+1<<" (between 0 and 1).\n";
cin>>red[i];
cout<<"enter the green value for point "<<i+1<<" (between 0 and 1).\n";
cin>>green[i];
cout<<"enter the blue value for point "<<i+1<<" (between 0 and 1).\n";
cin>>blue[i];
}
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (WIDTH, HEIGHT);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Chris's Final Project");
glClearColor (0, 0, 0, 0);
glMatrixMode (GL_MODELVIEW);
glOrtho(HMIN, HMAX, KMIN, KMAX, -1, 1);
glClear (GL_COLOR_BUFFER_BIT);
glutKeyboardFunc (keyboard);
glutMouseFunc(mouse);
glutDisplayFunc (display);
glutSpecialFunc(special);
glutIdleFunc(idle);
//glutMotionFunc(motion);
//glutPassiveMotionFunc();
glutMainLoop ();
return;
}