Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am currently drawing a rectangle to the screen and would like to move it by using the arrow keys. However, when I press an arrow key the vertex data changes but the display does refresh to reflect these changes, even though I am calling glutPostRedisplay(). Is there something else that I must do?

My code:

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/freeglut_ext.h>
#include <iostream>
#include "Shaders.h"
using namespace std;

const int NUM_VERTICES = 6;
const GLfloat POS_Y = -0.1;
const GLfloat NEG_Y = -0.01;

struct Vertex {
    GLfloat x;
    GLfloat y;
    Vertex() : x(0), y(0) {}
    Vertex(GLfloat givenX, GLfloat givenY) : x(givenX), y(givenY) {}

Vertex left_paddle[NUM_VERTICES];

void init() {
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);

    left_paddle[0] = Vertex(-0.95f, 0.95f);
    left_paddle[1] = Vertex(-0.95f,  0.0f);
    left_paddle[2] = Vertex(-0.85f,  0.95f);
    left_paddle[3] = Vertex(-0.85f,  0.95f);
    left_paddle[4] = Vertex(-0.95f,  0.0f);
    left_paddle[5] = Vertex(-0.85f,  0.0f);

    GLuint vao;
    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );

    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(left_paddle), 

    GLuint program = init_shaders( "vshader.glsl", "fshader.glsl" );
    glUseProgram( program );

    GLuint loc = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( loc );
    glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0,


void movePaddle(Vertex* array, GLfloat change) {
    for(int i = 0; i < NUM_VERTICES; i++) {
        array[i].y = array[i].y + change;

void special( int key, int x, int y ) {
    switch ( key ) {
    case GLUT_KEY_DOWN:
        movePaddle(left_paddle, NEG_Y);

void display() {
    glDrawArrays(GL_TRIANGLES, 0, 6);

int main(int argc, char **argv) {

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);



    return 0;

share|improve this question
up vote 1 down vote accepted

You need to actually update the vPosition vertex attribute in your draw function. As is, you're always redrawing with the original value you used in the init function.

Do not be fooled by the fact that OpenGL takes pointer arguments. It does not store a reference to the memory of your position vector, but rather makes a copy of it (likely into the video memory).

share|improve this answer
Thank you! That worked perfectly :) – A D Jul 7 '12 at 5:33

Add glutIdleFunc(display); to main().

share|improve this answer
This answer could be improved by elaborating on why/how the proposed solution fixes the problem. – bornander Oct 26 '15 at 8:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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