Rendering学習日記

日々3DCGの世界は進歩しているけど、勉強して理解したことをまとめていきたい。RenderMan互換レンダラーやグローバル・イルミネーション。いろんなことに好奇心を持ち面白くなってくる。目指せShader書き!!

MayaからRIB出力した基本図形

Gelatoでは、デフォルトで持っている基本図形は球体だけ。後はInputフォルダにあるティーポットなどのアーカイブのみ。
primitive.jpg
ということで、MayaのデフォルトのNurbsサーフェイスをrib exportしてみた。まず、注意しなければならないのは、そのままではつかえないこと。
WorldBegin〜EndやObjectBeginステートメント、Perspectiveなどはすべて削除。オブジェクトのみ。Nupatchだけにした。当然、原点で作成している。たとえば、円錐Coneは、次のようになる。
ファイル名はcone.ribとした。なお、gelatoでribを読み込むには
RMRからlibをダウンロードする必要がある。
NuPatch 11 4 [-2 -2 -1
0 1 2 3 4 5 6 7 8 9 10 10] 0 8 4 4 [0 0 0 0 2.23607 2.23607 2.23607 2.23607] 0 2.23607 "Pw" [0.783612 -1 -0.783612 1 1.10819 -1 6.51994e-017 1
0.783612 -1 0.783612 1 3.21127e-016 -1 1.10819 1 -0.783612 -1 0.783612 1
-1.10819 -1 2.72688e-016 1 -0.783612 -1 -0.783612 1 -5.95213e-016 -1 -1.10819 1
0.783612 -1 -0.783612 1 1.10819 -1 6.51994e-017 1 0.783612 -1 0.783612 1
0.522408 -0.333333 -0.522408 1 0.738796 -0.333333 6.3877e-017 1 0.522408 -0.333333 0.522408 1
2.14085e-016 -0.333333 0.738796 1 -0.522408 -0.333333 0.522408 1 -0.738796 -0.333333 2.02203e-016 1
-0.522408 -0.333333 -0.522408 1 -3.96809e-016 -0.333333 -0.738796 1 0.522408 -0.333333 -0.522408 1
0.738796 -0.333333 6.3877e-017 1 0.522408 -0.333333 0.522408 1 0.261204 0.333333 -0.261204 1
0.369398 0.333333 6.25547e-017 1 0.261204 0.333333 0.261204 1 1.07042e-016 0.333333 0.369398 1
-0.261204 0.333333 0.261204 1 -0.369398 0.333333 1.31718e-016 1 -0.261204 0.333333 -0.261204 1
-1.98404e-016 0.333333 -0.369398 1 0.261204 0.333333 -0.261204 1 0.369398 0.333333 6.25547e-017 1
0.261204 0.333333 0.261204 1 0 1 6.12323e-017 1 0 1 6.12323e-017 1
0 1 6.12323e-017 1 0 1 6.12323e-017 1 0 1 6.12323e-017 1
0 1 6.12323e-017 1 0 1 6.12323e-017 1 0 1 6.12323e-017 1
0 1 6.12323e-017 1 0 1 6.12323e-017 1 0 1 6.12323e-017 1]

NuPatch 11 4 [-2 -2 -1
0 1 2 3 4 5 6 7 8 9 10 10] 0 8 4 4 [0 0 0 0 2.23607 2.23607 2.23607 2.23607] 0 2.23607 "Pw" [0.783612 -1 -0.783612 1 1.10819 -1 6.51994e-017 1
0.783612 -1 0.783612 1 3.21127e-016 -1 1.10819 1 -0.783612 -1 0.783612 1
-1.10819 -1 2.72688e-016 1 -0.783612 -1 -0.783612 1 -5.95213e-016 -1 -1.10819 1
0.783612 -1 -0.783612 1 1.10819 -1 6.51994e-017 1 0.783612 -1 0.783612 1
0.522408 -0.333333 -0.522408 1 0.738796 -0.333333 6.3877e-017 1 0.522408 -0.333333 0.522408 1
2.14085e-016 -0.333333 0.738796 1 -0.522408 -0.333333 0.522408 1 -0.738796 -0.333333 2.02203e-016 1
-0.522408 -0.333333 -0.522408 1 -3.96809e-016 -0.333333 -0.738796 1 0.522408 -0.333333 -0.522408 1
0.738796 -0.333333 6.3877e-017 1 0.522408 -0.333333 0.522408 1 0.261204 0.333333 -0.261204 1
0.369398 0.333333 6.25547e-017 1 0.261204 0.333333 0.261204 1 1.07042e-016 0.333333 0.369398 1
-0.261204 0.333333 0.261204 1 -0.369398 0.333333 1.31718e-016 1 -0.261204 0.333333 -0.261204 1
-1.98404e-016 0.333333 -0.369398 1 0.261204 0.333333 -0.261204 1 0.369398 0.333333 6.25547e-017 1
0.261204 0.333333 0.261204 1 0 1 6.12323e-017 1 0 1 6.12323e-017 1
0 1 6.12323e-017 1 0 1 6.12323e-017 1 0 1 6.12323e-017 1
0 1 6.12323e-017 1 0 1 6.12323e-017 1 0 1 6.12323e-017 1
0 1 6.12323e-017 1 0 1 6.12323e-017 1 0 1 6.12323e-017 1]

