Merge branch 'master' of github.com:klaute/Cthn.de-MiniLEDCube

This commit is contained in:
Kai Lauterbach 2011-12-22 19:19:11 +01:00
commit 9c78a2fc64
8 changed files with 87 additions and 115 deletions

View file

@ -1,13 +1,13 @@
CC=gcc CC=gcc
CFLAGS=-Wall CFLAGS=-Wall
LINKER_FLAGS=-lglut -export-dynamic 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 SRCDIR=src
all: all:
$(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/display.c $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/display.c
$(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/input.c $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/input.c
$(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/event_callbacks.c $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/usb.c
$(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/main.c $(CC) $(CFLAGS) $(GTKLIBS) -c $(SRCDIR)/main.c
$(CC) $(CFLAGS) $(LINKER_FLAGS) $(GTKLIBS) -o ledcube-edit main.o display.o input.o $(CC) $(CFLAGS) $(LINKER_FLAGS) $(GTKLIBS) -o ledcube-edit main.o display.o input.o
@ -17,5 +17,5 @@ all:
clean: clean:
rm -f *.o rm -f *.o
rm -f gui rm -f gui
#rm -f ledcube-edit rm -f ledcube-edit

View file

@ -24,6 +24,7 @@ void drawLEDs(int mode) {
glColor3ub(0, 0, ledIndex*8); glColor3ub(0, 0, ledIndex*8);
} else { } else {
glMaterialfv(GL_FRONT, GL_AMBIENT, (currentFrame[ledIndex-1] == 1 ? ledOnMaterial : ledOffMaterial)); glMaterialfv(GL_FRONT, GL_AMBIENT, (currentFrame[ledIndex-1] == 1 ? ledOnMaterial : ledOffMaterial));
glMaterialfv(GL_FRONT, GL_DIFFUSE, (currentFrame[ledIndex-1] == 1 ? ledOnMaterial : ledOffMaterial));
} }
glPushMatrix(); glPushMatrix();
@ -69,6 +70,7 @@ void drawWires() {
} }
void setScene() { void setScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
gluPerspective(ZOOM_LEVEL, WINDOW_WIDTH/WINDOW_HEIGHT, 1.0, 350.0); gluPerspective(ZOOM_LEVEL, WINDOW_WIDTH/WINDOW_HEIGHT, 1.0, 350.0);
@ -79,26 +81,17 @@ void setScene() {
} }
// OpenGL Display function // OpenGL Display function
void display() { void display(gboolean onlyForPicking) {
glClearColor(0.0, 0.0, 0.0, 1.0); glClearColor(0.0, 0.0, 0.0, 1.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING); glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0); glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light0Pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, backgroundColor); glLightfv(GL_LIGHT0, GL_AMBIENT, backgroundColor);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
setScene(); setScene();
drawWires();
drawLEDs(RENDER_MODE);
}
// Picking function if (onlyForPicking == TRUE) {
void displayPickingObjects() {
setScene();
glDisable(GL_DITHER); glDisable(GL_DITHER);
glDisable(GL_LIGHTING); glDisable(GL_LIGHTING);
@ -106,5 +99,9 @@ void displayPickingObjects() {
glEnable(GL_LIGHTING); glEnable(GL_LIGHTING);
glEnable(GL_DITHER); glEnable(GL_DITHER);
} else {
drawLEDs(RENDER_MODE);
drawWires();
}
} }

View file

@ -4,8 +4,7 @@
void drawLEDs(int mode); void drawLEDs(int mode);
void drawWires(); void drawWires();
void display(); void display(gboolean onlyForPicking);
void displayPickingObjects();
void setScene(); void setScene();

View file

@ -1,52 +1,69 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gtk/gtkgl.h> #include <gtk/gtkgl.h>
#include <GL/glut.h> #include <GL/glut.h>
#include <gdk/gdkkeysyms.h>
#include "config.h" #include "config.h"
#include "input.h"
#include "display.h" #include "display.h"
void on_main_window_delete_event(GtkObject *object, gpointer userData) { void on_main_window_delete_event(GtkObject *object, gpointer userData) {
gtk_main_quit(); 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) { gboolean on_drawing_area_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data) {
GdkGLContext *glContext = gtk_widget_get_gl_context(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);
if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return FALSE; if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return FALSE;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
display(); display(FALSE);
if (gdk_gl_drawable_is_double_buffered(glDrawable)) if (gdk_gl_drawable_is_double_buffered(glDrawable))
gdk_gl_drawable_swap_buffers(glDrawable); gdk_gl_drawable_swap_buffers(glDrawable);
else else
glFlush(); glFlush();
gdk_gl_drawable_gl_end(glDrawable); gdk_gl_drawable_gl_end(glDrawable);
return FALSE; return FALSE;
} }
void on_drawing_area_realize(GtkWidget *widget, gpointer data) { 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);
}
void on_drawing_area_button_press_event(GtkWidget *widget, gpointer data) {
gtk_widget_grab_focus(widget);
GdkGLContext *glContext = gtk_widget_get_gl_context(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; if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
display(TRUE);
mouse(x, y);
gdk_gl_drawable_gl_end(glDrawable); gdk_gl_drawable_gl_end(glDrawable);
} gtk_widget_queue_draw_area(widget, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
void on_drawing_area_key_press_event(GtkWidget *widget, gpointer data) {
g_print("pressed");
} }

View file

@ -23,40 +23,10 @@ void moveCameraAngle(float angle) {
if (eyeAngle < 0) eyeAngle = 0; if (eyeAngle < 0) eyeAngle = 0;
} }
void mouse(int x, int y) {
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]; int position, viewport[4];
GLubyte pixel[3]; GLubyte pixel[3];
displayPickingObjects();
glGetIntegerv(GL_VIEWPORT, viewport); glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(x, viewport[3]-y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, (void*)pixel); glReadPixels(x, viewport[3]-y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, (void*)pixel);
position = ((int)pixel[2]/8)-1; // Selected LED position = ((int)pixel[2]/8)-1; // Selected LED
@ -64,9 +34,5 @@ void mouse(int button, int state, int x, int y) {
printf("%d\n", position); printf("%d\n", position);
currentFrame[position] = currentFrame[position] == 0 ? 1 : 0; currentFrame[position] = currentFrame[position] == 0 ? 1 : 0;
printf("%d\n", currentFrame[position]); printf("%d\n", currentFrame[position]);
// FIXME: Redraw the gl-container
//display();
}
} }

