Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

vuPreviewWin.cpp

Go to the documentation of this file.
00001 #include <iostream.h>
00002 #include "vuArcBall.h"
00003 #include "vuPreviewWin.h"
00004 
00005 //----------------------------------------------------------------------------
00006 //------------------------- vuPrewviewWin implementation ---------------------
00007 //----------------------------------------------------------------------------
00008 
00009 BEGIN_EVENT_TABLE(vuPreviewWin, vuGLCanvas)
00010   EVT_MOUSE_EVENTS(vuPreviewWin::glOnMouse)
00011 END_EVENT_TABLE()
00012 
00013 vuPreviewWin::vuPreviewWin(vuBasicUtility *parent, int size_x, int size_y)
00014   : vuGLCanvas(parent,-1,wxPoint(0,0),wxSize(size_x,size_y)), m_Parent(parent) 
00015 {
00016   m_Camera = NULL;
00017 }
00018 
00019 vuPreviewWin::~vuPreviewWin()
00020 {
00021 }
00022 
00023 void vuPreviewWin::attachCamera(vuCamera* cam)
00024 {
00025   m_Camera = cam;
00026 }
00027 
00028 void vuPreviewWin::setCubeSize(int sx, int sy, int sz)
00029 {
00030   m_MX = sx;
00031   m_MY = sy;
00032   m_MZ = sz;
00033   center = vuVector(sx/2,sy/2,sz/2);
00034 }
00035 
00036 void vuPreviewWin::render()
00037 {
00038   glClear(GL_COLOR_BUFFER_BIT);
00039 
00040   glDisable(GL_CULL_FACE);
00041   glEnable(GL_DEPTH_TEST);
00042   glDepthFunc(GL_LESS);
00043   //    glCullFace(GL_FRONT);                   //what a shit
00044   //glCullFace(GL_BACK);                        //what a shit
00045   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
00046   //glClear(GL_COLOR_BUFFER_BIT);
00047         
00048   //Set the opengl projection matrix.
00049   glMatrixMode(GL_PROJECTION);
00050   glLoadIdentity();
00051   m_Camera->glInit();
00052   //glOrtho(-m_Data->m_Camera.Width()/2,m_Data->m_Camera.Width()/2, 
00053   //    -m_Data->m_Camera.Height()/2, m_Data->m_Camera.Height()/2, 
00054   //    10000.0f, -10000.0f);
00055   glMatrixMode(GL_MODELVIEW);
00056   glLoadIdentity();
00057   m_Camera->gluLookAt();
00058   //vuMatrix cam = m_Data->m_Camera.getViewMat();
00059   //glTranslatef(m_Data->center[0],m_Data->center[1],m_Data->center[2]);
00060   //glMultMatrixf(cam.getData());
00061   //glTranslatef(-m_Data->center[0],-m_Data->center[1],-m_Data->center[2]);
00062   //draw an axis aligned box
00063   drawRepresentation(m_MX, m_MY, m_MZ);
00064 }
00065 
00066 bool vuPreviewWin::glInit()
00067 {
00068   return true;
00069 }
00070 
00071 //----------------------------------------------------------------------------
00072 //------------------------- protected: glOnMouse() ---------------------------
00073 //----------------------------------------------------------------------------
00074 
00075 void vuPreviewWin::glOnMouse(wxMouseEvent &ev)
00076 {
00077     if (ev.LeftDown() || ev.RightDown())
00078     {
00079         //Store the click position.
00080         m_x = (int) ev.GetX();
00081         m_y = (int) ev.GetY();
00082     }
00083     else if (ev.LeftIsDown() && ev.Moving())
00084     {
00085         //Rotate the volume
00086         vuVector t = m_Camera->getPosition()-center;
00087         float d = t.norm();
00088         m_Camera->translateXYZ(0.0f, 0.0f, d);
00089         t = m_Camera->getPosition();
00090         //use the arc ball
00091         vuArcBall ball;
00092         ball.attachCamera(*m_Camera);
00093         int winx,winy;
00094         GetSize(&winx,&winy);
00095         ball.setWinSize(winx,winy);
00096         ball.turn(m_x, m_y, ev.GetX(), ev.GetY());
00097         m_Camera->translateXYZ(0.0f, 0.0f, -d);
00098         m_Camera->init();
00099         redraw();
00100 
00101         //Store the click position.
00102         m_x = (int) ev.GetX();
00103         m_y = (int) ev.GetY();
00104     }
00105     else if (ev.RightIsDown() && ev.Moving())
00106     {
00107         //distance from the volume.
00108         float s = ((float)ev.GetY() - m_y)/1.0f;
00109         vuVector t = m_Camera->getPosition()-center;
00110         //float d = t.norm();
00111         m_Camera->translateXYZ(0.0f, 0.0f, -s);
00112         resize();
00113         redraw();
00114 
00115         //Store the click position.
00116         m_x = (int) ev.GetX();
00117         m_y = (int) ev.GetY();
00118     }
00119 }
00120 
00121 
00122 void vuPreviewWin::drawRepresentation(float sx, float sy, float sz)
00123 {
00124   static GLfloat n[6][3] = {  /* Normals for the 6 faces of a cube. */
00125     {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
00126     {0.0, -1.0, 0.0}, {0.0, 0.0, -1.0}, {0.0, 0.0, 1.0} };
00127   static GLint faces[6][4] = {  /* Vertex indices for the 6 faces of a cube. */
00128     {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
00129     {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
00130   static GLfloat v[8][3];  /* Will be filled in with X,Y,Z vertexes. */
00131   
00132   /* Setup cube vertex data. */
00133   v[0][0] = v[1][0] = v[2][0] = v[3][0] = 0;
00134   v[4][0] = v[5][0] = v[6][0] = v[7][0] = sx;
00135   v[0][1] = v[1][1] = v[4][1] = v[5][1] = 0;
00136   v[2][1] = v[3][1] = v[6][1] = v[7][1] = sy;
00137   v[0][2] = v[3][2] = v[4][2] = v[7][2] = sz;
00138   v[1][2] = v[2][2] = v[5][2] = v[6][2] = 0;
00139   
00140   int i;
00141   for (i = 0; i < 6; i++) {
00142     glBegin(GL_QUADS);
00143     glNormal3fv(&n[i][0]);
00144     glColor3fv(v[faces[i][0]]);
00145     glVertex3fv(&v[faces[i][0]][0]);
00146     glColor3fv(v[faces[i][1]]);
00147     glVertex3fv(&v[faces[i][1]][0]);
00148     glColor3fv(v[faces[i][2]]);
00149     glVertex3fv(&v[faces[i][2]][0]);
00150     glColor3fv(v[faces[i][3]]);
00151     glVertex3fv(&v[faces[i][3]][0]);
00152     glEnd();
00153   }
00154 }

Generated on Wed Dec 15 21:20:35 2004 for vuVolume by  doxygen 1.3.9.1