NeHe's Tutorial als Klasse



  • Hey @all,
    ich habe mir mal ein Beispiel von NeHe genommen, und wollte dies von C nach C++ konvertieren (also mit klassen) jedoch funktioniert das nicht so wirklich, kann mir da jemand bitte Helfen?

    Original - Code (Main.c):

    #include <stdio.h>
    #include <stdlib.h>
    #include <GL/gl.h>              // Header File For The OpenGL32 Library
    #include <GL/glu.h>             // Header File For The GLu32 Library
    #include <GL/glut.h>            // Header File For GL Utility Toolkit
    
    #define WINDOW_WIDTH  800       // We want our screen width 800 pixels
    #define WINDOW_HEIGHT 600       // We want our screen height 600 pixels
    #define WINDOW_XPOS 100         // We want the window to appear at x = 100
    #define WINDOW_YPOS 100         // We want the window to appear at y = 100
    #define SCREEN_DEPTH  16        // We want 16 bits per pixel
    #define ESCAPE 27               // Ascii value for the escape key
    
    void registerCallbacks( void );
    
    int window;                     // This is an identifier for our window
    int fullscreen = 0;             // Whether the window is fullscreen now or not
    
    int winXPos = 0;                // X pos of the window
    int winYPos = 0;                // Y pos of the window
    int winWidth = 0;               // width of the window
    int winHeight = 0;              // height of the window
    
    void sizeOpenGLScreen( int width, int height ) {
        // Prevent a divide by 0 error
        if ( height == 0 ) {
            height = 1;
        }
    
        glViewport( 0, 0, width, height );
    
        glMatrixMode( GL_PROJECTION );      // Select The Projection Matrix
        glLoadIdentity();                   // Reset The Projection Matrix
    
        gluPerspective( 45.0f, ( GLfloat ) width / ( GLfloat ) height, 1, 150.0f );
    
        glMatrixMode( GL_MODELVIEW );       // Select The Modelview Matrix
        glLoadIdentity();                   // Reset The Modelview Matrix
    }
    
    void initializeOpenGL( int width, int height ) {
        // resize the OpenGL Viewport to the given height and width
        sizeOpenGLScreen( width, height );
    
        // enable double buffering
        glEnable( GL_DOUBLEBUFFER );
    
        // We aren't going to use the stencil buffer.  This is set to false by
        // default, but this line was included to stay as close as possible to
        // the tutorials written in other languages
        glDisable( GL_STENCIL_TEST );
    
        // Clears the accumulation buffer's RGBA values to 0
        glClearAccum( 0.0, 0.0, 0.0, 0.0 );
    }
    
    void init( void ) {
        initializeOpenGL( WINDOW_WIDTH, WINDOW_HEIGHT ); // Initialize openGL
    
        // *Hint* We will put all our game init stuff here
        // Some things include loading models, textures and network initialization
    }
    
    void toggleFullScreen( void ) {
        if ( fullscreen == 0 ) {
            // Store window size / position values to reposition it later
            winXPos = glutGet( ( GLenum ) GLUT_WINDOW_X );
            winYPos = glutGet( ( GLenum ) GLUT_WINDOW_Y );
            winWidth = glutGet( ( GLenum ) GLUT_WINDOW_WIDTH );
            winHeight = glutGet( ( GLenum ) GLUT_WINDOW_HEIGHT );
    
            // Switch to fullscreen
            glutFullScreen();
        } else { 
            // Switch back to windowed mode
            glutDestroyWindow( window );
            window = 
    	    glutCreateWindow( "www.GameTutorials.com - First OpenGL Program" );
    
            // resize and position our window 
    	glutReshapeWindow( winWidth, winHeight );
    	glutPositionWindow( WINDOW_XPOS, WINDOW_YPOS );
    
    	registerCallbacks();
        }
    
        fullscreen = !fullscreen;
    }
    
    void handleNormalKeys( unsigned char key, int x, int y ) {
        switch( key ) {                       // which key have we got
            case ESCAPE:                      // if it is ESCAPE
    	    glutDestroyWindow( window );  // Kill the current window
                exit( 0 );                    // quit after cleaning up
    	    break;
            default:                          // any other key
                break;                        // nothing to do
        }
    }
    
    void handleSpecialKeys( int key, int x, int y ) {
        switch( key ) {                       // which key have we got
            case GLUT_KEY_F1:                 // if it's F1
    	    toggleFullScreen();           // toggle fullscreen
    	    break;
    	default:                          // any other key
    	    break;                        // nothing to do
        }
    }
    
    void renderScene( void ) {
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glLoadIdentity();
        gluLookAt(0, 0, 6, 0, 0, 0, 0, 1, 0);
        glBegin (GL_TRIANGLES);       // This is where we begin drawing triangles 
            glVertex3f(0, 1, 0);      // Here is the top point of the triangle
            glVertex3f(-1, 0, 0);     // Here is the left point
            glVertex3f(1, 0, 0);      // Here is the right point
        glEnd();                      // This is the END of drawing
        glutSwapBuffers();
    }
    
    void registerCallbacks( void ) {
        // Register our callback functions
        glutKeyboardFunc( handleNormalKeys );
        glutSpecialFunc( handleSpecialKeys );
        glutDisplayFunc( renderScene );
        glutReshapeFunc( sizeOpenGLScreen );
    }
    
    int main( int argc, char** argv ) {
        printf( " Hit the F1 key to Toggle between Fullscreen and windowed mode" );
        printf( " \nHit ESC to quit\n" );
        glutInit( &argc, argv );
        glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
        glutInitWindowSize( WINDOW_WIDTH, WINDOW_HEIGHT );
        glutInitWindowPosition( WINDOW_XPOS, WINDOW_YPOS );
        window = glutCreateWindow( "www.GameTutorials.com - First OpenGL Program" );
        init();
        registerCallbacks();
        glutMainLoop();
        return 0;
    }
    

    Abgewandelte Dateien (triangle.h):

    #include <iostream>
    #include <GL/gl.h>              // Header File For The OpenGL32 Library
    #include <GL/glu.h>             // Header File For The GLu32 Library
    #include <GL/glut.h>            // Header File For GL Utility Toolkit
    
    #define WINDOW_WIDTH  800       // We want our screen width 800 pixels
    #define WINDOW_HEIGHT 600       // We want our screen height 600 pixels
    #define WINDOW_XPOS 100         // We want the window to appear at x = 100
    #define WINDOW_YPOS 100         // We want the window to appear at y = 100
    #define SCREEN_DEPTH  16        // We want 16 bits per pixel
    #define ESCAPE 27               // Ascii value for the escape key
    
    class OpenGL {
      private:
        int fullscreen, winXPos, winYPos, winWidth, winHeight;
    
      public:
        OpenGL();
        ~OpenGL();
        int window;
        void init();
        void initialize(int width, int height);
        void toggleFullScreen();
        void* handleNormalKeys(unsigned char key, int x, int y);
        void handleSpecialKeys(int key, int x, int y);
        void renderScene();
        void registerCallbacks();
        void sizeOpenGLScreen(int width, int height);
    };
    

    Abgewandelte Dateien (triangle.cpp):

    #include "triangle.h"
    OpenGL::OpenGL() {
    //  int window;                     // This is an identifier for our window
      int fullscreen = 0;             // Whether the window is fullscreen now or not
    
      int winXPos = 0;                // X pos of the window
      int winYPos = 0;                // Y pos of the window
      int winWidth = 0;               // width of the window
      int winHeight = 0;              // height of the window
    
    }
    
    OpenGL::~OpenGL() {
    }
    
    void OpenGL::sizeOpenGLScreen(int width, int height) {
        if ( height == 0 ) {
            height = 1;
        }
        glViewport( 0, 0, width, height );
        glMatrixMode( GL_PROJECTION );      // Select The Projection Matrix
        glLoadIdentity();                   // Reset The Projection Matrix
        gluPerspective( 45.0f, ( GLfloat ) width / ( GLfloat ) height, 1, 150.0f );
        glMatrixMode( GL_MODELVIEW );       // Select The Modelview Matrix
        glLoadIdentity();                   // Reset The Modelview Matrix
    }
    
    void OpenGL::initialize(int width, int height) {
        sizeOpenGLScreen( width, height );
        glEnable( GL_DOUBLEBUFFER );
        glDisable( GL_STENCIL_TEST );
        glClearAccum( 0.0, 0.0, 0.0, 0.0 );
    }
    
    void OpenGL::init() {
        initialize(WINDOW_WIDTH, WINDOW_HEIGHT); // Initialize openGL
    }
    
    void OpenGL::toggleFullScreen() {
        if ( fullscreen == 0 ) {
            winXPos = glutGet( ( GLenum ) GLUT_WINDOW_X );
            winYPos = glutGet( ( GLenum ) GLUT_WINDOW_Y );
            winWidth = glutGet( ( GLenum ) GLUT_WINDOW_WIDTH );
            winHeight = glutGet( ( GLenum ) GLUT_WINDOW_HEIGHT );
    
            glutFullScreen();
        } else { 
            glutDestroyWindow( window );
            window = glutCreateWindow( "www.GameTutorials.com - First OpenGL Program" );
    	glutReshapeWindow( winWidth, winHeight );
    	glutPositionWindow( WINDOW_XPOS, WINDOW_YPOS );
    	registerCallbacks();
        }
        fullscreen = !fullscreen;
    }
    
    void* OpenGL::handleNormalKeys(unsigned char key, int x, int y) {
        switch( key ) {                       // which key have we got
            case ESCAPE:                      // if it is ESCAPE
    	    glutDestroyWindow( window );  // Kill the current window
                exit( 0 );                    // quit after cleaning up
    	    break;
            default:                          // any other key
                break;                        // nothing to do
        }
        return 0;
    }
    
    void OpenGL::handleSpecialKeys(int key, int x, int y) {
        switch( key ) {                       // which key have we got
            case GLUT_KEY_F1:                 // if it's F1
    	    toggleFullScreen();           // toggle fullscreen
    	    break;
    	default:                          // any other key
    	    break;                        // nothing to do
        }
    }
    
    void OpenGL::renderScene() {
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glLoadIdentity();
        gluLookAt(0, 0, 6, 0, 0, 0, 0, 1, 0);
        glBegin (GL_TRIANGLES);       // This is where we begin drawing triangles 
            glVertex3f(0, 1, 0);      // Here is the top point of the triangle
            glVertex3f(-1, 0, 0);     // Here is the left point
            glVertex3f(1, 0, 0);      // Here is the right point
        glEnd();                      // This is the END of drawing
        glutSwapBuffers();
    }
    
    void OpenGL::registerCallbacks() {
        glutKeyboardFunc(handleNormalKeys);
        glutSpecialFunc(handleSpecialKeys);
        glutDisplayFunc(renderScene);
        glutReshapeFunc(sizeOpenGLScreen);
    }
    

    Abgewandelte Dateien (triangleMain.cpp):

    #include <iostream>
    #include "triangleImpl.h"
    #include "triangle.h"
    #include <GL/gl.h>              // Header File For The OpenGL32 Library
    #include <GL/glu.h>             // Header File For The GLu32 Library
    #include <GL/glut.h>            // Header File For GL Utility Toolkit
    
    using namespace std;
    
    int main( int argc, char** argv ) {
        OpenGL *myOpenGL = new OpenGL();
        cout << "Hit the F1 key to Toggle between Fullscreen and windowed mode" << endl;
        cout << "Hit ESC to quit" << endl;
        glutInit( &argc, argv );
        glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
        glutInitWindowSize( WINDOW_WIDTH, WINDOW_HEIGHT );
        glutInitWindowPosition( WINDOW_XPOS, WINDOW_YPOS );
        myOpenGL->window = glutCreateWindow( "www.GameTutorials.com - First OpenGL Program" );
        myOpenGL->init();
        myOpenGL->registerCallbacks();
        glutMainLoop();
        return 0;
    }
    

    Nun, wenn ich meine abgewandelten dateien kompiliere, erscheint immer folgende Fehlermeldung:

    triangle.cpp: In member function `void OpenGL::registerCallbacks()':
    triangle.cpp:92: no matches converting function `handleNormalKeys' to type `
       void (*)(unsigned char, int, int)'
    triangle.cpp:57: candidates are: void* OpenGL::handleNormalKeys(unsigned char,
       int, int)
    triangle.cpp:93: no matches converting function `handleSpecialKeys' to type `
       void (*)(int, int, int)'
    triangle.cpp:69: candidates are: void OpenGL::handleSpecialKeys(int, int, int)
    triangle.cpp:94: no matches converting function `renderScene' to type `void
       (*)()'
    triangle.cpp:79: candidates are: void OpenGL::renderScene()
    triangle.cpp:95: no matches converting function `sizeOpenGLScreen' to type `
       void (*)(int, int)'
    triangle.cpp:16: candidates are: void OpenGL::sizeOpenGLScreen(int, int)
    

    Vielen Dank im Voraus, und sorry, für die langen Texte.

    MfG smog_at


  • Mod

    du kannst nur c funktionen regestrieren, keine funktionen in klassen (c++).

    z.B.

    handleNormalKeys

    rapso->greets();



  • Kann man sonst irgendwie OpenGL mit C++ nutzen (Plattform unabhängig)

    MfG smog_at


  • Mod

    klar sollte nichts dagegen sprechen, gibt ja viele wrapper.
    siehe Unreal engine, siehe Quake3A engine,... zudem gibt es einige spiele die das machen.

    rapso->greets();



  • Hätte zufällig jemand eine Idee oder einen Link, stehe momentan voll auf der leitung. Ich dachte für OpenGL genügt GLX oder SDL, usw.? was muss man nun genau können? Kann mir da jemand helfen?

    MfG smog_at


  • Mod

    ja, hast richtig geglaubt, sdl würde vollkommen reichen, ich sehe irgendwie dein problem nicht... ?

    rapso->greets();



  • Mach die Funcs, die du registrierst Static. Wenn der Compiler immer noch Warnings oder Errors ausspucken tut ( zumindest Errors sollte er nicht ), dann mach einen Functioncast.


Anmelden zum Antworten