#include "..\glut.h" #include #include #include #include #include /* global variable declarations */ int plusOrMinus = 1; int xYorZ = 0; GLfloat myX = 2.0, myY = 3.0, myZ = 10.0; char fileNumber[2] = "0"; /* global class definition */ class myRGB { private: unsigned char r, g, b; public: myRGB () { r = g = b = 0; } }; // end of class myRGB /* function declarations ----------------------------------*/ void myInit(){ glClearColor(0.0, 0.0, 0.0, 1.0); glColor3f(1.0, 1.0, 1.0); glEnable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-100.0, 100.0, -100.0, 100.0, -100.0, 100.0); } // end of myInit() /* --------------------------------------------------------*/ void myDisplay() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(myX, myY, myZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // positive axes in blue glColor3f(0.0, 0.6, 0.8); glBegin(GL_LINES); glVertex3f(100.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 100.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 100.0); glVertex3f(0.0, 0.0, 0.0); glEnd(); // negative axes in orange glColor3f(1.0, 0.4, 0.2); glBegin(GL_LINES); glVertex3f(-100.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, -100.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, -100.0); glVertex3f(0.0, 0.0, 0.0); glEnd(); // 5 lightgreen tetrahedra for (int t = -2; t <= 2; t++) { glColor3f(0.6, 1.0, 0.8); glBegin(GL_POLYGON); glVertex3f(40.0*t, 30.0, 0.0); glVertex3f(40.0*t-10, 0.0, 0.0); glVertex3f(40.0*t+10, 0.0, 0.0); glEnd(); glColor3f(0.0, 0.8, 0.6); glBegin(GL_POLYGON); glVertex3f(40.0*t, 30.0, 0.0); glVertex3f(40.0*t-10, 0.0, 0.0); glVertex3f(40.0*t, 0.0, 20.0); glEnd(); glColor3f(0.2, 1.0, 0.8); glBegin(GL_POLYGON); glVertex3f(40.0*t, 30.0, 0.0); glVertex3f(40.0*t+10, 0.0, 0.0); glVertex3f(40.0*t, 0.0, 20.0); glEnd(); glColor3f(0.2, 0.8, 0.4); glBegin(GL_POLYGON); glVertex3f(40.0*t, 0.0, 20.0); glVertex3f(40.0*t-10, 0.0, 0.0); glVertex3f(40.0*t+10, 0.0, 0.0); glEnd(); } // end of for loop glColor3f(1.0, 1.0, 0.0); glBegin(GL_LINES); glVertex3f(0.0, 0.0, 0.0); glVertex3f(-100.0, -100.0, 100.0); glEnd(); glutSwapBuffers(); } // end of myDisplay() /* --------------------------------------------------------*/ void spinDisplay() { if (xYorZ == 1) myX += plusOrMinus*0.005; if (xYorZ == 2) myY += plusOrMinus*0.005; if (xYorZ == 3) myZ += plusOrMinus*0.005; glutPostRedisplay(); } /* --------------------------------------------------------*/ void myKeyboard(unsigned char key, int x, int y) { if (key == 'X' || key == 'x') xYorZ = 1; if (key == 'Y' || key == 'y') xYorZ = 2; if (key == 'Z' || key == 'z') xYorZ = 3; if (key == 'Q' || key == 'q') exit(0); } /* --------------------------------------------------------*/ void myMouse(int button, int state, int x, int y) { if (state == GLUT_DOWN) plusOrMinus *= -1; } /* --------------------------------------------------------*/ void saveToDisk() { int w, h; w = glutGet(GLUT_WINDOW_WIDTH); h = glutGet(GLUT_WINDOW_HEIGHT); unsigned char *pixelArray = new unsigned char[3 * w * h]; if ( !pixelArray ) { cerr << "ERROR: not enough memory available to copy frame buffer!\n\n"; exit(0); } glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixelArray); // make color table for this image unsigned char colorTable[32][3]; int numEntriesUsed = 0; int x; for (x=0; x<3*w*h; x+=3) { bool found = false; int i = 0; while (i < numEntriesUsed && !found) { if (pixelArray[x] == colorTable[i][0] && pixelArray[x+1] == colorTable[i][1] && pixelArray[x+2] == colorTable[i][2]) found = true; else i++; } if (!found && numEntriesUsed < 32) { colorTable[numEntriesUsed][0] = pixelArray[x]; colorTable[numEntriesUsed][1] = pixelArray[x+1]; colorTable[numEntriesUsed][2] = pixelArray[x+2]; numEntriesUsed++; cout << "color # " << numEntriesUsed << " added to table: " << (int)pixelArray[x] << " " << (int)pixelArray[x+1] << " " << (int)pixelArray[x+2] << endl << flush; } } cout << "end of color table...." << endl << flush; ofstream outFile; char filename[40]; strcpy(filename, "tetras"); strcat(filename, fileNumber); strcat(filename, ".mu2"); fileNumber[0]++; outFile.open(filename); outFile << h << endl; outFile << w << endl; outFile << numEntriesUsed << endl; for (int i=0; i < numEntriesUsed; i++) { outFile << (colorTable[i][0] / 255.0) << " "; outFile << (colorTable[i][1] / 255.0) << " "; outFile << (colorTable[i][2] / 255.0) << endl; } int prevPixel; long count = 1L; for (x = 0; x < w; x++ ) { cout << x << endl << flush; for (int y = 0; y < h; y++) { int pixel = 0; bool found = false; while (!found && pixel < numEntriesUsed) { if (colorTable[pixel][0] == pixelArray[y*w*3 + x*3] && colorTable[pixel][1] == pixelArray[y*w*3 + x*3 + 1] && colorTable[pixel][2] == pixelArray[y*w*3 + x*3 + 2]) found = true; else pixel++; } if (!found) pixel = numEntriesUsed - 1; if (x == 0 && y == 0) prevPixel = pixel; if (prevPixel != pixel || count >= 32000) { outFile << count << " " << prevPixel << endl; count = 1; } else count++; prevPixel = pixel; } } outFile << count << " " << prevPixel << endl; outFile.close(); } // end of saveToDisk() /* --------------------------------------------------------*/ void right_button_menu(int id) { switch (id) { case 1: xYorZ = 1; break; case 2: xYorZ = 2; break; case 3: xYorZ = 3; break; case 4: xYorZ = 0; break; case 5: saveToDisk(); break; default: exit(0); } } /* --------------------------------------------------------*/ void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-100.0, 100.0, -100.0, 100.0, -100.0, 100.0); } /* --------------------------------------------------------*/ int main(int argc, char **argv) { glutInit(&argc, argv); /* set up display window */ glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500,350); glutInitWindowPosition(150, 100); glutCreateWindow("Always Looking at the Origin"); /* register callback functions */ glutReshapeFunc( myReshape ); glutDisplayFunc( myDisplay ); glutKeyboardFunc( myKeyboard ); glutIdleFunc( spinDisplay ); glutMouseFunc( myMouse ); /* add a menu! */ glutCreateMenu(right_button_menu); glutAddMenuEntry("move along x axis", 1); glutAddMenuEntry("move along y axis", 2); glutAddMenuEntry("move along z axis", 3); glutAddMenuEntry("pause movement", 4); glutAddMenuEntry("SAVE TO DISK FILE!", 5); glutAddMenuEntry("quit", 6); glutAttachMenu(GLUT_RIGHT_BUTTON); /* start the event loop */ myInit(); glutMainLoop(); return 0; } // end of main