/*
- * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
+ * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include "grits-util.h"
#include "roam.h"
+static GdkGLConfig *glconfig;
+
// #define ROAM_DEBUG
/* Tessellation, "finding intersecting triangles" */
/* 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 *
***********/
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);
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",
return FALSE;
}
+static gboolean on_chained_event(GritsOpenGL *opengl, GdkEvent *event, gpointer _)
+{
+ _foreach_object(opengl, (GFunc)grits_object_event, event);
+ return FALSE;
+}
+
static gboolean _update_errors_cb(gpointer _opengl)
{
GritsOpenGL *opengl = _opengl;
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);
+ g_signal_connect_after(opengl, "key-press-event", G_CALLBACK(on_chained_event), NULL);
+ g_signal_connect_after(opengl, "key-release-event", G_CALLBACK(on_chained_event), NULL);
+ g_signal_connect_after(opengl, "button-press-event", G_CALLBACK(on_chained_event), NULL);
+ g_signal_connect_after(opengl, "button-release-event", G_CALLBACK(on_chained_event), NULL);
+ g_signal_connect_after(opengl, "motion-notify-event", G_CALLBACK(on_chained_event), 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);
return link;
}
-static GritsObject *grits_opengl_remove(GritsViewer *_opengl, gpointer _link)
+static GritsObject *grits_opengl_remove(GritsViewer *_opengl, GritsObject *object)
{
g_assert(GRITS_IS_OPENGL(_opengl));
GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
- GList *link = _link;
+ GList *link = object->ref;
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);
+ object->ref = NULL;
+ object->viewer = NULL;
g_free(link);
g_object_unref(object);
return object;
}
G_DEFINE_TYPE(GritsOpenGL, grits_opengl, GRITS_TYPE_VIEWER);
+void grits_init(int *argc, char ***argv)
+{
+ 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_warning("Failed to create glconfig, unsupported:%s%s%s%s.",
+ gdk_gl_config_new_by_mode(GDK_GL_MODE_RGBA) ? "" : " RGBA",
+ gdk_gl_config_new_by_mode(GDK_GL_MODE_DEPTH) ? "" : " DEPTH",
+ gdk_gl_config_new_by_mode(GDK_GL_MODE_DOUBLE) ? "" : " DOUBLE",
+ gdk_gl_config_new_by_mode(GDK_GL_MODE_ALPHA) ? "" : " ALPHA");
+ }
+}
static void grits_opengl_init(GritsOpenGL *opengl)
{
g_debug("GritsOpenGL: init");
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_warning("recreating glconfig");
+ 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");
+ g_error("Failed to create glconfig, unsupported:%s%s%s%s.",
+ gdk_gl_config_new_by_mode(GDK_GL_MODE_RGBA) ? "" : " RGBA",
+ gdk_gl_config_new_by_mode(GDK_GL_MODE_DEPTH) ? "" : " DEPTH",
+ gdk_gl_config_new_by_mode(GDK_GL_MODE_DOUBLE) ? "" : " DEPTH",
+ gdk_gl_config_new_by_mode(GDK_GL_MODE_ALPHA) ? "" : " ALPHA");
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 */
g_signal_connect(opengl, "realize", G_CALLBACK(on_realize), NULL);