Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Problems with C++ and OpenGL

  1. #11
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,240
    Distro
    Kubuntu

    Re: Problems with C++ and OpenGL

    Yep, this works for me:
    Code:
    #include <iostream>
    
    void glutDisplayFunc(void (*func)(void))
    {
      func();
    }
    
    class Foo
    {
      public:
        static void function(void) { std::cout << "Foo::function() called." << std::endl; }
    };
    
    int main()
    {
      glutDisplayFunc(Foo::function);
    }
    It appears that you need to remove the '&' from the parameter that you are passing glutDisplayFunc() and other OpenGL functions.
    Last edited by dwhitney67; March 1st, 2009 at 04:41 PM.

  2. #12
    Join Date
    Aug 2008
    Location
    Kaunas, Lithuania
    Beans
    18
    Distro
    Ubuntu UNR

    Re: Problems with C++ and OpenGL

    So in the end it seems I have to make the functions static.

    Any advise for using global variables in static functions?

    making variables static will prevent changing their values, right?

    And i cannot change the function arguments. I simply hit a roadblock.

    I tried using a simple test with static for global variables (Used your code):
    Code:
    #include <iostream>
    
    void glutDisplayFunc(void (*func)(void))
    {
      func();
    }
    
    class Foo
    {
      public:
        Foo();
        static char *a;
        static void function(void) { std::cout << "Foo::function() called." << Foo::a << std::endl; }
    };
    
    Foo::Foo(){a = "word";}
    
    int main()
    {
      glutDisplayFunc(&Foo::function);
    }
    And what the compiler says, I can't even understand
    Code:
    $ g++ test.c++ -o t
    test.c++: In constructor ‘Foo::Foo()’:
    test.c++:16: warning: deprecated conversion from string constant to ‘char*’
    /tmp/ccG7WunV.o: In function `Foo::Foo()':
    test.c++:(.text+0x17): undefined reference to `Foo::a'
    /tmp/ccG7WunV.o: In function `Foo::Foo()':
    test.c++:(.text+0x27): undefined reference to `Foo::a'
    /tmp/ccG7WunV.o: In function `Foo::function()':
    test.c++:(.text._ZN3Foo8functionEv[Foo::function()]+0x9): undefined reference to `Foo::a'
    collect2: ld returned 1 exit status
    Now if i figure this out, I might do my project in c++. And I will also have to make the same thing on java.
    Last edited by Gediminas2; March 1st, 2009 at 05:47 PM.

  3. #13
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,240
    Distro
    Kubuntu

    Re: Problems with C++ and OpenGL

    A variable that is declared 'static' can have its value changed; now if it is also declared 'const', then you are out of luck because it is read-only. But with plain-ol' 'static', your variable is read-writable.

    Now to your augmented example, and in the interest of brevity... The class is fine, however you do not require a constructor because there is nothing to construct. All of your function (err, methods) are static, as will be the data contain within the class.

    So something like:
    Code:
    class Foo
    {
      public:
        static void function() { ... }
    
      private:
        // declaration only
        static char* a;
    };
    
    // definition and initialization of static variable(s)
    char* Foo::a = 0;
    
    int main()
    {
      glutDisplayFunc(Foo::function);   // Note, the '&' is NOT required.
    }

    P.S. Earlier you asked about namespaces; here's an example:
    Code:
    namespace Foo
    {
      char* a = 0;
    
      void function()
      {
        // If implementing function in namespace, declaration of 'a' must
        // precede this function if it is to be used.
        std::cout << "value of a is "
                  << (a ? "non-zero" : "null")
                  << std::endl;
      }
    }
    Here, the data must be declared before the function uses it. There is no stead-fast rule that implies that one must implement their function within a class declaration or within a namespace. You can implement the function pretty much anywhere as long as the declaration has been made available.

    So,
    Code:
    namespace Foo
    {
      void function();
      char* a;
    }
    
    void Foo::function()
    {
      // access to 'a' is alright, even though it is declared after the
      // function in the namespace definition.
      ...
    }

  4. #14
    Join Date
    Aug 2008
    Location
    Kaunas, Lithuania
    Beans
    18
    Distro
    Ubuntu UNR

    Re: Problems with C++ and OpenGL

    It took me quite long to figure out that after i make my functions static, the errors I get are not because of MY code, the references to OpenGL functions and variables are undefined within my static functions (But I included the OpenGL headers).

    How do I fix it? (If it's even fixable)

    Thanks in advance,
    Gedas

  5. #15
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,240
    Distro
    Kubuntu

    Re: Problems with C++ and OpenGL

    Quote Originally Posted by Gediminas2 View Post
    It took me quite long to figure out that after i make my functions static, the errors I get are not because of MY code, the references to OpenGL functions and variables are undefined within my static functions (But I included the OpenGL headers).

    How do I fix it? (If it's even fixable)

    Thanks in advance,
    Gedas
    Are these runtime errors or compiler errors? If the former, I have a hard time believing OpenGL would have a problem with static methods and/or variables.

    Either way, I cannot comment too much because you have not been explicit in what errors you are getting, and furthermore you have not posted any code since your earlier postings.

  6. #16
    Join Date
    Aug 2008
    Location
    Kaunas, Lithuania
    Beans
    18
    Distro
    Ubuntu UNR

    Re: Problems with C++ and OpenGL

    Quote Originally Posted by dwhitney67 View Post
    Are these runtime errors or compiler errors? If the former, I have a hard time believing OpenGL would have a problem with static methods and/or variables.

    Either way, I cannot comment too much because you have not been explicit in what errors you are getting, and furthermore you have not posted any code since your earlier postings.
    I can't really say what kind of error this is, but this is the g++ output
    Code:
    gedas@gedas-desktop:~/OGL/planetos$ g++ PlanetGL.c++
    PlanetGL.c++: In function ‘void PlanetGL::DrawGLScene()’:
    PlanetGL.c++:241: warning: deprecated conversion from string constant to ‘char*’
    PlanetGL.c++:242: warning: deprecated conversion from string constant to ‘char*’
    PlanetGL.c++:244: warning: deprecated conversion from string constant to ‘char*’
    PlanetGL.c++:247: warning: deprecated conversion from string constant to ‘char*’
    PlanetGL.c++:250: warning: deprecated conversion from string constant to ‘char*’
    PlanetGL.c++:253: warning: deprecated conversion from string constant to ‘char*’
    PlanetGL.c++:254: warning: deprecated conversion from string constant to ‘char*’
    PlanetGL.c++:276: warning: deprecated conversion from string constant to ‘char*’
    /usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/crt1.o: In function `_start':
    (.text+0x18): undefined reference to `main'
    /tmp/ccizofFG.o: In function `PlanetGL::keyPressed(unsigned char, int, int)':
    PlanetGL.c++:(.text+0x18b): undefined reference to `glutDestroyWindow'
    PlanetGL.c++:(.text+0x1e8): undefined reference to `glDisable'
    PlanetGL.c++:(.text+0x1f9): undefined reference to `glEnable'
    PlanetGL.c++:(.text+0x29f): undefined reference to `glDisable'
    PlanetGL.c++:(.text+0x2ab): undefined reference to `glEnable'
    PlanetGL.c++:(.text+0x2bc): undefined reference to `glEnable'
    PlanetGL.c++:(.text+0x2c8): undefined reference to `glDisable'
    /tmp/ccizofFG.o: In function `ReSizeGLScene(int, int)':
    PlanetGL.c++:(.text+0x687): undefined reference to `glViewport'
    PlanetGL.c++:(.text+0x693): undefined reference to `glMatrixMode'
    PlanetGL.c++:(.text+0x698): undefined reference to `glLoadIdentity'
    PlanetGL.c++:(.text+0x6c6): undefined reference to `gluPerspective'
    PlanetGL.c++:(.text+0x6d2): undefined reference to `glMatrixMode'
    /tmp/ccizofFG.o: In function `PlanetGL::LoadGLTextures()':
    PlanetGL.c++:(.text+0xb6d): undefined reference to `glGenTextures'
    PlanetGL.c++:(.text+0xb99): undefined reference to `glBindTexture'
    PlanetGL.c++:(.text+0xbb5): undefined reference to `glTexParameteri'
    PlanetGL.c++:(.text+0xbd1): undefined reference to `glTexParameteri'
    PlanetGL.c++:(.text+0xc42): undefined reference to `glTexImage2D'
    /tmp/ccizofFG.o: In function `PlanetGL::glPrint(char*)':
    PlanetGL.c++:(.text+0xccf): undefined reference to `glPushAttrib'
    PlanetGL.c++:(.text+0xcdf): undefined reference to `glListBase'
    PlanetGL.c++:(.text+0xd03): undefined reference to `glCallLists'
    PlanetGL.c++:(.text+0xd08): undefined reference to `glPopAttrib'
    /tmp/ccizofFG.o: In function `PlanetGL::DrawGLScene()':
    PlanetGL.c++:(.text+0xd1c): undefined reference to `glClear'
    PlanetGL.c++:(.text+0xd21): undefined reference to `glLoadIdentity'
    PlanetGL.c++:(.text+0x1121): undefined reference to `glTranslatef'
    PlanetGL.c++:(.text+0x115e): undefined reference to `glRasterPos2f'
    PlanetGL.c++:(.text+0x11b3): undefined reference to `glRasterPos2f'
    PlanetGL.c++:(.text+0x11f0): undefined reference to `glRasterPos2f'
    PlanetGL.c++:(.text+0x122f): undefined reference to `glRasterPos2f'
    PlanetGL.c++:(.text+0x126f): undefined reference to `glRasterPos2f'
    PlanetGL.c++:(.text+0x12bd): undefined reference to `glRotatef'
    PlanetGL.c++:(.text+0x12e6): undefined reference to `glRotatef'
    PlanetGL.c++:(.text+0x12fb): undefined reference to `glBindTexture'
    PlanetGL.c++:(.text+0x1322): undefined reference to `gluSphere'
    PlanetGL.c++:(.text+0x1337): undefined reference to `glBindTexture'
    PlanetGL.c++:(.text+0x13b3): undefined reference to `glTranslatef'
    PlanetGL.c++:(.text+0x13da): undefined reference to `gluSphere'
    PlanetGL.c++:(.text+0x1456): undefined reference to `glTranslatef'
    PlanetGL.c++:(.text+0x14d6): undefined reference to `glRasterPos2f'
    PlanetGL.c++:(.text+0x1545): undefined reference to `glTranslatef'
    PlanetGL.c++:(.text+0x157a): undefined reference to `gluDisk'
    PlanetGL.c++:(.text+0x15c3): undefined reference to `glTranslatef'
    PlanetGL.c++:(.text+0x16dc): undefined reference to `glTranslatef'
    PlanetGL.c++:(.text+0x16e1): undefined reference to `glutSwapBuffers'
    /tmp/ccizofFG.o: In function `PlanetGL::KillFont()':
    PlanetGL.c++:(.text+0x16fe): undefined reference to `glDeleteLists'
    /tmp/ccizofFG.o: In function `PlanetGL::BuildFont()':
    PlanetGL.c++:(.text+0x1712): undefined reference to `glGenLists'
    PlanetGL.c++:(.text+0x1723): undefined reference to `XOpenDisplay'
    PlanetGL.c++:(.text+0x1739): undefined reference to `XLoadQueryFont'
    PlanetGL.c++:(.text+0x1755): undefined reference to `XLoadQueryFont'
    PlanetGL.c++:(.text+0x1793): undefined reference to `glXUseXFont'
    PlanetGL.c++:(.text+0x17a5): undefined reference to `XFreeFont'
    PlanetGL.c++:(.text+0x17b0): undefined reference to `XCloseDisplay'
    /tmp/ccizofFG.o: In function `PlanetGL::InitGL(int, int)':
    PlanetGL.c++:(.text+0x17c9): undefined reference to `glEnable'
    PlanetGL.c++:(.text+0x17f1): undefined reference to `glClearColor'
    PlanetGL.c++:(.text+0x17fb): undefined reference to `glClearDepth'
    PlanetGL.c++:(.text+0x1807): undefined reference to `glDepthFunc'
    PlanetGL.c++:(.text+0x1813): undefined reference to `glEnable'
    PlanetGL.c++:(.text+0x181f): undefined reference to `glShadeModel'
    PlanetGL.c++:(.text+0x182b): undefined reference to `glMatrixMode'
    PlanetGL.c++:(.text+0x1830): undefined reference to `glLoadIdentity'
    PlanetGL.c++:(.text+0x185e): undefined reference to `gluPerspective'
    PlanetGL.c++:(.text+0x186a): undefined reference to `glMatrixMode'
    PlanetGL.c++:(.text+0x188c): undefined reference to `glLightfv'
    PlanetGL.c++:(.text+0x18a9): undefined reference to `glLightfv'
    PlanetGL.c++:(.text+0x18c6): undefined reference to `glLightfv'
    PlanetGL.c++:(.text+0x18d2): undefined reference to `glEnable'
    PlanetGL.c++:(.text+0x18e6): undefined reference to `glBlendFunc'
    PlanetGL.c++:(.text+0x190e): undefined reference to `glColor4f'
    PlanetGL.c++:(.text+0x1913): undefined reference to `gluNewQuadric'
    PlanetGL.c++:(.text+0x192d): undefined reference to `gluQuadricNormals'
    PlanetGL.c++:(.text+0x1942): undefined reference to `gluQuadricTexture'
    /tmp/ccizofFG.o: In function `Main::main(int, char**)':
    PlanetGL.c++:(.text+0x2007): undefined reference to `glutInit'
    PlanetGL.c++:(.text+0x2013): undefined reference to `glutInitDisplayMode'
    PlanetGL.c++:(.text+0x2027): undefined reference to `glutInitWindowSize'
    PlanetGL.c++:(.text+0x203b): undefined reference to `glutInitWindowPosition'
    PlanetGL.c++:(.text+0x2047): undefined reference to `glutCreateWindow'
    PlanetGL.c++:(.text+0x2058): undefined reference to `glutDisplayFunc'
    PlanetGL.c++:(.text+0x205d): undefined reference to `glutFullScreen'
    PlanetGL.c++:(.text+0x2069): undefined reference to `glutIdleFunc'
    PlanetGL.c++:(.text+0x2070): undefined reference to `PlanetGL::ReSizeGLScene(int, int)'
    PlanetGL.c++:(.text+0x2075): undefined reference to `glutReshapeFunc'
    PlanetGL.c++:(.text+0x2081): undefined reference to `glutKeyboardFunc'
    PlanetGL.c++:(.text+0x208d): undefined reference to `glutSpecialFunc'
    PlanetGL.c++:(.text+0x20a6): undefined reference to `glutMainLoop'
    collect2: ld returned 1 exit status
    Don't mind the char vs strings warning.

    EDIT: I forgot to mention: I put all the functions under a namespace, as just using a class with everything static was leading to nowhere.

    Code:
    namespace PlanetGL
    {
      // public:
       //PlanetGL();
       void BuildFont();
       void KillFont();
       void glPrint(char *text);
       void DrawGLScene();
       int ImageLoad(char *filename, Image *image);
       void LoadGLTextures();
       void InitGL(GLsizei Width, GLsizei Height);
       void ReSizeGLScene(GLsizei Width, GLsizei Height);
       void keyPressed(unsigned char key, int x, int y);
       void specialKeyPressed(int key, int x, int y);
      // int main(int argc, char **argv);
       int getCords();
        int window,light,blend;
       //private:
      
       GLuint filter;		// Which Filter To Use (nearest/linear/mipmapped)
       GLuint texture[9];		// Storage for 9 textures.
       GLuint object;
       int objects;
       GLfloat xrot;		// X Rotation
       GLfloat yrot;		// Y Rotation
       GLfloat xspeed;		// x rotation speed
       GLfloat yspeed;		// y rotation speed
       cord cam;			// Camera position
       int orbita;
       float orsize;
       int PlanetName;
       float *CirKordX, *CirKordY, CirKordZ;
       float *k;
       float kf, kspeed;
       int ZoomSpeed;
       double *PlanetSpeed;
       GLuint base;        	 	// base display list for the font set.
       GLfloat cnt1;       	    	// 1st counter used to move text & for coloring.
       GLfloat cnt2;           	// 2nd counter used to move text & for coloring.
       GLUquadricObj *quadratic;	// Storage For Our Quadratic Objects
       GLfloat *LightAmbient;
       GLfloat *LightDiffuse;
       GLfloat *LightPosition;
       CO *Plan;
    };
    Last edited by Gediminas2; March 5th, 2009 at 11:54 AM.

  7. #17
    Join Date
    Feb 2009
    Beans
    202

    Re: Problems with C++ and OpenGL

    Quote Originally Posted by Gediminas2 View Post
    [code]gedas@gedas-desktop:~/OGL/planetos$ g++ PlanetGL.c++
    no openGL/Glut library linked to project. Pretty sure that's the reason for this.

    Try this:

    g++ planetGL.c++ -o planetGL -lGL -lGLU

  8. #18
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,240
    Distro
    Kubuntu

    Re: Problems with C++ and OpenGL

    I will mind the string warnings... fix these by declaring your strings to be constant. In lieu of:
    Code:
    char* someStr = "foo";
    
    // use
    
    const char* someStr = "foo";
    The rest of the errors, and there is quite a few (so forgive me if I missed one), are due to link problems... not C syntax errors; hence it is not the compiler. An "unresolved reference" is a link error.

    If your application is defined in multiple modules, say Mod1.c, Mod2.c, and Mod3.c, then you need to build your application in one of two ways:

    One way:
    Code:
    g++ Mod1.cpp Mod2.cpp Mod3.cpp -o myapp
    Another way:
    Code:
    g++ -c Mod1.cpp
    g++ -c Mod2.cpp
    g++ -c Mod3.cpp
    g++ Mod1.o Mod2.o Mod3.o -o myapp
    In the examples above, I did not specify "CFLAGS" or "LDFLAGS". Typically for CFLAGS, options are -Wall -ansi -pedantic. So for compiling:
    Code:
    g++ -Wall -ansi -pedantic -c Mod1.cpp
    ...
    For LDFLAGS, these are your -L and -l (lowercase ell) options. For example:
    Code:
    g++ Mod1.o Mod2.o Mod3.o -L/path/to/library -lopenGL -o myapp
    So essentially the format is:
    Code:
    g++ CFLAGS Mod1.cpp Mod2.cpp ... ModN.cpp LFLAGS -o myapp
    ------------

    Edit... I forgot... use g++, not gcc.
    Last edited by dwhitney67; March 5th, 2009 at 12:08 PM.

  9. #19
    Join Date
    Aug 2008
    Location
    Kaunas, Lithuania
    Beans
    18
    Distro
    Ubuntu UNR

    Re: Problems with C++ and OpenGL

    Quote Originally Posted by tneva82 View Post
    no openGL/Glut library linked to project. Pretty sure that's the reason for this.

    Try this:

    g++ planetGL.c++ -o planetGL -lGL -lGLU
    Thanks a million friend
    It now compiles. I forgot about linking

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •