-
Notifications
You must be signed in to change notification settings - Fork 0
/
glxanothersample.cpp
106 lines (91 loc) · 2.91 KB
/
glxanothersample.cpp
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
//gcc -Wall testGL.c -o testGL -lGL -lGLU -lX11 -lm `pkg-config --cflags --libs gtk+-3.0 gdk-x11-3.0`
#include<X11/Xlib.h>
#include<GL/glx.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<gtk/gtk.h>
#include<gdk/gdkx.h>
#include<stdio.h>
static GtkWidget *window=NULL;
static GtkWidget *da=NULL;
static GdkWindow *DrawingWindow=NULL;
static Window X_window;
static Display *X_display;
static GLXContext X_context;
static XVisualInfo *X_visual;
static XWindowAttributes X_attributes;
static GLint attributes[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
static float ang=0.0;
static guint timer_id;
static void drawGL(GtkWidget *da, gpointer data)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(ang, 1.0, 1.0, 0.0);
glShadeModel(GL_FLAT);
//Axis lines
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.8, 0.0, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.8, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 1.8);
glEnd();
glPopMatrix ();
glXSwapBuffers(X_display, X_window);
}
static void configureGL(GtkWidget *da, gpointer data)
{
printf("Congigure GL\n");
DrawingWindow=gtk_widget_get_window(GTK_WIDGET(da));
X_window=gdk_x11_window_get_xid(GDK_WINDOW(DrawingWindow));
X_display=gdk_x11_get_default_xdisplay();
X_visual=glXChooseVisual(X_display, 0, attributes);
X_context=glXCreateContext(X_display, X_visual, NULL, GL_TRUE);
XGetWindowAttributes(X_display, X_window, &X_attributes);
glXMakeCurrent(X_display, X_window, X_context);
XMapWindow(X_display, X_window);
printf("Viewport %i %i\n", (int)X_attributes.width, (int)X_attributes.height);
glViewport(0, 0, X_attributes.width, X_attributes.height);
glOrtho(-10,10,-10,10,-10,10);
glScalef(5.0, 5.0, 5.0);
}
static gboolean rotate(gpointer data)
{
ang++;
gtk_widget_queue_draw_area(GTK_WIDGET(da), 0, 0, 500, 500);
return TRUE;
}
void close_program()
{
//timer can trigger warnings when closing program.
g_source_remove(timer_id);
printf("Quit Program\n");
gtk_main_quit();
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 500, 500);
da=gtk_drawing_area_new();
gtk_widget_set_double_buffered(da, FALSE);
gtk_container_add(GTK_CONTAINER(window), da);
g_signal_connect_swapped(window, "destroy", G_CALLBACK(close_program), NULL);
gtk_widget_show(window);
g_signal_connect(da, "configure-event", G_CALLBACK(configureGL), NULL);
g_signal_connect(da, "draw", G_CALLBACK(drawGL), NULL);
gtk_widget_show_all(window);
timer_id=g_timeout_add(1000/60, rotate, da);
gtk_main();
return 0;
}