View file

@ -4,10 +4,7 @@
void moveCameraPosition(float direction); void moveCameraPosition(float direction);
void moveCameraAngle(float angle); void moveCameraAngle(float angle);
void keyboard(unsigned char key, int x, int y); void mouse(int x, int y);
void keyboard_special(int key, int x, int y);
void mouse(int button, int state, int x, int y);
#endif #endif

View file

@ -12,11 +12,13 @@
#include "input.h" #include "input.h"
#include "event_callbacks.c" #include "event_callbacks.c"
// TODO: Refactor to GLib-Datatypes (page 747)
// Materials // 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 ledOffMaterial[] = {0.1, 0.1, 0.1, 0.0};
float wireMaterial[] = {0.7, 0.7, 0.7, 1.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 // Colors
float backgroundColor[] = {0.3, 0.3, 0.3, 0.4}; float backgroundColor[] = {0.3, 0.3, 0.3, 0.4};
@ -48,31 +50,35 @@ int main(int argc, char *argv[]) {
gluQuadricNormals(quadric, GLU_SMOOTH); gluQuadricNormals(quadric, GLU_SMOOTH);
gluQuadricDrawStyle(quadric, GLU_FILL); gluQuadricDrawStyle(quadric, GLU_FILL);
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH); 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 // Configure the OpenGL widget
glConfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE); glConfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE);
if (glConfig == NULL) { 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); glConfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH);
if (glConfig == NULL) { 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); exit(1);
} }
} }
xml = glade_xml_new("src/main_gui.glade", NULL, NULL); xml = glade_xml_new("src/main_gui.glade", NULL, NULL);
glade_xml_signal_autoconnect(xml);
window = glade_xml_get_widget(xml, "main_window"); window = glade_xml_get_widget(xml, "main_window");
gtk_container_set_reallocate_redraws(GTK_CONTAINER(window), TRUE); gtk_container_set_reallocate_redraws(GTK_CONTAINER(window), TRUE);
drawingArea = glade_xml_get_widget(xml, "drawing_area"); drawingArea = glade_xml_get_widget(xml, "drawing_area");
gtk_widget_set_gl_capability(drawingArea, glConfig, NULL, TRUE, GDK_GL_RGBA_TYPE); gtk_widget_set_gl_capability(drawingArea, glConfig, NULL, TRUE, GDK_GL_RGBA_TYPE);
glade_xml_signal_autoconnect(xml);
gtk_widget_show(window); gtk_widget_show(window);
gtk_main(); gtk_main();

View file

@ -22,8 +22,9 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="border_width">7</property> <property name="border_width">7</property>
<child> <child>
<widget class="GtkComboBoxEntry" id="combobox_port"> <widget class="GtkLabel" id="connection_label">
<property name="visible">True</property> <property name="visible">True</property>
<property name="label" translatable="yes">LED Cube is not connected</property>
</widget> </widget>
</child> </child>
</widget> </widget>
@ -32,18 +33,6 @@
<property name="homogeneous">True</property> <property name="homogeneous">True</property>
</packing> </packing>
</child> </child>
<child>
<widget class="GtkToolButton" id="button_connect">
<property name="visible">True</property>
<property name="label" translatable="yes">Connect to cube</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-connect</property>
</widget>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child> <child>
<widget class="GtkSeparatorToolItem" id="saparator"> <widget class="GtkSeparatorToolItem" id="saparator">
<property name="visible">True</property> <property name="visible">True</property>
@ -100,10 +89,11 @@
<property name="width_request">500</property> <property name="width_request">500</property>
<property name="height_request">500</property> <property name="height_request">500</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property>
<property name="events">GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_FOCUS_CHANGE_MASK | GDK_STRUCTURE_MASK</property>
<signal name="expose_event" handler="on_drawing_area_expose_event"/> <signal name="expose_event" handler="on_drawing_area_expose_event"/>
<signal name="button_press_event" handler="on_drawing_area_button_press_event"/>
<signal name="key_press_event" handler="on_drawing_area_key_press_event"/> <signal name="key_press_event" handler="on_drawing_area_key_press_event"/>
<signal name="realize" handler="on_drawing_area_realize" after="yes"/>
<signal name="configure_event" handler="on_drawing_area_configure_event" after="yes"/>
</widget> </widget>
<packing> <packing>
<property name="position">2</property> <property name="position">2</property>