DEV Community

dwarfŧ
dwarfŧ

Posted on

Creating a window in opengl

step 1
Install opengl on your device. I am using ubuntu but you can use any os. I installed opengl from here.

step 2
Creating a window is the hardest thing i could find tutorials on. All of them were outdated or used a weird extra library that slowed your computer down.
however this is the working method:

#include<GL/freeglut.h>

//Program to create an empty Widdow
void init(){
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);  //Line C
    glutInitWindowSize(640,480);
    glutInitWindowPosition(1000,200);
    glutCreateWindow("Simple Window");
}

void display()
{
    glClearColor(1.0,1.0,1.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    // gluOrtho2D(0.0,100.0,0,100.0);
    glFlush();
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);           //Line A
    init();                 //Line B
    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

step 3
compiling is weird for c++. You would just think it would include the library for you, but NO. You have to specify them.

here's what worked for me:

g++ main.cpp -o main -lGLU -lglut -lGL
Enter fullscreen mode Exit fullscreen mode

step 4
enjoy and code until your heart is content.

made by dwarfŧ. :)

Top comments (3)

Collapse
 
abstratovcm profile image
Vinícius do Carmo Melício

What is glFlush() for in this context?

Collapse
 
dwarft profile image
dwarfŧ

if you remove it the background color will not be white (for linux anyway) but thanky you for the question :)

Collapse
 
abstratovcm profile image
Vinícius do Carmo Melício

I was wondering why my project didn't need a glFlush to change the background color, but it calls a function called glutSwapBuffers that apparently does an implicit glFlush. It was cool to find out, thanks for the post!