* algorithm for updating surface mesh the planet. The only thing GritsOpenGL
* can actually render on it's own is a wireframe of a sphere.
*
- * GritsOpenGL relies on #GtkGlExt and requires (at least) OpenGL 2.0.
+ * GritsOpenGL requires (at least) OpenGL 2.0.
*/
#include <config.h>
#include <string.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
-#include <gtk/gtkgl.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "grits-opengl.h"
#include "grits-util.h"
+#include "gtkgl.h"
#include "roam.h"
// #define ROAM_DEBUG
/* http://research.microsoft.com/pubs/70307/tr-2006-81.pdf */
/* http://www.opengl.org/wiki/Alpha_Blending */
+/* The unsorted/sroted GLists are blank head nodes,
+ * This way us we can remove objects from the level just by fixing up links
+ * I.e. we don't need to do a lookup to remove an object if we have its GList */
+struct RenderLevel {
+ GList unsorted;
+ GList sorted;
+};
+
/***********
* Helpers *
***********/
float light_diffuse[] = {2.0f, 2.0f, 2.0f, 1.0f};
#else
float light_ambient[] = {0.2f, 0.2f, 0.2f, 1.0f};
- float light_diffuse[] = {5.0f, 5.0f, 5.0f, 1.0f};
+ float light_diffuse[] = {0.8f, 0.8f, 0.8f, 1.0f};
#endif
float light_position[] = {-13*EARTH_R, 1*EARTH_R, 3*EARTH_R, 1.0f};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
- float material_ambient[] = {0.2, 0.2, 0.2, 1.0};
- float material_diffuse[] = {0.8, 0.8, 0.8, 1.0};
- float material_specular[] = {0.1, 0.1, 0.1, 1.0};
+ float material_ambient[] = {1.0, 1.0, 1.0, 1.0};
+ float material_diffuse[] = {1.0, 1.0, 1.0, 1.0};
+ float material_specular[] = {0.0, 0.0, 0.0, 1.0};
float material_emission[] = {0.0, 0.0, 0.0, 1.0};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
g_mutex_unlock(opengl->sphere_lock);
}
+static gboolean _foreach_object_cb(gpointer key, gpointer value, gpointer pointers)
+{
+ struct RenderLevel *level = value;
+ GFunc user_func = ((gpointer*)pointers)[0];
+ gpointer user_data = ((gpointer*)pointers)[1];
+ for (GList *cur = level->unsorted.next; cur; cur = cur->next)
+ user_func(cur->data, user_data);
+ for (GList *cur = level->sorted.next; cur; cur = cur->next)
+ user_func(cur->data, user_data);
+ return FALSE;
+}
+
+static void _foreach_object(GritsOpenGL *opengl, GFunc func, gpointer user_data)
+{
+ gpointer pointers[2] = {func, user_data};
+ g_tree_foreach(opengl->objects, _foreach_object_cb, pointers);
+}
/*************
* Callbacks *
*************/
-/* The unsorted/sroted GLists are blank head nodes,
- * This way us we can remove objects from the level just by fixing up links
- * I.e. we don't need to do a lookup to remove an object if we have its GList */
-struct RenderLevel {
- GList unsorted;
- GList sorted;
-};
-
static gboolean on_configure(GritsOpenGL *opengl, GdkEventConfigure *event, gpointer _)
{
g_debug("GritsOpenGL: on_configure");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double ang = atan(height/FOV_DIST);
- gluPerspective(rad2deg(ang)*2, width/height, 1000, 10*EARTH_R);
+ gluPerspective(rad2deg(ang)*2, width/height, 10, 100*EARTH_R);
#ifndef ROAM_DEBUG
g_mutex_lock(opengl->sphere_lock);
glClear(GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
roam_sphere_draw(opengl->sphere);
+ g_tree_foreach(opengl->objects, _draw_level, opengl);
}
g_mutex_unlock(opengl->objects_lock);
#endif
- GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(opengl));
- gdk_gl_drawable_swap_buffers(gldrawable);
+ gtk_gl_end(GTK_WIDGET(opengl));
g_debug("GritsOpenGL: on_expose - end\n");
return FALSE;
}
+static gboolean on_motion_notify(GritsOpenGL *opengl, GdkEventMotion *event, gpointer _)
+{
+ gdouble height = GTK_WIDGET(opengl)->allocation.height;
+ gdouble gl_x = event->x;
+ gdouble gl_y = height - event->y;
+
+ /* Configure view */
+ gint viewport[4];
+ gdouble projection[16];
+ glGetIntegerv(GL_VIEWPORT, viewport);
+ glGetDoublev(GL_PROJECTION_MATRIX, projection);
+
+ glMatrixMode(GL_PROJECTION);
+ glPushMatrix();
+ glLoadIdentity();
+ gluPickMatrix(gl_x, gl_y, 2, 2, viewport);
+ glMultMatrixd(projection);
+
+ /* Prepare for picking */
+ guint buffer[100][4] = {};
+ glSelectBuffer(G_N_ELEMENTS(buffer), (guint*)buffer);
+ glRenderMode(GL_SELECT);
+ glInitNames();
+
+ /* Run picking */
+ g_mutex_lock(opengl->objects_lock);
+ _foreach_object(opengl, (GFunc)grits_object_pick_begin, opengl);
+ int hits = glRenderMode(GL_RENDER);
+ g_debug("GritsOpenGL: on_motion_notify - hits=%d ev=%.0lf,%.0lf",
+ hits, gl_x, gl_y);
+ for (int i = 0; i < hits; i++) {
+ //g_debug("\tHit: %d", i);
+ //g_debug("\t\tcount: %d", buffer[i][0]);
+ //g_debug("\t\tz1: %f", (float)buffer[i][1]/0x7fffffff);
+ //g_debug("\t\tz2: %f", (float)buffer[i][2]/0x7fffffff);
+ //g_debug("\t\tname: %p", (gpointer)buffer[i][3]);
+ GritsObject *object = GRITS_OBJECT(buffer[i][3]);
+ grits_object_pick_pointer(object, gl_x, gl_y);
+ }
+ _foreach_object(opengl, (GFunc)grits_object_pick_end, NULL);
+ g_mutex_unlock(opengl->objects_lock);
+
+ /* Cleanup */
+ glMatrixMode(GL_PROJECTION);
+ glPopMatrix();
+
+ return FALSE;
+}
+
static gboolean on_key_press(GritsOpenGL *opengl, GdkEventKey *event, gpointer _)
{
g_debug("GritsOpenGL: on_key_press - key=%x, state=%x, plus=%x",
static void on_realize(GritsOpenGL *opengl, gpointer _)
{
g_debug("GritsOpenGL: on_realize");
-
- /* Start OpenGL */
- GdkGLContext *glcontext = gtk_widget_get_gl_context(GTK_WIDGET(opengl));
- GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(GTK_WIDGET(opengl));
- if (!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
- g_assert_not_reached();
+ gtk_gl_begin(GTK_WIDGET(opengl));
/* Connect signals and idle functions now that opengl is fully initialized */
gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
g_signal_connect(opengl, "location-changed", G_CALLBACK(on_view_changed), NULL);
g_signal_connect(opengl, "rotation-changed", G_CALLBACK(on_view_changed), NULL);
+ g_signal_connect(opengl, "motion-notify-event", G_CALLBACK(on_motion_notify), NULL);
#ifndef ROAM_DEBUG
opengl->sm_source[0] = g_timeout_add_full(G_PRIORITY_HIGH_IDLE+30, 33, (GSourceFunc)on_idle, opengl, NULL);
opengl->sm_source[1] = g_timeout_add_full(G_PRIORITY_HIGH_IDLE+10, 500, (GSourceFunc)on_idle, opengl, NULL);
{
g_assert(GRITS_IS_OPENGL(_opengl));
GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
- g_mutex_lock(opengl->objects_lock);
GList *link = _link;
+ g_mutex_lock(opengl->objects_lock);
GritsObject *object = link->data;
/* Just unlink and free it, link->prev is assured */
link->prev->next = link->next;
if (link->next)
link->next->prev = link->prev;
+ g_mutex_unlock(opengl->objects_lock);
g_free(link);
g_object_unref(object);
- g_mutex_unlock(opengl->objects_lock);
return object;
}
opengl->objects_lock = g_mutex_new();
opengl->sphere = roam_sphere_new(opengl);
opengl->sphere_lock = g_mutex_new();
-
- /* Set OpenGL before "realize" */
- GdkGLConfig *glconfig = gdk_gl_config_new_by_mode(
- GDK_GL_MODE_RGBA | GDK_GL_MODE_DEPTH |
- GDK_GL_MODE_DOUBLE | GDK_GL_MODE_ALPHA);
- if (!glconfig)
- g_error("Failed to create glconfig");
- if (!gtk_widget_set_gl_capability(GTK_WIDGET(opengl),
- glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
- g_error("GL lacks required capabilities");
- g_object_unref(glconfig);
-
- /* Finish OpenGL init after it's realized */
+ gtk_gl_enable(GTK_WIDGET(opengl));
g_signal_connect(opengl, "realize", G_CALLBACK(on_realize), NULL);
}
static void grits_opengl_dispose(GObject *_opengl)