NuPatch 11 4 [-2 -2 -1
0 1 2 3 4 5 6 7 8 9 10 10] 0 8 4 4 [0 0 0 0 1 1 1 1] 0 1 "Pw" [0 -1 0 1 0 -1 0 1
0 -1 0 1 0 -1 0 1 0 -1 0 1
0 -1 0 1 0 -1 0 1 0 -1 0 1
0 -1 0 1 0 -1 0 1 0 -1 0 1
0.261204 -1 -0.261204 1 0.369398 -1 -9.12038e-018 1 0.261204 -1 0.261204 1
1.58307e-016 -1 0.369398 1 -0.261204 -1 0.261204 1 -0.369398 -1 1.62571e-016 1
-0.261204 -1 -0.261204 1 -2.49669e-016 -1 -0.369398 1 0.261204 -1 -0.261204 1
0.369398 -1 -9.12038e-018 1 0.261204 -1 0.261204 1 0.522408 -1 -0.522408 1
0.738796 -1 -1.82408e-017 1 0.522408 -1 0.522408 1 3.16613e-016 -1 0.738796 1
-0.522408 -1 0.522408 1 -0.738796 -1 3.25142e-016 1 -0.522408 -1 -0.522408 1
-4.99337e-016 -1 -0.738796 1 0.522408 -1 -0.522408 1 0.738796 -1 -1.82408e-017 1
0.522408 -1 0.522408 1 0.783612 -1 -0.783612 1 1.10819 -1 -2.73611e-017 1
0.783612 -1 0.783612 1 4.7492e-016 -1 1.10819 1 -0.783612 -1 0.783612 1
-1.10819 -1 4.87713e-016 1 -0.783612 -1 -0.783612 1 -7.49006e-016 -1 -1.10819 1
0.783612 -1 -0.783612 1 1.10819 -1 -2.73611e-017 1 0.783612 -1 0.783612 1]

