diff --git a/Makefile b/Makefile index 78ed4f4..728eb82 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ WXCONFIG = ~/wxwidgets/3.0.2.gtk2-opengl/bin/wx-config all: gui imuread gui: gui.o visualize.o serialdata.o - g++ $(CFLAGS) -o $@ $^ `$(WXCONFIG) --libs` -lglut -lGLU -lGL + g++ $(CFLAGS) -o $@ $^ `$(WXCONFIG) --libs all,opengl` imuread: imuread.o visualize.o serialdata.o $(CC) $(CFLAGS) -o $@ $^ -lglut -lGLU -lGL diff --git a/gui.cpp b/gui.cpp index ecbcbd7..07fb5e6 100644 --- a/gui.cpp +++ b/gui.cpp @@ -1,9 +1,65 @@ #include "gui.h" #include "imuread.h" + + + + + +wxBEGIN_EVENT_TABLE(MyCanvas, wxGLCanvas) + EVT_SIZE(MyCanvas::OnSize) + EVT_PAINT(MyCanvas::OnPaint) + //EVT_CHAR(MyCanvas::OnChar) + //EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent) +wxEND_EVENT_TABLE() + +MyCanvas::MyCanvas(wxWindow *parent, wxWindowID id, int* gl_attrib) + : wxGLCanvas(parent, id, gl_attrib) +{ + //m_xrot = 0; + //m_yrot = 0; + //m_numverts = 0; + // Explicitly create a new rendering context instance for this canvas. + m_glRC = new wxGLContext(this); +} + +MyCanvas::~MyCanvas() +{ + delete m_glRC; +} + +void MyCanvas::OnSize(wxSizeEvent& event) +{ + //printf("OnSize\n"); + if (!IsShownOnScreen()) return; + SetCurrent(*m_glRC); + resize_callback(event.GetSize().x, event.GetSize().y); +} + +void MyCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) ) +{ + //printf("OnPaint\n"); + wxPaintDC dc(this); + SetCurrent(*m_glRC); + display_callback(); + SwapBuffers(); +} + +void MyCanvas::InitGL() +{ + //printf("Init\n"); + SetCurrent(*m_glRC); + visualize_init(); +} + + + + + BEGIN_EVENT_TABLE(MyFrame,wxFrame) EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) EVT_MENU(wxID_EXIT, MyFrame::OnQuit) + EVT_TIMER(ID_TIMER, MyFrame::OnTimer) END_EVENT_TABLE() @@ -11,7 +67,44 @@ MyFrame::MyFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style) : wxFrame( parent, id, title, position, size, style ) { + wxMenuBar *menuBar; + wxMenu *menu; + wxMenuItem *item; + menuBar = new wxMenuBar; + menu = new wxMenu; + menu->Append(wxID_EXIT, wxT("Quit")); + menuBar->Append(menu, wxT("&File")); + + menu = new wxMenu; + //item = new wxMenuItem(menu, ID_ABOUT, "About"); + menu->Append(wxID_ABOUT, wxT("About")); + menuBar->Append(menu, wxT("&Help")); + + SetMenuBar(menuBar); + + int gl_attrib[20] = + { WX_GL_RGBA, WX_GL_MIN_RED, 1, WX_GL_MIN_GREEN, 1, + WX_GL_MIN_BLUE, 1, WX_GL_DEPTH_SIZE, 1, + WX_GL_DOUBLEBUFFER, + None}; + + + m_canvas = new MyCanvas(this, wxID_ANY, gl_attrib); + Show(true); + Raise(); + m_canvas->InitGL(); + + open_port(PORT); + m_timer = new wxTimer(this, ID_TIMER); + m_timer->Start(40, wxTIMER_CONTINUOUS); +} + +void MyFrame::OnTimer(wxTimerEvent &event) +{ + //printf("OnTimer\n"); + read_serial_data(); + m_canvas->Refresh(); } void MyFrame::OnAbout(wxCommandEvent &event) @@ -29,6 +122,7 @@ void MyFrame::OnQuit( wxCommandEvent &event ) MyFrame::~MyFrame(void) { + close_port(); } diff --git a/gui.h b/gui.h index 71b942d..3326aaf 100644 --- a/gui.h +++ b/gui.h @@ -2,6 +2,60 @@ #define gui__h_ #include +#include "wx/timer.h" +#include "wx/glcanvas.h" +#include "wx/math.h" +#include "wx/log.h" +#include "wx/wfstream.h" +#include "wx/zstream.h" +#include "wx/txtstrm.h" +#if defined(__WXMAC__) || defined(__WXCOCOA__) +#ifdef __DARWIN__ +#include +#include +#else +#include +#include +#endif +#else +#include +#include +#endif + + +#define ID_TIMER 10000 + + +class MyCanvas : public wxGLCanvas +{ +public: + MyCanvas(wxWindow *parent, + wxWindowID id = wxID_ANY, + int *gl_attrib = NULL); + + virtual ~MyCanvas(); + + void OnPaint(wxPaintEvent& event); + void OnSize(wxSizeEvent& event); + void OnChar(wxKeyEvent& event); + void OnMouseEvent(wxMouseEvent& event); + + void LoadSurface(const wxString& filename); + void InitMaterials(); + void InitGL(); + +private: + wxGLContext *m_glRC; + + //GLfloat m_verts[MAXVERTS][3]; + //GLfloat m_norms[MAXVERTS][3]; + //GLint m_numverts; + //GLfloat m_xrot; + //GLfloat m_yrot; + + wxDECLARE_NO_COPY_CLASS(MyCanvas); + wxDECLARE_EVENT_TABLE(); +}; class MyFrame: public wxFrame @@ -13,9 +67,13 @@ public: const wxSize &size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE); ~MyFrame(void); + void InitGL(); private: + MyCanvas *m_canvas; + wxTimer *m_timer; void OnAbout(wxCommandEvent &event); void OnQuit(wxCommandEvent &event); + void OnTimer(wxTimerEvent &event); DECLARE_EVENT_TABLE() }; diff --git a/imuread.c b/imuread.c index 8fa1df6..e4692a4 100644 --- a/imuread.c +++ b/imuread.c @@ -1,23 +1,17 @@ #include "imuread.h" +#include // sudo apt-get install xorg-dev libglu1-mesa-dev freeglut3-dev -void timer_callback(int val) +static void timer_callback(int val) { glutTimerFunc(TIMEOUT_MSEC, timer_callback, 0); read_serial_data(); glutPostRedisplay(); // TODO: only redisplay if data changes } -void resize_callback(int width, int height) +static void glut_display_callback(void) { - const float ar = (float) width / (float) height; - - glViewport(0, 0, width, height); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity() ; + display_callback(); + glutSwapBuffers(); } int main(int argc, char *argv[]) @@ -30,7 +24,7 @@ int main(int argc, char *argv[]) visualize_init(); glutReshapeFunc(resize_callback); - glutDisplayFunc(display_callback); + glutDisplayFunc(glut_display_callback); glutTimerFunc(TIMEOUT_MSEC, timer_callback, 0); open_port(PORT); diff --git a/imuread.h b/imuread.h index c06c744..9b9661e 100644 --- a/imuread.h +++ b/imuread.h @@ -13,7 +13,8 @@ #include #include #include -#include // sudo apt-get install xorg-dev libglu1-mesa-dev freeglut3-dev +#include +#include #define PORT "/dev/ttyACM0" #define TIMEOUT_MSEC 40 @@ -53,7 +54,6 @@ extern void close_port(void); void visualize_init(void); void display_callback(void); void resize_callback(int width, int height); -void timer_callback(int val); #ifdef __cplusplus } // extern "C" diff --git a/visualize.c b/visualize.c index da8db92..22f0857 100644 --- a/visualize.c +++ b/visualize.c @@ -52,11 +52,11 @@ magdata_t caldata[MAGBUFFSIZE]; void display_callback(void) { int i; - //float x, y, z; float xscale, yscale, zscale; float xoff, yoff, zoff; float rotation[9]; magdata_t point, draw; + GLUquadric *sphere; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1, 0, 0); // set current color to red @@ -69,6 +69,9 @@ void display_callback(void) zoff = -7.0; if (hard_iron.valid) { + sphere = gluNewQuadric(); + gluQuadricDrawStyle(sphere, GLU_FILL); + gluQuadricNormals(sphere, GLU_SMOOTH); quad_to_rotation(¤t_orientation, rotation); for (i=0; i < MAGBUFFSIZE; i++) { if (caldata[i].valid) { @@ -87,12 +90,26 @@ void display_callback(void) draw.z * yscale + yoff, draw.y * zscale + zoff ); - glutSolidSphere(0.08,16,16); // radius, slices, stacks + gluSphere(sphere, 0.08, 12, 10); + glPopMatrix(); } } + gluDeleteQuadric(sphere); } - glutSwapBuffers(); +} + +void resize_callback(int width, int height) +{ + const float ar = (float) width / (float) height; + + glViewport(0, 0, width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity() ; }