G
G
German2019-05-16 23:23:49
C++ / C#
German, 2019-05-16 23:23:49

OpenGL not immediately rendering data from framebuffer?

There is a program that draws 500 points in random places.
The problem is that the image is updated only when the mouse clicks on it and at intervals of 1 (or a little less) second.
How to make it so that the image changes itself and without delay?
Here she is:

#include <iostream>
#include <conio.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctime>

using namespace std;

void init();
void displayFunction();

void init()
{
  glutDisplayFunc(displayFunction);
  glClearColor(1.0, 1.0, 1.0, 0.0);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(-300, 300, -300, 300);
}

void displayFunction()
{
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3f(0, 0, 0);
  glPointSize(3.0);

  glBegin(GL_POINTS);
  srand((unsigned)time(NULL));
  //генерация точек и их отрисовка
  for (unsigned int i = 0; i < 500; ++i)
  {
    bool sign = bool(rand() % 2);
    int x = 0 + rand() % 301;
    int y = 0 + rand() % 301;
    if (!sign)
      x = -x;
    sign = bool(rand() % 2);
    if (!sign)
      y = -y;
    glVertex2i(x, y);
  }
  glEnd();
  glutSwapBuffers();
  glFlush();
}

int main(int argc, char* argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowSize(600, 600);
  glutInitWindowPosition((GetSystemMetrics(SM_CXSCREEN) - 600) / 2, (GetSystemMetrics(SM_CYSCREEN) - 600) / 2);
  glutCreateWindow("Window");
  init();
  glutMainLoop();
  return 0;
}

By the way, tell me, is glFlush() needed here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shatunov, 2019-05-17
@mrjbom

I don't see in your code calling glutIdleFunc. The documentation says that GLUT will constantly call the passed function in its free time from processing system messages. And if in is glutIdleFuncpassed nullptr(or the idle function was not set), then GLUT will strictly wait for the next system message.
If I remember everything correctly, then passing a real function, in which the function will be called glutPostRedisplay, glutIdleFuncwill also lead to a regular redrawing of the screen.
As a rule, an update function is passed as an idle function to GLUT, in which the state of objects is updated before rendering.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question