From b2d11488fa1bd10d135581c273a4ac5d759376f7 Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Sun, 30 Oct 2011 23:42:46 +0100 Subject: [PATCH 1/3] Strip the resulting binary to redurce some space --- editor/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/editor/Makefile b/editor/Makefile index e9e50d8..ef0f056 100644 --- a/editor/Makefile +++ b/editor/Makefile @@ -12,6 +12,7 @@ all: $(CC) $(CFLAGS) $(LIBS) -o ledcube-edit main.o display.o input.o chmod +x ledcube-edit + strip ledcube-edit clean: rm -f *.o From 37e885f02eae5a2f154b4ec1928803242771ee3d Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Sun, 30 Oct 2011 23:55:53 +0100 Subject: [PATCH 2/3] Add the picking feature. We can now select a LED on the screen and get the index of the LED. To get the right index, we use the color picking technique mentioned in the OpenGL RedBook. This works suprisingly pretty well (and fast!). --- editor/src/config.h | 8 ++++++-- editor/src/display.c | 40 ++++++++++++++++++++++++++++++---------- editor/src/display.h | 7 ++++++- editor/src/input.c | 14 ++++++++++++++ editor/src/input.h | 2 ++ editor/src/main.c | 1 + 6 files changed, 59 insertions(+), 13 deletions(-) diff --git a/editor/src/config.h b/editor/src/config.h index dfe7447..6937709 100644 --- a/editor/src/config.h +++ b/editor/src/config.h @@ -10,8 +10,12 @@ #define PI 3.1415926535897932 -#define TOP_ORIENTATION 1 -#define SIDE_ORIENTATION 2 +// Poor Man's enums +#define TOP_ORIENTATION 0x01 +#define SIDE_ORIENTATION 0x02 + +#define RENDER_MODE 0x01 +#define PICKING_MODE 0x02 // Materials extern float ledOnMaterial[]; diff --git a/editor/src/display.c b/editor/src/display.c index d5ec987..11f1cf5 100644 --- a/editor/src/display.c +++ b/editor/src/display.c @@ -5,21 +5,23 @@ #include "display.h" -void drawLEDCube(orientation) { +void drawLEDs(int orientation, int mode) { int x, y, z; + int colorIndex = 0; if (orientation == TOP_ORIENTATION) { glRotatef(90, 2, 0, 0); } - // LEDs - glMaterialfv(GL_FRONT, GL_AMBIENT, ledOnMaterial); for (z=-10; z<=10; z+=10) // Ebene for (y=-10; y<=10; y+=10) // Zeile for (x=-10; x<=10; x+=10) { // Spalte - - // TODO: Test different colors - glMaterialfv(GL_FRONT, GL_AMBIENT, (z == 0 ? ledOnMaterial : ledOffMaterial)); + if (mode == PICKING_MODE) { + glColor3ub(0, 0, colorIndex*8); + colorIndex++; + } else { + glMaterialfv(GL_FRONT, GL_AMBIENT, (z == 0 ? ledOnMaterial : ledOffMaterial)); + } glPushMatrix(); glTranslatef(x, y, z-0.8); @@ -35,8 +37,10 @@ void drawLEDCube(orientation) { glPopMatrix(); } +} - // Wires +void drawWires() { + int x, y; for (y=-10; y<=10; y+=10) for (x=-10; x<=10; x+=10) { glMaterialfv(GL_FRONT, GL_AMBIENT, ((x == 0 || y == 0) ? innerWireMaterial : wireMaterial)); @@ -61,9 +65,8 @@ void drawLEDCube(orientation) { } } -void display() { +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); @@ -71,8 +74,25 @@ void display() { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(lookX, eyeAngle, lookZ, 0, 0, 0, 0, 1, 0); +} - drawLEDCube(TOP_ORIENTATION); +// OpenGL Display function +void display() { + setScene(); + drawLEDs(TOP_ORIENTATION, RENDER_MODE); + drawWires(); glutSwapBuffers(); } +// Picking function +void displayPickingObjects() { + setScene(); + glDisable(GL_DITHER); + glDisable(GL_LIGHTING); + + drawLEDs(TOP_ORIENTATION, PICKING_MODE); + + glEnable(GL_LIGHTING); + glEnable(GL_DITHER); +} + diff --git a/editor/src/display.h b/editor/src/display.h index 079f5bc..99de370 100644 --- a/editor/src/display.h +++ b/editor/src/display.h @@ -2,8 +2,13 @@ #define _DISPLAY_H -void drawLEDCube(); +void drawLEDs(int orientation, int mode); +void drawWires(); + void display(); +void displayPickingObjects(); + +void setScene(); #endif diff --git a/editor/src/input.c b/editor/src/input.c index 8759e6c..0632dbb 100644 --- a/editor/src/input.c +++ b/editor/src/input.c @@ -4,6 +4,7 @@ #include "config.h" #include "input.h" +#include "display.h" void moveCameraPosition(float direction) { eyePos += direction; @@ -46,3 +47,16 @@ void keyboard_special(int key, int x, int y) { glutPostRedisplay(); } +void mouse(int button, int state, int x, int y) { + if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { + int viewport[4]; + GLubyte pixel[3]; + + displayPickingObjects(); + + glGetIntegerv(GL_VIEWPORT, viewport); + glReadPixels(x, viewport[3]-y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, (void*)pixel); + printf("%d\n", pixel[2]/8); // Selected LED + } +} + diff --git a/editor/src/input.h b/editor/src/input.h index d3bc2b9..bb48fa1 100644 --- a/editor/src/input.h +++ b/editor/src/input.h @@ -7,5 +7,7 @@ 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 d0f9eaf..3a1861c 100644 --- a/editor/src/main.c +++ b/editor/src/main.c @@ -33,6 +33,7 @@ int main(int argc, char* argv[]) { glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutSpecialFunc(keyboard_special); + glutMouseFunc(mouse); glClearColor(0.0, 0.0, 0.0, 1.0); glShadeModel(GL_SMOOTH); From 2694d9988f1051106c3fe860c4523dd41c0f1948 Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Mon, 31 Oct 2011 00:25:07 +0100 Subject: [PATCH 3/3] Implement the LED voltage switch. LEDs can now be switched on or off with the mouse. The current state of the LEd cube is saved in a array which can be easily printed out to STDOUT. --- editor/src/config.h | 4 ++++ editor/src/display.c | 16 ++++++++-------- editor/src/display.h | 2 +- editor/src/input.c | 11 +++++++++-- editor/src/main.c | 16 +++++++++++----- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/editor/src/config.h b/editor/src/config.h index 6937709..ae1ea1b 100644 --- a/editor/src/config.h +++ b/editor/src/config.h @@ -26,9 +26,13 @@ extern float innerWireMaterial[]; // Movement extern float lookX, lookZ; extern float eyePos, eyeAngle; +extern int ledOrientation; // Objects extern GLUquadricObj *quadric; +// LED data +extern int currentFrame[27]; + #endif diff --git a/editor/src/display.c b/editor/src/display.c index 11f1cf5..b009d74 100644 --- a/editor/src/display.c +++ b/editor/src/display.c @@ -5,22 +5,22 @@ #include "display.h" -void drawLEDs(int orientation, int mode) { +void drawLEDs(int mode) { int x, y, z; - int colorIndex = 0; + int ledIndex = 0; - if (orientation == TOP_ORIENTATION) { + if (ledOrientation == TOP_ORIENTATION) { glRotatef(90, 2, 0, 0); } for (z=-10; z<=10; z+=10) // Ebene for (y=-10; y<=10; y+=10) // Zeile for (x=-10; x<=10; x+=10) { // Spalte + ledIndex++; if (mode == PICKING_MODE) { - glColor3ub(0, 0, colorIndex*8); - colorIndex++; + glColor3ub(0, 0, ledIndex*8); } else { - glMaterialfv(GL_FRONT, GL_AMBIENT, (z == 0 ? ledOnMaterial : ledOffMaterial)); + glMaterialfv(GL_FRONT, GL_AMBIENT, (currentFrame[ledIndex-1] == 1 ? ledOnMaterial : ledOffMaterial)); } glPushMatrix(); @@ -79,7 +79,7 @@ void setScene() { // OpenGL Display function void display() { setScene(); - drawLEDs(TOP_ORIENTATION, RENDER_MODE); + drawLEDs(RENDER_MODE); drawWires(); glutSwapBuffers(); } @@ -90,7 +90,7 @@ void displayPickingObjects() { glDisable(GL_DITHER); glDisable(GL_LIGHTING); - drawLEDs(TOP_ORIENTATION, PICKING_MODE); + drawLEDs(PICKING_MODE); glEnable(GL_LIGHTING); glEnable(GL_DITHER); diff --git a/editor/src/display.h b/editor/src/display.h index 99de370..54e4cf2 100644 --- a/editor/src/display.h +++ b/editor/src/display.h @@ -2,7 +2,7 @@ #define _DISPLAY_H -void drawLEDs(int orientation, int mode); +void drawLEDs(int mode); void drawWires(); void display(); diff --git a/editor/src/input.c b/editor/src/input.c index 0632dbb..31cf7b5 100644 --- a/editor/src/input.c +++ b/editor/src/input.c @@ -6,6 +6,7 @@ #include "input.h" #include "display.h" + void moveCameraPosition(float direction) { eyePos += direction; if (eyePos > 360.0) eyePos = 0.0; @@ -49,14 +50,20 @@ void keyboard_special(int key, int x, int y) { void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { - int viewport[4]; + 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); - printf("%d\n", pixel[2]/8); // Selected LED + position = ((int)pixel[2]/8)-1; // Selected LED + + printf("%d\n", position); + currentFrame[position] = currentFrame[position] == 0 ? 1 : 0; + printf("%d\n", currentFrame[position]); + + display(); } } diff --git a/editor/src/main.c b/editor/src/main.c index 3a1861c..83c5f2c 100644 --- a/editor/src/main.c +++ b/editor/src/main.c @@ -1,4 +1,3 @@ - #include #include #include @@ -7,28 +6,35 @@ #include "display.h" #include "input.h" + +// Materials float ledOnMaterial[] = {0.0, 0.0, 1.0, 1.0}; 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}; +// Colors float backgroundColor[] = {0.3, 0.3, 0.3, 0.4}; -float light0Pos[] = {70, 70, 70, 0.0}; +// Positions +float light0Pos[] = {70, 70, 70, 0.0}; float lookX = 0.0, lookZ = 0.0; float eyePos = 0.0, eyeAngle = 45.0; +int ledOrientation = TOP_ORIENTATION; +// Objects GLUquadricObj *quadric; -extern void moveCameraPosition(float direction); +// LED data +int currentFrame[27] = {0}; + int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); - glutCreateWindow("CTHN LEDCube Editor"); + glutCreateWindow("CTHN LEDCube Editor v0.1"); glutDisplayFunc(display); glutKeyboardFunc(keyboard);