]> Pileus Git - aweather/blob - src/ridge.c
adding initial ridge overlay code, swiching from cubes to teapots, and a few other...
[aweather] / src / ridge.c
1 #include <config.h>
2 #include <gtk/gtk.h>
3 #include <gtk/gtkgl.h>
4 #include <gdk/gdkkeysyms.h>
5 #include <GL/gl.h>
6 #include <math.h>
7
8 static guint topo_tex;
9
10 guint load_texture(char *filename)
11 {
12         /* Load image */
13         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
14         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
15         int        width  = gdk_pixbuf_get_width(pixbuf);
16         int        height = gdk_pixbuf_get_height(pixbuf);
17
18         /* Create Texture */
19         guint id;
20         glGenTextures(1, &id);
21         glBindTexture(GL_TEXTURE_2D, id);   // 2d texture (x and y size)
22
23         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
24         glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0,
25                         GL_RGB, GL_UNSIGNED_BYTE, pixels);
26         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
27         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
28
29         g_message("loaded %s: w=%d h=%d", filename, width, height);
30         return id;
31 }
32
33 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
34 {
35         //g_message("ridge:expose");
36         glPushMatrix();
37         glScaled(500*1000, 500*1000, 0);
38
39         glBindTexture(GL_TEXTURE_2D, topo_tex);
40         glEnable(GL_TEXTURE_2D);
41
42         glBegin(GL_POLYGON);
43         glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,  1.0, 0.1);
44         glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, -1.0, 0.1);
45         glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, -1.0, 0.1);
46         glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,  1.0, 0.1);
47         glEnd();
48
49         glPopMatrix();
50         return FALSE;
51 }
52
53 static gboolean configure(GtkWidget *da, GdkEventConfigure *event, gpointer user_data)
54 {
55         topo_tex = load_texture("../data/topo.jpg");
56         return FALSE;
57 }
58
59 gboolean ridge_init(GtkDrawingArea *drawing, GtkNotebook *config)
60 {
61         /* Set up OpenGL Stuff */
62         g_signal_connect(drawing, "expose-event",    G_CALLBACK(expose),    NULL);
63         g_signal_connect(drawing, "configure-event", G_CALLBACK(configure), NULL);
64         return TRUE;
65 }