Visualization running in wxWidgets GUI

This commit is contained in:
PaulStoffregen 2016-02-18 01:31:30 -08:00
commit afc7c69942
6 changed files with 181 additions and 18 deletions

View file

@ -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

94
gui.cpp
View file

@ -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();
}

58
gui.h
View file

@ -2,6 +2,60 @@
#define gui__h_
#include <wx/wx.h>
#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 <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <gl.h>
#include <glu.h>
#endif
#else
#include <GL/gl.h>
#include <GL/glu.h>
#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()
};

View file

@ -1,23 +1,17 @@
#include "imuread.h"
#include <GL/glut.h> // 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);

View file

@ -13,7 +13,8 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <GL/glut.h> // sudo apt-get install xorg-dev libglu1-mesa-dev freeglut3-dev
#include <GL/gl.h>
#include <GL/glu.h>
#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"

View file

@ -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(&current_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() ;
}