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

C:/Documents/projects/nerek/nviz-3d/nviz3d.cpp

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 // Name:        cube.cpp
00003 // Purpose:     wxGLCanvas demo program
00004 // Author:      Julian Smart
00005 // Modified by:
00006 // Created:     04/01/98
00007 // RCS-ID:      $Id: cube.cpp,v 1.9 2002/08/31 22:30:50 GD Exp $
00008 // Copyright:   (c) Julian Smart
00009 // Licence:     wxWindows licence
00010 /////////////////////////////////////////////////////////////////////////////
00011 
00012 #if defined(__GNUG__) && !defined(__APPLE__)
00013 #pragma implementation
00014 #pragma interface
00015 #endif
00016 
00017 // For compilers that support precompilation, includes "wx.h".
00018 #include "wx/wxprec.h"
00019 
00020 #ifdef __BORLANDC__
00021 #pragma hdrstop
00022 #endif
00023 
00024 #ifndef WX_PRECOMP
00025 #include "wx/wx.h"
00026 #endif
00027 
00028 #include "wx/log.h"
00029 
00030 #if !wxUSE_GLCANVAS
00031 #error Please set wxUSE_GLCANVAS to 1 in setup.h.
00032 #endif
00033 
00034 #include "nviz3d.h"
00035 
00036 static GLfloat xrot = 0;
00037 static GLfloat yrot = 0;
00038 static GLfloat zoom = 0;
00039 static float last_x = 0,
00040              last_y = 0;
00041 
00042 //-----------------------------------------------------------------------------
00043 //  NViz3DCanvas class Implementation
00044 //-----------------------------------------------------------------------------
00045 
00046 BEGIN_EVENT_TABLE(Nviz3DCanvas, wxGLCanvas)
00047     EVT_SIZE(Nviz3DCanvas::OnSize)
00048     EVT_PAINT(Nviz3DCanvas::OnPaint)
00049     EVT_ERASE_BACKGROUND(Nviz3DCanvas::OnEraseBackground)
00050     EVT_ENTER_WINDOW( Nviz3DCanvas::OnEnterWindow )
00051     EVT_MOUSE_EVENTS( Nviz3DCanvas::OnMouse )
00052 END_EVENT_TABLE()
00053 
00054 //----------Nviz3DCanvas Constructor-------------------------------------------
00055 Nviz3DCanvas::Nviz3DCanvas(wxWindow *parent, wxWindowID id,
00056                            const wxPoint& pos, const wxSize& size,
00057                            long style, const wxString& name):
00058              wxGLCanvas(parent, (wxGLCanvas*) NULL, id, pos, size, style, name )
00059 {
00060     m_init = FALSE;
00061 }
00062 
00063 //----------Nviz3DCanvas Destructor--------------------------------------------
00064 Nviz3DCanvas::~Nviz3DCanvas()
00065 {
00066     // Nothing Here
00067 }
00068 
00069 //----------Nviz3DCanvas Render Method-----------------------------------------
00070 void Nviz3DCanvas::Render()
00071 {
00072     // This is apperantly a dummy to avoid an endless succession of paint
00073     // messages. OnPaint handlers must always create a wxPaintDC.
00074     wxPaintDC dc(this);
00075 
00076 #ifndef __WXMOTIF__
00077     if (!GetContext()) return;
00078 #endif
00079 
00080     SetCurrent();
00081     // init OpenGL once, but after SetCurrent
00082     if (!m_init)
00083     {
00084         InitGL();
00085         m_init = TRUE;
00086     }
00087 
00088     // clear color and depth buffers
00089     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00090 
00091     glPushMatrix();
00092     glMatrixMode(GL_MODELVIEW);
00093     glRotatef(yrot, 0.0, 1.0, 0.0);
00094     glRotatef(xrot, 1.0, 0.0, 0.0);
00095     glMatrixMode(GL_PROJECTION);
00096     glTranslatef(0.0, 0.0, zoom);
00097 
00098     GLUquadricObj *gl_neuron = gluNewQuadric();
00099     gluSphere(gl_neuron, 1.0, 10, 10);
00100 
00101     glPopMatrix();
00102 
00103   glFlush();
00104   SwapBuffers();
00105 }
00106 
00107 //----------Nviz3DCanvas OnEnterWindow Method----------------------------------
00108 void Nviz3DCanvas::OnEnterWindow( wxMouseEvent& event )
00109 {
00110     SetFocus();
00111 }
00112 
00113 //----------Nviz3DCanvas OnPaint Method----------------------------------------
00114 void Nviz3DCanvas::OnPaint( wxPaintEvent& event )
00115 {
00116     Render();
00117 }
00118 
00119 //----------Nviz3DCanvas OnSize Method-----------------------------------------
00120 void Nviz3DCanvas::OnSize(wxSizeEvent& event)
00121 {
00122     // this is also necessary to update the context on some platforms
00123     wxGLCanvas::OnSize(event);
00124 
00125     // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
00126     int w, h;
00127     GetClientSize(&w, &h);
00128 #ifndef __WXMOTIF__
00129     if (GetContext())
00130 #endif
00131     {
00132         SetCurrent();
00133         glViewport(0, 0, (GLint) w, (GLint) h);
00134     }
00135 }
00136 
00137 //----------Nviz3DCanvas OnMouse Method----------------------------------------
00138 void Nviz3DCanvas::OnMouse(wxMouseEvent &event)
00139 {
00140 // I think all this dragging stuff is a way to waste a cycle to get a last_x or
00141 // y before drawing. Still not working but good enough for now.
00142     static int dragging = 0;
00143 
00144     if (event.LeftIsDown())
00145     {
00146         if (!dragging)
00147         {
00148             dragging = 1;
00149         }
00150         else
00151         {
00152             yrot += (event.GetX() - last_x)*1.0;
00153             xrot += (event.GetY() - last_y)*1.0;
00154             Refresh(FALSE);
00155         }
00156         last_x = event.GetX();
00157         last_y = event.GetY();
00158     }
00159 
00160 
00161     if (event.RightIsDown())
00162     {
00163         if (!dragging)
00164         {
00165             dragging = 0;
00166         }
00167         else
00168         {
00169             xrot = 0.0;  // If this is not done it will rotate when zooming
00170             yrot = 0.0;
00171             zoom  += (event.GetY() - last_y)*0.25;
00172             Refresh(FALSE);
00173         }
00174         last_y = event.GetY();
00175     }
00176 //    else
00177 //        dragging = 0;
00178 }
00179 
00180 void Nviz3DCanvas::DrawNeuron(GLfloat x, GLfloat y, GLfloat z,)
00181 {
00182 }
00183 
00184 //----------Nviz3DCanvas OnEraseBackGround Method------------------------------
00185 void Nviz3DCanvas::OnEraseBackground(wxEraseEvent& event)
00186 {
00187   // Do nothing, to avoid flashing.
00188 }
00189 
00190 //----------Nviz3DCanvas InitGL Method-----------------------------------------
00191 void Nviz3DCanvas::InitGL()
00192 {
00193     SetCurrent();
00194 
00195     // set viewing projection
00196     glMatrixMode(GL_PROJECTION);
00197     glFrustum(-1.0F, 1.0F, -1.0F, 1.0F, 1.0F, 100.0F);
00198 
00199     // position viewer
00200     glMatrixMode(GL_MODELVIEW);
00201     glLoadIdentity();
00202     glTranslated(0.0, 0.0, -4.0);
00203 
00204     glEnable(GL_DEPTH_TEST);
00205     glEnable(GL_LIGHTING);
00206     glEnable(GL_LIGHT0);
00207 }
00208 
00209 //-----------------------------------------------------------------------------
00210 //  NVizFrame class Implementation
00211 //-----------------------------------------------------------------------------
00212 
00213 BEGIN_EVENT_TABLE(NvizFrame, wxFrame)
00214     EVT_MENU(wxID_EXIT, NvizFrame::OnExit)
00215 END_EVENT_TABLE()
00216 
00217 //----------NvizFrame Constructor----------------------------------------------
00218 NvizFrame::NvizFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
00219                      const wxSize& size, long style)
00220                      : wxFrame(frame, -1, title, pos, size, style)
00221 {
00222     glc_nvizcanvas = NULL;
00223 }
00224 
00225 //----------NvizFrame OnExit Method----------------------------------------------
00226 void NvizFrame::OnExit(wxCommandEvent &event)
00227 {
00228     Destroy();
00229 }
00230 
00231 //-----------------------------------------------------------------------------
00232 //  NViz3DApp class Implementation
00233 //-----------------------------------------------------------------------------
00234 
00235 IMPLEMENT_APP(Nviz3DApp)
00236 
00237 //----------Nviz3DApp OnInit Main Method----------------------------------------------
00238 bool Nviz3DApp::OnInit(void)
00239 {
00240   wxLog::SetTraceMask(wxTraceMessages);
00241 
00242   // Create the main frame window
00243   NvizFrame *frame = new NvizFrame(NULL, "Cube OpenGL Demo", wxPoint(50, 50),
00244                                wxSize(400, 300));
00245   // Give it an icon
00246 #ifdef wx_msw
00247   frame->SetIcon(wxIcon("mondrian"));
00248 #endif
00249 
00250   // Make a menubar
00251   wxMenu *winMenu = new wxMenu;
00252 
00253   winMenu->Append(wxID_EXIT, "&Close");
00254   wxMenuBar *menuBar = new wxMenuBar;
00255   menuBar->Append(winMenu, "&Window");
00256 
00257   frame->SetMenuBar(menuBar);
00258 
00259   frame->glc_nvizcanvas = new Nviz3DCanvas(frame, -1, wxDefaultPosition, wxDefaultSize);
00260 
00261   // Show the frame
00262   frame->Show(TRUE);
00263 
00264   return TRUE;
00265 }

Generated on Fri Jun 6 22:02:11 2003 for NeReK by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002