fernando
fernando

Reputation: 1

openGL window content copies what was behind it

I am trying to create a simple square, but the window content copies what was behind it, a screenshot, the code:

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

void Draw(void){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
                   
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_QUADS);
       glColor3f(1.0f,0.0f,0.0f);
       glVertex2i(100,150);
       glVertex2i(100,100);

       glColor3f(0.0f,0.0f,1.0f);
       glVertex2i(150,100);
       glVertex2i(150,150);               
    glEnd();

    glutSwapBuffers();
}

void Init(void){   
    glClearColor(0.0f,0.0f,0.0f,1.0f);
}

int main(int argc,char* argv[]){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE);

    glutInitWindowSize(400,350);
    glutInitWindowPosition(10,10);

    glutCreateWindow("Window");

    glutDisplayFunc(Draw);

    Init();
    glutMainLoop();
}

I compile with this command: g++ main.cpp -lglu32 -lglut32 -lopengl32 -o open.exe

My computer info:

Based on this post I replaced glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) to glutInitDisplayMode(GLUT_DOUBLE) and also replaced glFlush() to glutSwapBuffers() , however nothing changes.

Upvotes: 0

Views: 71

Answers (1)

fernando
fernando

Reputation: 1

Finally I solved it! explanation:

  1. Change MinGW to TDM-gcc
  2. Change Glut to Freeglut
  3. Set compiler to 64bits, g++ **-m64** main.cpp **-lfreeglut** -lglu32 -lopengl32 -o open.exe
  4. Solve code problem

new code:

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>

void Draw(void){
    //*
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,400,0,350,-1,1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //*
    glClearColor(0,0,0,1.0);

    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_QUADS);
       glColor3f(1.0f,0.0f,0.0f);
       glVertex2i(100,150);
       glVertex2i(100,100);

       glColor3f(0.0f,0.0f,1.0f);
       glVertex2i(150,100);
       glVertex2i(150,150);               
    glEnd();

    glutSwapBuffers();
}

//Remove Init function

int main(int argc,char* argv[]){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE);

    glutInitWindowSize(400,350);
    glutInitWindowPosition(10,10);

    glutCreateWindow("Window");

    glutDisplayFunc(Draw);

    glutMainLoop();
}

*For future readers with the same problem, use the libraries according to your compiler architecture! (32bits or 64bits)

Upvotes: 0

Related Questions