最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - glutCreateWindow not showing any window - Stack Overflow

programmeradmin1浏览0评论

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
  • How do you expect to have an exe output from g++? Are you using Mingw, Cygwin or anything else on windows? – Iman Abdollahzadeh Commented Mar 15 at 7:49
  • I was using mingw and the way I installed it was the problem, I started again with msys and the problem was solved, it shows the window output now – Endriu Commented Mar 16 at 8:18
Add a comment  | 

2 Answers 2

Reset to default 2

From 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

发布评论

评论列表(0)

  1. 暂无评论