Find the answer to your Linux question:
Results 1 to 7 of 7
Hey everyone I'm experimenting with classes in C++ when used with OpenGL and GLUT. They should work fine together, and for the most part they do, but I am having ...
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    26

    Question OpenGL, C++ Classes, and GLUT Keyhandler failue

    Hey everyone I'm experimenting with classes in C++ when used with OpenGL and GLUT. They should work fine together, and for the most part they do, but I am having an issue with creating an instance of a class within an if loop, which uses a variable that gets set to equal the correct number by using the keyboard input handler function provided by GLUT. However, something goes wrong. After a lot of debugging, I have concluded the following: the keyhandler function works, the creation of the variable used to represent the class may or may not obey the if statement, but the call to the public function that creates the triangle on screen doesn't. It automatically creates it. It seems as though it automatically calls the function either when the class is created or when it is called in the if loop (even if the statement isn't true). A strange issue, probably with a simple solution, as is usually my situation. I dumbed it down a bit so all you need to look at is the exact problem itself. If you have any idea about the issue, take a look at this code and tell me if you can figure it out (and you can rest assured that all of the other code [main loop, window creation, initialization] works just fine):

    //Triangle_Class
    class Triangle
    {
    public:
    void createTriangle(unsigned int _style);
    protected:
    void setVertices();
    void setTrans(unsigned int style);
    };

    void Triangle::setVertices()
    {
    glBegin(GL_TRIANGLES);

    glVertex3f(0.0,0.5,0.0);
    glVertex3f(-0.5,-0.3,0.0);
    glVertex3f(0.5,-0.3,0.0);

    glEnd();
    }

    void Triangle::setTrans(unsigned int style)
    {
    if(style==0)
    {
    glTranslatef(0.0,0.0,-5.0);
    glRotatef(0.0, 0.0, 0.0, 0.0);
    glScalef(1.0,1.0,1.0);
    }
    }

    void Triangle::createTriangle(unsigned int _style)
    {
    setTrans(_style);
    setVertices();
    }
    //End_Triangle_Class

    void inputFunc(unsigned char key, int mx, int my)
    {
    switch (key)
    {
    case 27:
    exit(0);
    case 48:
    {
    tridisp=1;
    tristyle=0;
    }
    }
    }

    void displayFunc()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    if(tridisp==1)
    {
    Triangle triangle;
    triangle.createTriangle(tristyle);
    }


    glutSwapBuffers();
    }

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    You don't show what the Triangle class default constructor does. I assume it is declared/defined?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Apr 2009
    Posts
    26
    I am somewhat new to classes, so I am not sure if it is possible (and the class does work properly as far as I can tell on its own), but I am using createTriangle() as a contructor in a way. Does that make sense or could that be my problem in some obscure way?

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by atrx View Post
    I am somewhat new to classes, so I am not sure if it is possible (and the class does work properly as far as I can tell on its own), but I am using createTriangle() as a contructor in a way. Does that make sense or could that be my problem in some obscure way?
    It would be a problem if the default constructor also drew the triangle. I don't know enough about the class. Do you have the header file handy to post?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Apr 2009
    Posts
    26
    Well as you can see, the function that draws the triangle is called inside the createTriangle function, which is intended to act as a constructor, although I don't know the exact syntax for creating one, unless it is just using the name of the class as your function name. I am using the headers iostream, stdlib.h, and glut, and g++ as my compiler. Classes don't exactly require a header though, i think. I will try making createTriangle() my contructor, using the method previously described, but please correct me if that is incorrect.

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by atrx View Post
    Well as you can see, the function that draws the triangle is called inside the createTriangle function, which is intended to act as a constructor, although I don't know the exact syntax for creating one, unless it is just using the name of the class as your function name. I am using the headers iostream, stdlib.h, and glut, and g++ as my compiler. Classes don't exactly require a header though, i think. I will try making createTriangle() my contructor, using the method previously described, but please correct me if that is incorrect.
    So, this is a class you wrote? In C++, every class has a default constructor, assignment operator, and destructor, even if you do not declare them. Since your triangle class apparently has no member variables, then constructing it should basically be a no-op, and likewise the destructor. IE, your class is a code-only class. That said, where is the variable tridisp defined?

    So, without more complete information, I cannot help you determine why what you are observing is happening.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Just Joined!
    Join Date
    Apr 2009
    Posts
    26
    I think that I might have discovered part of the issue, but do not know why or how to fix it. The if loop is always true, or at least what's inside it runs automatically (even if i put the stdlib function exit(0) in it). You can be basically sure of this, though I am not sure why. It isn't even a problem with the case loop because I can take the making on tridisp equal to 1 out and then make tridisp = 5 on the top but it still doesn't work. it could be a problem with the original declaration of the varialbe which looks like this:

    int tridisp, tristyle;

    stick that to the top of the code, beneath what would be the headers.

Posting Permissions

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