having trouble with simple program

im trying to get the square thats being drawn to move left or right when the r or s key is pressed, its not really working and i know the solution is simple im just not seeing it at the moment. heres the code.

/*

  • Main.h
  • Core
    */
    #include <stdlib.h>
    #include <GLUT/glut.h>
    #include <OpenGL/gl.h>

float x;

/*

  • Main.cpp
    */

#include “Main.h”

void reshape(int width, int height)
{
glViewport(0,0,width,height);
}

void display(void)
{
glLoadIdentity();
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//glLightfv(GL_LIGHT0, GL_POSITION, const GLfloat(30.0f, 30.0f, 30.0f));

glBegin(GL_QUADS);
glColor3f(0.0f , 0.0f, 1.0f);
glVertex3f(-0.1f + x, -0.1f, 0.1f);

glColor3f(0.0f , 1.0f, 0.0f);
glVertex3f(-0.1f + x, 0.1f, 0.1f);

glColor3f(1.0f , 0.0f, 0.0f);
glVertex3f(0.1f + x, 0.1f, 0.1f);

glColor3f(1.0f , 1.0f, 0.0f);
glVertex3f(0.1f + x, -0.1f, 0.1f);
glEnd();

glFlush();

}

void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case ‘s’:
case ‘S’:
x++;
glutPostRedisplay();
break;
case ‘r’:
case ‘R’:
x++;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}

void idle(void)
{
glutPostRedisplay();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640,480);

glutCreateWindow("Core Engine Test");

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
glutMainLoop();

return 0;

}
[/b]

Hm… can you describe your problem ?
You do not set initial value to X anywhere, also your keyboard function moves X always to ++ (right?).

And you do not set matrixes at all (ok you load identitymatrix, but I think there should also be few calls to glOrtho/glFrustum/some other convenience function…
you set viewport, but a box of size 0.2f*0.2f , at 0.1f units away from you (behind you?) is going to be small. and as you jump to the right 1.0f (everytime you press ‘s’/‘S’/‘r’/‘R’), it is going to go at warp speed.

im just trying to get the square to move and its not moving when i press either key. the speed and sizes i can fix later i just want the overall concept to start working

It would help if you told us what you see on the screen when you run your program.

… a square

How much of the window does the square cover? Can you do a triangle instead of a square?

Your problem is that you have a global variable ‘x’ and a local variable ‘x’ in your Keyboard function. That’s a conflict resulting in x always being set to zero by your keyboard function. An easy fix is to change the global variable to ‘q’. So change ‘x’ to ‘q’ everywhere except in the line:

void keyboard (unsigned char key, int x, int y)

BTW - this is not an OpenGL or graphics problem. It is a basic programming problem. A problem like this is diagnosed by using a debugger to examine the value of x as the program runs, or by putting some print statements in the code to write out the value of x to the console window. If you had done that you would have seen that x was always 0.0, no matter how many times you pushed ‘S’ or ‘R’. Thus your square was not moving. Good luck.