Page 1 of 1

AttachEntityObject not work

Posted: Sun Mar 01, 2015 7:09 pm
by Didaktik
I am creating a cube and want him to attach the text.
However, the team is working n. Causes a memory error. :shock:

AttachEntityObject(cubes()\Entity, "", Text3DID(cubes()\text))

What could be wrong? I copied the code from the examples and there he worked.

Perhaps the fact that I create a cube to manually and there are no bones?



Here is the code of the program. Press the spacebar:

Code: Select all


Declare DrawCube()


Define.f KeyX, KeyY, CameraSpeed = 0.1

Enumeration
  #mesh
  #entity
  #tex
  #tex2
  #tex3
  #tex4
  #tex5
  #texWhite
  #light
  #camera
  #sphere
  
EndEnumeration

Structure cube
  
  x.f
  y.f
  z.f
  
  Mesh.l
  Entity.l
  text.l
  
  scale_x.f
  scale_y.f
  scale_z.f
  
  rotate_x.f
  rotate_y.f
  rotate_z.f
  
  Material.l
  
  Texture.l[6]
  Texture_x1.f[6]
  Texture_y1.f[6]
  
  Texture_x2.f[6]
  Texture_y2.f[6]
  
  texture_scale.f[6]
  texture_x.l[6]
  texture_y.l[6]
  
EndStructure

Global NewList cubes.cube()

Global cameraFOV.f = 45
Global scaleGlobal.f = 1
Global rotateGlobal.f = 0

