Advertisement
aditya369

OpenGLFramework.h

Nov 28th, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.20 KB | None | 0 0
  1. #ifndef __FRAMEWORK_H__
  2. #define __FRAMEWORK_H__
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <iostream>
  7. #include <vector>
  8. #include <math.h>
  9. #include <string.h>
  10.  
  11. using namespace std;
  12.  
  13. // **Note:** Include GLUT after the standard c++ libraries to prevent linker errors
  14.  
  15. #ifdef WINDOWS
  16.     #include <windows.h>
  17.     #include <glut.h>
  18. #else
  19.     #include <glut.h>
  20. #endif
  21.  
  22. #define WINDOW_X_POSITION   100
  23. #define WINDOW_Y_POSITION   100
  24.  
  25. #define WINDOW_WIDTH        600
  26. #define WINDOW_HEIGHT       600
  27. #define BORDER_WIDTH        10
  28.  
  29.  
  30. /** Framework.h
  31.  * @author Aditya Sriram 8-22-10
  32.  */
  33. namespace openGLFramework {
  34.  
  35.     class OpenGLFramework {
  36.  
  37.     private:
  38.         float eyeX, eyeY, eyeZ, cenX, cenY, cenZ, upX, upY, upZ;
  39.         float zoom;
  40.        
  41.     protected:
  42.         unsigned int numberOfSubWindows;
  43.         int mainWindow, *subWindow;
  44.         int width, height, border;
  45.         float scaleFactor, rotateFactor, translateFactor;
  46.         string title;
  47.         static OpenGLFramework *instance;
  48.        
  49.     public:
  50.         OpenGLFramework() {
  51.             title = "Sample OpenGL Program";
  52.             numberOfSubWindows = 1;
  53.  
  54.             instance = NULL;
  55.             zoom = 1;
  56.  
  57.             width = WINDOW_WIDTH;
  58.             height = WINDOW_HEIGHT;
  59.             border = BORDER_WIDTH;
  60.  
  61.             scaleFactor = 1.5f;
  62.             rotateFactor = 5.0f;
  63.             translateFactor = 2.0f;
  64.         }
  65.  
  66.         virtual ~OpenGLFramework() {
  67.             cout << "Program Exited" << endl;
  68.         }
  69.  
  70.         void setInstance() {
  71.             //std::cout << "GlutFramework::setInstance()" << std::endl;
  72.             instance = this;
  73.         }
  74.  
  75.         void setNumberOfSubWindows(unsigned int number) {
  76.            
  77.             if( number <= 0 || number > 2 ) {
  78.                 cout << "Too few or too many sub windows" << endl;
  79.                 return ;
  80.             }
  81.            
  82.             numberOfSubWindows = number;
  83.  
  84.             subWindow = new int[numberOfSubWindows];
  85.         }
  86.  
  87.         unsigned int getNumberOfSubWindows(void ) {
  88.             return numberOfSubWindows;
  89.         }
  90.  
  91.         void setTitle(string name) {
  92.             title = name;
  93.         }
  94.  
  95.         void startFramework(int argc, char *argv[]) {
  96.  
  97.             setInstance();
  98.  
  99.             // Initialize GLUT
  100.             glutInit(&argc, argv);
  101.             glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  102.             glutInitWindowPosition(WINDOW_X_POSITION, WINDOW_Y_POSITION);
  103.             glutInitWindowSize(width, height);
  104.  
  105.             mainWindow = glutCreateWindow(instance->title.c_str());
  106.             glutSetWindow(mainWindow);
  107.  
  108.             // callbacks for main window
  109.             glutDisplayFunc(renderMainWindow);
  110.             glutReshapeFunc(changeSizeWrapper);
  111.             glutIdleFunc(renderSceneAllWrapper);
  112.  
  113.             //subwindows
  114.             int subwindowWidth = (width - numberOfSubWindows * border) / numberOfSubWindows;
  115.             int subWindowHeight = height - border;
  116.  
  117.             for(unsigned int i = 0; i < numberOfSubWindows; i++) {
  118.                 subWindow[i] = glutCreateSubWindow(mainWindow, (i * width / numberOfSubWindows) + (border / 2), border / 2, subwindowWidth, subWindowHeight);
  119.                 glutSetWindow(subWindow[i]);
  120.                 glutDisplayFunc(renderWrapper);
  121.  
  122.                 glEnable(GL_DEPTH_TEST);
  123.                 glEnable(GL_CULL_FACE);
  124.  
  125.                 // register callbacks
  126.                 //glutIgnoreKeyRepeat(1);
  127.                 glutKeyboardFunc(processNormalKeysWrapper);
  128.                 glutSpecialFunc(pressKeyWrapper);
  129.                 glutSpecialUpFunc(releaseKeyWrapper);
  130.                 glutMouseFunc(mouseButtonWrapper);
  131.             }  
  132.  
  133.             glutMainLoop();
  134.         }
  135.  
  136.         static void mouseButtonWrapper(int button, int state, int x, int y) {
  137.             instance->mouseButton(button, state, x, y);
  138.         }
  139.  
  140.         static void mouseMoveWrapper(int x, int y) {
  141.             instance->mouseMove(x, y);
  142.         }
  143.  
  144.         static void releaseKeyWrapper(int key, int x, int y) {
  145.             instance->releaseKey(key, x, y);
  146.         }
  147.  
  148.         static void pressKeyWrapper(int key, int x, int y) {
  149.             instance->pressKey(key, x, y);
  150.         }
  151.  
  152.         static void processNormalKeysWrapper(unsigned char key, int x, int y) {
  153.             instance->processNormalKeys(key, x, y);
  154.         }
  155.  
  156.         void mouseButton(int button, int state, int x, int y) {
  157.  
  158.             //// only start motion if the left button is pressed
  159.             //if (button == GLUT_LEFT_BUTTON) {
  160.  
  161.             //  // when the button is released
  162.             //  if (state == GLUT_UP) {
  163.             //      angle1 += deltaAngle1;
  164.             //      deltaAngle1 = 0.0f;
  165.             //      xOrigin1 = -1;
  166.             //  }
  167.             //  else  {// state = GLUT_DOWN
  168.             //      xOrigin1 = x;
  169.             //  }
  170.             //}
  171.         }
  172.  
  173.         void mouseMove(int x, int y) {
  174.  
  175.             //// this will only be true when the left button is down
  176.             //if (xOrigin2 >= 0) {
  177.  
  178.             //  // update deltaAngle
  179.             //  deltaAngle2 = (x - xOrigin2) * 0.01f;
  180.  
  181.             //  // update camera's direction
  182.             //  xUp2 = sin(angle2 + deltaAngle2);
  183.             //  zUp2 = -cos(angle2 + deltaAngle2);
  184.             //}
  185.         }
  186.  
  187.         void releaseKey(int key, int x, int y) {
  188.  
  189.             /*switch (key) {
  190.                 case GLUT_KEY_UP :
  191.                 case GLUT_KEY_DOWN :
  192.                     deltaMoveUpDown2 = 0;
  193.                     break;
  194.                 case GLUT_KEY_RIGHT :
  195.                 case GLUT_KEY_LEFT :
  196.                     deltaMoveLeftRight2 = 0;
  197.                     break;
  198.             }*/
  199.         }
  200.  
  201.         void pressKey(int key, int xx, int yy) {
  202.             //switch (key) {
  203.             //  case GLUT_KEY_UP :
  204.             //      deltaMoveUpDown2 = 2.0f; break;
  205.             //  case GLUT_KEY_RIGHT :
  206.             //      deltaMoveLeftRight2 = 2.0f; break;
  207.             //  case GLUT_KEY_DOWN :
  208.             //      deltaMoveUpDown2 = -2.0f; break;
  209.             //  case GLUT_KEY_LEFT :
  210.             //      deltaMoveLeftRight2 = -2.0f; break;
  211.             //}
  212.         }
  213.  
  214.         void processNormalKeys(unsigned char key, int xx, int yy) {
  215.  
  216.             switch(key) {
  217.  
  218.                 case 27:               
  219.                         for( unsigned int i = 0; i < numberOfSubWindows; i++) {
  220.                             glutDestroyWindow(subWindow[i]);
  221.                         }
  222.                         glutDestroyWindow(mainWindow);
  223.                         exit(0);
  224.  
  225.                 /*case 'o':
  226.                 case 'O':
  227.                         zoom = zoom  - 0.5;
  228.                         glMatrixMode(GL_PROJECTION);
  229.                         glOrtho(-1.5 + zoom, 1.0 + zoom, -2.0 + zoom, 0.5 + zoom, -1.0, 3.5);
  230.                         break;
  231.  
  232.                 case 'i':
  233.                 case 'I':
  234.                         zoom = zoom  + 0.5;
  235.                         glMatrixMode(GL_PROJECTION);
  236.                         glOrtho(-1.5 + zoom, 1.0 + zoom, -2.0 + zoom, 0.5 + zoom, -1.0, 3.5);
  237.                         break;*/
  238.             }
  239.        
  240.         }
  241.  
  242.         void setLookAt(float eyeX,      float eyeY,     float eyeZ,
  243.                        float centerX,   float centerY,  float centerZ,
  244.                        float upX,       float upY,      float upZ) {       
  245.             this->eyeX = eyeX;
  246.             this->eyeY = eyeX;
  247.             this->eyeY = eyeX;
  248.  
  249.             this->cenX = centerX;
  250.             this->cenY = centerX;
  251.             this->cenZ = centerX;
  252.  
  253.             this->upX = upX;
  254.             this->upY = upX;
  255.             this->upZ = upX;
  256.         }
  257.  
  258.         void invokeGlutLookAt() {
  259.             gluLookAt(eyeX, eyeY, eyeZ, cenX, cenY, cenZ, upX, upY, upZ);
  260.         }
  261.  
  262.         void setWindowContext(unsigned int index) {
  263.  
  264.             if(index < 0 || index >= numberOfSubWindows) {
  265.                 cout << "index out of range" << endl;
  266.             }
  267.  
  268.             glutSetWindow(subWindow[index]);
  269.         }
  270.  
  271.         static void renderWrapper() {
  272.             instance->draw();
  273.             glutPostRedisplay();
  274.         }
  275.  
  276.         static void renderMainWindow() {
  277.             glutSetWindow(instance->mainWindow);
  278.  
  279.             glClearColor(0.9f, 0.9f, 0.90f, 0.0f);
  280.             glClear(GL_COLOR_BUFFER_BIT);
  281.        
  282.             glutSwapBuffers();
  283.             glutPostRedisplay();
  284.         }
  285.  
  286.         virtual void draw() {
  287.             cout << "Base classes draw() functionality" << endl;
  288.         }
  289.  
  290.         static void changeSizeWrapper(int width, int height) {
  291.             instance->changeSize(width, height);
  292.         }
  293.  
  294.         void changeSize(int w1, int h1) {
  295.  
  296.             if(h1 == 0)
  297.                 h1 = 1;
  298.  
  299.             // we're keeping these values cause we'll need them latter
  300.             width = w1;
  301.             height = h1;
  302.  
  303.             // set subwindow 1 as the active window
  304.             glutSetWindow(mainWindow);
  305.             glutReshapeWindow(width, height);
  306.             setProjection(width, height);
  307.  
  308.             int subwindowWidth = (width - numberOfSubWindows * border) / numberOfSubWindows;
  309.             int subWindowHeight = height - border;
  310.  
  311.             for(unsigned int i = 0; i < numberOfSubWindows; i++) {
  312.                 glutSetWindow(subWindow[i]);
  313.  
  314.                 glutPositionWindow((i * width / numberOfSubWindows) + (border / 2), border / 2);
  315.                 glutReshapeWindow(subwindowWidth, subWindowHeight);
  316.                 setProjection(subwindowWidth, subWindowHeight);
  317.             }
  318.         }
  319.  
  320.         void setProjection(int w1, int h1) {
  321.             float ratio = (1.0f * w1) / h1;
  322.  
  323.             glMatrixMode(GL_PROJECTION);
  324.             glLoadIdentity();
  325.  
  326.             glViewport(0, 0, w1, h1);
  327.  
  328.             // Set the clipping volume
  329.             gluPerspective(45, ratio, 1, 1000);
  330.             glMatrixMode(GL_MODELVIEW);
  331.         }
  332.  
  333.         static void renderSceneAllWrapper() {
  334.             instance->renderSceneAll();
  335.         }
  336.  
  337.         void renderSceneAll() {
  338.             //cout << "Idle Function" << endl;
  339.             GLenum x = glGetError() ;
  340.             cout << "OpenGL error: " << gluErrorString(x) << endl;
  341.         }
  342.  
  343.         void draw3DCoordinateAxis() {
  344.            
  345.             char s1[10];
  346.  
  347.             glPushMatrix();
  348.             glColor3f(1.0f, 0.0f, 0.0f);
  349.  
  350.             sprintf(s1, "X-axis");
  351.             renderBitmapString(50.0f, 0.0f, 0.0f, GLUT_BITMAP_HELVETICA_12, s1);
  352.  
  353.             sprintf(s1, "Y-axis");
  354.             renderBitmapString(0.0f, 50.0f, 0.0f, GLUT_BITMAP_HELVETICA_12, s1);
  355.  
  356.             sprintf(s1, "Z-axis");
  357.             renderBitmapString(0.0f, 0.0f, 50.0f, GLUT_BITMAP_HELVETICA_12, s1);
  358.  
  359.             sprintf(s1, "Origin");
  360.             renderBitmapString(0.0f, 0.0f, 0.0f, GLUT_BITMAP_HELVETICA_12, s1);
  361.  
  362.             glPointSize(4.0f);
  363.  
  364.             glColor3f(0.0f, 1.0f, 1.0f);
  365.  
  366.             glBegin(GL_LINE_STRIP);
  367.                 glVertex3f(0.0f, 0.0f, 0.0f);
  368.                 glVertex3f(0.0f, 50.0f, 0.0f);
  369.             glEnd();
  370.  
  371.             glBegin(GL_LINE_STRIP);
  372.                 glVertex3f(0.0f, 0.0f, 0.0f);
  373.                 glVertex3f(50.0f, 0.0f, 0.0f);
  374.             glEnd();
  375.  
  376.             glBegin(GL_LINE_STRIP);
  377.                 glVertex3f(0.0f, 0.0f, 0.0f);
  378.                 glVertex3f(0.0f, 0.0f, 50.0f);
  379.             glEnd();
  380.  
  381.             glPopMatrix();
  382.         }
  383.  
  384.         void renderBitmapString(float x, float y, float z, void *font, char *string) {
  385.  
  386.             char *c;
  387.  
  388.             glRasterPos3f(x, y,z);
  389.  
  390.             for (c=string; *c != '\0'; c++) {
  391.                 glutBitmapCharacter(font, *c);
  392.             }
  393.         }
  394.     };
  395.  
  396.     OpenGLFramework *OpenGLFramework::instance = NULL;
  397.    
  398. } // namespace
  399. #endif
  400.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement