First working GTK + GL window

This commit is contained in:
Aaron Mueller 2011-12-06 22:35:48 +01:00 committed by Kai Lauterbach
parent be7e61d679
commit 37608b6b24
9 changed files with 157 additions and 101 deletions

View file

@ -1,22 +1,18 @@
CC=gcc
CFLAGS=-Wall
INCLUDES=-I/usr/include/GL
LIBS=-lglut -lGLU -lGL -lm
GTKLIBS=`pkg-config --cflags --libs gtk+-2.0` -export-dynamic
LINKER_FLAGS=-lglut -export-dynamic
GTKLIBS=`pkg-config --cflags --libs gtkglext-1.0 libglade-2.0 gmodule-export-2.0`
SRCDIR=src
all:
$(CC) $(CFLAGS) $(INCLUDES) -c $(SRCDIR)/display.c
$(CC) $(CFLAGS) $(INCLUDES) -c $(SRCDIR)/input.c
$(CC) $(CFLAGS) $(INCLUDES) -c $(SRCDIR)/main.c
$(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) $(LIBS) -o ledcube-edit main.o display.o input.o
$(CC) $(CFLAGS) $(LINKER_FLAGS) $(GTKLIBS) -o ledcube-edit main.o display.o input.o
chmod +x ledcube-edit
strip ledcube-edit
gui:
$(CC) $(CFLAGS) $(GTKLIBS) -o gui $(SRCDIR)/main_gui.c
#strip ledcube-edit
clean:
rm -f *.o

View file

@ -22,6 +22,7 @@ extern float ledOnMaterial[];
extern float ledOffMaterial[];
extern float wireMaterial[];
extern float innerWireMaterial[];
extern float backgroundColor[];
// Movement
extern float lookX, lookZ;
@ -30,6 +31,12 @@ extern int ledOrientation;
// Objects
extern GLUquadricObj *quadric;
extern GdkGLConfig *glConfig;
extern GdkGLWindow *glWindow;
extern GdkGLContext *glContext;
// Dimensions, positions
extern float light0Pos[];
// LED data
extern int currentFrame[27];

View file

@ -1,5 +1,8 @@
#include <stdio.h>
#include <glut.h>
#include <gtk/gtk.h>
#include <gdk/gdkgl.h>
#include <gtk/gtkgl.h>
#include <GL/glut.h>
#include "config.h"
#include "display.h"
@ -66,7 +69,6 @@ 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);
@ -78,10 +80,20 @@ void setScene() {
// OpenGL Display function
void display() {
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();
drawLEDs(RENDER_MODE);
drawWires();
glutSwapBuffers();
}
// Picking function

View file

@ -1,7 +1,6 @@
#ifndef _DISPLAY_H
#define _DISPLAY_H
void drawLEDs(int mode);
void drawWires();

View file

@ -0,0 +1,47 @@
#include <gtk/gtk.h>
#include <gtk/gtkgl.h>
#include <GL/glut.h>
#include "config.h"
#include "display.h"
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);
if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return FALSE;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
display();
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);
if (!gdk_gl_drawable_gl_begin(glDrawable, glContext)) return;
gdk_gl_drawable_gl_end(glDrawable);
}

View file

@ -1,6 +1,8 @@
#include <stdio.h>
#include <math.h>
#include <glut.h>
#include <gtk/gtk.h>
#include <gtk/gtkgl.h>
#include <GL/glut.h>
#include "config.h"
#include "input.h"
@ -63,7 +65,8 @@ void mouse(int button, int state, int x, int y) {
currentFrame[position] = currentFrame[position] == 0 ? 1 : 0;
printf("%d\n", currentFrame[position]);
display();
// FIXME: Redraw the gl-container
//display();
}
}

View file

