<< 4/7 >>

「実践CGへの誘い」例題をPythonで行う その6

「実践CGへの誘い」p.31
Listing 2.7


>list27.py
オブジェクトインスタンスを利用しようと組んだら、以下のエラー
3DL ERROR P1051: [C:0]: invalid context for 'RiTransformBegin'
3DL ERROR P1051: [C:0]: invalid context for 'RiRotate'
3DL ERROR P1051: [C:0]: invalid context for 'RiTransformEnd'

いろいろと調べたら、cgkitでは、
obj = RiObjectBegin()
RiSphere(0.5,-0.5,0.5,360)
RiObjectEnd()
RiObjectInstance(obj)

と記述しているが、これは問題ない。

cube = RiObjectBegin()
UnitCube()
RiObjectEnd()
問題は、UnitCube()
「実践CGへの誘い」によれば、物体の記述に、ジオメトリ変換を記入していると無視される。よってUnitCube()は、一番最初にやった面のみを記述する定義を用いる。
  • -
  • -

「実践CGへの誘い」例題をPythonで行う その7

できました。よく読まないといけません。
UnitCube()を最初に使った定義のものに戻しています。

「実践CGへの誘い」p.32
Listing 2.7

オブジェクトインスタンスを利用したサンプルです。
ありがとうございます。


#list27.py
#/* Copyrighted Pixar 1989 */
#/* From the RenderMan Companion p. 32 */
#/* Listing 27c A more efficient color cube */
#/*
# * ColorCube(): create a unit color cube from smaller cubes
# * Parameters:
# * n: the number of minicubes on a side
# * s: a scale factor for each minicube
# */

import cgkit.cri
from cgkit.cgtypes import *

# Load the RenderMan API.
# Replace the library name with whatever renderer you want to use.
ri = cgkit.cri.loadRI("3delight")
cgkit.cri.importRINames(ri, globals())

L= -.5 #* For x: left side */
R= .5 # /* For x: right side */
D= -.5 # /* For y: down side */
U= .5 # /* For y: upper side */
F= .5 # /* For z: far side */
N= -.5 # /* For z: near side */

#/* UnitCube(): define a cube in the graphics environment */
def UnitCube():
Cube=[
[ [L,D,F], [L,D,N], [R,D,N], [R,D,F] ], #/* Bottom face */
[ [L,D,F], [L,U,F], [L,U,N], [L,D,N] ], #/* Left face */
[ [R,U,N], [L,U,N], [L,U,F], [R,U,F] ], #/* Top face */
[ [R,U,N], [R,U,F], [R,D,F], [R,D,N] ], #/* Right face */
[ [R,D,F], [R,U,F], [L,U,F], [L,D,F] ], #/* Far face */
[ [L,U,N], [R,U,N], [R,D,N], [L,D,N] ] #/* Near face */
];
for i in range(6):
RiPolygon(P=Cube[i])

def ColorCube(n, s):
if n<=0:
return

cube = RiObjectBegin()
UnitCube()
RiObjectEnd()

RiAttributeBegin()
RiTranslate(-.5, -.5, -.5 )
RiScale(1.0/n, 1.0/n, 1.0/n)
color=[0,0,0]
for x in range(n):
for y in range(n):
for z in range(n):
color[0] = (x+1)/float(n)
color[1] = (y+1)/float(n)
color[2] = (z+1)/float(n)
RiColor(color)
RiTransformBegin()
RiTranslate (x+.5, y+.5, z+.5)
RiScale(s, s, s)
RiObjectInstance(cube)
RiTransformEnd()

RiAttributeEnd()


color = ( .2, .4, .6 )

RiBegin(RI_NULL); #/* Start the renderer */
RiDisplay("colormain2.tiff", RI_FILE, "rgb", RI_NULL)
RiFormat(512, 384, -1.0)
RiShadingRate(1.0)
RiLightSource("distantlight", RI_NULL)
RiProjection(RI_PERSPECTIVE,RI_FOV,65)
RiWorldBegin()
RiSides(1) # /* N E W */
RiTranslate(0.0, 0.0, 1.5)
RiRotate(30.0, -1.0, 0.0, 0.0)
RiRotate(30.0, 0.0, -1.0, 0.0)
RiColor(color) # /* Declare the color */
ColorCube(4, .8) #/* Define the cube */
RiWorldEnd()
RiEnd() #/* Clean up after the renderer */


  • -
  • -

「実践CGへの誘い」例題をPythonで行う その8

