Moving a square without moving the randomly generated points

Hello all, I am stuck with some code and I do not know how to fix it…I have a simple square which I move left, right, up and down, and I also generate some random points on screen. Now my problem is with the glutredisply function. Every time i move the sqaure the points are moving as well because the glutredisplay function redraws the screen. How can stop this from happening? I only want to move the square without the points…Below is the full code. I am using Opengl legacy…

#include "..\include\freeglut.h"	// OpenGL toolkit - in the local shared folder
#include <iostream>

//set up some constants
#define X_CENTRE 0.0      /* centre point of square */
#define Y_CENTRE 0.0
#define LENGTH   1.0      /* lengths of sides of square */

#define X_RANGE 32
#define Y_RANGE 9

GLfloat red = 1.0, green = 1.0, blue = 1.0;
GLint   xmove = 0.0, ymove = 0.0, zmove = 0.0;

void obstacles();
void square();

/* reshape callback function

/* reshape callback function
executed when window is moved or resized */
void reshape(int width, int height)
	glViewport(0, 0, width, height);
	/* uses orthographic (parallel) projection
	use xmin = -1, xmax = 1
	ymin = -1, ymax = 1
	znear = -1, zfar = 1 - not relevant here (2D) */
	glOrtho(-10.0, 10.0, -10.0, 10.0, -1.0, 1.0);

void display(void)
	glClear(GL_COLOR_BUFFER_BIT);   /* clear window */
	glColor3f(red, green, blue);        /* white drawing objects */



	glFlush();  /* execute drawing commands in buffer */

void square()
	glTranslatef(xmove, ymove, zmove);
	glVertex2f(X_CENTRE - LENGTH / 3, Y_CENTRE - LENGTH / 3);
	glVertex2f(X_CENTRE - LENGTH / 3, Y_CENTRE + LENGTH / 3);
	glVertex2f(X_CENTRE + LENGTH / 3, Y_CENTRE + LENGTH / 3);
	glVertex2f(X_CENTRE + LENGTH / 3, Y_CENTRE - LENGTH / 3);

void obstacles()

	for (int count = 0; count <= 12; count++)
		int p1 = rand() % 10 - 4;
		int p2 = rand() % 10 - 4;

		glVertex2f(p1, p2);

// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
	switch (key)
	case 'w':
		if (ymove >= 7) ymove = 0.0;
	case 's':
		//	if (ymove <= -5) ymove = 0.0;
	case 'd':
		//	if (xmove >= 5) xmove = 0.0;
	case 'a':
		//	if (xmove <= -5) xmove = 0.0;

/* graphics initialisation */
void init(void)
	glClearColor(1.0, 0.0, 0.5, 0.0);   /* window will be cleared to black */

//rename this to main(...) and change example 2 to run this main function
int main(int argc, char** argv)
	/* window management code ... */
	/* initialises GLUT and processes any command line arguments */
	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

	glutInitWindowSize(400, 400);

	glutInitWindowPosition(100, 100);




	return 0;

Your points (obstacles) move because every time you run your display function new random positions for them are calculated. If you want the points to remain at the same positions, compute those random positions once (e.g. in init), store them in an array and use those values when rendering.

Thanks a lot. That’s what I will do. The easy way to solve it was to call srand(0); at the beginning of the function but this is not a good solution…

Any advice on the following very much appreciated ::::
If you extract the keyboard input functions into their own header and cpp file and try to call them into the glutSpecialFunc you have to declare them as static to be able to call them from an object into the glutSpecialFunc?

Is there any other way without declaring them static?

GLUT callbacks need be callable from C; they can’t be object methods (i.e. non-static member functions). Any exceptions should be caught (if an exception propagates out of a callback, the program will either abort or crash).

1 Like

This is moving away from being about OpenGL, but in addition to what GClements said: non-static member functions require an object of their class in order to call them - conceptually you can think of them as having one extra parameter (that parameter is available as the this pointer inside the member function body). Therefore the signature of any non-static member function does not match the signature expected by the GLUT callbacks.

1 Like

Thanks a lot both of you. Makes more sense now.