Thread and Ogre3D

Just starting out? Need help? Post your questions and find answers here.
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 285
Joined: Thu Jul 09, 2015 9:07 am

Thread and Ogre3D

Post by pf shadoko »

Hi coders,

I have a problem
for v8 of my landscape series, i want to enlarge the terrain (to have the same depth of view as in my "mountains" demo)
until now all tiles were built in advance
in v8, the tiles are built in real time
to avoid latency, I want to do it in multithreading
but in opengl, the mesh creation in multithreading doesn't work

someone has a solution ?

Below is an example of multithreaded mesh creation (compile in opengl to see the crash)

Code: Select all

Procedure threadmesh(i)
	Shared Mutex
	;LockMutex(Mutex)
	CreateSphere(i,10+i*2)
	;UnlockMutex(Mutex)
	Debug "threadmesh "+i
EndProcedure


InitEngine3D():InitSprite():InitKeyboard():InitMouse()
OpenWindow(0, 0, 0, 0,0, "test",#PB_Window_Maximize)
ex=WindowWidth (0,#PB_Window_InnerCoordinate)
ey=WindowHeight(0,#PB_Window_InnerCoordinate)
OpenWindowedScreen(WindowID(0), 0, 0, ex, ey, 0, 0, 0)

CreateCamera(0, 0, 0, 100, 100):MoveCamera(0,0,0,-30):CameraLookAt(0,0,0,0)
CreateLight(0,$888888, 10000, 5000, 2000)
CameraBackColor(0,$220000) 
AmbientColor($444444)

ndt=4
Mutex = CreateMutex()
Dim thread(ndt-1)
For i=0 To ndt-1
	thread(i)=CreateThread(@ threadmesh(),i)
	Debug ""+thread(i)+" "+i
Next
For i=0 To ndt-1:WaitThread(thread(i)):Next
Debug "ok"

CreateTexture(0,256,256):StartDrawing(TextureOutput(0)):For i=0 To 1000:Circle(Random(255),Random(255),Random(10),Random($ffffff)):Next:StopDrawing()
CreateMaterial(0,TextureID(0)):ScaleMaterial(0,0.1,0.2)
CreateEntity(0,MeshID(0),MaterialID(0))

Define.f depx,depz,mousex,mousey,dist

Repeat
	WaitWindowEvent()
	ExamineKeyboard()
	ExamineMouse()
	depx=(-Bool(KeyboardPushed(#PB_Key_Left))+Bool(KeyboardPushed(#PB_Key_Right)))*0.1
	depz=(-Bool(KeyboardPushed(#PB_Key_Down))+Bool(KeyboardPushed(#PB_Key_Up   )))*0.1+MouseWheel()*2
	MouseX = -MouseDeltaX() *  0.05
	MouseY = -MouseDeltaY() *  0.05
	RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
	dist+(depz-dist)*0.05:MoveCamera  (0, depX, 0, -dist)
	RenderWorld()
	FlipBuffers()    
Until KeyboardReleased(#PB_Key_Escape) Or MouseButton(3)
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Thread and Ogre3D

Post by JHPJHP »

Hi pf shadoko,

Try the following.

Code: Select all

Enumeration #PB_Event_FirstCustomValue
  #EventThreadMesh
EndEnumeration

Procedure ThreadMesh(i)
	PostEvent(#EventThreadMesh, 0, 0, #PB_EventType_FirstCustomValue, i)
EndProcedure

InitEngine3D() : InitSprite() : InitKeyboard() : InitMouse()

If OpenWindow(0, 0, 0, 0, 0, "Thread Mesh", #PB_Window_Maximize)
  ex = WindowWidth (0, #PB_Window_InnerCoordinate)
  ey = WindowHeight(0, #PB_Window_InnerCoordinate)

  If OpenWindowedScreen(WindowID(0), 0, 0, ex, ey, 0, 0, 0)
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, -30)
    CameraLookAt(0, 0, 0, 0)
    CreateLight(0, $888888, 10000, 5000, 2000)
    CameraBackColor(0, $220000) 
    AmbientColor($444444)
    ndt = 4 : Dim thread(ndt - 1)

    For i = 0 To ndt - 1
      thread(i) = CreateThread(@ThreadMesh(), i)
    Next

    Define.f depx, depz, mousex, mousey, dist

    Repeat
    	Event = WaitWindowEvent()

    	Select Event
    	  Case #EventThreadMesh
          MeshRadius = EventData()
          CreateSphere(MeshRadius, 10 + MeshRadius * 2)
          CreateTexture(0, 256, 256)

          If StartDrawing(TextureOutput(0))
            For i = 0 To 1000
              Circle(Random(255), Random(255), Random(10), Random($ffffff))
            Next
            StopDrawing()
          EndIf
          CreateMaterial(0, TextureID(0))
          ScaleMaterial(0, 0.1, 0.2)
          CreateEntity(0, MeshID(0), MaterialID(0))
    	EndSelect
    	ExamineKeyboard()
    	ExamineMouse()
    	depx = (-Bool(KeyboardPushed(#PB_Key_Left)) + Bool(KeyboardPushed(#PB_Key_Right))) * 0.1
    	depz = (-Bool(KeyboardPushed(#PB_Key_Down)) + Bool(KeyboardPushed(#PB_Key_Up))) * 0.1 + MouseWheel() * 2
    	MouseX = -MouseDeltaX() * 0.05
    	MouseY = -MouseDeltaY() * 0.05
    	RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
    	dist + (depz - dist) * 0.05
    	MoveCamera(0, depX, 0, -dist)
    	RenderWorld()
    	FlipBuffers()    
    Until KeyboardReleased(#PB_Key_Escape) Or MouseButton(3)
  EndIf
EndIf
Last edited by JHPJHP on Thu Jul 22, 2021 12:16 am, edited 1 time in total.
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 285
Joined: Thu Jul 09, 2015 9:07 am

Re: Thread and Ogre3D

Post by pf shadoko »

Hi JHPJHP,

Thanks for your answer, but it doesn't solve my problem
your modification allows to know when the thread is finished (in my example I use WaitThread)
I'm afraid that the problem is related to Ogre3D
I'm surprised that it works under DX (at least for me)
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Thread and Ogre3D

Post by JHPJHP »

Hi pf shadoko,

I believe the following link is related to the same issue you're facing; sorry, not a solution.

Engine 3D Multi-threading
Post Reply