■簡単なアニメーション
60フレーム出力してみました。
「実践CGへの誘い」p.34
Listing 2.8を改良しました。
Python便利ですね。
ありがとうございます。



#list28change.py
#/* Copyrighted Pixar 1989 */
#/* From the RenderMan Companion p. 34 */
#/* Listing 2.8 A simple animation */
#/*
# * ColorCube(): create a unit color cube from smaller cubes
# * Parameters:
# * n: the number of minicubes on a side
# * s: a scale factor for each minicube
# */

import cgkit.cri
from cgkit.cgtypes import *

# Load the RenderMan API.
# Replace the library name with whatever renderer you want to use.
ri = cgkit.cri.loadRI("3delight")
cgkit.cri.importRINames(ri, globals())

L= -.5 #* For x: left side */
R= .5 # /* For x: right side */
D= -.5 # /* For y: down side */
U= .5 # /* For y: upper side */
F= .5 # /* For z: far side */
N= -.5 # /* For z: near side */

#/* UnitCube(): define a cube in the graphics environment */
def UnitCube():
Cube=[
[ [L,D,F], [L,D,N], [R,D,N], [R,D,F] ], #/* Bottom face */
[ [L,D,F], [L,U,F], [L,U,N], [L,D,N] ], #/* Left face */
[ [R,U,N], [L,U,N], [L,U,F], [R,U,F] ], #/* Top face */
[ [R,U,N], [R,U,F], [R,D,F], [R,D,N] ], #/* Right face */
[ [R,D,F], [R,U,F], [L,U,F], [L,D,F] ], #/* Far face */
[ [L,U,N], [R,U,N], [R,D,N], [L,D,N] ] #/* Near face */
];
for i in range(6):
RiPolygon(P=Cube[i])

def ColorCube(n, s):
if n<=0:
return

cube = RiObjectBegin()
UnitCube()
RiObjectEnd()

RiAttributeBegin()
RiTranslate(-.5, -.5, -.5 )
RiScale(1.0/n, 1.0/n, 1.0/n)
color=[0,0,0]
for x in range(n):
for y in range(n):
for z in range(n):
color[0] = (x+1)/float(n)
color[1] = (y+1)/float(n)
color[2] = (z+1)/float(n)
RiColor(color)
RiTransformBegin()
RiTranslate (x+.5, y+.5, z+.5)
RiScale(s, s, s)
RiObjectInstance(cube)
RiTransformEnd()

RiAttributeEnd()


color = ( .2, .4, .6 )

NFRAMES = 60 #/* number of frames in the animation */
NCUBES = 5 #/* # of minicubes on a side of the color cube */
FRAMEROT = 3.0 #/* # of degrees to rotate cube between frames */


RiBegin(RI_NULL) #/* Start the renderer */
frame = 1
for frame in range(frame,NFRAMES+1):
if len(str(frame))==1:
print "anim00"+"%d.tif" % frame
filename="anim00"+"%d.tif" % frame
elif len(str(frame))==2:
print "anim0"+"%d.tif" % frame
filename="anim0"+"%d.tif" % frame
else:
print "anim%d.tif" % frame
filename="anim%d.tif" % frame

RiFrameBegin(frame)
RiLightSource("distantlight", RI_NULL)
#/* Viewing transformation */
RiProjection("perspective", RI_NULL)
RiTranslate(0.0, 0.0, 1.5)
RiRotate(40.0, -1.0, 1.0, 0.0)

RiDisplay(filename, RI_FILE, RI_RGBA, RI_NULL)
RiFormat(512, 384, -1.0)
RiShadingRate(1.0)
RiWorldBegin()
RiSides(1); #/* N E W */
scale = (NFRAMES-(frame-1)) / float(NFRAMES)
RiRotate(FRAMEROT*frame, 0.0, 0.0, 1.0)
RiSurface("matte", RI_NULL)
ColorCube(NCUBES, scale) #/* Define the cube */
RiWorldEnd()
RiFrameEnd()

RiEnd()



出力画像はこちらを参考にしてください。
  • -
  • -

「実践CGへの誘い」例題をPythonで行う その9

「実践CGへの誘い」p.61
Listing 4.1

基本プリミティブの表示をします。いろいろと工夫できますね。


#list41.py
#/* Copyrighted Pixar 1989 */
#/* From the RenderMan Companion p. 61 */
#/* Listing 4.1 Routine generating quadric surfaces shown in Figure 4.1 */
#/*

import cgkit.cri
from cgkit.cgtypes import *

# Load the RenderMan API.
# Replace the library name with whatever renderer you want to use.
ri = cgkit.cri.loadRI("3delight")
cgkit.cri.importRINames(ri, globals())

