I have a strange problem at the moment, I have this sample application using GTK2, OpenGL and EGL. The strange behaviour I describe only happens on nVidia GPUs on Linux with the official nVidia driver. In the program you see a comment, if you dont use glFlush OR glGetError at this point in the code, the code will not draw the triangle, it will only display red (the clear color). If you call glGetError or glFlush, it works.
Can somebody explain to me why this is happening? Here is the code:
#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <EGL/egl.h>
#include <GL/gl.h>
static EGLDisplay egl_display;
static EGLSurface egl_surface;
static EGLContext egl_context;
static void realize_cb (GtkWidget *widget)
{
EGLConfig egl_config;
EGLint n_config;
EGLint attributes[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE };
EGLint surf_attrs[] = {
EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
EGL_NONE
};
egl_display = eglGetDisplay ((EGLNativeDisplayType) gdk_x11_display_get_xdisplay (gtk_widget_get_display (widget)));
eglInitialize (egl_display, NULL, NULL);
eglChooseConfig (egl_display, attributes, &egl_config, 1, &n_config);
egl_surface = eglCreateWindowSurface (egl_display, egl_config, GDK_WINDOW_XID (gtk_widget_get_window (widget)), NULL);
eglBindAPI (EGL_OPENGL_API);
egl_context = eglCreateContext (egl_display, egl_config, EGL_NO_CONTEXT, NULL);
eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
printf("GL Version: %s\n", glGetString(GL_VERSION));
}
static gboolean on_configure (GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
gtk_widget_queue_draw(widget);
return FALSE;
}
static gboolean draw_cb (GtkWidget *widget, GdkEventExpose *expose, gpointer userdata)
{
eglMakeCurrent (egl_display, egl_surface, egl_surface, egl_context);
GtkAllocation alloc;
gtk_widget_get_allocation(widget, &alloc);
glViewport (0, 0, alloc.width, alloc.height);
glClearColor (1, 0, 0, 1);
glClearDepth(1.0);
glClear (GL_COLOR_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, 100, 0, 100, 0, 1);
glBegin (GL_TRIANGLES);
glColor3f (1, 0, 0);
glVertex2f (50, 10);
glColor3f (0, 1, 0);
glVertex2f (90, 90);
glColor3f (0, 0, 1);
glVertex2f (10, 90);
glEnd ();
/****************************
* It does not matter if you call glGetError or glFlush here, if you do, it
* works, if not, it only displays a red surface
****************************/
//glGetError();
//glFlush();
eglSwapBuffers (egl_display, egl_surface);
return TRUE;
}
int main (int argc, char **argv)
{
GtkWidget *w;
gtk_init (&argc, &argv);
w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_double_buffered (GTK_WIDGET (w), FALSE);
g_signal_connect_after (G_OBJECT (w), "realize", G_CALLBACK (realize_cb), NULL);
g_signal_connect (G_OBJECT (w), "expose-event", G_CALLBACK (draw_cb), NULL);
g_signal_connect (G_OBJECT (w), "configure-event", G_CALLBACK (on_configure), NULL);
gtk_widget_show (w);
gtk_main ();
return 0;
}
Compile with:
g++ -o gtkegl gtkegl.cc -lEGL -lGL $(pkg-config --libs --cflags gtk+-2.0) -lX11