英文:
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("Window");
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 <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();
}
// 移除 Init 函数
int main(int argc,char* argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(400,350);
glutInitWindowPosition(10,10);
glutCreateWindow("Window");
glutDisplayFunc(Draw);
glutMainLoop();
}
*对于未来遇到相同问题的读者,请根据您的编译器架构使用相应的库!(32 位或 64 位)
英文:
Finally I solved it! explanation:
- Change MinGW to TDM-gcc
- Change Glut to Freeglut
- Set compiler to 64bits,
g++ **-m64** main.cpp **-lfreeglut** -lglu32 -lopengl32 -o open.exe
- 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论