Page 1 of 1

I need little help with lighting effects

Posted: Thu Oct 04, 2012 11:04 pm
by Samuel
I've created a mesh and I'm trying to add the light to it, but it seems like some of the faces are not reacting properly to the lights.
As the Mesh rotates you'll notice that some of the faces are dark when they should be lit up like the others.
I am thinking it might have to do with the normalizing of the mesh, but I'm no expert on this.

Re: I need little help with lighting effects

Posted: Fri Oct 05, 2012 10:23 pm
by Comtois
may be this can help
http://purebasic.fr/english/viewtopic.php?t=11527
It is necessary To work clockwise Or against the clock. If you want To have both sides of a face visible, you will have To Define 2 faces in clockwise And opposite direction.

In your Case, you need only one face, choose the clockwise one.

Code: Select all

AddMeshFace(0,1,4)
AddMeshFace(0,4,1)
Then you can use NormalizeMesh(0)

Note : If you need both sides of a face visibles, you can use cull_hardware none in a materialScript.

Here an example showing how it work :

Code: Select all

Structure Vector3
  x.f
  y.f
  z.f
EndStructure

Macro SubVector3(n, v, w)
  n\x = v\x - w\x
  n\y = v\y - w\y
  n\z = v\z - w\z
EndMacro

Procedure Normalize(*V.Vector3)
  Define.f magSq, oneOverMag 
  
  magSq = *V\x * *V\x + *V\y * *V\y + *V\z * *V\z 
  If magsq > 0
    oneOverMag = 1.0 / Sqr(magSq)
    *V\x * oneOverMag
    *V\y * oneOverMag
    *V\z * oneOverMag
  EndIf  
  
EndProcedure

Procedure NormalFace(*n.Vector3, *v1.Vector3, *v2.Vector3, *v3.Vector3)
  Protected.Vector3 v2v1, v3v1
  SubVector3(v2v1, *v2, *v1)
  SubVector3(v3v1, *v3, *v1)
  
  *n\x = v2v1\y * v3v1\z - v2v1\z * v3v1\y
  *n\y = v2v1\z * v3v1\x - v2v1\x * v3v1\z
  *n\z = v2v1\x * v3v1\y - v2v1\y * v3v1\x
EndProcedure                

Define.Vector3 Normal, v1, v2, v3

;Calculate normal for this face --> AddMeshFace(0,4,1)
v1\x = -280 : v1\y = -84 : v1\z = 50 ; Vertex 0
v2\x = -143 : v2\y =  92 : v2\z = 50 ; Vertex 4
v3\x = -231 : v3\y = -16 : v3\z = 50 ; Vertex 1

NormalFace(@normal, @v1, @v2, @v3)
Normalize(@normal)

;Normal for vertices 0, 4 and 1
Debug normal\x
Debug normal\y
Debug normal\z

Re: I need little help with lighting effects

Posted: Sat Oct 06, 2012 12:01 am
by Samuel
Thanks for the link and example. After looking through all of that i learned several more things. I'll have to add the fixes later after work. Thanks again.

Re: I need little help with lighting effects

Posted: Sat Oct 06, 2012 9:47 am
by Comtois
to get a good result, you must read and follow this kind of calculation :)
http://www.emeyex.com/site/tuts/VertexNormals.pdf

Re: I need little help with lighting effects

Posted: Sat Oct 06, 2012 10:28 pm
by Samuel
Thanks, this would take forever without your help. You've made this a lot easier for me. :D

Re: I need little help with lighting effects

Posted: Fri Oct 12, 2012 12:30 am
by Samuel
After experimenting with these routines i seem to have a little problem. I'm trying to add normal to two different faces, but they both are coming up with the same result.
So only one of the faces is getting the lighting effects. The one face does seem to be working correctly though. Its just that i can't get the other faces to work right.

Code: Select all


;***********************************************
;PRESS ESCAPE TO EXIT PROGRAM
;***********************************************
Global x,y,z
Procedure CalculateNormalFace(x1,y1,z1,x2,y2,z2,x3,y3,z3)
    Norm_x1 = (y1 - y2) * (z2 - z3) - (z1 - z2) * (y2 - y3)
    Norm_y1 = (z1 - z2) * (x2 - x3) - (x1 - x2) * (z2 - z3)
    Norm_z1 = (x1 - x2) * (y2 - y3) - (y1 - y2) * (x2 - x3)
 
    Length = Sqr((Norm_x1*Norm_x1) + (Norm_y1*Norm_y1) + (Norm_z1*Norm_z1))
    If Length = 0 : Length = 1 : EndIf
x=Norm_x1 / Length
y=Norm_y1 / Length
z=Norm_z1 / Length
Debug x
Debug y
Debug z
EndProcedure

ExamineDesktops()

  InitKeyboard()
  InitMouse()
  
