Thread: Attempting to make a square creating function

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    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();
    }
    Last edited by Shamino; 05-18-2005 at 11:03 AM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    ??

    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.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    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 .....

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Should I paint a picture? 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.....
    Last edited by Shamino; 05-20-2005 at 10:48 AM.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    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!

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    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();
    }

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    that lil function is nice and all, but....

    I want one that will change the position of the cube while keeping the same translatef

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    but why?
    Its easier to use glPushMatrix(), glPopMatrix() and glTranslatef().

  10. #10
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    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();
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM