Another simple Static geometry example...

Everything related to 3D programming
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Another simple Static geometry example...

Post by DK_PETER »

If your want to try it, save these two images (requires PB 5.20)
in a folder named: 'textures'

images removed..

save the code at the same location as you created the 'textures' folder..

Code: Select all

;Example: Simple static Geometry 2
;This example shows how to use CreateStaticGeometry()
;If you want to add a vast number of objects to your scene,
;which are always stationary.
;Once the Static Geometry has been build, it is no longer
;possible to move or rotate the Entities.
;This example requires PB 5.20
;By DK_PETER

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

UseJPEGImageDecoder()
UsePNGImageDecoder()


Add3DArchive("textures",#PB_3DArchive_FileSystem)

DeclareModule _3D_Geo
	
	Structure Geo_Data
		ID.i
		Mesh.i
		Tex.i
		Mat.i
		ent.i
	EndStructure
	
	
	Global CamSpeed = 10
	Global MouseSpeed = 10
	Global DefaultCam.i
	Global Win.i, Light.i
	Global earth.Geo_Data
	Global Geo.Geo_Data
	Global cl.Geo_Data
	Global deadworld.Geo_Data
	
	Declare SetupWinScreen(Width.i, Height.i, Title.s = "")
	Declare MakeStaticGeo()
	Declare MakeTestTerrain()
	Declare CreateClouds()
	Declare CreateDeadWorld()
	Declare Do_Mouse()
	Declare Do_Keyboard()
	
EndDeclareModule

Module _3D_Geo
	
	Procedure SetupWinScreen(Width.i, Height.i, Title.s = "") ; Setup screen, Get Dimensions, and set a default camera

		Win = OpenWindow(#PB_Any, 0, 0, Width, Height, Title, #PB_Window_SystemMenu )
		OpenWindowedScreen(WindowID(Win), 0, 0, Width, Height, 0, 0, 0,#PB_Screen_SmartSynchronization)
		
		;Create camera
		DefaultCam = CreateCamera(#PB_Any,0,0,100,100) 
		MoveCamera(DefaultCam,2000,550,2000)	
		CameraLookAt(DefaultCam,1000,0,1000)
		CameraProjectionMode(DefaultCam, #PB_Camera_Perspective)
		
		;Setup light
		light = CreateLight(#PB_Any ,RGB(210, 200, 200), 6000, 2200, 2000,#PB_Light_Directional)
		SetLightColor(light, #PB_Light_SpecularColor, RGB(220, 200, 200)) 
		AmbientColor(RGB(250, 250, 250))
	EndProcedure
	
	Procedure MakeTestTerrain()
		
		earth\Mesh = CreatePlane(#PB_Any,22000,22000,1,1,1,1) 
		earth\Tex = CreateTexture(#PB_Any,512,512)
		
		StartDrawing(TextureOutput(earth\Tex))
		Box(0,0,512,512,$444444)
		For y = 0 To 512 Step 16
			For x = 0 To 512 Step 16
				LineXY(0,y,512,y,$999999)
				LineXY(x,0,x,512,$999999)
			Next x
		Next y
		StopDrawing()
		
		earth\Mat = CreateMaterial(#PB_Any,TextureID(earth\Tex))
		ScrollMaterial(earth\Mat,0,0.004,#PB_Material_Animated)
		earth\ID 	= CreateEntity(#PB_Any,MeshID(earth\Mesh),MaterialID(earth\Mat))
		MoveEntity(earth\ID,11000,0,11000,#PB_Absolute)
		
	EndProcedure		
		
	Procedure MakeStaticGeo() ; Static geometry can't be moved or rotated when created.
		;Let's create a large city
		geo\Mesh = CreateCube(#PB_Any, 50)
		geo\Tex  = CreateTexture(#PB_Any, 512, 512)
		
		StartDrawing(TextureOutput(geo\Tex))
		Box(0, 0, 512, 512, $868988)
		Box(0, 0, 512, 60 , $5E5F60)
		For y = 64 To 512 Step 32
			For x = 0 To 512 Step 32
				Box(x,y,16,15,$E1BF3F)
			Next x
		Next y
		StopDrawing()
		
		geo\Mat = CreateMaterial(#PB_Any, TextureID(geo\Tex))
		geo\ent = CreateEntity(#PB_Any, MeshID(geo\Mesh), MaterialID(geo\Mat))
		
		;Create static geometry city...
		geo\ID = CreateStaticGeometry(#PB_Any,20100,20100,20100,#True)
		For z = 0 To 20000 Step 200
			For x = 0 To 20000 Step 200
				newsizey 	= Random(10,1)
				entnewval = newsizey * 25
				AddStaticGeometryEntity(geo\ID, EntityID(geo\ent), x, entnewval, z, 1, newsizey, 1)
			Next x
		Next z
		
		BuildStaticGeometry(geo\ID)
	EndProcedure	
	
	Procedure CreateClouds()
		
	  cl\mesh = CreatePlane(#PB_Any,2000, 2000, 10, 10, 2, 2)
	  cl\tex  = LoadTexture(#PB_Any, "clouds.jpg")
	  cl\mat  = CreateMaterial(#PB_Any, TextureID(cl\tex))
	  AddMaterialLayer(cl\Mat, TextureID(cl\tex), #PB_Material_Add) 
	  MaterialBlendingMode(cl\mat,#PB_Material_Color)
	  RotateMaterial(cl\mat, 0.001, #PB_Material_Animated|#PB_Material_Fixed)
	  RotateMaterial(cl\mat, 0.004, #PB_Material_Animated|#PB_Material_Fixed,1)
	  SetMeshMaterial(cl\mesh, MaterialID(cl\mat))
	  cl\Ent = CreateEntity(#PB_Any, MeshID(cl\mesh), MaterialID(cl\mat))
	  MoveEntity(cl\Ent, 0, 5000, 2000)
	  ;RotateEntity(cl\Ent,0, 0, 0) ;Clouds facing upwards
	  RotateEntity(cl\Ent,0,0,180) ;Clouds facing downwards
	  ScaleEntity(cl\Ent,100,100,100)

	EndProcedure
	
	Procedure CreateDeadWorld()
		deadworld\Mesh = CreateSphere(#PB_Any, 200,40,40)
		deadworld\Tex = LoadTexture(#PB_Any,"deadworld.png")
		deadworld\Mat = CreateMaterial(#PB_Any, TextureID(deadworld\Tex))
		deadworld\ent = CreateEntity(#PB_Any,MeshID(deadworld\Mesh), MaterialID(deadworld\Mat))
		MoveEntity(deadworld\ent, 6000,5000,6000)
	EndProcedure
	Procedure Do_Mouse()
		
		ExamineMouse()
		MouseX = -MouseDeltaX() * MouseSpeed * 0.01
		MouseY = -MouseDeltaY() * MouseSpeed * 0.01
		RotateCamera(DefaultCam, MouseY, MouseX, 0, #PB_Relative)
		
	EndProcedure		
	
	Procedure Do_Keyboard()
		
		ExamineKeyboard()
		
		If KeyboardPushed(#PB_Key_A)
			KeyX = 0 - CamSpeed 
		EndIf
		If KeyboardPushed(#PB_Key_D)
			KeyX = CamSpeed 
		EndIf
		
		If KeyboardPushed(#PB_Key_W)
			KeyY = 0 - CamSpeed
		EndIf
		
		If KeyboardPushed(#PB_Key_S)
			KeyY = CamSpeed
		EndIf
		
		MoveCamera(DefaultCam, KeyX, 0, KeyY)
		
		If Not KeyboardReleased(#PB_Key_Escape)
			KeyX = 0: KeyY = 0
		EndIf
	EndProcedure	
	
EndModule


_3D_Geo::SetupWinScreen(1024,768)
_3D_Geo::MakeTestTerrain()
_3D_Geo::MakeStaticGeo()
_3D_Geo::CreateClouds()
_3D_Geo::CreateDeadWorld()

Repeat
	
	ev = WindowEvent()
	
	RotateEntity(_3D_Geo::deadworld\ent,0.1,0,0,#PB_Relative)
	
	elaps = RenderWorld()
	
	
	_3D_Geo::Do_Mouse()
	_3D_Geo::Do_Keyboard()
	
	FlipBuffers()
	
Until  KeyboardPushed(#PB_Key_Escape)
Best regards
Peter
Last edited by DK_PETER on Sun Aug 10, 2014 5:32 am, edited 4 times in total.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Another simple Static geometry example...

Post by Tenaja »

I tried to run this, but it sits for about 15 seconds (even very long files compile nearly instantly) and then errors because in this line...
cl\tex = LoadTexture(#PB_Any, #Clouds)

cl\tex always fails and assigned to zero. Note, I replaced the hardcoded filename with a constant to change directories, etc.

I'm using 5.2 b10.
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Another simple Static geometry example...

Post by DK_PETER »

Tenaja wrote:I tried to run this, but it sits for about 15 seconds (even very long files compile nearly instantly) and then errors because in this line...
cl\tex = LoadTexture(#PB_Any, #Clouds)

cl\tex always fails and assigned to zero. Note, I replaced the hardcoded filename with a constant to change directories, etc.

I'm using 5.2 b10.
Hey Tenaja.

You're not loading the image so the texture path must be set wrong.
Did you save the clouds.jpg to a folder named 'textures' and code outside that folder?
It looks inside the 'textures' folder for the 'clouds.jpg' and 'deadworld.png'.

'Add3DArchive("textures",#PB_3DArchive_FileSystem)'

Edit: My guess is, that you've put the code inside the textures folder.....Am i right?

Best regards
Peter
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Another simple Static geometry example...

Post by Tenaja »

Peter,
I put everything in the same folder, and hardcoded the filename to include the full file path; still didn't work. After your post, I made a subfolder "textures" and deleted the filepath from the filename, and now it works. I do not normally expect files to be found in a subfolder without a path directing them there...

Anyway, your demo is neat; thanks for sharing it. I do not work with 3d games (in much of any capacity), but still find the occasional demo neat to see. Maybe someday I will have time to play with them a bit more.
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Another simple Static geometry example...

Post by DK_PETER »

Tenaja wrote:Peter,
I put everything in the same folder, and hardcoded the filename to include the full file path; still didn't work. After your post, I made a subfolder "textures" and deleted the filepath from the filename, and now it works. I do not normally expect files to be found in a subfolder without a path directing them there...

Anyway, your demo is neat; thanks for sharing it. I do not work with 3d games (in much of any capacity), but still find the occasional demo neat to see. Maybe someday I will have time to play with them a bit more.
Glad you got it working and you're welcome :-)
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Another simple Static geometry example...

Post by applePi »

i call this drawing using programming, or a close encounter of the third kind in which the UFO are expected to land over the dead city, the dark sky and omen clouds refer to that. certainly the code are useful and will be of usage.
thanks
Post Reply