Valid OpenGL Context

How and at what stage is the OpenGL context created in my code valid? I get errors even in plain OpenGL code.

+3
source share
1 answer

From the posts, comp.graphics.api.openglit seems that most newbies burn their hands in their first OpenGL program. In most cases, an error occurs because OpenGL functions are called before the correct OpenGL context is created. OpenGL is a state machine. Only after the machine has been started and humming in a ready state can it be started.

Here is simple code to create a valid OpenGL context:

#include <stdlib.h>
#include <GL/glut.h>

// Window attributes
static const unsigned int WIN_POS_X = 30;
static const unsigned int WIN_POS_Y = WIN_POS_X;
static const unsigned int WIN_WIDTH = 512;
static const unsigned int WIN_HEIGHT = WIN_WIDTH;

void glInit(int, char **);

int main(int argc, char * argv[])
{
    // Initialize OpenGL
    glInit(argc, argv);

    // A valid OpenGL context has been created.
    // You can call OpenGL functions from here on.

    glutMainLoop();

    return 0;
}

void glInit(int argc, char ** argv)
{
    // Initialize GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowPosition(WIN_POS_X, WIN_POS_Y);
    glutInitWindowSize(WIN_WIDTH, WIN_HEIGHT);
    glutCreateWindow("Hello OpenGL!");

    return;
}

Note:

  • Challenge of interest here glutCreateWindow(). It not only creates a window, but also creates an OpenGL context.
  • , glutCreateWindow(), , glutMainLoop().
+4

Source: https://habr.com/ru/post/1696431/


All Articles