fileMotionFilter.cpp
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
*  MotionFilter.cpp
*
*  Created by Daisuke Nogami on Nov, 05.
*  opyright 2005 Daisuke Nogami [null-null.net]. Some rights reserved.
*/
 
#include "stdafx.h"
#include "MotionFilter.h"
 
 
#include <GL/glut.h>
 
#define HIT_RANGE 15
 
 
// x[0-1], y[0-1]
void MotionFilter::getRgbAt( float x, float y, unsigned char* rgb ){
  // normalize
  x = x < 0 ? 0 : x;
  x = x > 1 ? 1 : x;
  y = y < 0 ? 0 : y;
  y = y > 1 ? 1 : y;
  int ix = (int)(((float)_cWidth-1)*x);
  int iy = (int)(((float)_cHeight-1)*y);
  int  rowSize = _cWidth * 3;
  rgb[0] = btmp[(ix*3)+iy*rowSize];
  rgb[1] = btmp[(ix*3)+iy*rowSize+1];
  rgb[2] = btmp[(ix*3)+iy*rowSize+2];
}
 
// draw motion shadow
void MotionFilter::draw(){
  glBegin( GL_POLYGON );
  glTexCoord2f( 0.0f, 0.0f );
  glVertex2f( 0, 768 );
  glTexCoord2f( 1.0f, 0.0f );
  glVertex2f( 1024, 768 );
  glTexCoord2f( 1.0f, 1.0f );
  glVertex2f( 1024, 0 );
  glTexCoord2f( 0.0f, 1.0f );
  glVertex2f( 0,0 );
  glEnd();
}
 
// attach raindrop
void MotionFilter::attach( CellSet* cellset, RainFilter* rfilter ){
  int x, y, i, idx;
  float lenx, leny, len_a, len_b;
  float flow;
  unsigned char rgb[3];
  Cell *tcell_a, *tcell_b;
  RainDrop* rd;
 
  for( i = 0; i < rfilter->raindrops.size(); i++ ){
 
    rd = &rfilter->raindrops[i];
 
    getRgbAt( rd->xpos/1024, 1-rd->ypos/768, rgb );
    flow = 1-(float)rgb[2]/255;
 
    // hit test
    if( flow > 0.04 ){
 
      for( y = 0; y < cellset->hdiv; y++ ){
        for( x = 0; x < cellset->wdiv; x++ ){
 
          idx = x+y*cellset->wdiv;
          tcell_a = &cellset->cells_a[idx];
          tcell_b = &cellset->cells_b[idx];
 
          // cell A
          lenx = (tcell_a->xpos + cellset->xpos) - rd->xpos;
          leny = (tcell_a->ypos + cellset->ypos) - rd->ypos;
          len_a = sqrt( lenx*lenx+leny*leny );
 
          // cell B
          lenx = (tcell_b->xpos + cellset->xpos) - rd->xpos;
          leny = (tcell_b->ypos + cellset->ypos) - rd->ypos;
          len_b = sqrt( lenx*lenx+leny*leny );
 
          // hit test [ between raindrop and cell ]
          if( len_a < HIT_RANGE || len_b < HIT_RANGE ){
            tcell_a->flow = flow;
            tcell_b->flow = flow;
          }
        }
      }
 
      // clear raindrop
      rd->init();
    }
  }
}
 
// attach airstream
void MotionFilter::attach( CellSet* cellset, AirstreamFilter* asfilter ){
 
  // tailwind & headwind
  int i;
  float mpowx, mpowy;
  nlRectangle* mf;
  for( i = 0; i < mfv.size(); i++ ){
    mf = &mfv[i];
    mpowx += mf->vx;
    mpowy += mf->vy;
  }
  asfilter->as_src.relative_velx = mpowx*7;
  asfilter->as_src.relative_vely = mpowy*3;
 
  // attach by motion shadow
  unsigned char rgb_a[3], rgb_b[3];
  float m_a, m_b;
  int x, y, idx;
  Cell *tcell_a, *tcell_b;
 
  for( y = 0; y < cellset->hdiv; y++ ){
    for( x = 0; x < cellset->wdiv; x++ ){
 
      idx = x+y*cellset->wdiv;
      tcell_a = &cellset->cells_a[idx];
      tcell_b = &cellset->cells_b[idx];
 
      // cell A
      getRgbAt(
        (tcell_a->xpos + cellset->xpos)/1024,
        1-(tcell_a->ypos + cellset->ypos)/768,
        rgb_a
      );
      m_a = 1-(float)rgb_a[2]/255;
      if( m_a > 0.1 ){
        tcell_a->nextpow = m_a*2+2;
      }
      else{
        tcell_a->nextpow = 1;
      }
 
      // cell B
      getRgbAt(
        (tcell_b->xpos + cellset->xpos)/1024,
        1-(tcell_b->ypos + cellset->ypos)/768,
        rgb_b
      );
      m_b = 1-(float)rgb_b[2]/255;
      if( m_b > 0.1 ){
        tcell_b->nextpow = m_b*2+2;
      }
      else{
        tcell_b->nextpow = 1;
      }
    }
  }
}
[トップ] [一覧] [最終更新] [検索] [バックアップ]