Language:
en ru

math lib examples

Fist part of library provide next classes for vectors etc...:

XY - Two dimentional vector
XYZ - Three dimentional vector
XYZ_w - Four dimentional vector
MATRIX3x3 - matrix 3x3 class
MATRIX4x4 - matrix 4x4 class
quaternion - quaternion class
Source code:
#include "vmmath.h"
using namespace mvct;//math classes;
int main(){
  XYZ a(1,2,0),b(3,2,1);
  double m(2);
  std::cout<<(a+b)<<'\n' // vector summ
      <<(a-b)<<'\n' // vector 
      <<(a*b)<<'\n' // scalar product 
      <<(a^b)<<'\n' // cross product
      <<(m*b)<<'\n' // myltiply vector b by m
      <<(a*m)<<'\n' // myltiply vector a by m 
      <<(b/m)<<'\n' // divide vector b by m 
      <      <  return 0;
}

program outputs:


4.0000000000000000e+00	4.0000000000000000e+00	1.0000000000000000e+00
-2.0000000000000000e+00	0.0000000000000000e+00	-1.0000000000000000e+00
7.0000000000000000e+00
2.0000000000000000e+00	-1.0000000000000000e+00	-4.0000000000000000e+00
6.0000000000000000e+00	4.0000000000000000e+00	2.0000000000000000e+00
2.0000000000000000e+00	4.0000000000000000e+00	0.0000000000000000e+00
1.5000000000000000e+00	1.0000000000000000e+00	5.0000000000000000e-01
2.2360679774997898e+00
fltk-1.x test program

 When using library with fltk we need to make derived from FlGlArcballWindow class and redefine void draw() virtual function. In this example we have window with axes (drawAxis()) and coordinate orts at right top corner draw3DOrbit(). We need to call at first reshape() for setting Viewport and projections and than arcball_transform() to apply transformation matrix.

We can use mouse for transformations:

  • left button - rotation
  • rigth - zomming
  • middle - translation
fltk-1
Source code:
#include "FlGlArcballWindow.h"
//make derived class
class testarc : public FlGlArcballWindow{
  public:
    testarc(int W,int H,const char*L=0):FlGlArcballWindow(W,H,L){}
    //rewrite draw method
    virtual void draw(){
      reshape ();
      valid (1);
      glClearColor (0.25,0.5,0.5, 1.0);
      glDepthFunc (GL_LESS);
      glEnable (GL_DEPTH_TEST);
      static GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};
      static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
      glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
      glLightfv(GL_LIGHT0, GL_POSITION, light_position);
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glPushMatrix();
      arcball_transform();//apply transformation
      drawAxis();
      draw3DOrbit();
      glPopMatrix();
      glFinish();
    }
};
int main(int argc,char** argv){
  testarc arc(320,240,"MA test fltk-1");
  arc.resizable(arc);
  arc.show(argc,argv);
  return Fl::run();
}
gtkmm test program
gtkmm
Source code:
#include "gtkmm_arcball.h"
#include "vmmath.h"
class TestArc : public Gtk::ArcballWidget{
  public:
  TestArc(const Glib::RefPtr& config):Gtk::ArcballWidget(config){};
  bool on_expose_event(GdkEventExpose* event){
  Glib::RefPtr glwindow = get_gl_window();
  // GL calls.
  // *** OpenGL BEGIN ***
  if (!glwindow->gl_begin(get_gl_context())) return false;
      glClearColor(0.25,0.5,0.5, 1.0);
      glDepthFunc(GL_LESS);
      glEnable(GL_DEPTH_TEST);
      static GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};
      static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
      glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
      glLightfv(GL_LIGHT0, GL_POSITION, light_position);
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glPushMatrix();
      arcball_transform();
      drawAxis();
      Gdk::GL::Drawable::draw_teapot(true,.5);
      draw3DOrbit();
      glPopMatrix();
  // Swap buffers.
  if (glwindow->is_double_buffered()) glwindow->swap_buffers();
  else glFlush();
  glwindow->gl_end();
  // *** OpenGL END ***
  return true;
}
};
int main(int argc,char** argv){
  Gtk::Main m(&argc,&argv);
  Gtk::GL::init(argc, argv);
  Glib::RefPtr glconfig;
  // Try double-buffered visual
  glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB    |
                                     Gdk::GL::MODE_DEPTH  |
                                     Gdk::GL::MODE_DOUBLE);
  if (!glconfig) {
      std::cerr << "*** Cannot find the double-buffered visual.\n"
                << "*** Trying single-buffered visual.\n";
      // Try single-buffered visual
      glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB   |
                                         Gdk::GL::MODE_DEPTH);
      if (!glconfig) {
          std::cerr << "*** Cannot find any OpenGL-capable visual.\n";
          return 0;
      }
  }
  Gtk::Window wnd;
  Gtk::VBox bx;
  wnd.set_title("MA test GTKMM");
  TestArc arc(glconfig);
  wnd.add(bx);
  bx.add(arc);
  wnd.show_all_children();
  m.run(wnd);
  return 0;
}

© Yury Fedorchenko

Рейтинг@Mail.ru be number one Rambler's Top100