/* ********************************************************************** October 2000 Author: Brian J Ross Brock University Dept of Computer Science random_circle: This program generates K random 2D points a unit distance from the origin. It plots it using OpenGL's colour map mode. The points are saved in a dynamically allocated array. Type Q to exit. Usage: random_circle K (K is # points to generate; default 100) Index pallets crash unless the video card can support the mode. Modified July 13 05, To use RGB color, Manage color list internally. ************************************************************************* */ #if !defined(Linux) #include //Not Linux must be windows #endif #include #include #include #include #include #define X 0 #define Y 1 #define WINMIN 0 #define WINMAX 400 #define COORDMIN -1.0 #define COORDMAX 1.0 typedef struct point { GLfloat coord[2]; } Point; typedef Point *PointArray; struct globstruct { int np; // number of points in table PointArray pts; // point table }; float color[10][3]={{0.0f,0.0f,0.0f}, //Example color list {1.0f,0.0f,0.0f}, {1.0f,1.0f,0.0f}, {1.0f,1.0f,1.0f}, {0.5f,0.5f,0.5f}, {0.0f,0.3f,0.4f}, {0.0f,0.0f,0.0f}, {0.0f,0.0f,0.0f}, {0.0f,0.0f,0.0f}, {0.0f,0.0f,0.0f}}; struct globstruct global = {100, 0}; /* ------------------------------------------------------------------------- */ void random_points(PointArray pts, int np, GLfloat low, GLfloat high) { int i; // loop counter GLfloat temp_x, temp_y, length; // scratch variables /* Find 'np' normalized random coordinates, each a GLfloat between low and high, a unit distance from (0, 0). */ for (i=0; i < np; i++) { temp_x = (GLfloat) (rand()*(high-low)/RAND_MAX + low); temp_y = (GLfloat) (rand()*(high-low)/RAND_MAX + low); length = sqrt(temp_x*temp_x + temp_y*temp_y); pts[i].coord[X] = temp_x/length; pts[i].coord[Y] = temp_y/length; } } /* ------------------------------------------------------------------------- */ void print_points(PointArray pts, int np) { int i; // loop counter /* Print the points in the table. */ for (i=0; i < np; i++) { printf("%4d : (%f, %f)\n", i, pts[i].coord[X], pts[i].coord[Y]); } } /* ------------------------------------------------------------------------- */ void plot_points(void) { int i; // loop counter /* Plot the points in the table. Plot the origin too. */ glClear(GL_COLOR_BUFFER_BIT); printf("In Plot points - 1\n"); glPointSize(4.0); //glIndexi(3); //SGI glColor3fv(color[4]); //VS, added by db glBegin(GL_POINTS); glVertex2f (0.0, 0.0); glEnd(); glPointSize(2.0); //glIndexi(2); //SGI glColor3fv(color[5]); //VS, added by db glBegin(GL_POINTS); for (i = 0; i < global.np; i++) { glVertex2f(global.pts[i].coord[X], global.pts[i].coord[Y]); } glEnd(); glFlush(); } /* ------------------------------------------------------------------------- */ void keyboard(unsigned char key, int x, int y) { switch (key){ case 0x1B: case 'q': case 'Q': free(global.pts); // It is PC to free your allocated memory. :-) return; break; } } /* ------------------------------------------------------------------------- */ int main(int argc, char **argv) { glutInit(&argc, argv); if (argc == 2) { sscanf(argv[1], "%d", &global.np); // read in 'K' from cmd line } global.pts = (PointArray) calloc(global.np, sizeof(Point)); random_points(global.pts, global.np, COORDMIN, COORDMAX); print_points(global.pts, global.np); printf("Finished printing points\n"); glutInitWindowSize(WINMAX, WINMAX); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH); //glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX); //SGI -- will crash unless mode is supported glutCreateWindow("randompoints"); // printf("Finished Main At 1 \n"); glutDisplayFunc(plot_points); glutKeyboardFunc(keyboard); glClearColor(0.0, 0.0, 0.0, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(COORDMIN*1.2, COORDMAX*1.2, COORDMIN*1.2, COORDMAX*1.2); //glutSetColor(1, 0.0, 0.0, 0.0); // entry 1 <-- black SGI //glutSetColor(2, 1.0, 0.0, 0.0); // entry 2 <-- red SGI //glutSetColor(3, 1.0, 1.0, 1.0); // entry 2 <-- white SGI glClearIndex(1.0); glutMainLoop(); return(0); }