Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __FRAMEWORK_H__
- #define __FRAMEWORK_H__
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #include <vector>
- #include <math.h>
- #include <string.h>
- using namespace std;
- // **Note:** Include GLUT after the standard c++ libraries to prevent linker errors
- #ifdef WINDOWS
- #include <windows.h>
- #include <glut.h>
- #else
- #include <glut.h>
- #endif
- #define WINDOW_X_POSITION 100
- #define WINDOW_Y_POSITION 100
- #define WINDOW_WIDTH 600
- #define WINDOW_HEIGHT 600
- #define BORDER_WIDTH 10
- /** Framework.h
- * @author Aditya Sriram 8-22-10
- */
- namespace openGLFramework {
- class OpenGLFramework {
- private:
- float eyeX, eyeY, eyeZ, cenX, cenY, cenZ, upX, upY, upZ;
- float zoom;
- protected:
- unsigned int numberOfSubWindows;
- int mainWindow, *subWindow;
- int width, height, border;
- float scaleFactor, rotateFactor, translateFactor;
- string title;
- static OpenGLFramework *instance;
- public:
- OpenGLFramework() {
- title = "Sample OpenGL Program";
- numberOfSubWindows = 1;
- instance = NULL;
- zoom = 1;
- width = WINDOW_WIDTH;
- height = WINDOW_HEIGHT;
- border = BORDER_WIDTH;
- scaleFactor = 1.5f;
- rotateFactor = 5.0f;
- translateFactor = 2.0f;
- }
- virtual ~OpenGLFramework() {
- cout << "Program Exited" << endl;
- }
- void setInstance() {
- //std::cout << "GlutFramework::setInstance()" << std::endl;
- instance = this;
- }
- void setNumberOfSubWindows(unsigned int number) {
- if( number <= 0 || number > 2 ) {
- cout << "Too few or too many sub windows" << endl;
- return ;
- }
- numberOfSubWindows = number;
- subWindow = new int[numberOfSubWindows];
- }
- unsigned int getNumberOfSubWindows(void ) {
- return numberOfSubWindows;
- }
- void setTitle(string name) {
- title = name;
- }
- void startFramework(int argc, char *argv[]) {
- setInstance();
- // Initialize GLUT
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
- glutInitWindowPosition(WINDOW_X_POSITION, WINDOW_Y_POSITION);
- glutInitWindowSize(width, height);
- mainWindow = glutCreateWindow(instance->title.c_str());
- glutSetWindow(mainWindow);
- // callbacks for main window
- glutDisplayFunc(renderMainWindow);
- glutReshapeFunc(changeSizeWrapper);
- glutIdleFunc(renderSceneAllWrapper);
- //subwindows
- int subwindowWidth = (width - numberOfSubWindows * border) / numberOfSubWindows;
- int subWindowHeight = height - border;
- for(unsigned int i = 0; i < numberOfSubWindows; i++) {
- subWindow[i] = glutCreateSubWindow(mainWindow, (i * width / numberOfSubWindows) + (border / 2), border / 2, subwindowWidth, subWindowHeight);
- glutSetWindow(subWindow[i]);
- glutDisplayFunc(renderWrapper);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_CULL_FACE);
- // register callbacks
- //glutIgnoreKeyRepeat(1);
- glutKeyboardFunc(processNormalKeysWrapper);
- glutSpecialFunc(pressKeyWrapper);
- glutSpecialUpFunc(releaseKeyWrapper);
- glutMouseFunc(mouseButtonWrapper);
- }
- glutMainLoop();
- }
- static void mouseButtonWrapper(int button, int state, int x, int y) {
- instance->mouseButton(button, state, x, y);
- }
- static void mouseMoveWrapper(int x, int y) {
- instance->mouseMove(x, y);
- }
- static void releaseKeyWrapper(int key, int x, int y) {
- instance->releaseKey(key, x, y);
- }
- static void pressKeyWrapper(int key, int x, int y) {
- instance->pressKey(key, x, y);
- }
- static void processNormalKeysWrapper(unsigned char key, int x, int y) {
- instance->processNormalKeys(key, x, y);
- }
- void mouseButton(int button, int state, int x, int y) {
- //// only start motion if the left button is pressed
- //if (button == GLUT_LEFT_BUTTON) {
- // // when the button is released
- // if (state == GLUT_UP) {
- // angle1 += deltaAngle1;
- // deltaAngle1 = 0.0f;
- // xOrigin1 = -1;
- // }
- // else {// state = GLUT_DOWN
- // xOrigin1 = x;
- // }
- //}
- }
- void mouseMove(int x, int y) {
- //// this will only be true when the left button is down
- //if (xOrigin2 >= 0) {
- // // update deltaAngle
- // deltaAngle2 = (x - xOrigin2) * 0.01f;
- // // update camera's direction
- // xUp2 = sin(angle2 + deltaAngle2);
- // zUp2 = -cos(angle2 + deltaAngle2);
- //}
- }
- void releaseKey(int key, int x, int y) {
- /*switch (key) {
- case GLUT_KEY_UP :
- case GLUT_KEY_DOWN :
- deltaMoveUpDown2 = 0;
- break;
- case GLUT_KEY_RIGHT :
- case GLUT_KEY_LEFT :
- deltaMoveLeftRight2 = 0;
- break;
- }*/
- }
- void pressKey(int key, int xx, int yy) {
- //switch (key) {
- // case GLUT_KEY_UP :
- // deltaMoveUpDown2 = 2.0f; break;
- // case GLUT_KEY_RIGHT :
- // deltaMoveLeftRight2 = 2.0f; break;
- // case GLUT_KEY_DOWN :
- // deltaMoveUpDown2 = -2.0f; break;
- // case GLUT_KEY_LEFT :
- // deltaMoveLeftRight2 = -2.0f; break;
- //}
- }
- void processNormalKeys(unsigned char key, int xx, int yy) {
- switch(key) {
- case 27:
- for( unsigned int i = 0; i < numberOfSubWindows; i++) {
- glutDestroyWindow(subWindow[i]);
- }
- glutDestroyWindow(mainWindow);
- exit(0);
- /*case 'o':
- case 'O':
- zoom = zoom - 0.5;
- glMatrixMode(GL_PROJECTION);
- glOrtho(-1.5 + zoom, 1.0 + zoom, -2.0 + zoom, 0.5 + zoom, -1.0, 3.5);
- break;
- case 'i':
- case 'I':
- zoom = zoom + 0.5;
- glMatrixMode(GL_PROJECTION);
- glOrtho(-1.5 + zoom, 1.0 + zoom, -2.0 + zoom, 0.5 + zoom, -1.0, 3.5);
- break;*/
- }
- }
- void setLookAt(float eyeX, float eyeY, float eyeZ,
- float centerX, float centerY, float centerZ,
- float upX, float upY, float upZ) {
- this->eyeX = eyeX;
- this->eyeY = eyeX;
- this->eyeY = eyeX;
- this->cenX = centerX;
- this->cenY = centerX;
- this->cenZ = centerX;
- this->upX = upX;
- this->upY = upX;
- this->upZ = upX;
- }
- void invokeGlutLookAt() {
- gluLookAt(eyeX, eyeY, eyeZ, cenX, cenY, cenZ, upX, upY, upZ);
- }
- void setWindowContext(unsigned int index) {
- if(index < 0 || index >= numberOfSubWindows) {
- cout << "index out of range" << endl;
- }
- glutSetWindow(subWindow[index]);
- }
- static void renderWrapper() {
- instance->draw();
- glutPostRedisplay();
- }
- static void renderMainWindow() {
- glutSetWindow(instance->mainWindow);
- glClearColor(0.9f, 0.9f, 0.90f, 0.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- glutSwapBuffers();
- glutPostRedisplay();
- }
- virtual void draw() {
- cout << "Base classes draw() functionality" << endl;
- }
- static void changeSizeWrapper(int width, int height) {
- instance->changeSize(width, height);
- }
- void changeSize(int w1, int h1) {
- if(h1 == 0)
- h1 = 1;
- // we're keeping these values cause we'll need them latter
- width = w1;
- height = h1;
- // set subwindow 1 as the active window
- glutSetWindow(mainWindow);
- glutReshapeWindow(width, height);
- setProjection(width, height);
- int subwindowWidth = (width - numberOfSubWindows * border) / numberOfSubWindows;
- int subWindowHeight = height - border;
- for(unsigned int i = 0; i < numberOfSubWindows; i++) {
- glutSetWindow(subWindow[i]);
- glutPositionWindow((i * width / numberOfSubWindows) + (border / 2), border / 2);
- glutReshapeWindow(subwindowWidth, subWindowHeight);
- setProjection(subwindowWidth, subWindowHeight);
- }
- }
- void setProjection(int w1, int h1) {
- float ratio = (1.0f * w1) / h1;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glViewport(0, 0, w1, h1);
- // Set the clipping volume
- gluPerspective(45, ratio, 1, 1000);
- glMatrixMode(GL_MODELVIEW);
- }
- static void renderSceneAllWrapper() {
- instance->renderSceneAll();
- }
- void renderSceneAll() {
- //cout << "Idle Function" << endl;
- GLenum x = glGetError() ;
- cout << "OpenGL error: " << gluErrorString(x) << endl;
- }
- void draw3DCoordinateAxis() {
- char s1[10];
- glPushMatrix();
- glColor3f(1.0f, 0.0f, 0.0f);
- sprintf(s1, "X-axis");
- renderBitmapString(50.0f, 0.0f, 0.0f, GLUT_BITMAP_HELVETICA_12, s1);
- sprintf(s1, "Y-axis");
- renderBitmapString(0.0f, 50.0f, 0.0f, GLUT_BITMAP_HELVETICA_12, s1);
- sprintf(s1, "Z-axis");
- renderBitmapString(0.0f, 0.0f, 50.0f, GLUT_BITMAP_HELVETICA_12, s1);
- sprintf(s1, "Origin");
- renderBitmapString(0.0f, 0.0f, 0.0f, GLUT_BITMAP_HELVETICA_12, s1);
- glPointSize(4.0f);
- glColor3f(0.0f, 1.0f, 1.0f);
- glBegin(GL_LINE_STRIP);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(0.0f, 50.0f, 0.0f);
- glEnd();
- glBegin(GL_LINE_STRIP);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(50.0f, 0.0f, 0.0f);
- glEnd();
- glBegin(GL_LINE_STRIP);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(0.0f, 0.0f, 50.0f);
- glEnd();
- glPopMatrix();
- }
- void renderBitmapString(float x, float y, float z, void *font, char *string) {
- char *c;
- glRasterPos3f(x, y,z);
- for (c=string; *c != '\0'; c++) {
- glutBitmapCharacter(font, *c);
- }
- }
- };
- OpenGLFramework *OpenGLFramework::instance = NULL;
- } // namespace
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement