Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Creating an OpenGL Cube Class  (Read 12544 times)

0 Members and 1 Guest are viewing this topic.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Creating an OpenGL Cube Class
« Reply #15 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.
SFML / OS X developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #16 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?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Creating an OpenGL Cube Class
« Reply #17 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.
SFML / OS X developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #18 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.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Creating an OpenGL Cube Class
« Reply #19 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
}
SFML / OS X developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #20 on: December 21, 2010, 06:06:24 pm »
i kinda understand, but not how to it

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Creating an OpenGL Cube Class
« Reply #21 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:
SFML / OS X developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #22 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?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Creating an OpenGL Cube Class
« Reply #23 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:
SFML / OS X developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #24 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?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Creating an OpenGL Cube Class
« Reply #25 on: December 21, 2010, 07:26:52 pm »
yes.

Did you try ? You'll see  :wink:
SFML / OS X developer

Terrydil

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Creating an OpenGL Cube Class
« Reply #26 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...

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Creating an OpenGL Cube Class
« Reply #27 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)



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

 

anything