def Go():
ShowQuads()

OFFSET = 1.2

def ShowQuads():
RiRotate(-90.0, 1.0, 0.0, 0.0)

RiTranslate(-OFFSET, 0.0, (OFFSET/2))
RiSphere(0.5, -0.5, 0.5, 360.0, RI_NULL) #/* Declare a sphere */

RiTranslate(OFFSET, 0.0, 0.0)
RiTranslate(0.0, 0.0, -0.5)
RiCone(1.0, 0.5, 360.0, RI_NULL) #/* Declare a cone */

RiTranslate(0.0, 0.0, 0.5)
RiTranslate(OFFSET, 0.0, 0.0)
RiCylinder(0.5, -0.5, 0.5, 360.0, RI_NULL) #/* Declare cylinder */

RiTranslate(-(OFFSET*2), 0.0, -OFFSET)
hyperpt1 =[ 0.4, -0.4, -0.4]
hyperpt2 =[ 0.4, 0.4, 0.4]

#/* Declare hyperboloid */
RiHyperboloid(hyperpt1, hyperpt2, 360.0, RI_NULL)

RiTranslate(OFFSET, 0.0, -0.5)
RiParaboloid(0.5, 0.0, 0.9, 360.0, RI_NULL) #/* Declare paraboloid */

RiTranslate(OFFSET, 0.0, 0.5)
RiTorus(.4, .15, 0.0, 360.0, 360.0, RI_NULL) #/* Declare torus */


RiBegin (RI_NULL) #/* Start Renderer */
RiDisplay("quadsmain.tiff", RI_FILE, "rgb", RI_NULL)
RiFormat( 512, 372, -1.0)
RiProjection (RI_PERSPECTIVE,RI_FOV,45)
RiTranslate (0.0, 0.0, 3.5)
RiWorldBegin ()
RiLightSource ("distantlight", RI_NULL)
RiAttributeBegin()

RiSurface ("plastic", Kd=1.0)
RiColor((0.97,0.64,0.39))
Go()
RiAttributeEnd()
RiWorldEnd()
RiEnd()




quadsmain.jpg
  • -
  • -

「実践CGへの誘い」例題をPythonで・・その10

list42.jpg
「実践CGへの誘い」p.65
Listing 4.2



#list42.py
#/* Copyrighted Pixar 1989 */
#/* From the RenderMan Companion p. 65 */
#/* Listing 4.2 Using hyperboloids to create a surface of revolution */

import cgkit.cri
from cgkit.cgtypes import *

# Load the RenderMan API.
# Replace the library name with whatever renderer you want to use.
ri = cgkit.cri.loadRI("3delight")
cgkit.cri.importRINames(ri, globals())

NPOINTS=16

points = [
[1.5000,.0000],
[1.4600,.0900],
[1.3500,.1273],
[1.2625,.1203],
[1.1750,.1047],
[1.0875,.0935],
[1.0000,.0899],
[0.9375,.0982],
[0.8625,.1236],
[0.7250,.1851],
[0.5875,.2281],
[0.4500,.2383],
[0.3375,.2255],
[0.2250,.1953],
[0.0750,.1414],
[0.0000,.1125]
]

color = (1,0.26,0.15)

def Go():
global color
RiColor(color)
RiSurface ("plastic", Kd=1.0)
RiRotate(-90.0, 1.0, 0.0, 0.0)
SurfOR(points, NPOINTS)


#/* Listing 4.2 Using hyperboloids to create a surface of revolution */

def SurfOR(points, npoints):
#/*
# * For each adjacent pair of x,y points in the outline description,
# * draw a hyperboloid by sweeping the line segment defined by
# * those points about the z axis.
# */
pp1 =[ points[0][1], 0, points[0][0]]
for pt in range(1, npoints):
pp2 =[ points[pt][1], 0, points[pt][0]]
RiHyperboloid(pp1, pp2, 360.0, RI_NULL)
tmp = pp1
pp1 = pp2
pp2 = tmp


RiBegin (RI_NULL) #/* Start Renderer */
RiDisplay("list42.tif", RI_FILE, "rgb", RI_NULL)
RiFormat( 512, 372, -1.0)
RiProjection (RI_PERSPECTIVE,RI_FOV,45)
RiTranslate (0.0, -0.75, 2.2)
RiWorldBegin ()
RiLightSource ("distantlight", RI_NULL)
RiAttributeBegin()

Go()
RiAttributeEnd()
RiWorldEnd()
RiEnd()

  • -
  • -

