Thread: OpenGL general question

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    OpenGL general question

    Hi, I am learning OpenGL and I am trying to make an object I created cross a 2D grid. Using glTranslate I have figured out how to move the entire image, but how would I say move my object ( Which is a GL quad)?

    Code:
    for(int i=-0; i<10;i+=10)
    	{
    		glBegin(GL_QUADS);
    		glVertex2f(-5.0f,i);
    		glVertex2f(i,5.0f);
    		glVertex2f(5.0f,i);
    		glVertex2f(i,-5.0f);
    		glEnd();
    	}
    
    
    	for(int i=-500; i<500; i+=30)
    	{
    	glBegin(GL_LINES);
    	glVertex2f(600.0f,i); //much simpler than all of that .........
    	glVertex2f(-600.0f,i);
    	glVertex2f(i,600.0f);
    	glVertex2f(i,-600.0f);
    	glEnd();
    	}
    	drawFont("Test Blue");
    Mod Edit: Added code tags.
    Last edited by VirtualAce; 11-24-2009 at 12:13 AM. Reason: Edit: Here's my code. I also have keyboard commands rigged to switch statements under their keyboard function method.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Try:
    Code:
    glPushMatrix();
    glTranslate(...);
    // Render your triangle here
    glPopMatrix();
    That ought to work...

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Fundamental to the possibility of motion is an "event loop" -- that is, a loop that continuously redraws the screen/window. OpenGL does not do that by itself.

    However, if you are using glut or SDL you probably are using an event loop if you have been following some kind of tutorial. If you are not sure, ask.

    With glut, you have your scene drawing function registered like this, then you initialize the main event loop:
    Code:
    glutDisplayFunc(drawScene);
    [...]
    glutMainLoop();
    You must also include this:
    Code:
    glutPostRedisplay();
    at the end of drawScene() (or whatever you called it).

    Then you just use the technique EVOEx mentioned. As is, your quad is centered on the origin. So, working with that example:
    Code:
    static float square[3] = {0.0f};
    glTranslatef(square[0],square[1],square[2]);
    glBegin(GL_QUADS);
        for(int i=-0; i<10;i+=10) {
            glVertex2f(-5.0f,i);
            glVertex2f(i,5.0f);
            glVertex2f(5.0f,i);
            glVertex2f(i,-5.0f);
        }
    glEnd();
    square[0]++;
    square[1]++;
    This will cause the square to move diagonally right. Using the z-axis in 2D effectively scales the shape (as if it were receeding), I think...

    Note the scene drawing routine registered by glut does not accept parameters, so you must use either "static" variables or globals. Also noticed I put the for loop inside glEnd/Begin, which is a little more efficient.
    Last edited by MK27; 11-23-2009 at 09:13 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about openGL
    By two31d in forum C++ Programming
    Replies: 0
    Last Post: 01-29-2006, 01:39 PM
  2. quick OpenGL question
    By Thantos in forum Game Programming
    Replies: 4
    Last Post: 04-04-2004, 08:46 AM
  3. struct problem and general C question
    By techrolla in forum C Programming
    Replies: 8
    Last Post: 01-09-2004, 01:37 AM
  4. OpenGL question
    By Leeman_s in forum C++ Programming
    Replies: 8
    Last Post: 09-07-2002, 11:17 AM
  5. OpenGL question
    By kas2002 in forum Game Programming
    Replies: 16
    Last Post: 08-02-2002, 12:29 PM