/* Just messing around with OpenGL * * Copyright Clint Byrum, 2002. All Rights Reserved. * * This code is covered under the GNU General Public * License, v2.0 or later. * */ #include #include #include #include #include #define DISPLAY_DELAY 50 #define MOVE_FACTOR 8.2 #define MASS1_SIZE 250 #define XSTART 450.0 #define YSTART 450.0 #define ZSTART 0.0 #define WORLDX 640.0 #define WORLDY 480.0 #define WORLDZ 150.0 struct toyMass { int vcount; GLfloat *vertexes; GLfloat *colors; }; void toyDisplayWin(void); void toyTimerFunc(int value); struct toyMass *toyMakeLots(int howmany,struct toyMass *mass); void toyDisplayMass(struct toyMass *mass); GLfloat toyRand(GLfloat low,GLfloat high); /* Global mass */ struct toyMass *onemass; static GLfloat lastpos= 20; static GLfloat lastpos2=40; int main(int argcp, char **argv) { int winhndl; glutInitWindowSize(640,480); glutInitWindowPosition(0,0); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInit(&argcp, argv); winhndl=glutCreateWindow("My god this is idiotic"); glutSetCursor(GLUT_CURSOR_CROSSHAIR); glClearColor(0.0,0.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,WORLDX,0.0,WORLDY,-WORLDZ,WORLDZ); glutDisplayFunc(toyDisplayWin); glutTimerFunc(DISPLAY_DELAY,toyTimerFunc,0); srand(time(NULL)); /*fprintf(stderr,"time=%d",time(NULL));*/ onemass=malloc(sizeof(struct toyMass)); if(onemass==NULL) { exit(1); }; onemass->vertexes=malloc(sizeof(GLfloat)*MASS1_SIZE); if(onemass->vertexes==NULL) { exit(1); }; onemass->colors=malloc(sizeof(GLfloat)*MASS1_SIZE); if(onemass->colors==NULL) { exit(1); }; toyMakeLots(MASS1_SIZE,onemass); /*fprintf(stderr,"Calling glutMainLoop\n");*/ glutMainLoop(); sleep(1); return 0; } void toyDisplayWin(void) { GLdouble sphereRadius=30; GLint sphereSlices=30; GLint sphereStacks=30; GLfloat oldpos,diff; GLfloat r1[3],r2[3],r3[3],r4[3]; diff=((-1.0*MOVE_FACTOR)+(2.0*MOVE_FACTOR)*rand()/(RAND_MAX+1.0)); lastpos=lastpos+diff; r1[0]=20.0;r1[1]=40.0+lastpos;r1[2]=0.0; r2[0]=40.0; diff=((-1.0*MOVE_FACTOR)+(2.0*MOVE_FACTOR)*rand()/(RAND_MAX+1.0)); lastpos=lastpos+diff; r2[1]=lastpos; r2[2]=lastpos-(lastpos*0.4); r3[0]=40.0+lastpos;r3[1]=80.0;r3[2]=0.0; /*r4[0]=20.0;*/ oldpos=lastpos; diff=((-1.0*MOVE_FACTOR)+(2.0*MOVE_FACTOR)*rand()/(RAND_MAX+1.0)); lastpos=lastpos+diff; r4[0]=lastpos; r4[1]=80.0; r4[2]=(-1.0+20.0*rand()/(RAND_MAX+1.0));; /*fprintf(stderr,"In toyDisplayWin [%f]\n",r4[0]);*/ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); /* glBegin(GL_POLYGON); glVertex3f(2.0, 4.0, 0.0); glColor3f(0.0,1.0,0.0); glVertex3f(8.0, 4.0, 0.0); glColor3f(0.0,0.0,1.0); glVertex3f(8.0, 6.0, 0.0); glVertex3f(2.0, 6.0, 0.0); glEnd(); */ /*glutWireSphere(sphereRadius,sphereSlices,sphereStacks);*/ glColor3f(0.0,1.0,0.0); /* glBegin(GL_POLYGON); */ /* glVertex3f(20.0,40.0,0.0); glVertex3f(40.0,40.0,0.0); glVertex3f(40.0,80.0,0.0); glVertex3f(20.0,80.0,0.0); */ /* glVertex3fv(r1); glColor3f(1.0,0.5,0.0); glVertex3fv(r2); glVertex3fv(r3); */ /*glColor3f(rand()/(RAND_MAX+1.0),0.0,1.0);*/ /* glColor3f(1.0,0.0,1.0); glVertex3fv(r4); glEnd(); */ toyDisplayMass(onemass); /*glFlush();*/ glutSwapBuffers(); glutTimerFunc(DISPLAY_DELAY,toyTimerFunc,0); /*usleep(600);*/ } void toyTimerFunc(int value) { /*fprintf(stderr,"In toyTimerFunc\n");*/ glutPostRedisplay(); } /* howmany = howmany polygons to make * mass = pointer to allocated RAM for masses */ struct toyMass *toyMakeLots(int howmany, struct toyMass *mass) { int i,vpos; GLfloat lastx; GLfloat lasty; GLfloat lastz; GLfloat diff; lastx=XSTART+toyRand(0,10); lasty=YSTART+toyRand(0,10); lastz=ZSTART+toyRand(0,10); for(i=0;i<=howmany;i++) { /* Move a small amount x, then y, then z */ vpos=i*3; diff=(-1.0*MOVE_FACTOR)+(MOVE_FACTOR*2)*rand()/(RAND_MAX+1.0); lastx+=diff; mass->vertexes[vpos+0]=lastx; mass->colors[vpos+0]=lastx-(lastx*0.9); diff=(-1.0*MOVE_FACTOR)+(MOVE_FACTOR*2)*rand()/(RAND_MAX+1.0); lasty+=diff; mass->vertexes[vpos+1]=lasty; mass->colors[vpos+0]=lasty-(lasty*0.9); diff=(-1.0*MOVE_FACTOR)+(MOVE_FACTOR*2)*rand()/(RAND_MAX+1.0); lastz+=diff; mass->vertexes[vpos+2]=lastz; mass->colors[vpos+0]=lastz-(lastz*0.9); mass->vcount++; } return mass; } void toyDisplayMass(struct toyMass *mass) { int i; GLfloat tmp[3]; GLfloat tmpc[4]; glBegin(GL_POLYGON); for(i=0;i<=mass->vcount;i++) { tmp[0]=mass->vertexes[i] +toyRand(0,0.1); tmp[1]=mass->vertexes[i+1]+toyRand(0,0.1); tmp[2]=mass->vertexes[i+2]+toyRand(0,0.1); tmpc[0]=mass->colors[i]; tmpc[1]=mass->colors[i+1]; tmpc[2]=mass->colors[i+2]; tmpc[3]=toyRand(0,1); /*fprintf(stderr,"tmpc[3]=%f",tmpc[3]);*/ /*glColor3f(toyRand(0,3),toyRand(0,3),toyRand(0,3)); */ /*glColor3f(toyRand(0,0.2),0.25,1.0);*/ glColor4fv(tmpc); glVertex3fv(tmp); } glEnd(); } GLfloat toyRand(GLfloat low,GLfloat high) { return low+(high*rand()/(RAND_MAX+1.0)); }