Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

nviz3d.cpp

Go to the documentation of this file.
00001 
00002 // Name:        nviz3d.cpp
00003 // Purpose:     NeReK OpenGL Visualization Code
00004 // Author:      Chad Rempp
00005 // Modified by: Chad Rempp
00006 // Created:     06-01-03
00007 // Copyright:   (c) Chad Rempp 2003
00008 // Licence:     GNU Public Licence
00010 
00011 //TODO
00012 /*
00013    The camera is situated at the origin and every thing rotates around it
00014    it should be the other way around.
00015 */
00016 
00017 #if defined(__GNUG__) && !defined(__APPLE__)
00018 #pragma implementation
00019 #pragma interface
00020 #endif
00021 
00022 // For compilers that support precompilation, includes "wx.h".
00023 #include "wx/wxprec.h"
00024 
00025 #ifdef __BORLANDC__
00026 #pragma hdrstop
00027 #endif
00028 
00029 #ifndef WX_PRECOMP
00030 #include "wx/wx.h"
00031 #endif
00032 
00033 #if !wxUSE_GLCANVAS
00034 #error Please set wxUSE_GLCANVAS to 1 in setup.h.
00035 #endif
00036 
00037 #include "nviz3d/nviz3d.h"
00038 
00039 // !!--TODO--!!
00040 // Try to get rid of these.
00041 
00042 GLfloat neuron_radius = 0.5;
00043 int neuron_stacks = 10;
00044 int neuron_slices = 10;
00045 
00046 static float last_x = 0,
00047              last_y = 0;
00048 static GLfloat camera_x = 0;
00049 static GLfloat camera_y = 0;
00050 static GLfloat camera_zoom = -6.0;
00051 static GLfloat camera_rotate = 0.0;
00052 
00053 GLfloat drag_scale_x = 0.05;
00054 GLfloat drag_scale_y = 0.05;
00055 GLfloat rotate_scale = 0.01;
00056 GLfloat zoom_scale   = 0.05;
00057 
00058 wxTextCtrl *txt_drag_scale_x;
00059 wxTextCtrl *txt_drag_scale_y;
00060 
00061 GLUquadricObj *gl_neuron_00 = gluNewQuadric();
00062 GLUquadricObj *gl_neuron_01 = gluNewQuadric();
00063 GLUquadricObj *gl_neuron_02 = gluNewQuadric();
00064 GLUquadricObj *gl_neuron_03 = gluNewQuadric();
00065 GLUquadricObj *gl_neuron_04 = gluNewQuadric();
00066 GLUquadricObj *gl_neuron_05 = gluNewQuadric();
00067 
00069 //  NViz3DCanvas class Implementation
00071 
00072 BEGIN_EVENT_TABLE(Nviz3DCanvas, wxGLCanvas)
00073     EVT_SIZE(Nviz3DCanvas::OnSize)
00074     EVT_PAINT(Nviz3DCanvas::OnPaint)
00075     EVT_ERASE_BACKGROUND(Nviz3DCanvas::OnEraseBackground)
00076     EVT_ENTER_WINDOW( Nviz3DCanvas::OnEnterWindow )
00077     EVT_MOUSE_EVENTS( Nviz3DCanvas::OnMouse )
00078 END_EVENT_TABLE()
00079 
00081 Nviz3DCanvas::Nviz3DCanvas(wxWindow *parent, wxWindowID id,
00082                            const wxPoint& pos, const wxSize& size,
00083                            long style, const wxString& name):
00084              wxGLCanvas(parent, (wxGLCanvas*) NULL, id, pos, size, style, name )
00085 {
00086     m_init = FALSE;
00087 }
00088 
00090 Nviz3DCanvas::~Nviz3DCanvas()
00091 {
00092     // Nothing Here
00093 }
00094 
00096 void Nviz3DCanvas::DrawNeuron(GLUquadricObj *obj, GLfloat x, GLfloat y, GLfloat z)
00097 {
00098     glPushMatrix();
00099     glTranslatef(x, y, z);
00100     gluSphere(obj, neuron_radius, neuron_stacks, neuron_slices);
00101     glPopMatrix();
00102 }
00103 
00105 void Nviz3DCanvas::InitGL()
00106 {
00107     SetCurrent();
00108 
00109     // set viewing projection
00110     glMatrixMode(GL_PROJECTION);
00111     glFrustum(-1.0F, 1.0F, -1.0F, 1.0F, 1.0F, 100.0F);
00112 
00113     // position viewer
00114     glMatrixMode(GL_MODELVIEW);
00115     glLoadIdentity();
00116     glTranslated(0.0, 0.0, -4.0);
00117 
00118     glEnable(GL_DEPTH_TEST);
00119     glEnable(GL_LIGHTING);
00120     glEnable(GL_LIGHT0);
00121 }
00122 
00124 void Nviz3DCanvas::OnEnterWindow( wxMouseEvent& event )
00125 {
00126     SetFocus();
00127 }
00128 
00130 void Nviz3DCanvas::OnEraseBackground(wxEraseEvent& event)
00131 {
00132   // Do nothing, to avoid flashing.
00133 }
00134 
00136 void Nviz3DCanvas::OnMouse(wxMouseEvent &event)
00137 {
00138 // I think all this dragging stuff is a way to waste a cycle to get a last_x or
00139 // y before drawing. Still not working but good enough for now.
00140     static int dragging = 0;
00141 
00142     if (event.LeftIsDown())
00143     {
00144         if (!dragging)
00145         {
00146             dragging = 1;
00147         }
00148         else
00149         {
00150             camera_x += (event.GetX() - last_x)*(drag_scale_x);
00151             camera_y -= (event.GetY() - last_y)*(drag_scale_y);
00152             Refresh(FALSE);
00153         }
00154         last_x = event.GetX();
00155         last_y = event.GetY();
00156     }
00157     else if (event.RightIsDown())
00158     {
00159         if (!dragging)
00160         {
00161             dragging = 1;
00162         }
00163         else
00164         {
00165             camera_rotate += (event.GetX() - last_x)*(rotate_scale);
00166             camera_zoom  += (event.GetY() - last_y)*(zoom_scale);
00167             Refresh(FALSE);
00168         }
00169         last_x = event.GetX();
00170         last_y = event.GetY();
00171     }
00172     else
00173     {
00174         dragging = 0;
00175         camera_rotate = 0;
00176     }
00177 }
00178 
00180 void Nviz3DCanvas::OnPaint( wxPaintEvent& event )
00181 {
00182     Render();
00183 }
00184 
00186 void Nviz3DCanvas::OnSize(wxSizeEvent& event)
00187 {
00188     // this is also necessary to update the context on some platforms
00189     wxGLCanvas::OnSize(event);
00190 
00191     // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
00192     int w, h;
00193     GetClientSize(&w, &h);
00194 #ifndef __WXMOTIF__
00195     if (GetContext())
00196 #endif
00197     {
00198         SetCurrent();
00199         glViewport(0, 0, (GLint) w, (GLint) h);
00200     }
00201 }
00202 
00204 void Nviz3DCanvas::Render()
00205 {
00206     // This is apperantly a dummy to avoid an endless succession of paint
00207     // messages. OnPaint handlers must always create a wxPaintDC.
00208     wxPaintDC dc(this);
00209 
00210 #ifndef __WXMOTIF__
00211     if (!GetContext()) return;
00212 #endif
00213 
00214     SetCurrent();
00215     // init OpenGL once, but after SetCurrent
00216     if (!m_init)
00217     {
00218         InitGL();
00219         m_init = TRUE;
00220     }
00221 
00222     // clear color and depth buffers
00223     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00224 
00225     glPushMatrix();
00226     glMatrixMode(GL_MODELVIEW);
00227     glTranslatef(camera_x, camera_y, camera_zoom);
00228     glMatrixMode(GL_PROJECTION);
00229     glRotatef(camera_rotate, 0.0, 1.0, 0.0);
00230     glMatrixMode(GL_MODELVIEW);
00231 
00232     DrawNeuron(gl_neuron_00 , -2.0, -4.0, 0.0);
00233     DrawNeuron(gl_neuron_01 , 2.0, -4.0, 0.0);
00234     DrawNeuron(gl_neuron_02 , -2.0, 0.0, 0.0);
00235     DrawNeuron(gl_neuron_03 , 2.0, .0, 0.0);
00236     DrawNeuron(gl_neuron_04 , -2.0, 4.0, 0.0);
00237     DrawNeuron(gl_neuron_05 , 2.0, 4.0, 0.0);
00238 
00239     glLineWidth(8);
00240     glBegin(GL_LINES);
00241        glVertex3f(-2.0, -4.0, 0.0);
00242        glVertex3f(-2.0, 0.0, 0.0);
00243     glEnd();
00244 
00245     glPopMatrix();
00246 
00247   glFlush();
00248   SwapBuffers();
00249 }
00250 
00252 //  NVizFrame class Implementation
00254 
00255 BEGIN_EVENT_TABLE(NvizFrame, wxFrame)
00256     EVT_MENU(wxID_EXIT, NvizFrame::OnExit)
00257 END_EVENT_TABLE()
00258 
00260 NvizFrame::NvizFrame(wxDialog *parent, const wxString& title, const wxPoint& pos,
00261                      const wxSize& size, long style)
00262                      : wxFrame(parent, -1, title, pos, size, style)
00263 {
00264     glc_nvizcanvas = new Nviz3DCanvas(this, -1, wxDefaultPosition, wxDefaultSize);
00265 }
00266 
00268 void NvizFrame::OnExit(wxCommandEvent &event)
00269 {
00270     Destroy();
00271 }
00272 

Generated on Mon Jun 23 23:09:52 2003 for NeReK Documentation by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002