How does light work ???

Everything related to 3D programming
Papala
User
User
Posts: 38
Joined: Wed Sep 12, 2012 5:09 pm

How does light work ???

Post by Papala »

Hello, I'm unable to project a light on a mesh created with createmsh()... Can someone tell me what am I doing wrong ? ^^"

Code: Select all

InitEngine3D()
InitSprite()

OpenWindow(0,0,0,500,500,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,500,500)

CreateLight(0,RGB(255,0,0),0,0,1000,#PB_Light_Point)

CreateCamera(0,0,0,100,100)
MoveCamera(0,0,0,1000)

CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)

MeshVertexPosition(-128,128,0)
MeshVertexTextureCoordinate(0,0)
MeshVertexPosition(128,128,0)
MeshVertexTextureCoordinate(0,1)
MeshVertexPosition(128,-128,0)
MeshVertexTextureCoordinate(1,1)
MeshVertexPosition(-128,-128,0)
MeshVertexTextureCoordinate(1,0)

MeshFace(2,1,0)
MeshFace(0,3,2)

FinishMesh(#True)

CreateCube(1,50)

CreateEntity(0,MeshID(0),#PB_Material_None)

CreateEntity(1,MeshID(1),#PB_Material_None)
MoveEntity(1,-130,0,0)

Repeat
  RenderWorld()
  FlipBuffers()
Until WindowEvent() = #PB_Event_CloseWindow


PB 5.70
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: How does light work ???

Post by Samuel »

Your mesh lacks vertex normals. https://www.purebasic.com/documentation ... ormal.html

This tutorial is for ray tracing, but it might help you understand how vertex normals work. https://www.scratchapixel.com/lessons/3 ... ng-normals
Papala
User
User
Posts: 38
Joined: Wed Sep 12, 2012 5:09 pm

Re: How does light work ???

Post by Papala »

Code: Select all

InitEngine3D()
InitSprite()

OpenWindow(0,0,0,500,500,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,500,500)

CreateLight(0,RGB(255,0,0),0,0,1000,#PB_Light_Point)

CreateCamera(0,0,0,100,100)
MoveCamera(0,0,0,1000)

CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)

MeshVertexPosition(-128,128,0)
MeshVertexTextureCoordinate(0,0)
MeshVertexNormal(0,0,1)
MeshVertexPosition(128,128,0)
MeshVertexTextureCoordinate(0,1)
MeshVertexPosition(128,-128,0)
MeshVertexTextureCoordinate(1,1)
MeshVertexPosition(-128,-128,0)
MeshVertexTextureCoordinate(1,0)

MeshFace(2,1,0)
MeshFace(0,3,2)

FinishMesh(#True)

CreateCube(1,50)

CreateEntity(0,MeshID(0),#PB_Material_None)

CreateEntity(1,MeshID(1),#PB_Material_None)
MoveEntity(1,-130,0,0)

Repeat
  RenderWorld()
  FlipBuffers()
Until WindowEvent() = #PB_Event_CloseWindow
Hoo so this is what normal use for !! Thx Samuel... I just don't understand anything with 3D ^^"
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: How does light work ???

Post by Olliv »

There is three ways to calculate normal manually :

1) by means of torks of adjacents segments of triangles (to simplify, imagine you are on the top of an umbrella)
Result is graduate (Phong)

2) by just tork calculating. The simplest. The eldest. Tiled result.

3) by... opposite of mean of adjacents segments (c'est pas garanti, no garanty, it is my own idea, do not try if beginning)

All 3 results might be normalized (maximum search cross product) to reduce maximum to 1.

For a Phong sphere, it is simpler : just substract vertices position by(<<-- added) sphere position and normalize.

Added : (3) same problem as Atan2 calculating (several quadrants) : virtual sphere around the vertex is divided by a virtual plane, and this last one is too complex to calculate, using heuristics. So, "North" and "South" should be okay, but "equator" of virtual sphere is unabled to be calculate, due to float precision limit to normalize.
Apologize for the third method which is false.
Post Reply