I'm studying some basic computer graphics with gl, glu and glut and I'm writing a C example that I watched online but when I compile with
g++ main.c -o gl -lfreeglut -lopengl32 -lglu32
and then run with
.\gl.exe
no windows will appear; I have no errors in my console
#include <GL/gl.h> //Inclusione libreria GL
#include <GL/glut.h> //Inclusione libreria GLU
void init() { }
void redraw(void) {
glClear(GL_COLOR_BUFFER_BIT); //Pulizia buffer del colore (per questo tutto nero)
glBegin(GL_POINTS); //Inizio disegno primitive
glVertex2f(0.0, 0.0); //Disegno punto
glEnd(); //Fine disegno primitive
glFlush(); //Richiesta visualizzazione primitiva (definita in glBegin - glEnd)
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("Punto"); //Creazione finestra
glutDisplayFunc(redraw); //Richiamo funzione di disegno
glutMainLoop(); //Ciclo principale
}
I am on Windows 11 with a Nvidia RTX 4050 Laptop GPU
I'm studying some basic computer graphics with gl, glu and glut and I'm writing a C example that I watched online but when I compile with
g++ main.c -o gl -lfreeglut -lopengl32 -lglu32
and then run with
.\gl.exe
no windows will appear; I have no errors in my console
#include <GL/gl.h> //Inclusione libreria GL
#include <GL/glut.h> //Inclusione libreria GLU
void init() { }
void redraw(void) {
glClear(GL_COLOR_BUFFER_BIT); //Pulizia buffer del colore (per questo tutto nero)
glBegin(GL_POINTS); //Inizio disegno primitive
glVertex2f(0.0, 0.0); //Disegno punto
glEnd(); //Fine disegno primitive
glFlush(); //Richiesta visualizzazione primitiva (definita in glBegin - glEnd)
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("Punto"); //Creazione finestra
glutDisplayFunc(redraw); //Richiamo funzione di disegno
glutMainLoop(); //Ciclo principale
}
I am on Windows 11 with a Nvidia RTX 4050 Laptop GPU
Share Improve this question edited Mar 15 at 7:29 Endriu asked Mar 15 at 7:19 EndriuEndriu 93 bronze badges 2 |2 Answers
Reset to default 2From a GL syntax perspective, your program is correct; however, you're missing some initialization steps for GLUT.
When using GLUT, you need to set up display modes and specify the window size and position explicitly. Your code lacks these lines:
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400); // specify width and height
glutInitWindowPosition(100, 100); // specify window position
Also, your init()
function is currently empty, and you're not explicitly calling it. An init()
function is used to set a clear color or configure projection/view matrices. It's not strictly required for a minimal example, but it is a good practice.
Here's a correct example:
#include <GL/gl.h>
#include <GL/glut.h>
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
}
void redraw(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
glVertex2f(0.0, 0.0);
glEnd();
glFlush();
}
int main(int argc, char ** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("Punto");
init();
glutDisplayFunc(redraw);
glutMainLoop();
return 0;
}
The solution was in the mingw compiler, I started from scratch with MSYS and installed freeglut from there then it worked
exe
output fromg++
? Are you usingMingw
,Cygwin
or anything else on windows? – Iman Abdollahzadeh Commented Mar 15 at 7:49