DeskWid=DesktopWidth(0)
DeskHei=DesktopHeight(0)

#CameraRotateSpeed=2
#CameraArrowSpeed=5
If OpenWindow(0, 0, 0, DeskWid, DeskHei, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
  If InitEngine3D()
  Else
    MessageRequester("Information", "ERROR///Could not InitEngine3D", 0) 
    End
  EndIf
  If  InitSprite()
  Else
    MessageRequester("Information", "ERROR///Could not InitSprite", 0)     
    End
  EndIf  

 If OpenWindowedScreen(WindowID(0), 0, 0, DeskWid, DeskHei, 0, 0, 0)

   
    CreateTexture(2, 512, 512)
    StartDrawing(TextureOutput(2))
       Box(0, 0, 512, 512 ,RGB(255,100,20))
    StopDrawing()
       
    CreateTexture(1, 512, 512)
    StartDrawing(TextureOutput(1))
       Box(0, 0, 512, 512 ,RGB(150,150,150))
    StopDrawing()
    CreateMaterial(2, TextureID(1))
    
     CreateMaterial(3, TextureID(2))
     MaterialAmbientColor(3, RGB(0,0,0))
     CreatePlane(3, 30000, 30000, 100, 100, 100, 100)
     CreateEntity(3,MeshID(3),MaterialID(3), 0, -1000, 0)
     
     CreateLight(0, RGB(250, 250, 250), 0, 0, 400)
;     WorldShadows(#PB_Shadow_Modulative, 600,RGB(150,150,150))
     
     CreateCamera(0, 0, 0, 100, 100)
     CameraLocate(0, 1250, 0, 100)
     CameraLookAt(0, 0, 0, 0)
     CameraBackColor(0, RGB(155, 155, 155)) 
     
     
If CreateMesh(0)     
AddMeshVertex(-210,176,50)    ;0
AddMeshVertex(203,176,50)     ;1
AddMeshVertex(203,-166,50)    ;2
AddMeshVertex(-210,-166,50)   ;3
AddMeshVertex(-210,176,-50)   ;4
AddMeshVertex(203,176,-50)    ;5
AddMeshVertex(203,-166,-50)   ;6
AddMeshVertex(-210,-166,-50)  ;7
;***********************************************************************************************************FIRST FACE*****************
AddMeshFace(0,2,1)
CalculateNormalFace(-210,176,50,203,176,50,203,-166,50)
MeshVertexNormal(x,y,z)
;***********************************************************************************************************SECOND FACE*************
AddMeshFace(4,5,6)
CalculateNormalFace(-210,176,-50,203,176,-50,203,-166,-50)
MeshVertexNormal(x,y,z)

AddMeshFace(2,0,3)
AddMeshFace(6,7,4)
AddMeshFace(0,1,4)
AddMeshFace(5,4,1)
AddMeshFace(1,2,5)
AddMeshFace(6,5,2)
AddMeshFace(2,3,6)
AddMeshFace(7,6,3)
AddMeshFace(3,0,7)
AddMeshFace(4,7,0)
FinishMesh()

MeshCreated=1 
;NormalizeMesh(0)
CreateEntity(1, MeshID(0), MaterialID(2))
EndIf
     
     
 Repeat
      Event = WaitWindowEvent(0) 

      If ExamineKeyboard()
        If KeyboardPushed(#PB_Key_Up);**********************Edits the variables for the camera to Zoom in
          KeyZ = -#CameraArrowSpeed 
        ElseIf KeyboardPushed(#PB_Key_Down);**********************Edits the variables for the camera to Zoom out
          KeyZ = #CameraArrowSpeed 
        Else
          KeyZ = 0
        EndIf
      EndIf
      
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)*#CameraRotateSpeed/2
        MouseY = -(MouseDeltaY()/10)*#CameraRotateSpeed/2
      EndIf
      
      If MeshCreated=1 
        MX=MX+MouseX
        MY=MY+MouseY
      RotateEntity(1, MY, MX, RollZ, #PB_Absolute)
      EndIf   
MoveCamera(0, KeyX, 0, KeyZ)
      RenderWorld()
      FlipBuffers()
      Delay(10)
      
  Until KeyboardPushed(#PB_Key_Escape)
 EndIf
EndIf    
End

Re: I need little help with lighting effects

Posted: Fri Oct 12, 2012 8:54 pm
by Comtois
keep the same vertex order, for AddMeshFace() and CalculateNormalFace()

Code: Select all

AddMeshFace(0,2,1)
CalculateNormalFace(-210,176,50,203,-166,50,203,176,50) 
MeshVertexNormal(x,y,z) should be added after AddMeshVertex(), for each vertex.

Re: I need little help with lighting effects

Posted: Fri Oct 12, 2012 11:21 pm
by Samuel
You saved me again. I made those changes and everything seems to be working now. Thank you for taking the time to help me.