#include <windows.h>
#include <GL/gl.h>
#include <gl/glut.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int WindowWidth = 640; // declares width of window
int WindowHeight = 480; // declares height of window
int WindowLeft = 100; // sets the left position of window
int WindowTop = 150; // sets the top location of window
int MaxNoOfLines = 100; // max no of lines
int MaxNoOfPolygons = 7; //max no of polygons
int MaxNoOfVertices = 7; //max no of polygon vertices must be > 3
int MaxNoOfDots = 100; //max no of dots
int RotationPosition = 0; //defines which shape to draw
float RandomColour(void){ //randomises a no between 0-0.99 for colours
float RndColour;
RndColour = rand()%99;
RndColour = RndColour / 100;
return (RndColour);
}
void DrawDots(void){ //draw dot function
glBegin(GL_POINTS);
int i, NoOfDots;
NoOfDots = (rand()%MaxNoOfDots); // randomises no of dots to display
for (i=1; i<NoOfDots+1; i++) { // loops through random no of dots
glColor3f(RandomColour(),RandomColour(),RandomColour()); // randomises dot colour
glVertex2i((rand()%WindowWidth),(rand()%WindowHeight)); // draws dot random location between screen boundries
}
glEnd();
}
void DrawLines(void){
glBegin(GL_LINES);
int i, NoOfLines;
NoOfLines = (rand()%MaxNoOfLines);
for (i=1; i<NoOfLines+1; i++) {
glColor3f(RandomColour(),RandomColour(),RandomColour());
glVertex2i((rand()%WindowWidth),(rand()%WindowHeight));
glVertex2i((rand()%WindowWidth),(rand()%WindowHeight));
}
glEnd();
}
void DrawPolygons(void){
int i,h, NoOfPolygons, NoOfVertices;
NoOfPolygons = (rand()%MaxNoOfPolygons); // randomised no of polygons to draw
for (i=1; i<MaxNoOfPolygons+1; i++) {
glBegin(GL_POLYGON);
NoOfVertices = (3+rand()%(MaxNoOfVertices-3))
; // randomises no of vertices for each polygon
glColor3f(RandomColour(),RandomColour(),RandomColour());
for (h=1; h<MaxNoOfVertices+1; h++) {
glVertex2i((rand()%WindowWidth),(rand()%WindowHeight));
}
glEnd();
}
}
void myInit(void){
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
void RotateShapes(void){
switch(RotationPosition)
{
case 0:
DrawDots(); //draw dots
break;
case 1:
DrawLines(); //draw lines
break;
case 2:
DrawPolygons(); //draw polygons
break;
default:
break; //do nothing
}
}
void myKeyboard(unsigned char key, int x, int y){
switch(key)
{
case 100: //check if key = d for dots
DrawDots();
break;
case 108: //check if key = l for lines
DrawLines();
break;
case 112: //check if key = p for polygons
DrawPolygons();
break;
case 113: //check if key = q to quit
exit(-1);
break;
case 32: //check if key = space
RotateShapes();
break;
default:
break; //do nothing
}
}
void myMouse(int button, int state, int x, int y){
//if (state==GLUT_DOWN) TakeKeyInput(1, 1 ,button);
if (state==GLUT_DOWN && button==GLUT_LEFT_BUTTON) RotateShapes();
if (state==GLUT_DOWN && button==GLUT_RIGHT_BUTTON) {
glClear(GL_COLOR_BUFFER_BIT);
if (RotationPosition<2) {
RotationPosition++;
} else {
RotationPosition = 0;
}
}
}
void myDisplay(void){
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void main(int argc, char** argv){
srand ( time(NULL) );
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(WindowWidth,WindowHeight);
glutInitWindowPosition(WindowLeft,WindowTop);
glutCreateWindow(“Random Stuffage”);
printf("[d] for dots
[l] for lines
[p] for polygons
[leftmouse/space] for shape rotation
[rightmouse] for clear and next shape
");
glutDisplayFunc(myDisplay);
glutMouseFunc(myMouse);
glutKeyboardFunc(myKeyboard);
myInit();
glutMainLoop();
}