Page 1 of 1

How does light work ???

Posted: Fri Jan 11, 2019 3:13 pm
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

Re: How does light work ???

Posted: Fri Jan 11, 2019 7:21 pm
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

Re: How does light work ???

Posted: Fri Jan 11, 2019 11:16 pm
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 ^^"

Re: How does light work ???

Posted: Sun Jan 13, 2019 1:20 pm
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.