「実践CGへの誘い」例題をPythonで・・その11

「実践CGへの誘い」p.67
Listing 4.3
wavemain.jpg


#list43.py
#/* Copyrighted Pixar 1989 */
#/* From the RenderMan Companion p. 67 */
#/* Listing 4.3 Using RiTorus to create a wavelike pattern */

import cgkit.cri
from cgkit.cgtypes import *

# Load the RenderMan API.
# Replace the library name with whatever renderer you want to use.
ri = cgkit.cri.loadRI("3delight")
cgkit.cri.importRINames(ri, globals())

def TorusWave(nwaves, thetamax):
if nwaves < 1:
print "Need a positive number of waves\n"

#/* Divide the net radius 1.0 among the waves and the hemisphere */
innerrad = 2 / (8.0*nwaves + 2)
RiRotate(90.0, 1.0, 0.0, 0.0)
#/* Create the downward-opening hemisphere */
RiSphere(innerrad, -innerrad, 0.0, thetamax, RI_NULL)

outerrad = 0
for wave in range(1,nwaves+1):
#* Each iteration creates a downward-opening half-torus
#* and a larger upward-opening half-torus.
outerrad = outerrad + (innerrad * 2)
RiTorus(outerrad, innerrad, 0.0, 180.0, thetamax, RI_NULL)
outerrad = outerrad + (innerrad * 2)
RiTorus(outerrad, innerrad, 180.0, 360.0, thetamax, RI_NULL)

def Go():
TorusWave(4, 250.0)


RiBegin (RI_NULL) #/* Start Renderer */
RiDisplay("wavemain.tif", RI_FILE, "rgb", RI_NULL)
RiFormat( 512, 372, -1.0)
RiProjection ("perspective", RI_NULL)

RiWorldBegin ()
RiLightSource("ambientlight","intensity",0.4)
RiLightSource ("distantlight", RI_NULL)
RiAttributeBegin()
RiTranslate (-0.1, 0.0, 1.1)
RiRotate (50.0, -1.0, 1.0, 0.0)
RiColor((0.42,0.97,0.45))
RiSurface ("plastic", Kd=1.0)
Go()
RiAttributeEnd()
RiWorldEnd()
RiEnd()


wavemain2.jpg
RiProjection ("perspective", "fov", 45)
と書き換えてみた。
  • -
  • -

Depth of field

Essential RenderManから、loop.cをPythonで試してみる。
Depth of field、PixelSamples、ShadingRateを調整しています。
ありがとうございます。
loop.jpg


#/* loop.py - Create a line of Spheres */
# Essential RenderMan p.113
import cgkit.cri
from cgkit.cgtypes import *

# Load the RenderMan API.
# Replace the library name with whatever renderer you want to use.
ri = cgkit.cri.loadRI("3delight")
cgkit.cri.importRINames(ri, globals())

RiBegin(RI_NULL)
RiDisplay ("loop.tif",RI_FILE, RI_RGB)
RiFormat(320, 240,1)
RiPixelSamples( 8, 8)
RiShadingRate(0.25)
RiProjection (RI_PERSPECTIVE)
RiDepthOfField(2.8,0.100,2)
RiTranslate(0,0,0.25)
RiRotate(-10,0,1,0)
RiWorldBegin()
RiLightSource("ambientlight","intensity",0.2)
RiLightSource("distantlight", "from",[-1,1,-1])
RiTranslate(0.3,0,0)
for i in range(12):
RiSurface ("plastic")
RiColor((1.0,0.05,0.05))
RiSphere(0.25,-0.25,0.25,360,RI_NULL)
RiTranslate(-0.2,0,0.5)

RiWorldEnd()
RiEnd()


RiLightSource("distantlight", "from",[-4,1,-1])
ディスタントライトの方向を変更してみた。

#RiDepthOfField(2.8,0.1,2)
とコメントアウトすると、くっきりになる。
cgkitと3Delightを使用しました。
  • -
  • -

pythonでパーティクル

Essential RenderManから、point.cをPythonで試してみる。
パーティクルも面白くなりそうです。
記述に関しては、cgkitのritest.pyが参考になります。
ありがとうございます。レンダリングは3Delightです。
以前書いた記事、CAPIでパーティクル
point.jpg


#point.py - Create a simple Particle System
import random, math
import cgkit.cri
from cgkit.cgtypes import *


# Load the RenderMan API.
# Replace the library name with whatever renderer you want to use.
ri = cgkit.cri.loadRI("3delight")
cgkit.cri.importRINames(ri, globals())


