带有 Gtk+ 的 OpenGL,尽管清除了背景,但仍未绘制形状
OpenGL with Gtk+, shapes are not being drawn despite background being cleared
我正在尝试让 OpenGL 与 gtk+ 一起工作。它似乎是工作尺寸我能够清除背景颜色。但是,当我去画东西时,它不在那里。我错过了什么吗?我把眼睛放在 10, 10, 10 处,我正在看原点。我应该在原点附近看到一个后三角形。
#include <gtk/gtk.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
GtkWidget* wnd;
GtkWidget* glarea;
static gboolean render(GtkGLArea *area, GdkGLContext *context)
{
int w = gtk_widget_get_allocated_width(GTK_WIDGET(area));
int h = gtk_widget_get_allocated_height(GTK_WIDGET(area));
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(180, (double)w / (double)h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(10, 10, 10, 0, 0, 0, 0, 1, 0);
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 0, 0);
glBegin(GL_TRIANGLES);
glVertex3f(0, 0, 0);
glVertex3f(-1, 2, -1);
glVertex3f(1, 3, 2);
glEnd();
return TRUE;
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
glarea = gtk_gl_area_new();
gtk_container_add(GTK_CONTAINER(wnd), glarea);
g_signal_connect(wnd, "destroy", gtk_main_quit, 0);
g_signal_connect(glarea, "render", G_CALLBACK(render), NULL);
gtk_widget_show_all(wnd);
gtk_main();
return 0;
}
来源:Emanuele Bassi's blog - GTK+ developer
[...] The OpenGL support inside GTK+ requires core GL profiles, and thus it won’t work with the fixed pipeline API that was common until OpenGL 3.2 and later versions. this means that you won’t be able to use API like glRotatef(), or glBegin()/glEnd() pairs, or any of that stuff.
解决方案:删除固定功能管道。
我正在尝试让 OpenGL 与 gtk+ 一起工作。它似乎是工作尺寸我能够清除背景颜色。但是,当我去画东西时,它不在那里。我错过了什么吗?我把眼睛放在 10, 10, 10 处,我正在看原点。我应该在原点附近看到一个后三角形。
#include <gtk/gtk.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
GtkWidget* wnd;
GtkWidget* glarea;
static gboolean render(GtkGLArea *area, GdkGLContext *context)
{
int w = gtk_widget_get_allocated_width(GTK_WIDGET(area));
int h = gtk_widget_get_allocated_height(GTK_WIDGET(area));
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(180, (double)w / (double)h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(10, 10, 10, 0, 0, 0, 0, 1, 0);
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 0, 0);
glBegin(GL_TRIANGLES);
glVertex3f(0, 0, 0);
glVertex3f(-1, 2, -1);
glVertex3f(1, 3, 2);
glEnd();
return TRUE;
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
glarea = gtk_gl_area_new();
gtk_container_add(GTK_CONTAINER(wnd), glarea);
g_signal_connect(wnd, "destroy", gtk_main_quit, 0);
g_signal_connect(glarea, "render", G_CALLBACK(render), NULL);
gtk_widget_show_all(wnd);
gtk_main();
return 0;
}
来源:Emanuele Bassi's blog - GTK+ developer
[...] The OpenGL support inside GTK+ requires core GL profiles, and thus it won’t work with the fixed pipeline API that was common until OpenGL 3.2 and later versions. this means that you won’t be able to use API like glRotatef(), or glBegin()/glEnd() pairs, or any of that stuff.
解决方案:删除固定功能管道。