Main Page   Namespace List   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:
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.h"
00038 
00039 //-----------------------------------------------------------------------------
00040 //  NViz3DApp class Implementation
00041 //-----------------------------------------------------------------------------
00042 
00043 IMPLEMENT_APP(Nviz3DApp)
00044 
00045 //----------Nviz3DApp OnInit Main Method----------------------------------------------
00046 bool Nviz3DApp::OnInit(void)
00047 {
00048     // Create the main frame window
00049     NvizFrame *frame = new NvizFrame(NULL, "Nviz3D OpenGL Demo", wxPoint(50, 50),
00050                                wxSize(400, 300));
00051     frame->glc_nvizcanvas = new Nviz3DCanvas(frame, -1, wxDefaultPosition, wxDefaultSize);
00052 
00053     // Make a menubar
00054     wxMenu *winMenu = new wxMenu;
00055     winMenu->Append(wxID_EXIT, "&Close");
00056     winMenu->Append(SETTINGS, "&Settings");
00057     wxMenuBar *menuBar = new wxMenuBar;
00058     menuBar->Append(winMenu, "&Window");
00059     frame->SetMenuBar(menuBar);
00060 
00061     // Show the frame
00062     frame->Show(TRUE);
00063 
00064     return TRUE;
00065 }
00066 
00067 //-----------------------------------------------------------------------------
00068 //  NViz3DCanvas class Implementation
00069 //-----------------------------------------------------------------------------
00070 
00071 BEGIN_EVENT_TABLE(Nviz3DCanvas, wxGLCanvas)
00072     EVT_SIZE(Nviz3DCanvas::OnSize)
00073     EVT_PAINT(Nviz3DCanvas::OnPaint)
00074     EVT_ERASE_BACKGROUND(Nviz3DCanvas::OnEraseBackground)
00075     EVT_ENTER_WINDOW( Nviz3DCanvas::OnEnterWindow )
00076     EVT_MOUSE_EVENTS( Nviz3DCanvas::OnMouse )
00077 END_EVENT_TABLE()
00078 
00079 //----------Nviz3DCanvas Constructor-------------------------------------------
00080 Nviz3DCanvas::Nviz3DCanvas(wxWindow *parent, wxWindowID id,
00081                            const wxPoint& pos, const wxSize& size,
00082                            long style, const wxString& name):
00083              wxGLCanvas(parent, (wxGLCanvas*) NULL, id, pos, size, style, name )
00084 {
00085     m_init = FALSE;
00086 }
00087 
00088 //----------Nviz3DCanvas Destructor--------------------------------------------
00089 Nviz3DCanvas::~Nviz3DCanvas()
00090 {
00091     // Nothing Here
00092 }
00093 
00094 //----------Nviz3DCanvas DrawNeuron Method-------------------------------------
00095 void Nviz3DCanvas::DrawNeuron(GLUquadricObj *obj, GLfloat x, GLfloat y, GLfloat z)
00096 {
00097     glPushMatrix();
00098     glTranslatef(x, y, z);
00099     gluSphere(obj, neuron_radius, neuron_stacks, neuron_slices);
00100     glPopMatrix();
00101 }
00102 
00103 //----------Nviz3DCanvas InitGL Method-----------------------------------------
00104 void Nviz3DCanvas::InitGL()
00105 {
00106     SetCurrent();
00107 
00108     // set viewing projection
00109     glMatrixMode(GL_PROJECTION);
00110     glFrustum(-1.0F, 1.0F, -1.0F, 1.0F, 1.0F, 100.0F);
00111 
00112     // position viewer
00113     glMatrixMode(GL_MODELVIEW);
00114     glLoadIdentity();
00115     glTranslated(0.0, 0.0, -4.0);
00116 
00117     glEnable(GL_DEPTH_TEST);
00118     glEnable(GL_LIGHTING);
00119     glEnable(GL_LIGHT0);
00120 }
00121 
00122 //----------Nviz3DCanvas OnEnterWindow Method----------------------------------
00123 void Nviz3DCanvas::OnEnterWindow( wxMouseEvent& event )
00124 {
00125     SetFocus();
00126 }
00127 
00128 //----------Nviz3DCanvas OnEraseBackGround Method------------------------------
00129 void Nviz3DCanvas::OnEraseBackground(wxEraseEvent& event)
00130 {
00131   // Do nothing, to avoid flashing.
00132 }
00133 
00134 //----------Nviz3DCanvas OnMouse Method----------------------------------------
00135 void Nviz3DCanvas::OnMouse(wxMouseEvent &event)
00136 {
00137 // I think all this dragging stuff is a way to waste a cycle to get a last_x or
00138 // y before drawing. Still not working but good enough for now.
00139     static int dragging = 0;
00140 
00141     if (event.LeftIsDown())
00142     {
00143         if (!dragging)
00144         {
00145             dragging = 1;
00146         }
00147         else
00148         {
00149             camera_x += (event.GetX() - last_x)*(drag_scale_x);
00150             camera_y -= (event.GetY() - last_y)*(drag_scale_y);
00151             Refresh(FALSE);
00152         }
00153         last_x = event.GetX();
00154         last_y = event.GetY();
00155     }
00156     else if (event.RightIsDown())
00157     {
00158         if (!dragging)
00159         {
00160             dragging = 1;
00161         }
00162         else
00163         {
00164             camera_rotate += (event.GetX() - last_x)*(rotate_scale);
00165             camera_zoom  += (event.GetY() - last_y)*(zoom_scale);
00166             Refresh(FALSE);
00167         }
00168         last_x = event.GetX();
00169         last_y = event.GetY();
00170     }
00171     else
00172     {
00173         dragging = 0;
00174         camera_rotate = 0;
00175     }
00176 }
00177 
00178 //----------Nviz3DCanvas OnPaint Method----------------------------------------
00179 void Nviz3DCanvas::OnPaint( wxPaintEvent& event )
00180 {
00181     Render();
00182 }
00183 
00184 //----------Nviz3DCanvas OnSize Method-----------------------------------------
00185 void Nviz3DCanvas::OnSize(wxSizeEvent& event)
00186 {
00187     // this is also necessary to update the context on some platforms
00188     wxGLCanvas::OnSize(event);
00189 
00190     // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
00191     int w, h;
00192     GetClientSize(&w, &h);
00193 #ifndef __WXMOTIF__
00194     if (GetContext())
00195 #endif
00196     {
00197         SetCurrent();
00198         glViewport(0, 0, (GLint) w, (GLint) h);
00199     }
00200 }
00201 
00202 //----------Nviz3DCanvas Render Method-----------------------------------------
00203 void Nviz3DCanvas::Render()
00204 {
00205     // This is apperantly a dummy to avoid an endless succession of paint
00206     // messages. OnPaint handlers must always create a wxPaintDC.
00207     wxPaintDC dc(this);
00208 
00209 #ifndef __WXMOTIF__
00210     if (!GetContext()) return;
00211 #endif
00212 
00213     SetCurrent();
00214     // init OpenGL once, but after SetCurrent
00215     if (!m_init)
00216     {
00217         InitGL();
00218         m_init = TRUE;
00219     }
00220 
00221     // clear color and depth buffers
00222     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00223 
00224     glPushMatrix();
00225     glMatrixMode(GL_MODELVIEW);
00226     glTranslatef(camera_x, camera_y, camera_zoom);
00227     glMatrixMode(GL_PROJECTION);
00228     glRotatef(camera_rotate, 0.0, 1.0, 0.0);
00229     glMatrixMode(GL_MODELVIEW);
00230 
00231     DrawNeuron(gl_neuron_00 , -2.0, -4.0, 0.0);
00232     DrawNeuron(gl_neuron_01 , 2.0, -4.0, 0.0);
00233     DrawNeuron(gl_neuron_02 , -2.0, 0.0, 0.0);
00234     DrawNeuron(gl_neuron_03 , 2.0, .0, 0.0);
00235     DrawNeuron(gl_neuron_04 , -2.0, 4.0, 0.0);
00236     DrawNeuron(gl_neuron_05 , 2.0, 4.0, 0.0);
00237 
00238     glLineWidth(8);
00239     glBegin(GL_LINES);
00240        glVertex3f(-2.0, -4.0, 0.0);
00241        glVertex3f(-2.0, 0.0, 0.0);
00242     glEnd();
00243 
00244     glPopMatrix();
00245 
00246   glFlush();
00247   SwapBuffers();
00248 }
00249 
00250 //-----------------------------------------------------------------------------
00251 //  NVizFrame class Implementation
00252 //-----------------------------------------------------------------------------
00253 
00254 BEGIN_EVENT_TABLE(NvizFrame, wxFrame)
00255     EVT_MENU(wxID_EXIT, NvizFrame::OnExit)
00256     EVT_MENU(SETTINGS, NvizFrame::OnSettings)
00257 END_EVENT_TABLE()
00258 
00259 //----------NvizFrame Constructor----------------------------------------------
00260 NvizFrame::NvizFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
00261                      const wxSize& size, long style)
00262                      : wxFrame(frame, -1, title, pos, size, style)
00263 {
00264     glc_nvizcanvas = NULL;
00265 }
00266 
00267 //----------NvizFrame OnExit Method----------------------------------------------
00268 void NvizFrame::OnExit(wxCommandEvent &event)
00269 {
00270     Destroy();
00271 }
00272 
00273 void NvizFrame::OnSettings(wxCommandEvent &event)
00274 {
00275     wxDialog *dlg_settings = new wxDialog(this, -1, "Settings", wxPoint(100,100), wxSize(200,300));
00276     wxStaticText *stc_drag_scale_x;
00277     stc_drag_scale_x = new wxStaticText(dlg_settings, -1, "X Dragging Scale", wxPoint(10,10));
00278     wxStaticText *stc_drag_scale_y;
00279     stc_drag_scale_y = new wxStaticText(dlg_settings, -1, "Y Dragging Scale", wxPoint(10,30));
00280     txt_drag_scale_x = new wxTextCtrl(dlg_settings, drag_scale_x, "0.01", wxPoint(50, 10));
00281     txt_drag_scale_y = new wxTextCtrl(dlg_settings, drag_scale_y, "0.01", wxPoint(50, 30));
00282     dlg_settings->Show();
00283 }
00284 
00285 

Generated on Mon Jun 9 19:13:21 2003 for NeReK Documentation by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002