SFML community forums

Help => Graphics => Topic started by: Fierce_Dutch on December 19, 2010, 08:30:07 pm

Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 19, 2010, 08:30:07 pm
Ok Well I am trying to create an OpenGL cube class but if I try and translate the cube and then have a gl shape folowing it, it affects that shape. Is there anyway that I can have a matrix or something for each cube shape and have every tranlation or rotation done no matter where it's done only effects my shape. So it kinda like each shape has it's own little space in memory and when I call a function it affects that variable or matrix which is in each class.

So pretty much how would I make a function only affect a shape of my choice. would I have to put the shape in a seperate matrix and then translate that martix which is a variable for the cube class?

If so how would I do this?
Title: Creating an OpenGL Cube Class
Post by: Hiura on December 19, 2010, 10:19:49 pm
Do you use glPushMatrix / glPopMatrix ?
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 20, 2010, 05:55:13 am
Quote from: "Hiura"
Do you use glPushMatrix / glPopMatrix ?


Ya but wouldn't I be pushing the same matrix for all of the shapes since there all on the modelview matrix. And if I push the martix, how does it know which shapes matrix I am pushing?

How would I use I use glLoadMatrixf(const GLfloat *     m) to load each classes matrix? Like would I load a matrix and then call my shape's verts? And then load it whenever I want to make translations or rotations. But would the matrix be big enough? And how would I do this?
Title: Creating an OpenGL Cube Class
Post by: tntexplosivesltd on December 20, 2010, 09:20:02 am
You need to look more at how OpenGL works. Have a read of this:
http://www.opengl.org/documentation/red_book/
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 20, 2010, 03:47:36 pm
Ok well This is waht I am trying to do. I have an array of floats called size.
See, I am trying to put the shape in the array called sides. Then I load "Sides" and do a rotation or translation, but it doesnt work...

void   Block::SetPosition(float x,float y, float z)
{
   //glMatrixMode(GL_MODELVIEW);
   glLoadMatrixf(Sides);
   glLoadIdentity();
   glTranslatef(x,y,z);
}

void   Block::Rotate(float angle, float x, float y, float z)
{
   //glMatrixMode(GL_MODELVIEW);
   glLoadMatrixf(Sides);
   glLoadIdentity();
   glRotatef(angle,x,y,z);
}

void   Block::Draw()
{
   glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);
   
   //glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(Sides);
    glPushMatrix();
    glBindTexture(GL_TEXTURE_2D, SideTexture);
   
   glColor4f(1.f, 1.f, 1.f, 1.f);
        // Draw a cube
        float size = 20.f;
        glBegin(GL_QUADS);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f( size,  size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, -size);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, size);
            glTexCoord2f(1, 1); glVertex3f( size,  size, size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, size);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f(-size,  size,  size);
            glTexCoord2f(1, 0); glVertex3f(-size, -size,  size);

            glTexCoord2f(0, 0); glVertex3f(size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f(size,  size,  size);
            glTexCoord2f(1, 0); glVertex3f(size, -size,  size);

      glEnd();

      glBindTexture(GL_TEXTURE_2D, BottomTexture);

      glBegin(GL_QUADS);
         
         glColor4f(1.f, 1.f, 1.f, 1.f);

            glTexCoord2f(0, 1); glVertex3f(-size, -size,  size);
            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, -size);
            glTexCoord2f(1, 1); glVertex3f( size, -size,  size);

      glEnd();

      glBindTexture(GL_TEXTURE_2D, TopTexture);
         glColor4f(1.f, 1.f, 1.f, 1.f);

      glBegin(GL_QUADS);

            glTexCoord2f(0, 1); glVertex3f(-size, size,  size);
            glTexCoord2f(0, 0); glVertex3f(-size, size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, size, -size);
            glTexCoord2f(1, 1); glVertex3f( size, size,  size);

        glEnd();

      glPopMatrix();
}
Title: Creating an OpenGL Cube Class
Post by: Hiura on December 20, 2010, 06:06:48 pm
I didn't understand what you mean but I see glLoadMatrixf(Sides); in both set functions and draw function.
BTW, please use [ code ][/ code ]
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 20, 2010, 06:11:55 pm
Quote from: "Hiura"
I didn't understand what you mean but I see glLoadMatrixf(Sides); in both set functions and draw function.
BTW, please use [ code ][/ code ]


