SFML community forums
Help => Graphics => Topic started 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?
-
Do you use glPushMatrix / glPopMatrix ?
-
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?
-
You need to look more at how OpenGL works. Have a read of this:
http://www.opengl.org/documentation/red_book/
-
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();
}
-
I didn't understand what you mean but I see glLoadMatrixf(Sides); in both set functions and draw function.
BTW, please use [ code ][/ code ]
-
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
-
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 ?
-
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..
-
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.
-
You still can have set/get functions with this technique.
-
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
-
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...
-
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:
-
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?
-
I would have done something like
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.
-
I would have done something like 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?
-
I don't have light example on my computer so here is only the part between the glPush/PopMatrix :
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.
-
I don't have light example on my computer so here is only the part between the glPush/PopMatrix :
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.
-
But you can draw and set rotation/position/whatever-you-like with my approach. :wink: Look at this pseudo-code:
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 :
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
}
-
i kinda understand, but not how to it
-
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:
-
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?
-
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:
-
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?
-
yes.
Did you try ? You'll see :wink:
-
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...
-
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..