c - 无法在openGL中绘制对象
问题描述
我在第一个窗口的 opengl 中绘制了一个正方形,当我尝试在第二个屏幕上绘制一些对象时。我得到一个空白屏幕。
这是我的代码。
#include <GL/glut.h>
void display() {
glClearColor(1.0f, 0.0f, 0.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_QUADS); // Each set of 4 vertices form a quad
glColor3f(0.0f, 1.0f, 0.0f); // Red
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Render now
}
void displayc2()
{
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(0.0f, 1.0f, 0.0f); // green
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
}
void keycb(unsigned char key,int x , int y)
{
int win2;
if(key=='a') exit(0);
else if(key == 'b')
{
win2 = glutCreateWindow("window 2");
glutInitWindowSize(450, 450); // Set the window's initial width & height
glutInitWindowPosition(50, 50);
glutDisplayFunc(displayc2);
glutMainLoop(); // Enter the event-processing loop
}
}
int main(int argc, char** argv) {
int win1;
glutInit(&argc, argv); // Initialize GLUT
win1 = glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutInitWindowSize(450, 450); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutKeyboardFunc(keycb);
glutMainLoop(); // Enter the event-processing loop
return 0;
}
我想做的是,当我按键盘上的字符“b”时,它应该显示第二个屏幕。第一个屏幕和对象成功出现但是,在这里我得到了第二个屏幕,但我没有在第二个屏幕中得到对象。在这种情况下,第二个屏幕是空白的。告诉我这段代码有什么问题还是有其他方法可以实现这一点?
我正在使用 C 编程在 ubuntu 18.04 中进行 opengl。
解决方案
使您的问题有效的一些说明:
在使用之前
glutDisplayFunc
,您必须选择窗口。如果你只有一个窗口,问题不问,但如果你有两个,你必须先打电话glutSetWindow(...)
。还要注意该
glutInitWindow...
功能适用于要创建的下一个窗口。glutMainLoop
应该调用一次。- 最后,不要忘记
glFlush()
在函数结束时display
调用:
#include <GL/glut.h>
int win1, win2;
void display()
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f(0.5f, -0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Render now
}
void displayc2()
{
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(0.0f, 1.0f, 0.0f); // green
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f(0.5f, -0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Render now
}
void keycb(unsigned char key, int x, int y)
{
if (key == 'a')
exit(0);
else if (key == 'b'&&win2==0) {
glutInitWindowSize(450, 450);
glutInitWindowPosition(250, 250);
win2 = glutCreateWindow("window 2");
// Select the window for glutDisplayFunc
glutSetWindow(win2);
glutDisplayFunc(displayc2);
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv); // Initialize GLUT
glutInitWindowSize(450, 450); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
win1 = glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutKeyboardFunc(keycb);
glutMainLoop(); // Enter the event-processing loop
return 0;
}
推荐阅读
- c++ - 尝试使用 MSVC 工具集 14.28.29910 构建 OpenCV,但在构建期间出现 MSVC 14.29.30033 错误
- javascript - Sequelize 多对多关系过滤
- google-apps-script - 如何获取下拉列表的预填充链接
- asp.net - ASP.NET 核心应用 Windows 身份验证,在 EC2 Linux 实例上运行
- php - 无法在 Google 日历中为不同用户创建活动
- rust - 如何从内部过滤器传播错误
- python - 我想在 tkinter 的计算器中绑定键盘号码,有人可以帮我吗?
- php - livewire 表单不通过 laravel
- php - Phalcon在mac m1上安装失败
- mysql - 将 Docker Superset 连接到 Windows 本地数据库