From 998d0e3e15ef6fb66586ffd2b1f339c3aab17194 Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Wed, 21 Dec 2011 21:04:58 +0100 Subject: [PATCH 1/3] Add movement and fix some compile bugs --- editor/Makefile | 1 - editor/src/event_callbacks.c | 21 +++++++++++++++++++-- editor/src/input.c | 27 --------------------------- editor/src/input.h | 3 --- editor/src/main.c | 2 +- editor/src/main_gui.glade | 17 ++++------------- 6 files changed, 24 insertions(+), 47 deletions(-) diff --git a/editor/Makefile b/editor/Makefile index 70ae65f..c4b9224 100644 --- a/editor/Makefile +++ b/editor/Makefile @@ -7,7 +7,6 @@ SRCDIR=src all: $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/display.c $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/input.c - $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/event_callbacks.c $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/main.c $(CC) $(CFLAGS) $(LINKER_FLAGS) $(GTKLIBS) -o ledcube-edit main.o display.o input.o diff --git a/editor/src/event_callbacks.c b/editor/src/event_callbacks.c index 6d05df9..ad366ea 100644 --- a/editor/src/event_callbacks.c +++ b/editor/src/event_callbacks.c @@ -1,8 +1,10 @@ #include #include #include +#include #include "config.h" +#include "input.h" #include "display.h" void on_main_window_delete_event(GtkObject *object, gpointer userData) { @@ -46,7 +48,22 @@ void on_drawing_area_realize(GtkWidget *widget, gpointer data) { gdk_gl_drawable_gl_end(glDrawable); } -void on_drawing_area_key_press_event(GtkWidget *widget, gpointer data) { - g_print("pressed"); +void on_drawing_area_key_press_event(GtkWidget *widget, GdkEventKey *event) { + switch (event->keyval) { + case GDK_KEY_Left: + moveCameraPosition(MOVE_SPEED); + break; + case GDK_KEY_Right: + moveCameraPosition(MOVE_SPEED*-1); + break; + case GDK_KEY_Up: + moveCameraAngle(MOVE_SPEED); + break; + case GDK_KEY_Down: + moveCameraAngle(MOVE_SPEED*-1); + break; + } + + gtk_widget_queue_draw_area(widget, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); } diff --git a/editor/src/input.c b/editor/src/input.c index 79392cf..39c87c4 100644 --- a/editor/src/input.c +++ b/editor/src/input.c @@ -23,33 +23,6 @@ void moveCameraAngle(float angle) { if (eyeAngle < 0) eyeAngle = 0; } - -void keyboard(unsigned char key, int x, int y) { - switch (key) { - case 27: // ESC - exit(0); - break; - } - glutPostRedisplay(); -} - -void keyboard_special(int key, int x, int y) { - switch (key) { - case GLUT_KEY_LEFT: - moveCameraPosition(MOVE_SPEED); - break; - case GLUT_KEY_RIGHT: - moveCameraPosition(MOVE_SPEED*-1); - break; - case GLUT_KEY_UP: - moveCameraAngle(MOVE_SPEED); - break; - case GLUT_KEY_DOWN: - moveCameraAngle(MOVE_SPEED*-1); - } - glutPostRedisplay(); -} - void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { int position, viewport[4]; diff --git a/editor/src/input.h b/editor/src/input.h index bb48fa1..1c3751f 100644 --- a/editor/src/input.h +++ b/editor/src/input.h @@ -4,9 +4,6 @@ void moveCameraPosition(float direction); void moveCameraAngle(float angle); -void keyboard(unsigned char key, int x, int y); -void keyboard_special(int key, int x, int y); - void mouse(int button, int state, int x, int y); #endif diff --git a/editor/src/main.c b/editor/src/main.c index d1a7741..53aa65f 100644 --- a/editor/src/main.c +++ b/editor/src/main.c @@ -66,13 +66,13 @@ int main(int argc, char *argv[]) { } xml = glade_xml_new("src/main_gui.glade", NULL, NULL); - glade_xml_signal_autoconnect(xml); window = glade_xml_get_widget(xml, "main_window"); gtk_container_set_reallocate_redraws(GTK_CONTAINER(window), TRUE); drawingArea = glade_xml_get_widget(xml, "drawing_area"); gtk_widget_set_gl_capability(drawingArea, glConfig, NULL, TRUE, GDK_GL_RGBA_TYPE); + glade_xml_signal_autoconnect(xml); gtk_widget_show(window); gtk_main(); diff --git a/editor/src/main_gui.glade b/editor/src/main_gui.glade index 0bf1120..0fb50f7 100644 --- a/editor/src/main_gui.glade +++ b/editor/src/main_gui.glade @@ -22,8 +22,9 @@ True 7 - + True + LED Cube is not connected @@ -32,18 +33,6 @@ True - - - True - Connect to cube - True - gtk-connect - - - False - True - - True @@ -100,6 +89,8 @@ 500 500 True + True + GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_FOCUS_CHANGE_MASK | GDK_STRUCTURE_MASK From e95b066044551c5c5d0e642e78689e5b09500501 Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Wed, 21 Dec 2011 22:58:57 +0100 Subject: [PATCH 2/3] Make The LEDs clickable --- editor/Makefile | 4 ++-- editor/src/display.c | 29 ++++++++++++++--------------- editor/src/display.h | 3 +-- editor/src/event_callbacks.c | 30 +++++++++++++++++++++++++----- editor/src/input.c | 25 +++++++++---------------- editor/src/input.h | 2 +- editor/src/main.c | 6 ++++-- editor/src/main_gui.glade | 3 ++- 8 files changed, 58 insertions(+), 44 deletions(-) diff --git a/editor/Makefile b/editor/Makefile index c4b9224..cdb975c 100644 --- a/editor/Makefile +++ b/editor/Makefile @@ -1,7 +1,7 @@ CC=gcc CFLAGS=-Wall LINKER_FLAGS=-lglut -export-dynamic -GTKLIBS=`pkg-config --cflags --libs gtkglext-1.0 libglade-2.0 gmodule-export-2.0` +GTKLIBS=`pkg-config --cflags --libs gtk+-2.0 gtkglext-1.0 libglade-2.0 gmodule-export-2.0` SRCDIR=src all: @@ -16,5 +16,5 @@ all: clean: rm -f *.o rm -f gui - #rm -f ledcube-edit + rm -f ledcube-edit diff --git a/editor/src/display.c b/editor/src/display.c index b12ae13..0a31d1f 100644 --- a/editor/src/display.c +++ b/editor/src/display.c @@ -79,7 +79,7 @@ void setScene() { } // OpenGL Display function -void display() { +void display(gboolean onlyForPicking) { glClearColor(0.0, 0.0, 0.0, 1.0); glShadeModel(GL_SMOOTH); @@ -92,19 +92,18 @@ void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); setScene(); - drawWires(); - drawLEDs(RENDER_MODE); -} - -// Picking function -void displayPickingObjects() { - setScene(); - glDisable(GL_DITHER); - glDisable(GL_LIGHTING); - - drawLEDs(PICKING_MODE); - - glEnable(GL_LIGHTING); - glEnable(GL_DITHER); + + if (onlyForPicking == TRUE) { + glDisable(GL_DITHER); + glDisable(GL_LIGHTING); + + drawLEDs(PICKING_MODE); + + glEnable(GL_LIGHTING); + glEnable(GL_DITHER); + } else { + drawWires(); + drawLEDs(RENDER_MODE); + } } diff --git a/editor/src/display.h b/editor/src/display.h index 0397f85..d6236fb 100644 --- a/editor/src/display.h +++ b/editor/src/display.h @@ -4,8 +4,7 @@ void drawLEDs(int mode); void drawWires(); -void display(); -void displayPickingObjects(); +void display(gboolean onlyForPicking); void setScene(); diff --git a/editor/src/event_callbacks.c b/editor/src/event_callbacks.c index ad366ea..c0a7bf1 100644 --- a/editor/src/event_callbacks.c +++ b/editor/src/event_callbacks.c @@ -13,7 +13,7 @@ void on_main_window_delete_event(GtkObject *object, gpointer userData) { gboolean on_drawing_area_configure_event(GtkWidget *widget, GdkEventConfigure *event, gpointer data) { GdkGLContext *glContext = gtk_widget_get_gl_context(widget); - GdkGLDrawable *glDrawable =gtk_widget_get_gl_drawable(widget); + GdkGLDrawable *glDrawable = gtk_widget_get_gl_drawable(widget); if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return FALSE; @@ -25,26 +25,26 @@ gboolean on_drawing_area_configure_event(GtkWidget *widget, GdkEventConfigure *e gboolean on_drawing_area_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data) { GdkGLContext *glContext = gtk_widget_get_gl_context(widget); - GdkGLDrawable *glDrawable =gtk_widget_get_gl_drawable(widget); + GdkGLDrawable *glDrawable = gtk_widget_get_gl_drawable(widget); if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return FALSE; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - display(); + display(FALSE); if (gdk_gl_drawable_is_double_buffered(glDrawable)) gdk_gl_drawable_swap_buffers(glDrawable); else glFlush(); gdk_gl_drawable_gl_end(glDrawable); + return FALSE; } void on_drawing_area_realize(GtkWidget *widget, gpointer data) { GdkGLContext *glContext = gtk_widget_get_gl_context(widget); - GdkGLDrawable *glDrawable =gtk_widget_get_gl_drawable(widget); + GdkGLDrawable *glDrawable = gtk_widget_get_gl_drawable(widget); if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return; - gdk_gl_drawable_gl_end(glDrawable); } @@ -67,3 +67,23 @@ void on_drawing_area_key_press_event(GtkWidget *widget, GdkEventKey *event) { gtk_widget_queue_draw_area(widget, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); } +void on_drawing_area_button_press_event(GtkWidget *widget, gpointer data) { + gtk_widget_grab_focus(widget); + + GdkGLContext *glContext = gtk_widget_get_gl_context(widget); + GdkGLDrawable *glDrawable =gtk_widget_get_gl_drawable(widget); + + if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return; + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + + gint x, y; + gtk_widget_get_pointer(widget, &x, &y); + + display(TRUE); + mouse(x, y); + + gdk_gl_drawable_gl_end(glDrawable); + gtk_widget_queue_draw_area(widget, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); +} + diff --git a/editor/src/input.c b/editor/src/input.c index 39c87c4..7a0286c 100644 --- a/editor/src/input.c +++ b/editor/src/input.c @@ -23,23 +23,16 @@ void moveCameraAngle(float angle) { if (eyeAngle < 0) eyeAngle = 0; } -void mouse(int button, int state, int x, int y) { - if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { - int position, viewport[4]; - GLubyte pixel[3]; +void mouse(int x, int y) { + int position, viewport[4]; + GLubyte pixel[3]; - displayPickingObjects(); + glGetIntegerv(GL_VIEWPORT, viewport); + glReadPixels(x, viewport[3]-y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, (void*)pixel); + position = ((int)pixel[2]/8)-1; // Selected LED - glGetIntegerv(GL_VIEWPORT, viewport); - glReadPixels(x, viewport[3]-y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, (void*)pixel); - position = ((int)pixel[2]/8)-1; // Selected LED - - printf("%d\n", position); - currentFrame[position] = currentFrame[position] == 0 ? 1 : 0; - printf("%d\n", currentFrame[position]); - - // FIXME: Redraw the gl-container - //display(); - } + printf("%d\n", position); + currentFrame[position] = currentFrame[position] == 0 ? 1 : 0; + printf("%d\n", currentFrame[position]); } diff --git a/editor/src/input.h b/editor/src/input.h index 1c3751f..d010515 100644 --- a/editor/src/input.h +++ b/editor/src/input.h @@ -4,7 +4,7 @@ void moveCameraPosition(float direction); void moveCameraAngle(float angle); -void mouse(int button, int state, int x, int y); +void mouse(int x, int y); #endif diff --git a/editor/src/main.c b/editor/src/main.c index 53aa65f..cc9329a 100644 --- a/editor/src/main.c +++ b/editor/src/main.c @@ -12,6 +12,8 @@ #include "input.h" #include "event_callbacks.c" +// TODO: Refactor to GLib-Datatypes (page 747) + // Materials float ledOnMaterial[] = {0.0, 0.0, 1.0, 1.0}; float ledOffMaterial[] = {0.1, 0.1, 0.1, 0.0}; @@ -57,10 +59,10 @@ int main(int argc, char *argv[]) { // Configure the OpenGL widget glConfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE); if (glConfig == NULL) { - g_print("EEE Double buffer not available, trying single buffer."); + g_warning("EEE Double buffer not available, trying single buffer."); glConfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH); if (glConfig == NULL) { - g_print("EEE Sorry, can't configure the OpenGL window. Giving up."); + g_error("EEE Sorry, can't configure the OpenGL window. Giving up."); exit(1); } } diff --git a/editor/src/main_gui.glade b/editor/src/main_gui.glade index 0fb50f7..6b9a1d9 100644 --- a/editor/src/main_gui.glade +++ b/editor/src/main_gui.glade @@ -90,8 +90,9 @@ 500 True True - GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_FOCUS_CHANGE_MASK | GDK_STRUCTURE_MASK + GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_FOCUS_CHANGE_MASK | GDK_STRUCTURE_MASK + From bda0d37e4b235d132a290ff1cb860c46b7077a72 Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Thu, 22 Dec 2011 01:07:43 +0100 Subject: [PATCH 3/3] Remove some unused signals and prettyfy the rendering --- editor/Makefile | 1 + editor/src/display.c | 8 +++----- editor/src/event_callbacks.c | 28 ++++------------------------ editor/src/main.c | 14 +++++++++----- editor/src/main_gui.glade | 2 -- 5 files changed, 17 insertions(+), 36 deletions(-) diff --git a/editor/Makefile b/editor/Makefile index cdb975c..993fc85 100644 --- a/editor/Makefile +++ b/editor/Makefile @@ -7,6 +7,7 @@ SRCDIR=src all: $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/display.c $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/input.c + $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/usb.c $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/main.c $(CC) $(CFLAGS) $(LINKER_FLAGS) $(GTKLIBS) -o ledcube-edit main.o display.o input.o diff --git a/editor/src/display.c b/editor/src/display.c index 0a31d1f..a086ac4 100644 --- a/editor/src/display.c +++ b/editor/src/display.c @@ -24,6 +24,7 @@ void drawLEDs(int mode) { glColor3ub(0, 0, ledIndex*8); } else { glMaterialfv(GL_FRONT, GL_AMBIENT, (currentFrame[ledIndex-1] == 1 ? ledOnMaterial : ledOffMaterial)); + glMaterialfv(GL_FRONT, GL_DIFFUSE, (currentFrame[ledIndex-1] == 1 ? ledOnMaterial : ledOffMaterial)); } glPushMatrix(); @@ -69,6 +70,7 @@ void drawWires() { } void setScene() { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(ZOOM_LEVEL, WINDOW_WIDTH/WINDOW_HEIGHT, 1.0, 350.0); @@ -81,16 +83,12 @@ void setScene() { // OpenGL Display function void display(gboolean onlyForPicking) { glClearColor(0.0, 0.0, 0.0, 1.0); - glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHT0); - glLightfv(GL_LIGHT0, GL_POSITION, light0Pos); glLightfv(GL_LIGHT0, GL_AMBIENT, backgroundColor); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - setScene(); if (onlyForPicking == TRUE) { @@ -102,8 +100,8 @@ void display(gboolean onlyForPicking) { glEnable(GL_LIGHTING); glEnable(GL_DITHER); } else { - drawWires(); drawLEDs(RENDER_MODE); + drawWires(); } } diff --git a/editor/src/event_callbacks.c b/editor/src/event_callbacks.c index c0a7bf1..2f389a7 100644 --- a/editor/src/event_callbacks.c +++ b/editor/src/event_callbacks.c @@ -11,18 +11,6 @@ void on_main_window_delete_event(GtkObject *object, gpointer userData) { gtk_main_quit(); } -gboolean on_drawing_area_configure_event(GtkWidget *widget, GdkEventConfigure *event, gpointer data) { - GdkGLContext *glContext = gtk_widget_get_gl_context(widget); - GdkGLDrawable *glDrawable = gtk_widget_get_gl_drawable(widget); - - if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return FALSE; - - setScene(); - - gdk_gl_drawable_gl_end(glDrawable); - return FALSE; -} - gboolean on_drawing_area_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data) { GdkGLContext *glContext = gtk_widget_get_gl_context(widget); GdkGLDrawable *glDrawable = gtk_widget_get_gl_drawable(widget); @@ -41,13 +29,6 @@ gboolean on_drawing_area_expose_event(GtkWidget *widget, GdkEventExpose *event, return FALSE; } -void on_drawing_area_realize(GtkWidget *widget, gpointer data) { - GdkGLContext *glContext = gtk_widget_get_gl_context(widget); - GdkGLDrawable *glDrawable = gtk_widget_get_gl_drawable(widget); - if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return; - gdk_gl_drawable_gl_end(glDrawable); -} - void on_drawing_area_key_press_event(GtkWidget *widget, GdkEventKey *event) { switch (event->keyval) { case GDK_KEY_Left: @@ -71,14 +52,13 @@ void on_drawing_area_button_press_event(GtkWidget *widget, gpointer data) { gtk_widget_grab_focus(widget); GdkGLContext *glContext = gtk_widget_get_gl_context(widget); - GdkGLDrawable *glDrawable =gtk_widget_get_gl_drawable(widget); + GdkGLDrawable *glDrawable = gtk_widget_get_gl_drawable(widget); + gint x, y; + + gtk_widget_get_pointer(widget, &x, &y); if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - - gint x, y; - gtk_widget_get_pointer(widget, &x, &y); display(TRUE); mouse(x, y); diff --git a/editor/src/main.c b/editor/src/main.c index cc9329a..dd9ab68 100644 --- a/editor/src/main.c +++ b/editor/src/main.c @@ -15,10 +15,10 @@ // TODO: Refactor to GLib-Datatypes (page 747) // Materials -float ledOnMaterial[] = {0.0, 0.0, 1.0, 1.0}; +float ledOnMaterial[] = {0.0, 0.0, 1.0, 0.4}; float ledOffMaterial[] = {0.1, 0.1, 0.1, 0.0}; float wireMaterial[] = {0.7, 0.7, 0.7, 1.0}; -float innerWireMaterial[] = {0.2, 0.2, 0.2, 0.3}; +float innerWireMaterial[] = {0.3, 0.3, 0.3, 0.3}; // Colors float backgroundColor[] = {0.3, 0.3, 0.3, 0.4}; @@ -50,11 +50,15 @@ int main(int argc, char *argv[]) { gluQuadricNormals(quadric, GLU_SMOOTH); gluQuadricDrawStyle(quadric, GLU_FILL); - glEnable(GL_LIGHTING); glShadeModel(GL_SMOOTH); - moveCameraPosition(0); - currentFrame[0] = 1; // TODO: remove + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT0, GL_POSITION, light0Pos); + glLightfv(GL_LIGHT0, GL_AMBIENT, backgroundColor); + + glMatrixMode(GL_MODELVIEW); + moveCameraPosition(0); // Configure the OpenGL widget glConfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE); diff --git a/editor/src/main_gui.glade b/editor/src/main_gui.glade index 6b9a1d9..e759a7a 100644 --- a/editor/src/main_gui.glade +++ b/editor/src/main_gui.glade @@ -94,8 +94,6 @@ - - 2