I want to write a simple game on openGL, in which there will be red dots, and a blue dot, with which you have to collect them, moving the blue dot with the arrow keys. Moreover, the red move faster than the blue.

Just in case, if it's not difficult, tell me how to make it so that the screen does not display a static image, but a moving one? There are no options, as they did not work.

  • one
    There are only two options for outputting a “non-static” image (i.e., animated): 1. You erase old objects (for example, by drawing them in black, or by reproducing a previously stored background element in their place), then drawing objects in new places. 2. You redraw the ALL picture (perhaps, just completely clear the "screen" filling everything with black, then drawing objects) ... - gote
  • look at OpenGL SuperBible, in it among the first examples is the movement of a rectangle around the application window - Yury Shadchnev

2 answers 2

Usually like this:

  1. Initialization
  2. Check "input" - i.e. keystrokes, mouse, etc.
  3. Received "exit" signal? - if yes, to 8
  4. Counting all game objects
  5. Drawing all objects in the back buffer (off-screen buffer, memory surface, somehow, in memory) is shorter
  6. Conclusion drawn on the screen.
  7. Return to 2
  8. Deinitialization, release of resources.

    Tied to the arrows on the keyboard the turn of the scene, was this how

    #include <GLUT.H> #include <Windows.h> #include <tchar.h> #include <iostream> #include <cmath> using namespace std; bool g_is_full_screen = false; GLfloat winH = 100.f, winW = 100.f; GLfloat xRot = 0.0f, yRot = 0.0f; GLfloat rotationSpeed = 2.0f; void drawAxises() { glShadeModel(GL_SMOOTH); glBegin(GL_LINES); glColor3f(1.0f, 1.0f, 1.0f); glVertex3f(0.0f, 0.0f, 0.0f); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(90.0f, 0.0f, 0.0f); glColor3f(1.0f, 1.0f, 1.0f); glVertex3f(0.0f, 0.0f, 0.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(0.0f, 90.0f, 0.0f); glColor3f(1.0f, 1.0f, 1.0f); glVertex3f(0.0f, 0.0f, 0.0f); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(0.0f, 0.0f, 90.0f); glEnd(); glShadeModel(GL_FLAT); } void renderScene() { glEnable(GL_DEPTH_TEST); glEnable(GL_STENCIL_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glStencilFunc(GL_NEVER, 0x0, 0x0); glStencilOp(GL_INCR, GL_INCR, GL_INCR); glFrontFace(GL_CW); glPushMatrix(); glRotatef(xRot, 10.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 10.0f, 0.0f); glColor3f(1.0f, 1.0f, 1.0f); GLubyte bitmap[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x07, 0xf0, 0x0f, 0x00, 0x1f, 0xe0, 0x1f, 0x80, 0x1f, 0xc0, 0x0f, 0xc0, 0x3f, 0x80, 0x07, 0xe0, 0x7e, 0x00, 0x03, 0xf0, 0xff, 0x80, 0x03, 0xf5, 0xff, 0xe0, 0x07, 0xfd, 0xff, 0xf8, 0x1f, 0xfc, 0xff, 0xe8, 0xff, 0xe3, 0xbf, 0x70, 0xde, 0x80, 0xb7, 0x00, 0x71, 0x10, 0x4a, 0x80, 0x03, 0x10, 0x4e, 0x40, 0x02, 0x88, 0x8c, 0x20, 0x05, 0x05, 0x04, 0x40, 0x02, 0x82, 0x14, 0x40, 0x02, 0x40, 0x10, 0x80, 0x02, 0x64, 0x1a, 0x80, 0x00, 0x92, 0x29, 0x00, 0x00, 0xb0, 0x48, 0x00, 0x00, 0xc8, 0x90, 0x00, 0x00, 0x85, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, }; glEnable(GL_POLYGON_STIPPLE); glPolygonStipple(bitmap); glBegin(GL_QUADS); glVertex3f(-50.0f, 50.0f, 5.0f); glVertex3f(50.0f, 50.0f, 5.0f); glVertex3f(50.0f, -50.0f, 5.0f); glVertex3f(-50.0f, -50.0f, 5.0f); glEnd(); glStencilFunc(GL_NOTEQUAL, 0x1, 0x1); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); glDisable(GL_POLYGON_STIPPLE); glEnable(GL_CULL_FACE); glColor3f(1.0f, 1.0f, 1.0f); glBegin(GL_TRIANGLE_FAN); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(30.0f, 30.0f, 30.0f); glVertex3f(20.0f, 40.0f, 30.0f); glVertex3f(40.0f, 40.0f, 30.0f); glVertex3f(40.0f, 20.0f, 30.0f); glVertex3f(20.0f, 20.0f, 30.0f); glVertex3f(20.0f, 40.0f, 30.0f); glEnd(); glFrontFace(GL_CCW); glBegin(GL_TRIANGLE_FAN); glColor3f(1.0f, 1.0f, 1.0f); glVertex3f(30.0f, 30.0f, 60.0f); glVertex3f(20.0f, 40.0f, 30.0f); glVertex3f(40.0f, 40.0f, 30.0f); glColor3f(0.5f, 0.5f, 0.5f); glVertex3f(40.0f, 20.0f, 30.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(20.0f, 20.0f, 30.0f); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(20.0f, 40.0f, 30.0f); glEnd(); glDisable(GL_CULL_FACE); drawAxises(); glPopMatrix(); glutSwapBuffers(); } void setupRC() { glClearColor(0.f, 0.f, 0.f, 1.f); glClearStencil(0.0f); } void changeSize(GLsizei w, GLsizei h) { GLfloat aspectRatio; if (h == 0) h = 1; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); aspectRatio = (GLfloat)w / (GLfloat)h; if (w <= h) { winW = 100.f; winH = 100.f / aspectRatio; glOrtho(-100.0, 100.0, - winH, winH, 100.0, -100.0); } else { winH = 100.f; winW = 100.f * aspectRatio; glOrtho(-winW, winW, -100.0, 100.0 , 100.0, -100.0); } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void specialFunc(int key, int x, int y) { switch(key) { case GLUT_KEY_UP: xRot -= rotationSpeed; break; case GLUT_KEY_DOWN: xRot += rotationSpeed; break; case GLUT_KEY_RIGHT: yRot += rotationSpeed; break; case GLUT_KEY_LEFT: yRot -= rotationSpeed; break; case GLUT_KEY_F1: if(g_is_full_screen) { glutReshapeWindow(400, 400); } else { glutFullScreen(); } g_is_full_screen = !g_is_full_screen; break; } glutPostRedisplay(); } /* for console application */ /* int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitWindowSize(400, 400); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_STENCIL); glutCreateWindow("SimpleWindows"); glutDisplayFunc(renderScene); glutReshapeFunc(changeSize); glutSpecialFunc(specialFunc); setupRC(); glutMainLoop(); } */ /* for win application */ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow) { //MessageBox(NULL, L"Simple Program.", L"WINAPI + OpenGL", 0); //glutInit(0, 0); glutInitWindowSize(400, 400); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_STENCIL); glutCreateWindow("SimpleWindows"); glutDisplayFunc(renderScene); glutReshapeFunc(changeSize); glutSpecialFunc(specialFunc); setupRC(); glutMainLoop(); return 0; }