This entry was posted on 金曜日, 6月 27th, 2008 at 12:54:24 and is filed under Scandinavia. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


まずは玉

なかなかコードを書く時間が取れなくて
電車の中とか昼休みとか毎日5分づつくらい
少しずつ書いています
スマートフォンで動くsubversionが欲しいぜ
前回はここからobjファイルの読み込みやら
テクスチャマッピングやら余計な方に
行ってしまいゴチャゴチャしてきたので
今回はそれを反省して
大量オブジェクトの高速レンダリング
を目標で
パフォーマンスを発揮できる方に
シェーディングもblinnでいいや
まずは10000個の球のレンダリング
画面には収まりきらなさそうだけど
画面外のオブジェクトを排除するような仕組み
を入れていなかったのでそれも入れつつ
空間分割の実装が甘かったのでそちらをメインで
一応kd-treeまでは作ったけど
まともに動いてなかったし
100万球くらいはやりたいです
以下は一応メイン関数のソース
#include <stdio.h>
#include "camera.h"
#include "light.h"
#include "object.h"
#include "shading.h"
#include "SMIL.h"
#include "bmpClass.h"
#include "camera.h"
#include "light.h"
#include "object.h"
#include "shading.h"
#include "SMIL.h"
#include "bmpClass.h"
int main(int argc, char **argv){
// Initialize
// Screen
int width = 320;
int height = 240;
int bitDepth = 3;
// camera
camera cam;
cam.pos = VECT(0, 0, -3);
cam.aimPos = VECT(0, 0, 0);
cam.picAngle = 30.0;
cam.bgColor = COL3(0.5, 0.5, 0.5);
cam.initAxisConv(width);
// light
pointLight plit;
plit.intencity = 1.0;
plit.pos = VECT(-1.0,1.0,-1.5);
// shader
blinn blnSHD(COL3(1.0, 0.50, 0.0));// color
blnSHD.power = 2;
// sphere
sphere sph = sphere(VECT(0.0, 0.0, 0.0), 1, &blnSHD);// center , radius, shader
// image buffer
image img(width*height*bitDepth);
// Render Loop
int w, h;
for(h=0;h<height;h++){
for(w=0;w<width;w++){
// create vector
VECT screenPos = cam.AxisConv(VECT(w-width/2, height/2 - h, 0));
cam.unit = SMILunit(cam.pos, screenPos);
int idx=(h*(width)+w)*3;
// hit judge
VECT hitPos;
int isHit = sph.hitJudge(cam.pos, cam.unit, hitPos);
if(isHit){
// Point light
VECT faceNormal = sph.getHitNormal(hitPos);
plit.setLightDir(hitPos);
COL3 col = sph.shd->shading(faceNormal, &plit);
img.setColor(idx, col);
}else{
img.setColor(idx, cam.bgColor);
}
}
}
// Output Image
bmpLib bl;
bl.bmpExport("output.bmp",width, height, bitDepth, img.c_buf);
return 0;
}
なるべくシンプルにしようとしていますが
boostを使おうと思ったけどまだ使っていない
SMIL.hは自前の数学関数ライブラリで
ベクトル計算(内積、外積etc)や乱数生成などをしています
一応の高速化はしてあるのですが
最も高速に演算処理を行えるExpression Templateという実装はしてないのです。
これを実装しているboost::mathに徐々に置き換えて行こう
forでまわしているのもboost::threadベースに変えていきたい
One Response to “まずは玉”
Leave a Reply

6月 27th, 2008 at 20:05:14
いいねぇ。既存APIに頼らず自分で画を出力できたときは楽しんだろうなぁ。
ついでに、レポジトリの公開を期待してみたり。
ちなみに、構造的な最適化を図る場合は、かならずプロファイルを取ってボトルネックを見つけてからの方がいいよ。C++でスレッドやテンプレートを導入・管理するのは大変で確実に複雑・難読化するけど、必ずしも性能アップに繋がるとは限らないから。