2

I'm trying to draw 2 squares side by side of different colours, my problem is I can't get glTranslatef to move my second square to the right, the second square just draws over the first.

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);   
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPointSize(10);

    glBegin(GL_QUADS);

        glColor3f(0.0,1.0,0.0);
        drawSquare();

        glTranslatef(10,0,0);

        glColor3f(1.0,0.0,0.0);
        drawSquare();

    glEnd();    

    glFlush();  // Process all OpenGL routines
}

void drawSquare()
{
    glVertex3f(0,0,0);
    glVertex3f(10,0,0);
    glVertex3f(10,10,0);
    glVertex3f(0,10,0);
}

2 Answers 2

8

You cannot translate inside a glBegin/glEnd block, you have break it up into two blocks.

I recommend to start using glGetError() in your code, it will help you find mistakes like this.

1
  • Thanks very much, your answer worked for me perfectly. Is there anything you'd recommend I'd read online for starting off openGL?
    – kev670
    Oct 9, 2012 at 16:25
7

Only a certain subset of OpenGL commands is allowed between glBegin and glEnd. So, when you call glTranslate between those two calls, it is ignored.

Remove glBegin and glEnd from display, and put in drawSquare - that should work.

1
  • Not only will it be ignores, it actually triggers the GL_INVALID_OPERATION error flag.
    – datenwolf
    Oct 9, 2012 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.