# Attempting to make a square creating function

• 05-18-2005
Shamino
Attempting to make a square creating function
It will take two arguments, two integers, it will take those two integers and poof a square by manipulating the two integers....

I'm hoping my logic is correct in saying changing x/y will change the location the square is drawn, rather than its size.. If it isnt, please explain...

I ultimately want to use this function, to create more complex shapes in other functions for the telltale shapes of tetris..

The thing about making those shapes is this:

1. They must have separate pieces to make up the shapes(so you can delete lines)

2. All parts of the shape must rotate on the same axis

By looking at this code and writing it out just now, I'm already seeing faults in it, I'm thinking I will be changing the size of the box by changing x and y.... Course I can't test it, because when I use the function in my DrawGLScene() it does absolutely nothing :\, please help!

Code:

```void part(int x, int y) {         glLoadIdentity();         glOrtho(0,640,0,480,-1,1);         glLoadIdentity();         glTranslated(partx, party, 0);         glBegin(GL_QUADS);                 glVertex2f(x, y);                 glVertex2f(x-30, y);                 glVertex2f(x-30, y-30);                 glVertex2f(x, y-30);         glEnd(); }```
• 05-19-2005
VirtualAce
??

I'm lost.

To create a square is easy.

-1.0f,-1.0f,0.0f
1.0f,-1.0f,0.0f
-1.0f,1.0f,0.0f
1.0f,1.0f,0.0f

This is a unit 2D square. Multiply these by a scaling transformation matrix and you can have any size you want? The square is centered on the x,y and z axes.
• 05-20-2005
Shamino
yah yah, I know, I'm trying to do it with only two variables multiple times and keep the same translatef, so the whole shape is made up of different and seperate quads but still rotates around the same axis.... Like, have a function that takes two arguments to build a square, (of the same size all the time), but all the two arguments does is change where the square is drawn, not how big it is....

But you see, I cant change the translatef or it wont rotate all together!!

Hmm, do I really need to use a function for that?......

"mumble mumble, carry the two...."

Guess not .....
• 05-20-2005
VirtualAce
What?

Translate the square using a translation matrix. As long as the square is created equidistant around model space 0.0f,0.0f,0.0f it will always rotate around it's center. To rotate around another point simply translate to the desired rotation center and then rotate.

I'm not sure what you are wanting to do.
• 05-20-2005
Shamino
Should I paint a picture? :D j/k... here goes again

im wondering, if I can build the same size square, with the same translatef, in different locations...

Because like, the only thing I've been able to do, is build bigger squares, while keeping the same translatef.....
• 05-20-2005
Shamino
Wait.... I've got it, all I have to do is change the first set of points on the square...

so instead of having 1.0, 1.0, 0.0...
I could have 2.0, 1.0, 0.0.....

that would place a square with the same translatef right on the X axis more!
• 05-20-2005
If its a cube you want then
Code:

```// DrawCube void DrawCube(float xPos, float yPos, float zPos) {         glPushMatrix();                 glTranslatef(xPos, yPos, zPos);                 glBegin(GL_POLYGON);                         glVertex3f(0.5f, 0.5f, 0.5f);                                glVertex3f(0.5f, 0.5f, -0.5f);                         glVertex3f(-0.5f, 0.5f, -0.5f);                         glVertex3f(-0.5f, 0.5f, 0.5f);                 glEnd();                 glBegin(GL_POLYGON);                         glVertex3f(0.5f, 0.5f, 0.5f);                                glVertex3f(-0.5f, 0.5f, 0.5f);                         glVertex3f(-0.5f, -0.5f, 0.5f);                         glVertex3f(0.5f, -0.5f, 0.5f);                 glEnd();                 glBegin(GL_POLYGON);                         glVertex3f(0.5f, 0.5f, 0.5f);                                glVertex3f(0.5f, -0.5f, 0.5f);                         glVertex3f(0.5f, -0.5f, -0.5f);                         glVertex3f(0.5f, 0.5f, -0.5f);                 glEnd();                 glBegin(GL_POLYGON);                         glVertex3f(-0.5f, 0.5f, 0.5f);                                glVertex3f(-0.5f, 0.5f, -0.5f);                         glVertex3f(-0.5f, -0.5f, -0.5f);                         glVertex3f(-0.5f, -0.5f, 0.5f);                 glEnd();                 glBegin(GL_POLYGON);                         glVertex3f(-0.5f, -0.5f, 0.5f);                         glVertex3f(-0.5f, -0.5f, -0.5f);                         glVertex3f(0.5f, -0.5f, -0.5f);                         glVertex3f(0.5f, -0.5f, 0.5f);                        glEnd();                 glBegin(GL_POLYGON);                         glVertex3f(0.5f, -0.5f, -0.5f);                         glVertex3f(-0.5f, -0.5f, -0.5f);                         glVertex3f(-0.5f, 0.5f, -0.5f);                         glVertex3f(0.5f, 0.5f, -0.5f);                        glEnd();         glPopMatrix(); }```
• 05-22-2005
Shamino
that lil function is nice and all, but....

I want one that will change the position of the cube while keeping the same translatef :)
• 05-22-2005
sand_man
but why?
Its easier to use glPushMatrix(), glPopMatrix() and glTranslatef().
• 05-23-2005
no-one
this is rather simplified, and doesn't account for a lot of things, but it should give you the basic idea, and i dont have a compiler nor have i done drawing like this in a LONG time so i cant guarantee it works, at all.

Code:

```// positioned by top left corner. void DrawCube(float xPos, float yPos, float zPos, float width, float height, float depth) {         float w = width + xPos;         float h = height + yPos;         float d = depth + zPos;         glBegin(GL_QUADS);                 // front                 glVertex3f(xPos, yPos, zPos);                        glVertex3f(w, yPos, zPos);                 glVertex3f(w, h, zPos);                 glVertex3f(xPos, h, zPos);                 // back                 glVertex3f(xPos, yPos, d);                        glVertex3f(w, yPos, d);                 glVertex3f(w, h, d);                 glVertex3f(xPos, h, d);                 // left                 glVertex3f(xPos, yPos, zPos);                        glVertex3f(xPos, yPos, d);                 glVertex3f(xPos, h, d);                 glVertex3f(xPos, h, zPos);                 // right                 glVertex3f(w, yPos, zPos);                        glVertex3f(w, yPos, d);                 glVertex3f(w, h, d);                 glVertex3f(w, h, zPos);                 // top                 glVertex3f(xPos, yPos, zPos);                 glVertex3f(xPos, yPos, d);                 glVertex3f(w, yPos, d);                 glVertex3f(w, yPos, zPos);                 // bottom                 glVertex3f(xPos, h, zPos);                 glVertex3f(xPos, h, d);                 glVertex3f(w, h, d);                 glVertex3f(w, h, zPos);         glEnd(); }```