COUNT = 1000

def jitter(scale):
val=random.random()*1000
return (val/500-1)*scale

position=[]
red=[1,0,0]
fov=30

#/*Generate Particle Postions*/
for i in range(COUNT):
x=math.sin(i*0.5)*50+jitter(2)
y=math.cos(i*0.1)*50+jitter(2)
z=math.cos(i*0.5)*100+jitter(2)
position.append(vec3(x, y, z))


RiBegin(RI_NULL)
RiDisplay ("point.tif","file","rgb",RI_NULL)
RiFormat(512, 384, -1.0)
RiPixelSamples( 4, 4)
RiShadingRate(0.5)
RiProjection ("perspective","fov",fov,RI_NULL)
RiWorldBegin()
RiTranslate(0,0,300)
RiColor(red)
RiPoints("P",position,RI_NULL)
RiWorldEnd()
RiEnd()

  • -
  • -

pythonでパーティクル その2

イギリスのbournemouth大学のサイトを参考に作成してみた。
レンダリングは3Delightです。
勉強になります。ありがとうございます。
point2.jpg


#point2.py - Create a simple Particle System
import random, math
import cgkit.cri
from cgkit.cgtypes import *
from random import uniform as ru


# Load the RenderMan API.
# Replace the library name with whatever renderer you want to use.
ri = cgkit.cri.loadRI("3delight")
cgkit.cri.importRINames(ri, globals())

points=[]
width=[]
colour=[]
normals=[]
pappend=points.append
wappend=width.append
cappend=colour.append
nappend=normals.append

for i in range(0,1500):
for ix in range(0,3):
cappend(ru(0,1))
pappend(ru(-2,2))
nappend(ru(0,1))
wappend(ru(0.01,0.2))


RiBegin(RI_NULL)
RiImager("background", "color background",(.2,.4,.6))
RiDisplay ("point2.tif","file","rgb",RI_NULL)
RiFormat(512, 384, -1.0)
RiPixelSamples( 4, 4)
RiShadingRate(1)
RiProjection ("perspective","fov",30,RI_NULL)
RiWorldBegin()
RiLightSource("ambientlight","intensity",0.4)
RiLightSource("distantlight", "from",[0,0,1])
RiTranslate(0,0,6)
RiSurface("plastic")
RiPoints("P",points,"Cs",colour,"width",width,"N",normals,RI_NULL)
RiWorldEnd()
RiEnd()



実は、3DelightのPointsは、法線N部分(,"N",normals)が無視されるようだ。図のようにdiskはカメラ方向を見ている。Prmanではdiskの面がいろいろ変わってくる。次の記事を参照してください。
  • -
  • -

pythonでパーティクル その3

cgkitとPrmanを組み合わせる。どうも3Delightとprmanでは、RiPointの仕様がちがうようです。
3Delightのマニュアルはこちらになりますが、blobbyが使えて、遊べそうです。

さてさてPrmanだと以下の通りになります。


#point2.py - Create a simple Particle System
import random, math
import cgkit.cri
from cgkit.cgtypes import *
from random import uniform as ru


# Load the RenderMan API.
# Replace the library name with whatever renderer you want to use.
ri = cgkit.cri.loadRI("C:\Program Files (x86)\Pixar\RenderManProServer-14.4\lib\libprman")
cgkit.cri.importRINames(ri, globals())

points=[]
width=[]
colour=[]
normals=[]
pappend=points.append
wappend=width.append
cappend=colour.append
nappend=normals.append

for i in range(0,1500):
for ix in range(0,3):
cappend(ru(0,1))
pappend(ru(-2,2))
nappend(ru(0,1))
wappend(ru(0.01,0.2))


RiBegin(RI_NULL)
RiImager("background", "color background",(.2,.4,.6))
RiDisplay ("point2_pr.tif","file","rgb",RI_NULL)
RiFormat(512, 384, -1.0)
RiPixelSamples( 4, 4)
RiShadingRate(1)
RiProjection ("perspective","fov",30,RI_NULL)
RiWorldBegin()
RiLightSource("ambientlight","intensity",0.4)
RiLightSource("distantlight", "from",[0,0,1])
RiTranslate(0,0,6)
RiSurface("plastic")
RiPoints("P",points,"Cs",colour,"width",width,"N",normals,RI_NULL)
RiWorldEnd()
RiEnd()



実行方法は、

>point2_pr.py |prman

point2_pr.jpg
ありがとうございます。調べて納得して、繰り返し繰り返しですね。
  • -
  • -

<< 4/7 >>