コード例 #1
0
ファイル: clip2.cpp プロジェクト: nomovok-opensource/wrath
ClipExample::
ClipExample(cmd_line_type *cmd_line):
  DemoKernel(cmd_line),
  m_shader_hoard(WRATHGLShader::shader_source()
                 .add_source("item.vert.glsl", WRATHGLShader::from_resource),
                 WRATHGLShader::shader_source()
                 .add_source("item.frag.glsl", WRATHGLShader::from_resource)),
  m_first_frame(true)
{
  /*
    Create the WRATHTripleBufferEnabler object
    which our visual items will use to sync
   */
  m_tr=WRATHNew WRATHTripleBufferEnabler();

  /*
    Create the WRATHLayer which will contain and
    draw our shape
   */
  m_layer=WRATHNew WRATHLayer(m_tr);

  /*
    these are the transforms that will be
    applied to all elements contained in the
    WRATHLayer:

    + a 3D transform
    + a Projection transform

    For the purpose of this example the 3D transform
    will be the identity matrix. In other words no
    transform will be applied to the vertex data.

    Projection will be orthographic
    */

  float_orthogonal_projection_params proj_params(0, width(),
                                                 height(), 0);

  m_layer->simulation_matrix(WRATHLayer::projection_matrix, float4x4(proj_params));

  /*
    now we make a child layer of m_layer 
   */
  m_child_layer=WRATHNew WRATHLayer(m_layer);
  
  const int num_sides_on_clip=cmd_line->m_num_sides_on_clip_widgets.m_value;
  const int num_clip_widgets=cmd_line->m_num_clip_widgets.m_value;
  const int num_widgets=cmd_line->m_num_widgets.m_value;

  /*
    create our clip widgets, we will set their z_order values
    so that:
     - m_ring_widget is in order of ctor 
     - m_clip_out_widget of each is above all m_ring_widget
       AND below all elements in m_widgets
     - m_clip_in_widget is above all m_clip_out_widget 
    
    So what happens is that when 2 clipwidget intesect
    their rings are not drawn over the clip inside region
    of either one.
  */
  for(int i=0; i<num_clip_widgets; ++i)
    {
      ClipWidget *N;
      vec2 pos;
      float r1, r2;

      r1=static_cast<float>(rand())/static_cast<float>(RAND_MAX);
      r2=static_cast<float>(rand())/static_cast<float>(RAND_MAX);
      
      pos=vec2( r1*width(), r2*height());

      N=WRATHNew ClipWidget(pos,
                            num_sides_on_clip,
                            m_shader_hoard,
                            m_layer, m_child_layer,
                            vec4(0.0f, 0.0f, 0.0f, 1.0f));

      N->m_ring_widget->z_order(-i);
      N->m_clip_out_widget->z_order(-i-num_clip_widgets);
      N->m_clip_in_widget->z_order(-i-2*num_clip_widgets);

      m_clip_widgets.push_back(N);
    }
  

  load_images();

  //make a background widget for m_layer:
  m_background_widget=make_image_widget(m_layer, m_images[0]);
  m_background_widget->z_order(1); //below everything in m_layer.

  //make a background widget for m_child_layer
  m_background_widget2=make_image_widget(m_child_layer, m_images[1]);
  m_background_widget2->z_order(-3*num_clip_widgets); //below everything in m_child_layer.


  for(int i=0, zi=-3*num_clip_widgets-1; i<num_widgets; ++i, --zi)
    {
      ImageWidget *widget;
      WRATHImage *img;
      int num_sides;

      img=m_images[rand()%m_images.size()];
      num_sides=rand()%5 + 3;
      widget=make_widget<ImageWidget>(m_child_layer, num_sides, 
                                      m_shader_hoard,
                                      WRATHDrawType::opaque_pass(),
                                      img);

      widget->translation(vec2(rand()%width(), rand()%height()) );
      widget->rotation( static_cast<float>(rand())/RAND_MAX*M_PI);

      widget->m_velocity=vec2( (float)rand()/RAND_MAX*190.0f - 95.0f, 
                               (float)rand()/RAND_MAX*190.0f - 95.0f);

      widget->m_inner_radius=rand()%100;
      widget->m_outer_radius=100 + rand()%100;

      //make the z-order of widget decrease from -1,
      //thus layer widgets are drawn on top.
      widget->z_order(zi);
      m_widgets.push_back(widget);
    }
                                                     
  
                  
  glClearColor(1.0, 1.0, 1.0, 1.0);
}