Operation System: windows 10
BuildEnv: msys2 + mingw32
GTK Version: 3.24.33
I’m new to opengl , i use GtkGLArea widget to display a image on win32 platform, but it didn’t work, the window’s background color is changed, but image not display, main code as follow:
static gboolean render(GtkGLArea *area, GdkGLContext *context)
{
//work, background color changed
glClearColor (0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
cairo_surface_t* surface = cairo_image_surface_create_from_png("./test.PNG");
unsigned char* data= cairo_image_surface_get_data(surface);
//not work
glRasterPos2f(-1,1);
glPixelZoom( 1, -1 );
glDrawPixels(
cairo_image_surface_get_width(surface),
cairo_image_surface_get_height(surface),
GL_BGRA,GL_UNSIGNED_BYTE,data);
cairo_surface_destroy(surface);
return TRUE;
}
static void activate(GtkApplication *app, gpointer user_data)
{
GtkWidget* window;
GtkWidget* gl_area;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "picplayer-gl");
gtk_window_set_default_size(GTK_WINDOW(window), 1920, 1080);
gl_area = gtk_gl_area_new();
gtk_container_add(GTK_CONTAINER(window),gl_area);
g_signal_connect(gl_area,"render",G_CALLBACK(render),NULL);
gtk_widget_show_all(window);
}
int main (int argc, char *argv[])
{
GtkApplication *app;
int status;
app = gtk_application_new("org.gtk3.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
and i also test the follow code, use texture, but not work
static gboolean render(GtkGlArea *area, GdkGlContext *content)
{
//work, background color changed
glClearColor (0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
cairo_surface_t* surface = cairo_image_surface_create_from_png("./test.png");
unsigned char* data= cairo_image_surface_get_data(surface);
//use texture , not work
GLuint texture = 0;
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,
cairo_image_surface_get_width(surface),
cairo_image_surface_get_height(surface),
0,GL_BGRA_EXT,GL_UNSIGNED_BYTE,data);
cairo_surface_destroy(surface);
glFlush();
return TRUE;
}
what’s wrong with it ? the gtk3-opengl demo draw triangle is ok,and the above code on glfw works well