NuPatch 11 4 [-2 -2 -1
0 1 2 3 4 5 6 7 8 9 10 10] 0 8 4 4 [0 0 0 0 1 1 1 1] 0 1 "Pw" [0 -1 0 1 0 -1 0 1
0 -1 0 1 0 -1 0 1 0 -1 0 1
0 -1 0 1 0 -1 0 1 0 -1 0 1
0 -1 0 1 0 -1 0 1 0 -1 0 1
0.261204 -1 -0.261204 1 0.369398 -1 -9.12038e-018 1 0.261204 -1 0.261204 1
1.58307e-016 -1 0.369398 1 -0.261204 -1 0.261204 1 -0.369398 -1 1.62571e-016 1
-0.261204 -1 -0.261204 1 -2.49669e-016 -1 -0.369398 1 0.261204 -1 -0.261204 1
0.369398 -1 -9.12038e-018 1 0.261204 -1 0.261204 1 0.522408 -1 -0.522408 1
0.738796 -1 -1.82408e-017 1 0.522408 -1 0.522408 1 3.16613e-016 -1 0.738796 1
-0.522408 -1 0.522408 1 -0.738796 -1 3.25142e-016 1 -0.522408 -1 -0.522408 1
-4.99337e-016 -1 -0.738796 1 0.522408 -1 -0.522408 1 0.738796 -1 -1.82408e-017 1
0.522408 -1 0.522408 1 0.783612 -1 -0.783612 1 1.10819 -1 -2.73611e-017 1
0.783612 -1 0.783612 1 4.7492e-016 -1 1.10819 1 -0.783612 -1 0.783612 1
-1.10819 -1 4.87713e-016 1 -0.783612 -1 -0.783612 1 -7.49006e-016 -1 -1.10819 1


以下は、上記の画像をつくるために配置したpygです。
#primitive.pyg
Output ("primitive.tif", "tiff", "rgb", "camera", "float gain", 1, "float gamma",
1, "string filter", "gaussian", "float[2] filterwidth", (2, 2))
Attribute ("string projection", "perspective")
Attribute ("float fov", 20)
Attribute ("int[2] resolution", (640, 480))
#placecam 10 5 -10 0 0 0
Rotate (-19.47, 1.00, 0.00, 0.00)
Rotate (45.00, 0.00, 1.00, 0.00)
Translate (-10.00, -5.00, 10.00)

Attribute ("string geometryset", "+shadows")

World ()
Light ("light1", "pointlight", "float intensity", 350, "point from", (-10, 10, -10),"string shadowname", "shadows")
Light ("light2", "pointlight", "float intensity", 350, "point from", (10, 10, -10),"string shadowname", "shadows")

Attribute ("color C", (0.8, 1, 0.8))
Input("backplane.pyg")

PushTransform ()
Attribute ("color C", (0.2, 0.9, 0.2))
Shader ("surface", "clay")
Translate ( 2, 1.1, 0)
Input ("cone.rib")
PopTransform ()

PushTransform ()
Attribute ("color C", (0.9, 0.9, 0.9))
Shader ("surface", "veinedmarble")
Translate ( -2, 1.1, 0)
Input ("cyl.rib")
PopTransform ()

PushTransform ()
Attribute ("color C", (0.9, 0.5, 0.5))
Shader ("surface", "plastic")
Translate ( 1, 0.5, -3)
Input ("torus.rib")
PopTransform ()

Scale (2,2,2)
#Rotate (90.00, 0.00, 1.00, 0.00)
Rotate (90.00, 1.00, 0.00, 0.00)
Attribute ("color C", (1, 1, 1))
Shader ("surface", "oakplank","float Km", 1)
Input("PlaneGeom.rib")

Render()
NVIDIA Gelato | - | -

Inputを使ってbackground

Imageシェーダのようなものがないので、Inputフォルダにあるbackplane.pygを利用した。ついでライト設定やcube、teapotを利用する。
test.jpg

#test.pyg
Output ("test.tif", "tiff", "rgb", "camera", "float gain", 1, "float gamma",
1, "string filter", "gaussian", "float[2] filterwidth", (2, 2))
Attribute ("string projection", "perspective")
Attribute ("float fov", 45)
Attribute ("int[2] resolution", (640, 480))

Input("cameralights.pyg")

Rotate (-10.14, 1, 0, 0)
Rotate (26.57, 0, 1, 0)
Translate (-5, -2, 10)


World ()
Attribute ("color C", (0.8, 1, 0.8))
Input("backplane.pyg")


PushTransform()
Attribute ("color C", (1, 0, 0))
Shader ("surface", "plastic")
Translate(-2,-1,0)
Rotate(-90,1,0,0)
Input("teapot.pyg")
PopTransform()

PushTransform()
Attribute ("color C", (1, 1, 1))
Shader ("surface", "clay")
Translate(2,-1,0)
Input("cube.pyg")
PopTransform()

Render()
NVIDIA Gelato | - | -

NVIDIA Gelatoでdisplacement

サンプル見ながら、ディスプレイスメント設定。
メモリたくさん消費するようです。
警告が出て、シェーダ空間?設定がわからないところです。
Gelato(R) 2.2 Release 1 winnt (Apr 17 2008 11:09:24)
(c) Copyright 2003-2008 NVIDIA Corporation. All Rights Reserved.
Elapsed: 0.0004s Done: 0% |* |#rib.generat
or.so v1.0.0.24: reading './PlaneGeom.rib'.
Elapsed: 2.8160s Done: 0% |* |WARNING: Shader "dented" displaced 0.100454 units in "common" space,
compared to displacement radius 0.1
(object name "")
WARNING: Shader "dented" displaced 0.127336 units in "common" space,
compared to displacement radius 0.1
(object name "")
Elapsed: 6.0283s Done: 100% |*******************************|
Total render time was 7.2702s (7.27 s)
displace01.jpg

#displace01.pyg
Output ("displace01.tif", "tiff", "rgb", "camera", "float gain", 1, "float gamma",
1, "string filter", "gaussian", "float[2] filterwidth", (2, 2))
Attribute ("string projection", "perspective")
Attribute ("float fov", 45)
Attribute ("int[2] resolution", (640, 480))
#placecam 0 0 -10 0 0 0

Translate (0.00, 0.00, 8.00)
Rotate(-15,1,0,0)

Attribute ("string geometryset", "+shadows")
Attribute ("string geometryset", "+reflection")

World ()
Light ("light1", "pointlight", "float intensity", 1000, "point from", (-10, 3, -15),"string shadowname", "shadows")
Light ("light2", "pointlight", "float intensity", 300, "point from", (10, 10, -10))

PushTransform ()
Attribute ("color C", (0.2, 0.4, 0.75))
Sphere (100, -100, 100, 360)
PopTransform ()

PushTransform ()
Scale(3,3,3)
Translate (0.00, -1.0, 0.00)
Rotate (90.00, 1.00, 0.00, 0.00)
ShaderGroupBegin ()
Attribute ("color C", (1, 1, 1))
Shader ("surface", "checker", "checklayer","float stilesize",0.02,"float ttilesize",0.02,"color color2", ( 0.0, 1, 0.0 ))
Shader ("surface", "plastic", "plasticlayer")
ConnectShaders ("checklayer", "Cout", "plasticlayer", "C")
ConnectShaders ("checklayer", "fout", "plasticlayer", "Ks")
ShaderGroupEnd ()
Input("PlaneGeom.rib")
PopTransform ()

PushTransform ()
Attribute ("color C", (0.9, 0.9, 0.7))
Shader ( "surface", "metal","string envname","reflection" )
Shader ( "displacement", "dented", "float Km", 0.1 )
Attribute ("float displace:maxradius", 0.1)

PushAttributes ()
Translate ( 0, -2, 0 )
Rotate ( -90, 1, 0, 0 )
Input ("teapot.pyg")
PopAttributes ()
PopTransform ()

Render()
NVIDIA Gelato | - | -

これだけで大丈夫?「情報」におけるグラフィック3

「これだけで大丈夫 「情報」におけるグラフィック」を参考にNVIDIA Gelatoでつくりました。
ありがとうございます。
http://izumi-math.jp/sanae/cg/pov/pov_2.htm

【第2回】よりリアルなシーンファイル
例題3 背景と地面
 例題2のシーンに背景と地面を入れよ。
box_kore3.jpg

#box_kore3.pyg
Output ("box_kore03.tif", "tiff", "rgb", "camera", "float gain", 1, "float gamma",
1, "string filter", "gaussian", "float[2] filterwidth", (2, 2))
Attribute ("string projection", "perspective")
Attribute ("float fov", 20)
Attribute ("int[2] resolution", (320, 240))
#placecam 0 0 -10 0 0 0

Translate (0.00, 0.00, 10.00)

Attribute ("string geometryset", "+shadows")

World ()
Light ("light1", "pointlight", "float intensity", 800, "point from", (-10, 3, -20),"string shadowname", "shadows")
Light ("light2", "pointlight", "float intensity", 300, "point from", (10, 10, -10))

PushTransform ()
Attribute ("color C", (0.2, 0.4, 0.75))
Sphere (100, -100, 100, 360)
PopTransform ()

PushTransform ()
Attribute ("color C", (1, 0, 0))
Shader ("surface", "plastic")
Rotate (20,0,1,0)
Rotate (-30,1,0,0)
Input ("BoxGeom.rib")
PopTransform ()

PushTransform ()
Translate (0.00, -1.0, 0.00)
Rotate (90.00, 1.00, 0.00, 0.00)
ShaderGroupBegin ()
Attribute ("color C", (1, 1, 1))
Shader ("surface", "checker", "checklayer","float stilesize",0.05,"float ttilesize",0.05,"color color2", ( 0.0, 1, 0.0 ))
Shader ("surface", "plastic", "plasticlayer")
ConnectShaders ("checklayer", "Cout", "plasticlayer", "C")
ConnectShaders ("checklayer", "fout", "plasticlayer", "Ks")
ShaderGroupEnd ()
Input("PlaneGeom.rib")
PopTransform ()

Render()
NVIDIA Gelato | - | -

これだけで大丈夫?「情報」におけるグラフィック2

例題2 図形の変換(1) 回転
例題1の立方体を回転させよ。
box_kore2.jpg
回転した表示が微妙に違います。
rotate <-30,20,0>
X軸、Y軸の順番だと思いましたが、
クォータニオン、オイラー角など
少し厳密にやらないといけないかな。
ありがとうございます。
#box_kore2.pyg
Output ("box_kore02.tif", "tiff", "rgb", "camera", "float gain", 1, "float gamma",
1, "string filter", "gaussian", "float[2] filterwidth", (2, 2))
Attribute ("string projection", "perspective")
Attribute ("float fov", 10)
Attribute ("int[2] resolution", (320, 240))
#placecam 0 0 -10 0 0 0
#Rotate (-19.47, 1.00, 0.00, 0.00)
#Rotate (45.00, 0.00, 1.00, 0.00)
Translate (0.00, 0.00, 10.00)
World ()
Light ("light1", "pointlight", "float intensity", 800, "point from", (-10, 3, -20))
Light ("light2", "pointlight", "float intensity", 350, "point from", (10, 10, -10))

PushTransform ()
Attribute ("color C", (1, 0, 0))
Shader ("surface", "plastic")
Rotate (20,0,1,0)
Rotate (-30,1,0,0)
Input ("BoxGeom.rib")
PopTransform ()

Render()
NVIDIA Gelato | - | -

これだけで大丈夫?「情報」におけるグラフィック1

以下、POV-Rayチュートリアルを参考にNVIDIA Gelatoでつくりました。
ありがとうございます。
これだけで大丈夫 「情報」におけるグラフィック
http://izumi-math.jp/sanae/cg/pov/pov.htm

【第1回】最初のシーンファイル
例題1 基本の3要素
立方体を作成せよ。
box_kore1.jpg

#box_kore1.pyg
Output ("box_kore01.tif", "tiff", "rgb", "camera", "float gain", 1, "float gamma",
1, "string filter", "gaussian", "float[2] filterwidth", (2, 2))
Attribute ("string projection", "perspective")
Attribute ("float fov", 10)
Attribute ("int[2] resolution", (320, 240))
#placecam 0 0 -10 0 0 0
#Rotate (-19.47, 1.00, 0.00, 0.00)
#Rotate (45.00, 0.00, 1.00, 0.00)
Translate (0.00, 0.00, 10.00)
World ()
Light ("light1", "pointlight", "float intensity", 1000, "point from", (-10, 3, -20))

PushTransform ()
Attribute ("color C", (1, 0, 0))
Shader ("surface", "plastic")
Input ("BoxGeom.rib")
PopTransform ()

Render()
NVIDIA Gelato | - | -

NVIDIA Gelatoでレンダリング

次のサイトを参考にして、組んでみました。
横浜国立大学教育人間科学部マルチメディア文化課程の西村研究室参考
http://nishimulabo.edhs.ynu.ac.jp/~povray/beginner/p4-14.html
情報B:POV-Rayが教材になるか?
http://arashi50.cocolog-nifty.com/blog/2008/04/bpovray_8e8d.html

nishimura_pov.jpg

#nishimura_pov.pyg
from math import *

Output ("nishimura_pov.tif", "tiff", "rgb", "camera", "float gain", 1, "float gamma",
1, "string filter", "gaussian", "float[2] filterwidth", (2, 2))
Attribute ("float shadingquality",1.2)
Attribute ("string projection", "perspective")
Attribute ("float fov", 80)
Attribute ("int[2] resolution", (640, 480))
#placecam 0 6 -9 0 0 0
Rotate (-33.69, 1.00, 0.00, 0.00)
Translate (0.00, -6.00, 9.00)

Attribute ("string geometryset", "+shadows")
Attribute ("string geometryset", "+reflection")

World ()
Light ( "light0", "ambientlight", "float intensity", 0.2 )
Light ("light1", "pointlight", "float intensity", 1400, "point from", (-20, 20, -20),"string shadowname", "shadows")

PushTransform ()
Attribute ("color C", (0.2, 0.4, 0.75))
Sphere (100, -100, 100, 360)
PopTransform ()

PushTransform ()
Attribute ("color C", (1, 1, 1))
Shader ("surface", "metal", "string envname", "reflection",
"float Kd", 0.05, "float roughness", 0.1, "float Kr", 0.75,
"float Ka", 0.5,
# "float samples", 4, "float blur", 0.05,
"float twosided", 1
)
Sphere (2, -2, 2, 360)
PopTransform ()

T = -360
while (T <= 360):
PushTransform ()
Attribute ("color C", (0.001*T, 1-0.001*T, 0.8))
Shader ("surface", "plastic")
Rotate (T,0,1,0)
Translate (3.5,0.01*T,0)
Scale (1,0.5,3.5)
Input ("BoxGeom.rib")
PopTransform ()
T = T + 45


Translate (0, -5, 0)
Rotate (90.00, 1.00, 0.00, 0.00)
Scale (10,10,10)
ShaderGroupBegin ()
Attribute ("color C", (1, 1, 1))
Shader ("surface", "checker", "checklayer","float stilesize",0.01,"float ttilesize",0.01,"color color2", ( 0.6, 0.6, 1.0 ))
Shader ("surface", "plastic", "plasticlayer")
ConnectShaders ("checklayer", "Cout", "plasticlayer", "C")
ConnectShaders ("checklayer", "fout", "plasticlayer", "Ks")
ShaderGroupEnd ()
Input("PlaneGeom.rib")
Render()
NVIDIA Gelato | - | -

POV-Rayの回転

通常回転は、(x軸まわり)→(y軸まわり)→(z軸まわり)の順で行われる。
ありがとうございます。
NVIDIA Gelato | - | -

NVIDIA Gelato ばね状トーラス

今回も「数学のいずみ」を参考にしました。ありがとうございます。
sphere_bane_torus.jpg

#sphere_bane_torus.pyg
from math import *

Output ("sphere_bane_torus.tif", "tiff", "rgb", "camera", "float gain", 1, "float gamma",
1, "string filter", "gaussian", "float[2] filterwidth", (2, 2))
Attribute ("float shadingquality",1.2)
Attribute ("string projection", "perspective")
Attribute ("float fov", 20)
Attribute ("int[2] resolution", (640, 480))
#placecam 0 16 -12 0 0 0
Rotate (-53.13, 1.00, 0.00, 0.00)
Translate (0.00, -16.00, 12.00)

Attribute ("string geometryset", "+shadows")
Attribute ("string geometryset", "+reflection")

World ()
Light ( "light0", "ambientlight", "float intensity", 0.3 )
Light ("light1", "pointlight", "float intensity", 300, "point from", (0, 10, -10),"string shadowname", "shadows")
Light ("light2", "pointlight", "float intensity", 300, "point from", (5, 0, -10),"string shadowname", "shadows")

PushTransform ()
Attribute ("color C", (0.2, 0.4, 0.75))
Sphere (100, -100, 100, 360)
PopTransform ()

MyCounter = -15*pi
while (MyCounter <= 15*pi):
PushTransform ()
Attribute ("color C", (1, 1, 1))
Shader ( "surface", "metal","string envname","reflection" )
Translate ( (2+0.5*cos(1.11*MyCounter))*cos(0.1*MyCounter),
(2+0.5*cos(1.11*MyCounter))*sin(0.1*MyCounter),
0.5*sin(1.11*MyCounter))
Sphere (0.13, -0.13, 0.13, 360)
PopTransform ()
MyCounter = MyCounter + 0.5


Translate (0, -5, 0)
Rotate (90.00, 1.00, 0.00, 0.00)
Scale (10,10,10)
ShaderGroupBegin ()
Attribute ("color C", (0, 0.8, 0))
Shader ("surface", "checker", "checklayer","float stilesize",0.01,"float ttilesize",0.01,"color color2", ( 0, 0, 0.9 ))
Shader ("surface", "plastic", "plasticlayer")
ConnectShaders ("checklayer", "Cout", "plasticlayer", "C")
ConnectShaders ("checklayer", "fout", "plasticlayer", "Ks")
ShaderGroupEnd ()
Input("PlaneGeom.rib")
Render()
NVIDIA Gelato | - | -

placecam.c

「実践CGの誘い」や、RenderMan Fastの解説サイト、RenderDotCサイトのマニュアルページに、placecam.cがある。pythonで書き換えるのもできるし、簡単にコンパイルができます。今日もありがとうございます。


/*
* PlaceCamera(): establish a viewpoint, viewing direction and orientation
* for a scene. This routine must be called before RiWorldBegin().
* position: a point giving the camera position
* aim: a point giving the location at which the camera is aimed
* roll: an optional rotation of the camera about its direction axis
* coneangle: an optional spotlight shader cone angle
*/

#include
#include
#include

#define PI 3.1415926535897932

void usage(void)
{
printf("usage: placecam pos_x pos_y pos_z aim_x aim_y aim_z\n");
printf(" [coneangle] [roll_angle]\n");
printf(" Calculate RenderMan transforms needed for camera transform\n");
printf(" from light position to aim point with the given roll angle.\n");
exit(1);
}

typedef double RtPoint[3];

void RiRotate(double angle, double x, double y, double z)
{
if (fabs(angle) > 0.001) {
printf("Rotate %0.2f %0.2f %0.2f %0.2f\n", angle, x, y, z);
}
}

void RiTranslate(double dx, double dy, double dz)
{
printf("Translate %0.2f %0.2f %0.2f\n", dx, dy, dz);
}

/*
* AimZ(): rotate the world so the direction vector points in
* positive z by rotating about the y axis, then x. The cosine
* of each rotation is given by components of the normalized
* direction vector. Before the y rotation the direction vector
* might be in negative z, but not afterward.
*/
void AimZ(RtPoint direction)
{
double xzlen, yzlen, yrot, xrot;

if (direction[0]==0 && direction[1]==0 && direction[2]==0)
return;

/*
* The initial rotation about the y axis is given by the projection of
* the direction vector onto the x,z plane: the x and z components
* of the direction.
*/
xzlen = sqrt(direction[0]*direction[0]+direction[2]*direction[2]);
if (xzlen == 0)
yrot = (direction[1] < 0) ? 180 : 0;
else
yrot = 180*acos(direction[2]/xzlen)/PI;

/*
* The second rotation, about the x axis, is given by the projection on
* the y,z plane of the y-rotated direction vector: the original y
* component, and the rotated x,z vector from above.
*/
yzlen = sqrt(direction[1]*direction[1]+xzlen*xzlen);
xrot = 180*acos(xzlen/yzlen)/PI; /* yzlen should never be 0 */

if (direction[1] > 0)
RiRotate(xrot, 1.0, 0.0, 0.0);
else
RiRotate(-xrot, 1.0, 0.0, 0.0);

/* The last rotation declared gets performed first */
if (direction[0] > 0)
RiRotate(-yrot, 0.0, 1.0, 0.0);
else
RiRotate(yrot, 0.0, 1.0, 0.0);
}

void PlaceCamera(RtPoint position, RtPoint direction, float roll)
{
RiRotate(-roll, 0.0, 0.0, 1.0);
AimZ(direction);
RiTranslate(-position[0], -position[1], -position[2]);
}

int main(int argc, char *argv[])
{
RtPoint pos, aim, dir;
double roll;
double coneangle, fov;

if (argc < 7) usage();

pos[0] = atof(argv[1]);
pos[1] = atof(argv[2]);
pos[2] = atof(argv[3]);
aim[0] = atof(argv[4]);
aim[1] = atof(argv[5]);
aim[2] = atof(argv[6]);

if (argc > 7) coneangle = atof(argv[7]);
else coneangle = 0.0;

if (argc > 8) roll = atof(argv[8]);
else roll = 0.0;

printf("position: %0.2f, %0.2f, %0.2f\n", pos[0], pos[1], pos[2]);
printf("aim: %0.2f, %0.2f, %0.2f\n", aim[0], aim[1], aim[2]);
printf("coneangle: %0.4f\n", coneangle);
printf("roll: %0.2f\n\n", roll);

if (coneangle != 0.0) {
fov = coneangle * 360.0 / PI;
printf("Projection \"perspective\" \"fov\" [%0.2f]\n", fov);
}

dir[0] = aim[0] - pos[0];
dir[1] = aim[1] - pos[1];
dir[2] = aim[2] - pos[2];

PlaceCamera(pos, dir, roll);
return 0;
}
RenderMan | - | -
1/3 >>