Bonjour à tous,
Je suis tombé un peu par hasard sur un programme (gtk2) qui permet de la 3D sous gtk sans passer par une quelconque librairie. La source d'origine est :
Ça se compile par :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 #include <stdlib.h> #include <GL/gl.h> #include <GL/glx.h> #include <gtk/gtk.h> #include <gdk/gdkx.h> GLXContext context; int area_start (GtkWidget *widget, void *data) { GdkWindow *window; Display *display; int id; window = gtk_widget_get_window (widget); display = gdk_x11_drawable_get_xdisplay (window); id = gdk_x11_drawable_get_xid (window); if (glXMakeCurrent (display, id, context) == TRUE) { glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL); glEnable (GL_CULL_FACE); glCullFace (GL_BACK); glDisable (GL_DITHER); glShadeModel (GL_SMOOTH); } return TRUE; } int area_expose (GtkWidget *widget, GdkEventExpose *event, void *data) { GdkWindow *window; Display *display; int id; if (event->count > 0) return TRUE; window = gtk_widget_get_window (widget); display = gdk_x11_drawable_get_xdisplay (window); id = gdk_x11_drawable_get_xid (window); if (glXMakeCurrent (display, id, context) == TRUE) { glClear (GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,100, 100,0, -1,1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1,1,1); glBegin(GL_TRIANGLES); glVertex2f(10,10); glVertex2f(10,90); glVertex2f(90,90); glEnd(); glXSwapBuffers (display, id); } return TRUE; } int area_configure (GtkWidget *widget, GdkEventConfigure *event, void *data) { GtkAllocation allocation; GdkWindow *window; Display *display; int id; window = gtk_widget_get_window (widget); display = gdk_x11_drawable_get_xdisplay (window); id = gdk_x11_drawable_get_xid (window); if (glXMakeCurrent (display, id, context) == TRUE) { gtk_widget_get_allocation (widget, &allocation); glViewport (0, 0, allocation.width, allocation.height); } return TRUE; } int main (int argc, char **argv) { GtkWidget *window; GtkWidget *area; GdkVisual* visual; GdkColormap *colormap; GdkScreen *screen; XVisualInfo *xvisual; Colormap xcolormap; Display *display; Window root; int xscreen; int attributes[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, True, GLX_DEPTH_SIZE, 12, None }; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW (window), "GLX"); area = gtk_drawing_area_new (); gtk_widget_set_double_buffered (area, FALSE); display = gdk_x11_get_default_xdisplay (); xscreen = DefaultScreen (display); screen = gdk_screen_get_default (); xvisual = glXChooseVisual (display, xscreen, attributes); visual = gdk_x11_screen_lookup_visual (screen, xvisual->visualid); root = RootWindow (display, xscreen); xcolormap = XCreateColormap (display, root, xvisual->visual, AllocNone); colormap = gdk_x11_colormap_foreign_new (visual, xcolormap); gtk_widget_set_colormap (area, colormap); context = glXCreateContext (display, xvisual, NULL, TRUE); free (xvisual); gtk_container_add (GTK_CONTAINER (window), area); gtk_widget_set_size_request (area, 100, 100); g_signal_connect (area, "expose_event", G_CALLBACK (area_expose), window); g_signal_connect (area, "configure_event", G_CALLBACK (area_configure), window); g_signal_connect (area, "realize", G_CALLBACK (area_start), window); gtk_widget_set_events (GTK_WIDGET (area), GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK); gtk_widget_show (area); gtk_widget_show (window); gtk_main (); glXDestroyContext (display, context); XFreeColormap (display, xcolormap); XFree (xvisual); g_object_unref (G_OBJECT (colormap)); g_object_unref (G_OBJECT (visual)); return 0; }
gcc glx.c `pkg-config --cflags --libs gtk+-2.0` -o glx
Après quelques très très longues minutes à chercher sur internet et le guide de migration, j'ai réussi à appliquer les modifications suivantes :
soit en code complet (plus facile à tester) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 --- gtk_opengl/glx.c 2011-06-27 15:43:58.000000000 +0200 +++ glx.c 2011-12-28 23:29:38.036858256 +0100 @@ -13,8 +13,8 @@ Display *display; int id; window = gtk_widget_get_window (widget); -display = gdk_x11_drawable_get_xdisplay (window); -id = gdk_x11_drawable_get_xid (window); +display = gdk_x11_display_get_xdisplay(gdk_window_get_display(window)); +id = gdk_x11_window_get_xid (window); if (glXMakeCurrent (display, id, context) == TRUE) { @@ -38,8 +38,8 @@ int id; if (event->count > 0) return TRUE; window = gtk_widget_get_window (widget); -display = gdk_x11_drawable_get_xdisplay (window); -id = gdk_x11_drawable_get_xid (window); +display = gdk_x11_display_get_xdisplay(gdk_window_get_display(window)); +id = gdk_x11_window_get_xid (window); if (glXMakeCurrent (display, id, context) == TRUE) { @@ -75,8 +75,8 @@ Display *display; int id; window = gtk_widget_get_window (widget); -display = gdk_x11_drawable_get_xdisplay (window); -id = gdk_x11_drawable_get_xid (window); +display = gdk_x11_display_get_xdisplay(gdk_window_get_display(window)); +id = gdk_x11_window_get_xid (window); if (glXMakeCurrent (display, id, context) == TRUE) { @@ -92,7 +92,6 @@ int main (int argc, char **argv) GtkWidget *window; GtkWidget *area; GdkVisual* visual; -GdkColormap *colormap; GdkScreen *screen; XVisualInfo *xvisual; Colormap xcolormap; @@ -117,16 +116,15 @@ xvisual = glXChooseVisual (display, xscr visual = gdk_x11_screen_lookup_visual (screen, xvisual->visualid); root = RootWindow (display, xscreen); xcolormap = XCreateColormap (display, root, xvisual->visual, AllocNone); -colormap = gdk_x11_colormap_foreign_new (visual, xcolormap); -gtk_widget_set_colormap (area, colormap); +gtk_widget_set_visual(window, visual); context = glXCreateContext (display, xvisual, NULL, TRUE); free (xvisual); gtk_container_add (GTK_CONTAINER (window), area); gtk_widget_set_size_request (area, 100, 100); -g_signal_connect (area, "expose_event", G_CALLBACK (area_expose), window); -g_signal_connect (area, "configure_event", G_CALLBACK (area_configure), window); +g_signal_connect (area, "draw", G_CALLBACK (area_expose), window); +g_signal_connect (area, "configure-event", G_CALLBACK (area_configure), window); g_signal_connect (area, "realize", G_CALLBACK (area_start), window); gtk_widget_set_events (GTK_WIDGET (area), GDK_EXPOSURE_MASK | @@ -142,7 +140,6 @@ gtk_main (); glXDestroyContext (display, context); XFreeColormap (display, xcolormap); XFree (xvisual); -g_object_unref (G_OBJECT (colormap)); g_object_unref (G_OBJECT (visual)); return 0;
Tout ça se compile avec : gcc glx.c `pkg-config --cflags --libs gtk+-3.0` -o glx
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 #include <stdlib.h> #include <GL/gl.h> #include <GL/glx.h> #include <gtk/gtk.h> #include <gdk/gdkx.h> GLXContext context; int area_start (GtkWidget *widget, void *data) { GdkWindow *window; Display *display; int id; window = gtk_widget_get_window (widget); display = gdk_x11_display_get_xdisplay(gdk_window_get_display(window)); id = gdk_x11_window_get_xid (window); if (glXMakeCurrent (display, id, context) == TRUE) { glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL); glEnable (GL_CULL_FACE); glCullFace (GL_BACK); glDisable (GL_DITHER); glShadeModel (GL_SMOOTH); } return TRUE; } int area_expose (GtkWidget *widget, GdkEventExpose *event, void *data) { GdkWindow *window; Display *display; int id; if (event->count > 0) return TRUE; window = gtk_widget_get_window (widget); display = gdk_x11_display_get_xdisplay(gdk_window_get_display(window)); id = gdk_x11_window_get_xid (window); if (glXMakeCurrent (display, id, context) == TRUE) { glClear (GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,100, 100,0, -1,1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1,1,1); glBegin(GL_TRIANGLES); glVertex2f(10,10); glVertex2f(10,90); glVertex2f(90,90); glEnd(); glXSwapBuffers (display, id); } return TRUE; } int area_configure (GtkWidget *widget, GdkEventConfigure *event, void *data) { GtkAllocation allocation; GdkWindow *window; Display *display; int id; window = gtk_widget_get_window (widget); display = gdk_x11_display_get_xdisplay(gdk_window_get_display(window)); id = gdk_x11_window_get_xid (window); if (glXMakeCurrent (display, id, context) == TRUE) { gtk_widget_get_allocation (widget, &allocation); glViewport (0, 0, allocation.width, allocation.height); } return TRUE; } int main (int argc, char **argv) { GtkWidget *window; GtkWidget *area; GdkVisual* visual; GdkScreen *screen; XVisualInfo *xvisual; Colormap xcolormap; Display *display; Window root; int xscreen; int attributes[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, True, GLX_DEPTH_SIZE, 12, None }; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW (window), "GLX"); area = gtk_drawing_area_new (); gtk_widget_set_double_buffered (area, FALSE); display = gdk_x11_get_default_xdisplay (); xscreen = DefaultScreen (display); screen = gdk_screen_get_default (); xvisual = glXChooseVisual (display, xscreen, attributes); visual = gdk_x11_screen_lookup_visual (screen, xvisual->visualid); root = RootWindow (display, xscreen); xcolormap = XCreateColormap (display, root, xvisual->visual, AllocNone); gtk_widget_set_visual(window, visual); context = glXCreateContext (display, xvisual, NULL, TRUE); free (xvisual); gtk_container_add (GTK_CONTAINER (window), area); gtk_widget_set_size_request (area, 100, 100); g_signal_connect (area, "draw", G_CALLBACK (area_expose), window); g_signal_connect (area, "configure-event", G_CALLBACK (area_configure), window); g_signal_connect (area, "realize", G_CALLBACK (area_start), window); gtk_widget_set_events (GTK_WIDGET (area), GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK); gtk_widget_show (area); gtk_widget_show (window); gtk_main (); glXDestroyContext (display, context); XFreeColormap (display, xcolormap); XFree (xvisual); g_object_unref (G_OBJECT (visual)); return 0; }
Seulement, voilà... ça ne marche pas... J'ai une belle fenêtre grise où rien ne s'affiche. J'ai fait une boulette quelque part ? Parce que depuis le temps que je cherche comment faire de la 3D via OpenGl sous gtk3...
Merci.
Partager