@ -1,11 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
#include <glut.h>
#include <gtk/gtk.h>
#include <gtk/gtkgl.h>
#include <glade/glade.h>
#include <GL/glut.h>
#include "config.h"
#include "display.h"
#include "input.h"
#include "event_callbacks.c"
// Materials
float ledOnMaterial[] = {0.0, 0.0, 1.0, 1.0};
@ -24,43 +29,53 @@ int ledOrientation = TOP_ORIENTATION;
// Objects
GLUquadricObj *quadric;
GdkGLConfig *glConfig;
GdkGLContext *glContext;
GtkWidget *window, *drawingArea;
// LED data
int currentFrame[27] = {0};
int main(int argc, char* argv[]) {
int main(int argc, char *argv[]) {
GladeXML *xml;
gtk_init(&argc, &argv);
gdk_gl_init(&argc, &argv);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("CTHN LEDCube Editor v0.1");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(keyboard_special);
glutMouseFunc(mouse);
glClearColor(0.0, 0.0, 0.0, 1.0);
glShadeModel(GL_SMOOTH);
// Lighting
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light0Pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, backgroundColor);
glMatrixMode(GL_MODELVIEW);
moveCameraPosition(0); // Init the Position
quadric = gluNewQuadric();
gluQuadricNormals(quadric, GLU_SMOOTH);
gluQuadricDrawStyle(quadric, GLU_FILL);
glutMainLoop();
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
moveCameraPosition(0);
currentFrame[0] = 1; // TODO: remove
// 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.");
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.");
exit(1);
}
}
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);
gtk_widget_show(window);
gtk_main();
return 0;
}

View file

@ -1,26 +0,0 @@
#include <gtk/gtk.h>
void on_window_destroy(GtkObject *object, gpointer userData) {
gtk_main_quit();
}
int main (int argc, char *argv[]) {
GtkBuilder *builder;
GtkWidget *window;
gtk_set_locale();
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "src/main_gui.ui", NULL);
gtk_builder_connect_signals(builder, NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "main_window"));
g_object_unref(G_OBJECT(builder));
gtk_widget_show_all(window);
gtk_main();
return 0;
}

View file

@ -1,111 +1,114 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<glade-interface>
<!-- interface-requires gtk+ 2.16 -->
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="main_window">
<property name="visible">True</property>
<widget class="GtkWindow" id="main_window">
<property name="extension_events">cursor</property>
<property name="resizable">False</property>
<property name="window_position">center-always</property>
<signal name="destroy" handler="on_window_destroy"/>
<signal name="delete_event" handler="on_main_window_delete_event"/>
<child>
<object class="GtkVBox" id="outher_frame">
<widget class="GtkVBox" id="outher_frame">
<property name="visible">True</property>
<child>
<object class="GtkHandleBox" id="handle">
<widget class="GtkHandleBox" id="handle">
<property name="visible">True</property>
<child>
<object class="GtkToolbar" id="toolbar">
<widget class="GtkToolbar" id="toolbar">
<property name="visible">True</property>
<property name="toolbar_style">both-horiz</property>
<child>
<object class="GtkToolItem" id="combobox_frame">
<widget class="GtkToolItem" id="combobox_frame">
<property name="visible">True</property>
<property name="border_width">7</property>
<child>
<object class="GtkComboBoxEntry" id="combobox_port">
<widget class="GtkComboBoxEntry" id="combobox_port">
<property name="visible">True</property>
</object>
</widget>
</child>
</object>
</widget>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="button_connect">
<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>
</object>
</widget>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkSeparatorToolItem" id="saparator">
<widget class="GtkSeparatorToolItem" id="saparator">
<property name="visible">True</property>
</object>
</widget>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="button_add">
<widget class="GtkToolButton" id="button_add">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton3</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-add</property>
</object>
</widget>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="button_remove">
<widget class="GtkToolButton" id="button_remove">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton3</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-delete</property>
</object>
</widget>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</object>
</widget>
</child>
</object>
</widget>
<packing>
<property name="expand">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkHScale" id="frame_control">
<widget class="GtkHScale" id="frame_control">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
</widget>
<packing>
<property name="expand">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkDrawingArea" id="drawing_area">
<widget class="GtkDrawingArea" id="drawing_area">
<property name="width_request">500</property>
<property name="height_request">500</property>
<property name="visible">True</property>
</object>
<signal name="expose_event" handler="on_drawing_area_expose_event"/>
<signal name="realize" handler="on_drawing_area_realize" after="yes"/>
<signal name="configure_event" handler="on_drawing_area_configure_event" after="yes"/>
</widget>
<packing>
<property name="position">2</property>
</packing>
</child>
</object>
</widget>
</child>
</object>
</interface>
</widget>
</glade-interface>