Page 1 of 1

SetMeshMaterial

Posted: Tue Sep 04, 2012 5:18 pm
by Polo
Does this function really work with submeshes?

I'm trying to use it but the final mesh remains white.
If I specify the same material when attaching the mesh to the entity then the mesh is textured.

I'm basically doing:

Code: Select all

mesh=CreateMesh(#PB_Any)
AddSubMesh()
SetMeshMaterial(mesh,materialID,0)

-- vertices/faces --


FinishMesh()
UpdateMeshBoundingBox(mesh)
entity = CreateEntity(#PB_Any,MeshID(mesh),#PB_Material_None)
Wherever I put the SetMeshMaterial (before or after adding faces) it doesn't change anything.

Re: SetMeshMaterial

Posted: Tue Sep 04, 2012 5:30 pm
by Comtois
Try this way, after FinishMesh()

Code: Select all

mesh=CreateMesh(#PB_Any)
AddSubMesh()

-- vertices/faces --


FinishMesh()

SetMeshMaterial(mesh,materialID,0)

UpdateMeshBoundingBox(mesh)


entity = CreateEntity(#PB_Any,MeshID(mesh),#PB_Material_None)

Re: SetMeshMaterial

Posted: Tue Sep 04, 2012 6:25 pm
by Polo
Thanks it works! This should be documented if possible :)

Re: SetMeshMaterial

Posted: Tue Sep 04, 2012 7:51 pm
by Comtois
I will add an example, may be shorter than this one :)

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - SetMeshMaterial
;
;    (c) 2012 - Fantaisie Software
;
; ------------------------------------------------------------
;

#CameraSpeed = 1

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY

Define.f x, y, z, nx, ny, nz, u, v
Define.l Co
Define.w t1, t2, t3

If InitEngine3D()
  
  Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    ; Create a cube manually
    ; 
    
    ; Define all the vertices and their attributes
    CreateMesh(0)
    
    AddMeshVertex(-0.5,0.5,-0.5)
      MeshVertexTextureCoordinate(0,0)
    AddMeshVertex(0.5,0.5,-0.5)
      MeshVertexTextureCoordinate(0,1)
    AddMeshVertex(0.5,0.5,0.5)
      MeshVertexTextureCoordinate(1,1)
    AddMeshVertex(-0.5,0.5,0.5)
      MeshVertexTextureCoordinate(1,0)
      
    ; Define all the faces, based on the vertex index
    AddMeshFace(2,1,0)
    AddMeshFace(0,3,2)  
    
    AddSubMesh()      
    AddMeshVertex(-0.5,-0.5,0.5)
      MeshVertexTextureCoordinate(0,0)
    AddMeshVertex(0.5,-0.5,0.5)
      MeshVertexTextureCoordinate(0,1)
    AddMeshVertex(0.5,-0.5,-0.5)
      MeshVertexTextureCoordinate(1,1)
    AddMeshVertex(-0.5,-0.5,-0.5)
      MeshVertexTextureCoordinate(1,0)
      
    ; Define all the faces, based on the vertex index
    AddMeshFace(2,1,0)
    AddMeshFace(0,3,2) 
    
    AddSubMesh()
    AddMeshVertex(-0.5,0.5,0.5)
      MeshVertexTextureCoordinate(0,0)
    AddMeshVertex(0.5,0.5,0.5)
      MeshVertexTextureCoordinate(0,1)
    AddMeshVertex(0.5,-0.5,0.5)
      MeshVertexTextureCoordinate(1,1)
    AddMeshVertex(-0.5,-0.5,0.5)
      MeshVertexTextureCoordinate(1,0)
      
    ; Define all the faces, based on the vertex index
    AddMeshFace(2,1,0)
    AddMeshFace(0,3,2)       
      
    AddSubMesh()  
    AddMeshVertex(0.5,0.5,-0.5)
      MeshVertexTextureCoordinate(0,0)
    AddMeshVertex(-0.5,0.5,-0.5)
      MeshVertexTextureCoordinate(0,1)
    AddMeshVertex(-0.5,-0.5,-0.5)
      MeshVertexTextureCoordinate(1,1)
    AddMeshVertex(0.5,-0.5,-0.5)
      MeshVertexTextureCoordinate(1,0)
      
    ; Define all the faces, based on the vertex index
    AddMeshFace(2,1,0)
    AddMeshFace(0,3,2)       
      
    AddSubMesh()
    AddMeshVertex(-0.5,0.5,-0.5)
      MeshVertexTextureCoordinate(0,0)
    AddMeshVertex(-0.5,0.5,0.5)
      MeshVertexTextureCoordinate(0,1)
    AddMeshVertex(-0.5,-0.5,0.5)
      MeshVertexTextureCoordinate(1,1)
    AddMeshVertex(-0.5,-0.5,-0.5)
      MeshVertexTextureCoordinate(1,0)
      
    ; Define all the faces, based on the vertex index
    AddMeshFace(2,1,0)
    AddMeshFace(0,3,2)       
            
    AddSubMesh()
    AddMeshVertex(0.5,0.5,0.5)
      MeshVertexTextureCoordinate(0,0)
    AddMeshVertex(0.5,0.5,-0.5)
      MeshVertexTextureCoordinate(0,1)
    AddMeshVertex(0.5,-0.5,-0.5)
      MeshVertexTextureCoordinate(1,1)
    AddMeshVertex(0.5,-0.5,0.5)
      MeshVertexTextureCoordinate(1,0)
      
    ; Define all the faces, based on the vertex index
    AddMeshFace(2,1,0)
    AddMeshFace(0,3,2)      
            
    FinishMesh()
    NormalizeMesh(0) 
    
    UpdateMeshBoundingBox(0)
    
    ; Material
    CreateMaterial(0, LoadTexture(0, "Geebee2.bmp"))
    CreateMaterial(1, LoadTexture(1, "RustySteel.jpg"))
    CreateMaterial(2, LoadTexture(2, "Dirt.jpg"))
    CreateMaterial(3, LoadTexture(3, "Clouds.jpg"))
    CreateMaterial(4, LoadTexture(4, "Wood.jpg"))
    CreateMaterial(5, LoadTexture(5, "caisse.png"))
    
    SetMeshMaterial(0, MaterialID(0), 0)
    SetMeshMaterial(0, MaterialID(1), 1)
    SetMeshMaterial(0, MaterialID(2), 2)
    SetMeshMaterial(0, MaterialID(3), 3)
    SetMeshMaterial(0, MaterialID(4), 4)
    SetMeshMaterial(0, MaterialID(5), 5)
    
    CreateEntity(0, MeshID(0), #PB_Material_None)
    ScaleEntity(0, 200, 200, 200)
    
    CreateCamera(0, 0, 0, 100, 100)
    CameraLocate(0, 0, 0, 1000)
    
    CreateLight(0, RGB(255,255,255), 300, 600, -100)
    AmbientColor(RGB(80, 80, 80))
    
    Repeat
      Screen3DEvents()
      
      ExamineMouse()
      
      ExamineKeyboard()
              
      RotateEntity(0, 1, 1, 1, #PB_Relative)
       
      RenderWorld()
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Re: SetMeshMaterial

Posted: Tue Sep 04, 2012 8:29 pm
by Polo
Great! :)

Re: SetMeshMaterial

Posted: Fri Feb 06, 2015 10:25 am
by royaltoon55
what direction does the light look? And is there a way to rotate the light?
Other questions I have: If the light is attached to a node and the node is moved/rotated, the light is moved and rotated? (yep, in the manual they say that nodes are always rotated/moved relative to the node they're attached to, but does this work for lights, too?