Well that is what I need help with. I am wondering how I can store data for each shape not on the stack but on a martix that each class has, and then being able to manipulate everything in that matrix with translation and rotation
Title: Creating an OpenGL Cube Class
Post by: Hiura on December 20, 2010, 06:13:58 pm
Maybe you can see things another way : you can for example set the pos and rotation _right_ before drawing the stuff. Do you see what I mean ?
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 20, 2010, 06:16:12 pm
Quote from: "Hiura"
Maybe you can see things another way : you can for example set the pos and rotation _right_ before drawing the stuff. Do you see what I mean ?


Ya but what if I want to have an input that changes the rotation and I can't draw it right then... So that is why I want everything more object oriented..
Title: Creating an OpenGL Cube Class
Post by: Laurent on December 20, 2010, 06:18:52 pm
You need a matrix class that you can store, manipulate and send to glLoadMatrix whenever you want (ie. before rendering the corresponding shape). I think you can find pretty good maths libraries that are designed for OpenGL.
Title: Creating an OpenGL Cube Class
Post by: Hiura on December 20, 2010, 06:19:02 pm
You still can have set/get functions with this technique.
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 20, 2010, 06:23:23 pm
Quote from: "Hiura"
You still can have set/get functions with this technique. In fact it has nothing to do with OO concept.


Why? I mean I want to set the position just for that class. How is that not OO
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 20, 2010, 06:26:23 pm
Quote from: "Laurent"
You need a matrix class that you can store, manipulate and send to glLoadMatrix whenever you want (ie. before rendering the corresponding shape). I think you can find pretty good maths libraries that are designed for OpenGL.


So I would need to find a math lib and then setup like a 3D matrix. And then load it and push it to manpulate and pop it when done. Also how would I call the shape?

I kinda want a class like rectangle in sfml except 3D...
Title: Creating an OpenGL Cube Class
Post by: Hiura on December 20, 2010, 06:31:14 pm
Quote from: "Fierce_Dutch"
Why? I mean I want to set the position just for that class. How is that not OO
Forget the last sentence. -I need some sleep- :oops:
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 20, 2010, 06:34:56 pm
Quote from: "Hiura"
Quote from: "Fierce_Dutch"
Why? I mean I want to set the position just for that class. How is that not OO
Forget the last sentence. -I need some sleep- :oops:


ok lol, but like I said in the previous message. I want a class like sf::Rect but for it to be 3D... And always have a set size.. So how would I do this? With a Matrix?
Title: Creating an OpenGL Cube Class
Post by: Hiura on December 20, 2010, 06:45:49 pm
I would have done something like
Code: [Select]
class Cube {
Position pos; Rotation rot; Size size;
setPosition/Rotation/Size(newValue) { pos/rot/size = newValue; }
draw() {
 // save state
 glPushMatrix();
 glPushAttrib(...);

 // use pos, rot, size here
 glTranslate(..);
 glRotation(..);
 glColor(..)
 :
 :

 glVertex..(..);
 :
 :

 // restore state
 glPopAttrib();
 glPopMatrix();
}
};


It's the way most of the students did their project this semester – and the way they were taught.
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 21, 2010, 09:15:47 am
Quote from: "Hiura"
I would have done something like
Code: [Select]
class Cube {
Position pos; Rotation rot; Size size;
setPosition/Rotation/Size(newValue) { pos/rot/size = newValue; }
draw() {
 // save state
 glPushMatrix();
 glPushAttrib(...);

 // use pos, rot, size here
 glTranslate(..);
 glRotation(..);
 glColor(..)
 :
 :

 glVertex..(..);
 :
 :

 // restore state
 glPopAttrib();
 glPopMatrix();
}
};


It's the way most of the students did their project this semester – and the way they were taught.


What? I am confused, can you give me like a full example?
Title: Creating an OpenGL Cube Class
Post by: Hiura on December 21, 2010, 01:30:20 pm
I don't have light example on my computer so here is only the part between the glPush/PopMatrix :
Code: [Select]
glBegin(GL_TRIANGLES);
    glColor3ub(0,0,255);
