*/
/**
- * SECTION:gis-opengl
+ * SECTION:grits-opengl
* @short_description: OpenGL based virtual globe
*
- * #GisOpenGL is the core rendering engine used by libgis. Theoretically other
- * renderers could be writte, but they have not been. GisOpenGL uses the ROAM
- * algorithm for updating surface mesh the planet. The only thing GisOpenGL can
- * actually render on it's own is a wireframe of a sphere.
+ * #GritsOpenGL is the core rendering engine used by grits. Theoretically other
+ * renderers could be writte, but they have not been. GritsOpenGL uses the ROAM
+ * algorithm for updating surface mesh the planet. The only thing GritsOpenGL
+ * can actually render on it's own is a wireframe of a sphere.
*
- * GisOpenGL 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 "gis-opengl.h"
-#include "gis-util.h"
+#include "grits-opengl.h"
+#include "grits-util.h"
+#include "gtkgl.h"
#include "roam.h"
// #define ROAM_DEBUG
/***********
* Helpers *
***********/
-static void _set_visuals(GisOpenGL *opengl)
+static void _set_visuals(GritsOpenGL *opengl)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* Camera 1 */
double lat, lon, elev, rx, ry, rz;
- gis_viewer_get_location(GIS_VIEWER(opengl), &lat, &lon, &elev);
- gis_viewer_get_rotation(GIS_VIEWER(opengl), &rx, &ry, &rz);
+ grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev);
+ grits_viewer_get_rotation(GRITS_VIEWER(opengl), &rx, &ry, &rz);
glRotatef(rx, 1, 0, 0);
glRotatef(rz, 0, 0, 1);
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);
GList sorted;
};
-static gboolean on_configure(GisOpenGL *opengl, GdkEventConfigure *event, gpointer _)
+static gboolean on_configure(GritsOpenGL *opengl, GdkEventConfigure *event, gpointer _)
{
- g_debug("GisOpenGL: on_configure");
+ g_debug("GritsOpenGL: on_configure");
double width = GTK_WIDGET(opengl)->allocation.width;
double height = GTK_WIDGET(opengl)->allocation.height;
static gboolean _draw_level(gpointer key, gpointer value, gpointer user_data)
{
- g_debug("GisOpenGL: _draw_level - level=%-4d", (int)key);
- GisOpenGL *opengl = user_data;
+ g_debug("GritsOpenGL: _draw_level - level=%-4d", (int)key);
+ GritsOpenGL *opengl = user_data;
struct RenderLevel *level = value;
int nsorted = 0, nunsorted = 0;
GList *cur = NULL;
glDepthMask(TRUE);
glClear(GL_DEPTH_BUFFER_BIT);
for (cur = level->unsorted.next; cur; cur = cur->next, nunsorted++)
- gis_object_draw( GIS_OBJECT(cur->data), opengl);
+ grits_object_draw(GRITS_OBJECT(cur->data), opengl);
/* Freeze depth buffer and draw transparent objects sorted */
/* TODO: sorting */
//glDepthMask(FALSE);
glAlphaFunc(GL_GREATER, 0.1);
for (cur = level->sorted.next; cur; cur = cur->next, nsorted++)
- gis_object_draw(GIS_OBJECT(cur->data), opengl);
+ grits_object_draw(GRITS_OBJECT(cur->data), opengl);
/* TODO: Prune empty levels */
- g_debug("GisOpenGL: _draw_level - drew %d,%d objects",
+ g_debug("GritsOpenGL: _draw_level - drew %d,%d objects",
nunsorted, nsorted);
return FALSE;
}
-static gboolean on_expose(GisOpenGL *opengl, GdkEventExpose *event, gpointer _)
+static gboolean on_expose(GritsOpenGL *opengl, GdkEventExpose *event, gpointer _)
{
- g_debug("GisOpenGL: on_expose - begin");
+ g_debug("GritsOpenGL: on_expose - begin");
glClear(GL_COLOR_BUFFER_BIT);
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("GisOpenGL: on_expose - end\n");
+ g_debug("GritsOpenGL: on_expose - end\n");
return FALSE;
}
-static gboolean on_key_press(GisOpenGL *opengl, GdkEventKey *event, gpointer _)
+static gboolean on_key_press(GritsOpenGL *opengl, GdkEventKey *event, gpointer _)
{
- g_debug("GisOpenGL: on_key_press - key=%x, state=%x, plus=%x",
+ g_debug("GritsOpenGL: on_key_press - key=%x, state=%x, plus=%x",
event->keyval, event->state, GDK_plus);
guint kv = event->keyval;
static gboolean _update_errors_cb(gpointer _opengl)
{
- GisOpenGL *opengl = _opengl;
+ GritsOpenGL *opengl = _opengl;
g_mutex_lock(opengl->sphere_lock);
roam_sphere_update_errors(opengl->sphere);
g_mutex_unlock(opengl->sphere_lock);
opengl->ue_source = 0;
return FALSE;
}
-static void on_view_changed(GisOpenGL *opengl,
+static void on_view_changed(GritsOpenGL *opengl,
gdouble _1, gdouble _2, gdouble _3)
{
- g_debug("GisOpenGL: on_view_changed");
+ g_debug("GritsOpenGL: on_view_changed");
_set_visuals(opengl);
#ifndef ROAM_DEBUG
if (!opengl->ue_source)
#endif
}
-static gboolean on_idle(GisOpenGL *opengl)
+static gboolean on_idle(GritsOpenGL *opengl)
{
- //g_debug("GisOpenGL: on_idle");
+ //g_debug("GritsOpenGL: on_idle");
g_mutex_lock(opengl->sphere_lock);
if (roam_sphere_split_merge(opengl->sphere))
gtk_widget_queue_draw(GTK_WIDGET(opengl));
return TRUE;
}
-static void on_realize(GisOpenGL *opengl, gpointer _)
+static void on_realize(GritsOpenGL *opengl, gpointer _)
{
- g_debug("GisOpenGL: 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();
+ g_debug("GritsOpenGL: on_realize");
+ 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);
}
/*********************
- * GisViewer methods *
+ * GritsViewer methods *
*********************/
/**
- * gis_opengl_new:
+ * grits_opengl_new:
* @plugins: the plugins store to use
* @prefs: the preferences object to use
*
* Create a new OpenGL renderer.
*
- * Returns: the new #GisOpenGL
+ * Returns: the new #GritsOpenGL
*/
-GisViewer *gis_opengl_new(GisPlugins *plugins, GisPrefs *prefs)
+GritsViewer *grits_opengl_new(GritsPlugins *plugins, GritsPrefs *prefs)
{
- g_debug("GisOpenGL: new");
- GisViewer *opengl = g_object_new(GIS_TYPE_OPENGL, NULL);
- gis_viewer_setup(opengl, plugins, prefs);
+ g_debug("GritsOpenGL: new");
+ GritsViewer *opengl = g_object_new(GRITS_TYPE_OPENGL, NULL);
+ grits_viewer_setup(opengl, plugins, prefs);
return opengl;
}
-static void gis_opengl_center_position(GisViewer *_opengl, gdouble lat, gdouble lon, gdouble elev)
+static void grits_opengl_center_position(GritsViewer *_opengl, gdouble lat, gdouble lon, gdouble elev)
{
glRotatef(lon, 0, 1, 0);
glRotatef(-lat, 1, 0, 0);
glTranslatef(0, 0, elev2rad(elev));
}
-static void gis_opengl_project(GisViewer *_opengl,
+static void grits_opengl_project(GritsViewer *_opengl,
gdouble lat, gdouble lon, gdouble elev,
gdouble *px, gdouble *py, gdouble *pz)
{
- GisOpenGL *opengl = GIS_OPENGL(_opengl);
+ GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
gdouble x, y, z;
lle2xyz(lat, lon, elev, &x, &y, &z);
gluProject(x, y, z,
px, py, pz);
}
-static void gis_opengl_set_height_func(GisViewer *_opengl, GisBounds *bounds,
+static void grits_opengl_set_height_func(GritsViewer *_opengl, GritsBounds *bounds,
RoamHeightFunc height_func, gpointer user_data, gboolean update)
{
- GisOpenGL *opengl = GIS_OPENGL(_opengl);
+ GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
/* TODO: get points? */
g_mutex_lock(opengl->sphere_lock);
GList *triangles = roam_sphere_get_intersect(opengl->sphere, TRUE,
g_mutex_unlock(opengl->sphere_lock);
}
-static void _gis_opengl_clear_height_func_rec(RoamTriangle *root)
+static void _grits_opengl_clear_height_func_rec(RoamTriangle *root)
{
if (!root)
return;
points[i]->height_data = NULL;
roam_point_update_height(points[i]);
}
- _gis_opengl_clear_height_func_rec(root->kids[0]);
- _gis_opengl_clear_height_func_rec(root->kids[1]);
+ _grits_opengl_clear_height_func_rec(root->kids[0]);
+ _grits_opengl_clear_height_func_rec(root->kids[1]);
}
-static void gis_opengl_clear_height_func(GisViewer *_opengl)
+static void grits_opengl_clear_height_func(GritsViewer *_opengl)
{
- GisOpenGL *opengl = GIS_OPENGL(_opengl);
+ GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
for (int i = 0; i < G_N_ELEMENTS(opengl->sphere->roots); i++)
- _gis_opengl_clear_height_func_rec(opengl->sphere->roots[i]);
+ _grits_opengl_clear_height_func_rec(opengl->sphere->roots[i]);
}
-static gpointer gis_opengl_add(GisViewer *_opengl, GisObject *object,
+static gpointer grits_opengl_add(GritsViewer *_opengl, GritsObject *object,
gint key, gboolean sort)
{
- g_assert(GIS_IS_OPENGL(_opengl));
- GisOpenGL *opengl = GIS_OPENGL(_opengl);
+ g_assert(GRITS_IS_OPENGL(_opengl));
+ GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
g_mutex_lock(opengl->objects_lock);
struct RenderLevel *level = g_tree_lookup(opengl->objects, (gpointer)key);
if (!level) {
return link;
}
-static GisObject *gis_opengl_remove(GisViewer *_opengl, gpointer _link)
+static GritsObject *grits_opengl_remove(GritsViewer *_opengl, gpointer _link)
{
- g_assert(GIS_IS_OPENGL(_opengl));
- GisOpenGL *opengl = GIS_OPENGL(_opengl);
- g_mutex_lock(opengl->objects_lock);
+ g_assert(GRITS_IS_OPENGL(_opengl));
+ GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
GList *link = _link;
- GisObject *object = link->data;
+ 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;
}
g_free(level);
}
-G_DEFINE_TYPE(GisOpenGL, gis_opengl, GIS_TYPE_VIEWER);
-static void gis_opengl_init(GisOpenGL *opengl)
+G_DEFINE_TYPE(GritsOpenGL, grits_opengl, GRITS_TYPE_VIEWER);
+static void grits_opengl_init(GritsOpenGL *opengl)
{
- g_debug("GisOpenGL: init");
+ g_debug("GritsOpenGL: init");
opengl->objects = g_tree_new_full(_objects_cmp, NULL, NULL, _objects_free);
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 gis_opengl_dispose(GObject *_opengl)
+static void grits_opengl_dispose(GObject *_opengl)
{
- g_debug("GisOpenGL: dispose");
- GisOpenGL *opengl = GIS_OPENGL(_opengl);
+ g_debug("GritsOpenGL: dispose");
+ GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
if (opengl->sm_source[0]) {
g_source_remove(opengl->sm_source[0]);
opengl->sm_source[0] = 0;
g_source_remove(opengl->ue_source);
opengl->ue_source = 0;
}
- G_OBJECT_CLASS(gis_opengl_parent_class)->dispose(_opengl);
+ G_OBJECT_CLASS(grits_opengl_parent_class)->dispose(_opengl);
}
-static void gis_opengl_finalize(GObject *_opengl)
+static void grits_opengl_finalize(GObject *_opengl)
{
- g_debug("GisOpenGL: finalize");
- GisOpenGL *opengl = GIS_OPENGL(_opengl);
+ g_debug("GritsOpenGL: finalize");
+ GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
roam_sphere_free(opengl->sphere);
g_tree_destroy(opengl->objects);
g_mutex_free(opengl->objects_lock);
g_mutex_free(opengl->sphere_lock);
- G_OBJECT_CLASS(gis_opengl_parent_class)->finalize(_opengl);
+ G_OBJECT_CLASS(grits_opengl_parent_class)->finalize(_opengl);
}
-static void gis_opengl_class_init(GisOpenGLClass *klass)
+static void grits_opengl_class_init(GritsOpenGLClass *klass)
{
- g_debug("GisOpenGL: class_init");
+ g_debug("GritsOpenGL: class_init");
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
- gobject_class->finalize = gis_opengl_finalize;
- gobject_class->dispose = gis_opengl_dispose;
-
- GisViewerClass *viewer_class = GIS_VIEWER_CLASS(klass);
- viewer_class->center_position = gis_opengl_center_position;
- viewer_class->project = gis_opengl_project;
- viewer_class->clear_height_func = gis_opengl_clear_height_func;
- viewer_class->set_height_func = gis_opengl_set_height_func;
- viewer_class->add = gis_opengl_add;
- viewer_class->remove = gis_opengl_remove;
+ gobject_class->finalize = grits_opengl_finalize;
+ gobject_class->dispose = grits_opengl_dispose;
+
+ GritsViewerClass *viewer_class = GRITS_VIEWER_CLASS(klass);
+ viewer_class->center_position = grits_opengl_center_position;
+ viewer_class->project = grits_opengl_project;
+ viewer_class->clear_height_func = grits_opengl_clear_height_func;
+ viewer_class->set_height_func = grits_opengl_set_height_func;
+ viewer_class->add = grits_opengl_add;
+ viewer_class->remove = grits_opengl_remove;
}