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);
glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT); /* clear window */
glColor3f(red, green, blue); /* white drawing objects */
obstacles();
square();
glFlush(); /* execute drawing commands in buffer */
}
void square()
{
glPushMatrix();
glTranslatef(xmove, ymove, zmove);
glBegin(GL_POLYGON);
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);
glEnd();
glPopMatrix();
glFlush();
}
void obstacles()
{
glPushMatrix();
glPointSize(2);
glBegin(GL_POINTS);
for (int count = 0; count <= 12; count++)
{
//srand(time(NULL));
int p1 = rand() % 10 - 4;
int p2 = rand() % 10 - 4;
glVertex2f(p1, p2);
}
glEnd();
glPopMatrix();
glFlush();
}
// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch (key)
{
case 'w':
ymove++;
if (ymove >= 7) ymove = 0.0;
break;
case 's':
ymove--;
// if (ymove <= -5) ymove = 0.0;
break;
case 'd':
xmove++;
// if (xmove >= 5) xmove = 0.0;
break;
case 'a':
xmove--;
// if (xmove <= -5) xmove = 0.0;
break;
}
glutPostRedisplay();
}
/* 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);
glutCreateWindow("Square");
glutKeyboardFunc(keyInput);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}