How should I use opengl and glut to translate object

Hey guys,
I'm making a small 2d game using c++, OpenGl, and Glut.
I need help using a class to create objects
and then using display() to translate them on the screen accordingly.

Pseudocode that I'm using:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 class Wall {
public:
    Wall (float tempposx, float tempposy){
        float posx;
        float posy;
        
        posx = tempposx;
        posy = tempposy;
     
       
            }
    
    
    

};
Wall newWallone(0.5,0.5); //create new wall with posx,posy
Wall newWalltwo(0.6,0.6);

To display the polygon for my player I use the following code (placed in the void display):
1
2
3
4
5
6
7
8
9
10
11
glPushMatrix();
    

    
    glTranslatef(posX_player,posY_player,posZ_player);
    
    player();
    
    
    glPopMatrix();

I would like to know how to translate the 2 new walls using gltranslate but without having to write pushmatrix, popmatrix and having to manually write it for each wall. I would like a class or a function to do this for me once I declare a new wall and its position.

Thanks in advance, a curious programmer.
Your question is about program organization. However, someone who is writing OpenGL code should already have a good grasp on that before even starting with OpenGL.

So, until you to figure it out, either use push/pop matrix or give up on OpenGL temporarily.

I would estimate that we can't help you because the real answer to your question depends on many circumstances.
Kevin, thank you for the reply. I will look further into program organisation and C++ in general. However, it would greatly help me if you could instruct me on how write a method that an instance calls to translate itself with its own posx, posy.

In addition, I will try using member variables. However I believe that member variables only handles its variables (posx,posy); it does not add any functionality (translation) to the instance.

Sorry for my English and lack of knowledge :3
Olivier
Ok, lets' give it a try...

You made 2 new walls at specified coordinates.

Now, first, what do you mean by 'translate walls'? I mean, you could to that but, as far as I know, walls shouldn't be moving except in some surrealistic art.
Last edited on
Hi Kevin,
Moving walls would indeed make for pretty interesting art. By translating the walls I meant displaying them at their fixed positions. Sorry for the confusion. :D

Thanks to the help of the c++ irc I was able to get what I needed.

Using member variables to handle the positions of each instance of a wall:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
float posx;
float posy;
class Wall {
public:
    float posx;
    float posy;
    Wall(float tempposx, float tempposy): posx(tempposx), posy(tempposy) {} //constructor?
    
    void display_wall(){
        
        glPushMatrix();
        glTranslatef(posx, posy, 0);
        
        glBegin(GL_POLYGON);
        
        glColor3f(0,1,1);
        
        glVertex2f(-0.05, -0.1);
        
        glVertex2f(-0.05, 0.1);
        
        glVertex2f(0.05, 0.1);
        glVertex2f(0.05, -0.1);
        glEnd();

        glPopMatrix();
    }
            }
;
Wall newWall1(1,0.5);
Wall newWall2(0,0.1);
Wall newWall3(0,0.5);


Then in the display void():
1
2
3
4
5
6
newWall1.display_wall();
newWall2.display_wall();
newWall3.display_wall();
   
    glFlush();
    

This works great and I will now look into creating an array of all the instances and than be able to display all the walls in one line of code. Pseudocode: allwalls.display_wall();

If you could aim me in the right direction to be able to render all the instances all at once I would appreciate it greatly.

Cheers, Olivier
Pseudocode: allwalls.display_wall();

If you want that syntax, you will need to create a class AllWalls which has std::vector of walls (named walls) as a member.

You can add a wall to vector walls directly, or through a member function add_wall which you need to write.

Then you can write member function display_wall of class AllWalls which calls display_wall of every element of vector walls by a for loop.
Last edited on
Topic archived. No new replies allowed.