vertex,triangle,quad's lighting problem

57 views
Skip to first unread message

天宇

unread,
Oct 29, 2024, 10:00:02 AM10/29/24
to VPython-users
I am learning vpython recently. In vpython reference, I know that almost every 3D shape is made of vertex, triangle and quad. So  I try to make a simple  tetrahedron. My code is as follows:

from vpython import *
scene.ambient = vector(0,0,0)
scene.lights = []
local_light(pos=vector(8, 2, 5), color=color.white)
v1 = vertex(pos=vector(0, 0, 0))
v2 = vertex(pos=vector(10, 0, 0))
v3 = vertex(pos=vector(10, 10, 0))
v4 = vertex(pos=vector(10, 10, -5))
t1 = triangle(v0=v1, v1=v2, v2=v3)
t2 = triangle(v0=v1, v1=v3, v2=v4)
t3 = triangle(v0=v1, v1=v4, v2=v2)
t4 = triangle(v0=v2, v1=v4, v2=v3)
while True:
    rate(60)

It really shows a tetrahedron for me which is nice.
未命名图片.png
However, when I look the back of the shape, It still light but not dark !
That  quite amazes me.
未命名图片.png
It seems that light acts on each triangle independently, and the concept of occlusion is lost.
When I make a simple sphere,  everything works well not like using vertex. Codes :
from vpython import *
scene.ambient = vector(0,0,0)
scene.lights = []
local_light(pos=vector(8, 2, 5), color=color.white)
sphere()
while True:
    rate(60)
未命名图片.png

Bruce Sherwood

unread,
Oct 29, 2024, 2:34:49 PM10/29/24
to VPython-users
For starters, it is important in a vertex statement to specify a normal.

Bruce

Bruce Sherwood

unread,
Oct 29, 2024, 2:58:20 PM10/29/24
to VPython-users
Here is a program that displays a red pyramid object and a green object made from vertex objects, triangle objects, and a quad object:

pyramid(pos=vec(-0.5,0,-0.5), length=1, axis=vec(0,1,0), color=color.red)

w = color.green
v1 = vertex(pos=vec(0,0,0), normal=vec(0,1,1), color=w)
v2 = vertex(pos=vec(1,0,0), normal=vec(0,1,1), color=w)
v3 = vertex(pos=vec(0.5,1,-0.5), normal=vec(0,1,1), color=w)
t1 = triangle(vs=[v1,v2,v3])

v4 = vertex(pos=vec(1,0,0), normal=vec(1,1,0), color=w)
v5 = vertex(pos=vec(1,0,-1), normal=vec(1,1,0), color=w)
v6 = vertex(pos=vec(0.5,1,-0.5), normal=vec(1,1,0), color=w)
t2 = triangle(vs=[v4,v5,v6])

v7 = vertex(pos=vec(1,0,-1), normal=vec(0,1,-1), color=w)
v8 = vertex(pos=vec(0,0,-1), normal=vec(0,1,-1), color=w)
v9 = vertex(pos=vec(0.5,1,-0.5), normal=vec(0,1,-1), color=w)
t3 = triangle(vs=[v7,v8,v9])

v10 = vertex(pos=vec(0,0,-1), normal=vec(0,1,1), color=w)
v11 = vertex(pos=vec(0,0,0), normal=vec(0,1,1), color=w)
v12 = vertex(pos=vec(0.5,1,-0.5), normal=vec(0,1,1), color=w)
t4 = triangle(vs=[v10,v11,v12])

v13 = vertex(pos=vec(0,0,0), normal=vec(0,-1,0), color=w)
v14 = vertex(pos=vec(1,0,0), normal=vec(0,-1,0), color=w)
v15 = vertex(pos=vec(1,0,-1), normal=vec(0,-1,0), color=w)
v16 = vertex(pos=vec(0,0,-1), normal=vec(0,-1,0), color=w)
floor = quad(vs=[v13,v14,v15,v16])

Bruce

天宇

unread,
Oct 29, 2024, 3:11:44 PM10/29/24
to VPython-users
I am so happy to receive your reply, bruce !
Thank you for correcting my code which unlocked the blind spot of my use of vertex.
Reply all
Reply to author
Forward
0 new messages