#include <string.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
-#include <GL/gl.h>
-#include <GL/glu.h>
#include "grits-opengl.h"
#include "grits-util.h"
***********/
static void _set_visuals(GritsOpenGL *opengl)
{
+ double lat, lon, elev, rx, ry, rz;
+ grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev);
+ grits_viewer_get_rotation(GRITS_VIEWER(opengl), &rx, &ry, &rz);
+
+ /* Set projection and clipping planes */
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+
+ double width = GTK_WIDGET(opengl)->allocation.width;
+ double height = GTK_WIDGET(opengl)->allocation.height;
+ double ang = atan((height/2)/FOV_DIST)*2;
+ double atmos = 100000;
+ double near = MAX(elev*0.75 - atmos, 50); // View 100km of atmosphere
+ double far = elev + 2*EARTH_R + atmos; // on both sides of the earth
+
+ grits_viewer_get_location(GRITS_VIEWER(opengl), &lat, &lon, &elev);
+ glViewport(0, 0, width, height);
+ gluPerspective(rad2deg(ang), width/height, near, far);
+
+ /* Setup camera and lighting */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* Camera 1 */
- double lat, lon, elev, 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);
g_tree_foreach(opengl->objects, _foreach_object_cb, pointers);
}
+static void _add_object(GritsObject *object, GPtrArray *array)
+{
+ g_ptr_array_add(array, object);
+}
+
+static GPtrArray *_objects_to_array(GritsOpenGL *opengl)
+{
+ GPtrArray *array = g_ptr_array_new();
+ _foreach_object(opengl, (GFunc)_add_object, array);
+ return array;
+}
+
/*************
* Callbacks *
*************/
{
g_debug("GritsOpenGL: on_configure");
- double width = GTK_WIDGET(opengl)->allocation.width;
- double height = GTK_WIDGET(opengl)->allocation.height;
-
- /* Setup OpenGL Window */
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- double ang = atan(height/FOV_DIST);
- gluPerspective(rad2deg(ang)*2, width/height, 10, 100*EARTH_R);
-
+ _set_visuals(opengl);
#ifndef ROAM_DEBUG
g_mutex_lock(opengl->sphere_lock);
roam_sphere_update_errors(opengl->sphere);
static gboolean _draw_level(gpointer key, gpointer value, gpointer user_data)
{
- g_debug("GritsOpenGL: _draw_level - level=%-4d", (int)key);
GritsOpenGL *opengl = user_data;
+ glong lnum = (glong)key;
struct RenderLevel *level = value;
+
+ g_debug("GritsOpenGL: _draw_level - level=%-4ld", lnum);
int nsorted = 0, nunsorted = 0;
GList *cur = NULL;
- /* Draw opaque objects without sorting */
- glDepthMask(TRUE);
- glClear(GL_DEPTH_BUFFER_BIT);
+ /* Configure individual levels */
+ if (lnum < GRITS_LEVEL_WORLD) {
+ /* Disable depth for background levels */
+ glDepthMask(FALSE);
+ glDisable(GL_ALPHA_TEST);
+ } else if (lnum < GRITS_LEVEL_OVERLAY) {
+ /* Enable depth and alpha for world levels */
+ glEnable(GL_ALPHA_TEST);
+ glAlphaFunc(GL_GREATER, 0.1);
+ glDepthMask(TRUE);
+ } else {
+ /* Disable depth for Overlay/HUD levels */
+ // This causes rendering glitches not sure why..
+ //glDepthMask(FALSE);
+ }
+
+ /* Draw unsorted objects without depth testing,
+ * these are polygons, etc, rather than physical objects */
+ glDisable(GL_DEPTH_TEST);
for (cur = level->unsorted.next; cur; cur = cur->next, nunsorted++)
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);
+ /* Draw sorted objects using depth testing
+ * These are things that are actually part of the world */
+ glEnable(GL_DEPTH_TEST);
for (cur = level->sorted.next; cur; cur = cur->next, nsorted++)
grits_object_draw(GRITS_OBJECT(cur->data), opengl);
{
g_debug("GritsOpenGL: on_expose - begin");
- glClear(GL_COLOR_BUFFER_BIT);
+ gtk_gl_begin(GTK_WIDGET(opengl));
+
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_set_visuals(opengl);
#ifdef ROAM_DEBUG
//roam_sphere_draw_normals(opengl->sphere);
#else
g_mutex_lock(opengl->objects_lock);
- g_tree_foreach(opengl->objects, _draw_level, opengl);
- if (opengl->wireframe) {
- glClear(GL_DEPTH_BUFFER_BIT);
+ if (opengl->wireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- roam_sphere_draw(opengl->sphere);
- g_tree_foreach(opengl->objects, _draw_level, opengl);
- }
+ g_tree_foreach(opengl->objects, _draw_level, opengl);
g_mutex_unlock(opengl->objects_lock);
#endif
/* Run picking */
g_mutex_lock(opengl->objects_lock);
- _foreach_object(opengl, (GFunc)grits_object_pick_begin, opengl);
+ GPtrArray *objects = _objects_to_array(opengl);
+ for (guint i = 0; i < objects->len; i++) {
+ glPushName(i);
+ GritsObject *object = objects->pdata[i];
+ object->state.picked = FALSE;
+ grits_object_pick(object, opengl);
+ glPopName();
+ }
int hits = glRenderMode(GL_RENDER);
g_debug("GritsOpenGL: on_motion_notify - hits=%d ev=%.0lf,%.0lf",
hits, gl_x, gl_y);
//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);
+ guint index = buffer[i][3];
+ GritsObject *object = objects->pdata[index];
+ object->state.picked = TRUE;
}
- _foreach_object(opengl, (GFunc)grits_object_pick_end, NULL);
+ for (guint i = 0; i < objects->len; i++) {
+ GritsObject *object = objects->pdata[i];
+ grits_object_set_pointer(object, object->state.picked);
+ }
+ g_ptr_array_free(objects, TRUE);
g_mutex_unlock(opengl->objects_lock);
+ /* Test unproject */
+ gdouble lat, lon, elev;
+ grits_viewer_unproject(GRITS_VIEWER(opengl),
+ gl_x, gl_y, -1, &lat, &lon, &elev);
+
/* Cleanup */
glMatrixMode(GL_PROJECTION);
glPopMatrix();
px, py, pz);
}
+static void grits_opengl_unproject(GritsViewer *_opengl,
+ gdouble px, gdouble py, gdouble pz,
+ gdouble *lat, gdouble *lon, gdouble *elev)
+{
+ GritsOpenGL *opengl = GRITS_OPENGL(_opengl);
+ if (!opengl->sphere->view)
+ return;
+ gdouble x, y, z;
+ if (pz < 0) {
+ gfloat tmp = 0;
+ glReadPixels(px, py, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &tmp);
+ pz = tmp;
+ }
+ gluUnProject(px, py, pz,
+ opengl->sphere->view->model,
+ opengl->sphere->view->proj,
+ opengl->sphere->view->view,
+ &x, &y, &z);
+ xyz2lle(x, y, z, lat, lon, elev);
+ //g_message("GritsOpenGL: unproject - "
+ // "%4.0lf,%4.0lf,(%5.3lf) -> "
+ // "%8.0lf,%8.0lf,%8.0lf -> "
+ // "%6.2lf,%7.2lf,%4.0lf",
+ // px, py, pz, x, y, z, *lat, *lon, *elev);
+}
+
static void grits_opengl_set_height_func(GritsViewer *_opengl, GritsBounds *bounds,
RoamHeightFunc height_func, gpointer user_data, gboolean update)
{
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);
+ struct RenderLevel *level = g_tree_lookup(opengl->objects, (gpointer)(gintptr)key);
if (!level) {
level = g_new0(struct RenderLevel, 1);
- g_tree_insert(opengl->objects, (gpointer)key, level);
+ g_tree_insert(opengl->objects, (gpointer)(gintptr)key, level);
}
GList *list = sort ? &level->sorted : &level->unsorted;
/* Put the link in the list */
****************/
static int _objects_cmp(gconstpointer _a, gconstpointer _b, gpointer _)
{
- gint a = (int)_a, b = (int)_b;
+ gintptr a = (gintptr)_a, b = (gintptr)_b;
return a < b ? -1 :
a > b ? 1 : 0;
}
opengl->sphere = roam_sphere_new(opengl);
opengl->sphere_lock = g_mutex_new();
gtk_gl_enable(GTK_WIDGET(opengl));
- g_signal_connect(opengl, "realize", G_CALLBACK(on_realize), NULL);
+ gtk_widget_add_events(GTK_WIDGET(opengl), GDK_KEY_PRESS_MASK);
+ g_signal_connect(opengl, "map", G_CALLBACK(on_realize), NULL);
}
static void grits_opengl_dispose(GObject *_opengl)
{
GritsViewerClass *viewer_class = GRITS_VIEWER_CLASS(klass);
viewer_class->center_position = grits_opengl_center_position;
viewer_class->project = grits_opengl_project;
+ viewer_class->unproject = grits_opengl_unproject;
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;