スキャンラインのソースを発見

ousttrue2009-02-18

http://www.lysator.liu.se/~mikaelk/doc/perspectivetexture/
一番下にあるサンプルソースの線形補間のものを動かしてみた。
やってみると簡単ではあるのだがそのままではコンパイルできないので
ちょっと困った。
どうせならそのままで動くものか、もう少しコメント書くなりしてくれれば
よいものをw


補ったソース。

// 先頭に

#include <vector>
#include <fstream>
#include <iostream>
static char *screen;

//////////////////////////////////////////
// 最後に
void loadTexture(char *texture)
{
  std::ifstream io("check16x16.pgm", std::ios::binary);
  std::string line;

  for(int i=0; i<3; ){
    std::getline(io, line);
    if(line[0]!='#'){
      std::cout << line << std::endl;
      ++i;
    }
  }

  io.read(texture, 256 * 256);

  std::cout << "load texture" << std::endl;
}

int main(int argc, char **argv)
{
  // screen 320 x 320
  std::vector<char> Screen(320 * 320, 0x00);
  screen=&Screen[0];

  // texture 256 x 256
  std::vector<char> Texture(256 * 256, 0x00);
  loadTexture(&Texture[0]);

  TPolytri poly={
    20, 20, 30,
    20, 280, 30,
    280, 280, 30,
    0, 0,
    0, 256,
    256, 256,
    &Texture[0]
  };
  drawtpolysubtri(&poly);

  std::ofstream io("tmp.ppm", std::ios::binary);
  io << "P5\n320 320\n255\n";
  io.write(&Screen[0], Screen.size());
}

これにgimpで作った
256x256のグレイスケールのテクスチャ(check16x16.pgm)を読ませてみた。


コードはちゃんと読んでないが、
ブレゼンハムアルゴリズムの縦横判定のような準備コードが半分くらいを
占めていて本体が
drawtpolysubtriseg
関数らしい。
他にパース補正のサンプルもあるのでぼちぼち解読予定。