OpenGL窗口内容复制了其后面的内容。

huangapple go评论61阅读模式
英文:

openGL window content copies what was behind it

问题

我正在尝试创建一个简单的正方形,但窗口内容复制了背后的内容,[截图](https://i.stack.imgur.com/LCVLN.png),代码如下:

```cpp
#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();
}

我使用以下命令编译:
g++ main.cpp -lglu32 -lglut32 -lopengl32 -o open.exe

我的电脑信息:

  • GPU:GMA 3150
  • CPU:Atom D525
  • RAM:4GB
  • OpenGL:1.40v
  • 64位

根据这个帖子,我将 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) 替换为 glutInitDisplayMode(GLUT_DOUBLE),并且将 glFlush() 替换为 glutSwapBuffers(),但仍然没有改变。


<details>
<summary>英文:</summary>

I am trying to create a simple square, but the window content copies what was behind it, a [screenshot](https://i.stack.imgur.com/LCVLN.png), 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(&quot;Window&quot;);

glutDisplayFunc(Draw);

Init();
glutMainLoop();

}


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

My computer info:
- GPU: GMA 3150
- CPU: atom D525
- RAM: 4GB
- openGL: 1.40v
- 64 BITS

Based on this [post](https://stackoverflow.com/questions/18477852/c-opengl-window-only-tracks-background) I replaced `glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)` to `glutInitDisplayMode(GLUT_DOUBLE)` and also replaced `glFlush()` to `glutSwapBuffers()` , however nothing changes.

</details>


# 答案1
**得分**: 0

I have translated the provided text:

最后我解决了它!解释:

1. 将 MinGW 更改为 TDM-gcc
2. 将 Glut 更改为 Freeglut
3. 将编译器设置为 64 位,`g++ **-m64** main.cpp **-lfreeglut** -lglu32 -lopengl32 -o open.exe`
4. 解决代码问题

新代码:
```cpp
#include &lt;windows.h&gt;
#include &lt;GL/gl.h&gt;
#include &lt;GL/glu.h&gt;
#include &lt;GL/freeglut.h&gt;

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();
}

// 移除 Init 函数

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

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

    glutCreateWindow(&quot;Window&quot;);

    glutDisplayFunc(Draw);

    glutMainLoop();
}

*对于未来遇到相同问题的读者,请根据您的编译器架构使用相应的库!(32 位或 64 位)

英文:

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 &lt;windows.h&gt;
#include &lt;GL/gl.h&gt;
#include &lt;GL/glu.h&gt;
#include &lt;GL/freeglut.h&gt;

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(&amp;argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE);

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

    glutCreateWindow(&quot;Window&quot;);

    glutDisplayFunc(Draw);

    glutMainLoop();
}

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

huangapple
  • 本文由 发表于 2023年6月5日 05:15:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定