glVertex3d(Position[0],Position[1],Position[2]);
glVertex3d(Position[0],Position[1],Position[2]);
glVertex3d(Position[0],Position[1],Position[2]);
    glEnd();

In this example there are (obviously) no dynamic size nor rotation.

What puzzles you exactly ? I'll try to be more understandable if I can.
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 21, 2010, 04:21:21 pm
Quote from: "Hiura"
I don't have light example on my computer so here is only the part between the glPush/PopMatrix :
Code: [Select]
glBegin(GL_TRIANGLES);
    glColor3ub(0,0,255);
glVertex3d(Position[0],Position[1],Position[2]);
glVertex3d(Position[0],Position[1],Position[2]);
glVertex3d(Position[0],Position[1],Position[2]);
    glEnd();

In this example there are (obviously) no dynamic size nor rotation.

What puzzles you exactly ? I'll try to be more understandable if I can.


Well I was wondering why you rotate adn translate it inside the function. The whole point of this is so that I can set the position of this shape and draw it in two different ends of the code.
Title: Creating an OpenGL Cube Class
Post by: Hiura on December 21, 2010, 05:31:03 pm
But you can draw and set rotation/position/whatever-you-like with my approach.  :wink: Look at this pseudo-code:

Code: [Select]

void f(void) {
  // Setting up some stuff.
  mySquare.SetPosition(0, 0);
  mySquare.SetRotation(pi/4); // or 45°

  // Some code
  :
  :
 
  // Drawing our beautiful square.
  mySquare.Draw();
}


with something like this implementation of Square :
Code: [Select]

Square::SetPosition(x, y) { myX = x, myY = y }
Square::SetRotation(alpha) { myAlpha = alpha }
Square::Draw() {
  glPushMatrix();

  glTranslate(myX, myY)
  glRotate(myAlpha)

  glVertex(-1, -1)
  glVertex(-1, 1)
  glVertex(1, 1)
  glVertex(1, -1)

  glPopMatrix(); // Restore previous coordinate system
}
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 21, 2010, 06:06:24 pm
i kinda understand, but not how to it
Title: Creating an OpenGL Cube Class
Post by: Hiura on December 21, 2010, 06:10:12 pm
Well, what's your main concern ? How to create the class ? How to display many objects ? (Just try, something we have to go that way.)

BTW : you don't have to quote everything/every time.  :wink:
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 21, 2010, 07:11:27 pm
Well, how is this class not going to get confused with other classes say if I call it like this..

block a1, a2;
a1.roatate(sfksuda)
a2.draw
a2.rotate
a1.draw.

Wont it get confused withing the class?
Title: Creating an OpenGL Cube Class
Post by: Hiura on December 21, 2010, 07:18:02 pm
If «Block::Rotate» from your example only store the new rotation (like in the example above) you won't mix up anything.

Look at the doc of glPush/PopMatrix too.  :wink:
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 21, 2010, 07:22:35 pm
Quote from: "Hiura"
If «Block::Rotate» from your example only store the new rotation (like in the example above) you won't mix up anything.

Look at the doc of glPush/PopMatrix too.  :wink:


but you are pushing the same matrix right?
Title: Creating an OpenGL Cube Class
Post by: Hiura on December 21, 2010, 07:26:52 pm
yes.

Did you try ? You'll see  :wink:
Title: Creating an OpenGL Cube Class
Post by: Terrydil on December 21, 2010, 09:42:37 pm
Its the same (modelview) matrix but basically you are resetting it, then changing it and drawing with those changes, then resetting it again and changing it in a different way and drawing something with those changes, then resetting it again ...etc...etc...
Title: Creating an OpenGL Cube Class
Post by: Fierce_Dutch on December 22, 2010, 09:19:11 pm
Quote from: "Terrydil"
Its the same (modelview) matrix but basically you are resetting it, then changing it and drawing with those changes, then resetting it again and changing it in a different way and drawing something with those changes, then resetting it again ...etc...etc...


Thanks a bunch, it works great! LOOK! But now I have another problem(below pic)

(http://i56.tinypic.com/oqhyzl.png)

anything sfml doesnt draw right... Its really weird..