Procedure AddCube()
  
  AddElement(cubes())
  
  cubes()\scale_x = 1
  cubes()\scale_y = 1
  cubes()\scale_z = 1
  
  cubes()\rotate_x = 0
  cubes()\rotate_y = 45
  cubes()\rotate_z = 0
  
  cubes()\Mesh = CreateMesh(#PB_Any, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
  
  ;pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  ;subMesh 0 ; Front Side
  MeshVertexPosition(-1,-1,1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(0, 0)
  
  MeshVertexPosition(1,-1,1)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(1, 0)
  
  MeshVertexPosition(1,1,1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(1, 1)
  
  
  MeshVertexPosition(-1,1,1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(0, 1)
  
  MeshFace(0, 1, 2)
  MeshFace(2,3,0)
  
  ;ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
  ;subMesh 1 ; right side
  AddSubMesh(#PB_Mesh_TriangleList)
  MeshVertexPosition(1,-1,1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(0, 0)
  
  MeshVertexPosition(1,-1,-1)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(1, 0)
  
  MeshVertexPosition(1,1,-1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(1, 1)
  
  
  MeshVertexPosition(1,1,1);;;;;;
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(0, 1)
  
  MeshFace(0, 1, 2)
  MeshFace(2,3,0)
  
  
  ;;ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
  ;subMesh 2  ; back side
  AddSubMesh(#PB_Mesh_TriangleList)
  MeshVertexPosition(1,-1,-1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(0, 0)
  
  MeshVertexPosition(-1,-1,-1)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(1, 0)
  
  MeshVertexPosition(-1,1,-1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(1, 1)
  
  
  MeshVertexPosition(1,1,-1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(0, 1)
  
  MeshFace(0, 1, 2)
  MeshFace(2,3,0)   
  
  
  ;000000000000000000000000000000000000000000000000000000000
  ;subMesh 3  ; Left side
  AddSubMesh(#PB_Mesh_TriangleList)
  MeshVertexPosition(-1,-1,-1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(0, 0)
  
  MeshVertexPosition(-1,-1,1)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(1, 0)
  
  MeshVertexPosition(-1,1,1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(1, 1)
  
  MeshVertexPosition(-1,1,-1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(0, 1)
  
  MeshFace(0, 1, 2)
  MeshFace(2,3,0) 
  
  
  ;000000000000000000000000000000000000000000000000000000000
  ;subMesh 4  ; Upper side
  AddSubMesh(#PB_Mesh_TriangleList)
  MeshVertexPosition(-1,1,1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(0, 0)
  
  MeshVertexPosition(1,1,1)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(1, 0)
  
  MeshVertexPosition(1,1,-1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(1, 1)
  
  MeshVertexPosition(-1,1,-1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(0, 1)
  
  MeshFace(0, 1, 2)
  MeshFace(2,3,0) 
  
  
  ;000000000000000000000000000000000000000000000000000000000
  ;subMesh 5  ; Bottom side
  AddSubMesh(#PB_Mesh_TriangleList)
  MeshVertexPosition(-1,-1,-1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(0, 0)
  
  MeshVertexPosition(1,-1,-1)
  MeshVertexNormal(0,1,0)
  MeshVertexTextureCoordinate(1, 0)
  
  MeshVertexPosition(1,-1,1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(1, 1)
  
  MeshVertexPosition(-1,-1,1)
  MeshVertexNormal(0,1,0) 
  MeshVertexTextureCoordinate(0, 1)
  
  MeshFace(0, 1, 2)
  MeshFace(2,3,0)  
  
  
  FinishMesh(#True)
  
  cubes()\Entity = CreateEntity(#PB_Any, MeshID(cubes()\Mesh),  MaterialID(#tex) ,0,0,0,  - 1 << 1)
  
  For n=0 To 5
    cubes()\Texture[n] = #tex5
  Next n
  
  For n=0 To 5
    SetEntityMaterial(cubes()\Entity, MaterialID( cubes()\Texture[n] ), n)
  Next n
  
  
  RotateEntity(cubes()\Entity, 0,45, 0, #PB_Absolute)
  
  cubes()\text = CreateText3D(#PB_Any, "Hello world")
  
  Debug cubes()\text
  
  Text3DColor(cubes()\text, RGBA(255, 0, 0, 255))
  Text3DAlignment(cubes()\text, #PB_Text3D_HorizontallyCentered)
  AttachEntityObject(cubes()\Entity, "", Text3DID(cubes()\text))
  
EndProcedure

Procedure CubeFound(Entity)
  
  If Entity > 0
    
    ResetList(cubes())
    
    While NextElement(cubes())
      
      If Entity = cubes()\Entity
        
        ProcedureReturn 1
        
      EndIf
      
    Wend
    
  EndIf  
  
  ProcedureReturn 0
  
EndProcedure

Procedure CubeSetMaterial(Entity, Material)
  
  If CubeFound(Entity)
    
    For n=0 To 5
      SetEntityMaterial(cubes()\Entity, MaterialID( Material ), n)
    Next n
    
  EndIf     
  
EndProcedure

Procedure CubeResetMaterial(Entity)
  
  If CubeFound(Entity)
    
    For n=0 To 5
      SetEntityMaterial(cubes()\Entity, MaterialID( cubes()\Texture[n] ), n)
    Next n
    
  EndIf     
  
EndProcedure

Procedure CubeMove(Entity, x, y)
  
  If CubeFound(Entity)
    
    cubes()\x + (0.01 * x)
    cubes()\y + (-0.01 * y)
    
    MoveEntity( cubes()\Entity, cubes()\x, cubes()\y, cubes()\z, #PB_Absolute)
    
  EndIf
  
EndProcedure

Procedure CubesGlobal()
  
  ResetList(cubes())
  
  While NextElement(cubes())
    
    ScaleEntity( cubes()\Entity, cubes()\scale_x*scaleGlobal, cubes()\scale_y*scaleGlobal, cubes()\scale_z*scaleGlobal, #PB_Absolute) 
    RotateEntity( cubes()\Entity, cubes()\rotate_x, cubes()\rotate_y+rotateGlobal, cubes()\rotate_z, #PB_Absolute) 
    
  Wend
  
EndProcedure



InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()

ExamineDesktops()

OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"Cube with different textures",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)


Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/fonts", #PB_3DArchive_FileSystem)

KeyboardMode(#PB_Keyboard_AllowSystemKeys)

CreateMaterial(#texWhite, LoadTexture(#texWhite, "white.jpg"))
MaterialBlendingMode(#texWhite, #PB_Material_AlphaBlend)
SetMaterialColor(#texWhite, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 50))
CreateMaterial(#tex, LoadTexture(#tex, "Geebee2.bmp"))
MaterialCullingMode(#tex, #PB_Material_NoCulling)

CreateMaterial(#tex2, LoadTexture(#tex2, "Wood.jpg"))
MaterialBlendingMode(#tex2, #PB_Material_AlphaBlend)
SetMaterialColor(#tex2, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 255))

CreateMaterial(#tex4, LoadTexture(#tex4, "DosCarte.png"))
MaterialCullingMode(#tex4, #PB_Material_NoCulling)

;CreateMaterial(#tex5, LoadTexture(#tex5, "ValetCoeur.jpg"))
CreateMaterial(#tex5, LoadTexture(#tex5, "Caisse.png"))
; MaterialCullingMode(#tex5, #PB_Material_NoCulling)
; MaterialBlendingMode(#tex5, #PB_Material_AlphaBlend)
; SetMaterialColor(#tex5, #PB_Material_DiffuseColor, RGBA(255, 255, 255, 120))


; MaterialCullingMode(#tex2, #PB_Material_NoCulling)
; CreateMaterial(#tex3, LoadTexture(#tex3, "MRAMOR6X6.jpg"))
; 
; CreatePlane(500, 30, 30, 5, 5, 2, 2)
; CreateEntity(500, MeshID(500), MaterialID(#tex3), 0,-6,0)

CreateLight(0,RGB(245,245,205),6,13,0)

;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,0, 10,#PB_Absolute)
CameraLookAt(0, 0, 0, 0)
;CameraBackColor(0, RGB(0,200,200))

;AmbientColor(RGB(100,100,100))

;WorldShadows(#PB_Shadow_Modulative  , -1, RGB(100, 100, 100))
;WorldShadows(#PB_Shadow_Additive )

;-Mesh
;DrawCube()

CreateSphere(#sphere, 0.5)
CreateEntity(#sphere,MeshID(#sphere), #PB_Material_None, 0,0,0 )



; CreateText3D(0, "Camera FOV: ")
; Text3DColor(0, RGBA(255, 0, 0, 255))
; Text3DAlignment(0, #PB_Text3D_Left)

OpenWindow3D(0, 0, 0, 400, 40, "More gadgets", #PB_Window3D_BorderLess)

TextGadget3D(0, 0, 0, 200, 20, "FOV:")
TextGadget3D(1, 0, 20, 200, 20, "FOV:")
CloseGadgetList3D()

SetGadgetText3D(0, "FOV: " + Str(cameraFOV))
SetGadgetText3D(1, "Scale: " + Str(cameraFOV))

ShowGUI(255, 1)

Repeat
  WindowEvent()
  
  If ExamineMouse()
    
    InputEvent3D(MouseX(), MouseY(), MouseButton(#PB_MouseButton_Left))
    
  EndIf
  
  event = WindowEvent3D()
  
  ; If ExamineMouse()
  ; MouseX = -MouseDeltaX() * CameraSpeed * 0.7
  ; MouseY = -MouseDeltaY() * CameraSpeed * 0.7
  ; EndIf
  ;ShowCursor_(0)
  
  ; Use arrow keys and mouse to rotate camera and fly in/out
  ExamineKeyboard()
  
  If KeyboardPushed(#PB_Key_Q)
    
    CameraFOV(0, cameraFOV)
    cameraFOV + 0.1
    SetGadgetText3D(0, "FOV: " + Str(cameraFOV))
    
  ElseIf KeyboardPushed(#PB_Key_A)
    
    CameraFOV(0, cameraFOV)
    cameraFOV - 0.1
    SetGadgetText3D(0, "FOV: " + Str(cameraFOV))
    
  EndIf
  
  If KeyboardPushed(#PB_Key_Z)
    
    scaleGlobal + 0.01
    SetGadgetText3D(1, "Scale: " + StrF(scaleGlobal))
    
  ElseIf KeyboardPushed(#PB_Key_X)
    
    scaleGlobal - 0.01
    SetGadgetText3D(1, "Scale: " + StrF(scaleGlobal))
    
  EndIf
  
  
  
  If KeyboardPushed(#PB_Key_W)
    
    If CubeFound(last_pick_entity)
      cubes()\scale_x - 0.01
      cubes()\scale_y - 0.01
      cubes()\scale_z - 0.01
      RotateEntity( cubes()\Entity, cubes()\scale_x, cubes()\scale_y, cubes()\scale_z, #PB_Absolute)
    EndIf
    
  ElseIf KeyboardPushed(#PB_Key_S)
    
    If CubeFound(last_pick_entity)
      cubes()\scale_x + 0.01
      cubes()\scale_y + 0.01
      cubes()\scale_z + 0.01
      RotateEntity( cubes()\Entity, cubes()\scale_x, cubes()\scale_y, cubes()\scale_z, #PB_Absolute)
    EndIf
    
  EndIf
  
  
  
  If KeyboardPushed(#PB_Key_E)
    
    If CubeFound(last_pick_entity)
      cubes()\rotate_y - 0.1
      RotateEntity( cubes()\Entity, cubes()\rotate_x, cubes()\rotate_y, cubes()\rotate_z, #PB_Absolute)
    EndIf
    
  ElseIf KeyboardPushed(#PB_Key_D)
    
    If CubeFound(last_pick_entity)
      cubes()\rotate_y + 0.1
      RotateEntity( cubes()\Entity, cubes()\rotate_x, cubes()\rotate_y, cubes()\rotate_z, #PB_Absolute)
    EndIf 
    
  EndIf
  
  If KeyboardPushed(#PB_Key_R)
    
    If CubeFound(last_pick_entity)
      cubes()\rotate_x - 0.1
      RotateEntity( cubes()\Entity, cubes()\rotate_x, cubes()\rotate_y, cubes()\rotate_z, #PB_Absolute)
    EndIf
    
  ElseIf KeyboardPushed(#PB_Key_F)
    
    If CubeFound(last_pick_entity)
      cubes()\rotate_x + 0.1
      RotateEntity( cubes()\Entity, cubes()\rotate_x, cubes()\rotate_y, cubes()\rotate_z, #PB_Absolute)
    EndIf 
    
  EndIf
  
  If KeyboardPushed(#PB_Key_T)
    
    If CubeFound(last_pick_entity)
      cubes()\rotate_z - 0.1
      RotateEntity( cubes()\Entity, cubes()\rotate_x, cubes()\rotate_y, cubes()\rotate_z, #PB_Absolute)
    EndIf
    
  ElseIf KeyboardPushed(#PB_Key_G)
    
    If CubeFound(last_pick_entity)
      cubes()\rotate_z + 0.1
      RotateEntity( cubes()\Entity, cubes()\rotate_x, cubes()\rotate_y, cubes()\rotate_z, #PB_Absolute)
    EndIf 
    
  EndIf
  
  
  
  ; If KeyboardPushed(#PB_Key_Left)
  ; KeyX.f = -CameraSpeed
  ; ElseIf KeyboardPushed(#PB_Key_Right)
  ; KeyX.f = CameraSpeed
  ; Else
  ; KeyX = 0
  ; EndIf
  ; 
  ; If KeyboardPushed(#PB_Key_Up)
  ; KeyY = -CameraSpeed
  ; ElseIf KeyboardPushed(#PB_Key_Down)
  ; KeyY = CameraSpeed
  ; Else
  ; KeyY = 0
  ; EndIf
  ; 
  ; If KeyboardReleased(#PB_Key_W)
  ; If wireFrame
  ; MaterialShadingMode(#tex, #PB_Material_Wireframe)
  ; MaterialShadingMode(#tex2, #PB_Material_Wireframe)
  ; MaterialShadingMode(#tex5, #PB_Material_Wireframe)
  ; MaterialShadingMode(#tex4, #PB_Material_Wireframe)
  ; wireFrame ! 1
  ; Else 
  ; MaterialShadingMode(#tex, #PB_Material_Solid)
  ; MaterialShadingMode(#tex2, #PB_Material_Solid)
  ; MaterialShadingMode(#tex5, #PB_Material_Solid)
  ; MaterialShadingMode(#tex4, #PB_Material_Solid)
  ; wireFrame ! 1
  ; EndIf
  ; EndIf
  ; 
  If KeyboardReleased(#PB_Key_Space) ;press key to toggle the rotation of the object
    ;    rot ! 1
    
    AddCube()
  EndIf
  
  If KeyboardReleased(#PB_Key_Return) And rotate = 0 ;press key to toggle the rotation of the object
    
    rotate = 1
    
  EndIf
  
  If rotate
    
    rotateGlobal + 5
    rotateCount + 5
    
    If rotateCount >= 90
      
      rotate = 0
      rotateCount = 0
      
      If rotateGlobal >= 360
        
        rotateGlobal = rotateGlobal - 360
        
      EndIf
      
    EndIf
    
  EndIf
  
  ; 
  ; 
  ; RotateEntity(0, 0, rot, 0,#PB_Relative)
  
  Entity = MouseRayCast(0, MouseX(), MouseY(), - 1 << 1)
  
  
  
  ;If Entity <> last_select_entity
  
  
  
  
  mousebutton = MouseButton(#PB_MouseButton_Left)
  
  If mousebutton = 0
    
    CubeResetMaterial(last_pick_entity)
    CubeSetMaterial(Entity, #tex4)
    last_pick_entity = Entity
    
  ElseIf mousebutton And last_pick_entity > 0
    
    CubeMove(last_pick_entity, MouseDeltaX(), MouseDeltaY())
    
  EndIf
  
  
  ; If Entity
  ; 
  ; CubeResetMaterial(last_select_entity)
  ; last_select_entity = Entity 
  ; 
  ; EndIf
  ; 
  ;EndIf
  
  
  
  
  
  ; Debug MouseX()
  
  ; RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
  ; MoveCamera  (0, KeyX, 0, KeyY)
  
  CubesGlobal()
  
  RenderWorld()
  
  
  
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
 



Re: AttachEntityObject not work

Posted: Sun Mar 01, 2015 7:41 pm
by falsam
Add Parse3DScripts() :)

Code: Select all

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/fonts", #PB_3DArchive_FileSystem)
Parse3DScripts()

Re: AttachEntityObject not work

Posted: Mon Mar 02, 2015 9:23 am
by Bananenfreak
Parse3DScripts() is a false friend. It should be marked in the documentation. With big red letters and a text: If you have a Problem, check Parse3DScripts() first. Even programer with knowledge forget this ****.