c++ - 为什么多个函数调用在 Opengl 中不起作用?
问题描述
我正在尝试使用不同的函数来制作各种对象并由一个函数调用,就像display()
在我的 Opengl 项目中一样。但是它不起作用,当我调用两个函数然后显示一个函数另一个没有显示时,我不知道为什么,
我试过这样:
#include <windows.h> // for MS Windows
#include <GL/glut.h> // GLUT, include glu.h and gl.h
#include <math.h>
void box() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
// Draw a Red 1x1 Square centered at origin
glBegin(GL_POLYGON); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.9f, -0.7f);
glVertex2f(-0.9f, 0.7f);
glVertex2f(0.9f, 0.7f);
glVertex2f(0.9f, -0.7f); // x, y
glEnd();
glFlush(); // Render now
}
void triangle()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON); // Each set of 4 vertices form a quad
glColor3f(0.0f, 1.0f, 0.0f); // green
glVertex2f(-0.9f, -0.7f);
glVertex2f(-0.4f, 0.7f);
glVertex2f(0.1f, -0.7f); // x, y
glEnd();
glFlush(); // Render now
}
void display()
{
box();
triangle();
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(1000, 600);// Set the window's initial width & height
glutCreateWindow("OpenGL Setup Test");
//gluOrtho2D(-0.1,0.7,-0.1,0.3); // Create a window with the given title
//glutInitWindowSize(500, 500);// Set the window's initial width & height
glutDisplayFunc(display);// Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}
请有任何建议。
解决方案
您在渲染每个形状之前清除缓冲区,从而擦除您刚刚绘制的前一个形状。相反,您应该只清除一次display
。同样适用glFlush
:
void box() {
// Draw a Red 1x1 Square centered at origin
glBegin(GL_POLYGON);
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.9f, -0.7f);
glVertex2f(-0.9f, 0.7f);
glVertex2f(0.9f, 0.7f);
glVertex2f(0.9f, -0.7f); // x, y
glEnd();
}
void triangle()
{
glBegin(GL_POLYGON);
glColor3f(0.0f, 1.0f, 0.0f); // green
glVertex2f(-0.9f, -0.7f);
glVertex2f(-0.4f, 0.7f);
glVertex2f(0.1f, -0.7f); // x, y
glEnd();
}
void display()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
box();
triangle();
glFlush(); // Render now
}
推荐阅读
- sql - 计数成对组合雪花
- pine-script - 根据价格收盘价高于或低于某些水平价格水平进行交易
- python - Python windows屏幕截图 - 连续流问题
- javascript - 如何在 pug 模板中使用 onClick 函数返回的值?
- c# - 在 WPF 中将自定义类与验证绑定
- database - 如何根据学生设计结果
- javascript - “获取文档时出错:FirebaseError:找不到匹配的索引。” -- 尝试在两个字段上使用 WHERE 子句
- android - 如何在 Android 中确定关闭 APK 的原因是什么?
- c# - 如何转换 IEnumerable
> 查找 ? - java - JDA - VoiceChannel#getMembers() 没有返回语音用户