Help needed with 4 viewport example code

Blitz3D Forums/Blitz3D Beginners Area/Help needed with 4 viewport example code

atv(Posted 2012) [#1]
I am working with this example code i botched together. I would like to accomplish that i can move through 3d space in all viewports. When i move, the viewport has to update in all. Currently moving does not work. Probably because TopView() is in a neverending loop. If i remove the repeat until loop there though and move it to the main loop it doesnt work properly anymore, i get wireframe and messed up graphics.

I also would like when i pick a sector in the orthogonal view, that the cube stays there and i can continue putting another cube somewhere else.

Because i wanted the user to change resolution, i included start.bb as a function instead. Instead of the cursor keys, i will map the bracket scancodes to it, [ and ] for changing resolution. That also i have not working yet. In addition, i would like to be able to turn the viewports on or off with 1,2,3,4 respectively, but that is of later concern.

Any help or pointers is/are greatly appreciated.

 AppTitle "Setup"

Graphics3D 640,480,0,0

Global G_grid_cell_size# = 1.0
Global G_camera = CreateCamera()
Global G_light = CreateLight()
Global G_cube = CreateCube()
Global G_picked_entity
Global G_grid = CreateGrid()

SetBuffer BackBuffer() ; Set Scene

cone=CreateCone(6)    ;Set example entities
cube=CreateCube()     ;Set example Entities

EntityColor cube,255,128,128 ;add effects to entities
EntityColor cone,128,128,64  ;add effects to entities

effectlight=CreateLight(1)   ; add lighting
PositionEntity effectlight,40,30,40  ; set lighting
PointEntity effectlight,cube ; etc. etc.

;TOP VIEW CAMERA
topcam=CreateCamera() ;create camera
CameraViewport topcam,0,(GraphicsHeight()/2),GraphicsWidth()/2,(GraphicsHeight()/2); set viewport
CameraProjMode topcam,2 ; make view orthogonal

;SIDE VIEW CAMERA
sidecam=CreateCamera()
CameraViewport sidecam,0,0,GraphicsWidth()/2,GraphicsHeight()/2
CameraProjMode sidecam,2

;FRONT VIEW CAMERA
frontcam=CreateCamera()
CameraViewport frontcam,GraphicsWidth()/2,0,width,GraphicsHeight()/2
CameraProjMode frontcam,2

rendercam=CreateCamera()
CameraViewport rendercam,GraphicsWidth()/2,GraphicsHeight()/2,GraphicsWidth()/2,GraphicsHeight()/2


;Set object positions
PositionEntity cone,50,0,50

PositionEntity cube,52,0,50

PositionEntity topcam, 51,10,50
PositionEntity sidecam, 61,0,50
PositionEntity frontcam, 51,0,60
PositionEntity rendercam, 56,5,55

;point cameras
PointEntity topcam,cone
TurnEntity topcam,0,0,180
PointEntity sidecam,cone
PointEntity frontcam,cone
PointEntity rendercam,cone

EntityParent topcam,cone
EntityParent sidecam,cone
EntityParent frontcam,cone
EntityParent rendercam,cone

;MAIN LOOP

While Not KeyDown(1)
TopView()
SetGfx()
;movement by cursor keys

TranslateEntity cone,(KeyDown(208)-KeyDown(200)),0,0
TranslateEntity cone,0,0,(KeyDown(203)-KeyDown(205))

;Rendering
WireFrame True

HideEntity rendercam
ShowEntity sidecam
ShowEntity topcam
ShowEntity frontcam

RenderWorld
Flip

WireFrame False

ShowEntity rendercam
HideEntity sidecam
HideEntity topcam
HideEntity frontcam

RenderWorld

Flip

Wend

;END OF PROGRAM

;EndGraphics
End 

Function TopView()

CameraZoom G_camera, 1.6
TurnEntity G_camera, 90.0, 0.0, 0.0
MoveEntity G_camera, 0.0, 0.0, -20.0

ScaleMesh G_cube, 0.5, 0.5, 0.5
PositionMesh G_cube, 0.5, 0.5, 0.5
UpdateNormals G_cube

EntityPickMode G_grid, 2


;SetBuffer BackBuffer()

Repeat

	G_picked_entity = CameraPick( G_camera, MouseX(), MouseY() )
	
	If G_picked_entity
	
		x# = Floor( PickedX() / G_grid_cell_size# ) * G_grid_cell_size#
		z# = Floor( PickedZ() / G_grid_cell_size# ) * G_grid_cell_size#		
		PositionEntity G_cube, x#, PickedY(), z#
	
	EndIf
	
	UpdateWorld
	RenderWorld
	Text 10,10,"Orthographic view mode " + x + z
	
	
	Flip
	Delay( 1 )

Until KeyHit( 1 )

End Function

Function CreateGrid()
	; -- Create grid texture.
	Local i, x, y
	Local grid_2d_tex = CreateTexture ( 256, 256, 11 )
	SetBuffer TextureBuffer ( grid_2d_tex )

	For i = 0 To 4
		Rect i, i, 256 - i - i, 256 - i - i, False
	Next
	For y = 5 To 250
		For x = 5 To 250
			WritePixel x, y, 0
		Next
	Next
	;^^^^^^
	; -- Create 2D grid.
	Local grid_2D = CreatePlane ()
	EntityTexture grid_2D, grid_2d_tex
	EntityFX grid_2D, 9

	;^^^^^^
	Return grid_2D
End Function 

Function SetGfx()
	If info1$<>""
		AppTitle info1$,"Exit "+info1$+" ?"
	EndIf
	FlushKeys()
	
	mode_cnt=CountGfxModes3D()
	If Not mode_cnt RuntimeError "Can't find any 3D graphics modes"
	
	mode=0:If Not Windowed3D() mode=1
		
	font=LoadFont( "verdana",16 ):SetFont font
	
	tx=640+160:nx=-160:ty=280

	Repeat
		Cls
					
		Color 0,255,0
		Text tx,ty+FontHeight()*0,info1$,True
		Text nx,ty+FontHeight()*1,info2$,True
		Text tx,ty+FontHeight()*2,info3$,True
		Text nx,ty+FontHeight()*3,info4$,True
	
		Color 255,255,255
		If mode=0
			Text 330,360,"Windowed",True
		Else
			Text 330,360,GfxModeWidth( mode )+","+GfxModeHeight( mode )+","+GfxModeDepth( mode ),True
		EndIf
		
		Color 255,0,0
		Text 330,400,"Choose screen resolution mode []",True
		
		If KeyHit( 203 )
			mode=mode-1
			If mode<0 Or (mode=0 And (Not Windowed3D())) mode=mode_cnt
			Graphics3D GfxModeWidth(mode),GfxModeHeight(mode),GfxModeDepth(mode),1
			Return
		Else If KeyHit( 205 )
			mode=mode+1
			If mode>mode_cnt
				mode=0
				If Not Windowed3D() mode=1
				Graphics3D GfxModeWidth(mode),GfxModeHeight(mode),GfxModeDepth(mode),1
				Return
			EndIf
		EndIf
		If tx>320 tx=tx-8
		If nx<320 nx=nx+8
		Flip
	Forever
End Function


Last edited 2012

Last edited 2012


RemiD(Posted 2012) [#2]

I would like to accomplish that i can move through 3d space in all viewports. When i move, the viewport has to update in all.



I would use only one camera for this, in order to avoid errors.

First i would update the rotations, movements of the entities in the scene.
Second i would position the camera to differents positions and rotate it to have the view i want. (Or you can use several pivots and parent/unparent the camera to each pivot when needed.)
Then i would draw the 4 views on a texture or an image and show it to the user.

To parent the camera to a pivot and to set the camera orientation, position exactly as those of the pivot, you can use :
RotateEntity(Camera,EntityPitch(Pivot,True),EntityYaw(Pivot,True),EntityRoll(Pivot,True))
PositionEntity(Camera,EntityX(Pivot,True),EntityY(Pivot,True),EntityZ(Pivot,True))
EntityParent(Camera,Pivot,True)

Then to unparent the Camera you can use :
EntityParent(Camera,0)




Because i wanted the user to change resolution, i included start.bb as a function instead



I don't think you can use Graphics3d() like this, i think it deletes all 3d entities, see :
Graphics3D(640,480,32,2)
SetBuffer(BackBuffer())
HidePointer()

Camera = CreateCamera()

Cube = CreateCube()

OLight = CreateLight(2)

Graphics3D(800,600,32,2)

PositionEntity(Camera,0,1.7,-3)

PositionEntity(Cube,0,0,5)

Repeat

 Cls()

 TurnEntity(Cube,0,1,0)

 RenderWorld()

 Flip(True)

Until(KeyDown(1)=1)

End



atv(Posted 2012) [#3]
Thanks Remy.
I don't want to sound lazy or anything or ask you to do my homework, but i am new to blitz3d and not sure how to code it up. Am getting a bit dissappointed with my lack of progress.

Could you post some code if it's not to much trouble to give me a headstart?

Would be much appreciated.
In any case, still thanks for your reply. I will try and keeping hacking away at it.


RemiD(Posted 2012) [#4]
You don't need to change your code, your example with several cameras seems to work.

What doesn't work is the way you use the Graphics3d() command.

You must either choose what Width, Height, Colors, Mode you want to use before using the Graphics3d() command or you must recreate all your 3d entities after because they seem to be deleted after having used the Graphics3d() command.


atv(Posted 2012) [#5]
Ok i will change that. The rest of my code seems to work, but it doesn't really. If you run it you see what i mean.

Thanks Remy, i guess i will have to really dive a bit more in tutorials.
Kind regards


RemiD(Posted 2012) [#6]

The rest of my code seems to work, but it doesn't really. If you run it you see what i mean.



I have ran your code and if i remove the SetGfx() function, it seems to work without errors.
You can't use the Graphics3d() command like this, as shown in my previous example, this command seems to delete the 3d entities.

What is the other problem ?

Last edited 2012


RemiD(Posted 2012) [#7]
Here, my try with 4 views (Front, Left, Top, 3D)
I have never tried to code a program like this, but apparently if you use a Camera with the Orthographic projection, you have to use CameraZoom in order to scale the meshes at the correct size. so it is proportional to those of the 3D view.

This example shows what i mean.
If the Camera is positionned at +30Z and look at the entities near the origine (0,0,0), to have meshes with the correct proportions compared to the 3d view at this distance, you have to calculate the CameraZoom like this (1.0/30 = 0.03333)

Code updated.
See here :
http://blitzbasic.com/codearcs/codearcs.php?code=3023


atv(Posted 2012) [#8]
Hey Remi, thanks for that.
What's the difference between front and left view? My code had one ortho view (meant for picking up objects and dropping them), one frontview, one from the top looking straight down on it (like ortho but in 3d mode, and one 3d mode.

I like your code.
How would i go about putting in the picking code. I.e. i would really like a grid of sectors instead of using a cube as a base, and using my previous code where you can move your mouse over a sector, and if i clicked it would drop the cube or pick it up and return the coordinates.

In addition, i would like to be able to move throughout the space in first person mode using keys for navigation and mouse for direction (as in a fps) and the viewpoints going along with it. So if i would move in one viewport, the others would update accordingly. The cubes don't have to move, but i would like to move the grid under it very slowly, that is when i am not in the viewport.

Many thanks, you really helped me a lot.
I wonder how many objects it can do on screen once textured :-)

Last edited 2012

Last edited 2012

Last edited 2012


RemiD(Posted 2012) [#9]

What's the difference between front and left view?



Front view : the camera is positionned at 0X,0Y,30Z and looks at the origine.
Left view : the camera is positionned at 30X,0Y,0Z and looks at the origine.



You can use the others parts of your code and search the code archives if you need to add others functionalities.
There are Linepick examples, and FPV examples in the code archives. Search, read and try to learn.
Here an example of a FPV : http://blitzbasic.com/codearcs/codearcs.php?code=2933 (just use the parts about the FPV, you don't need collisions)

If you don't want to update the Camera position, orientation, zoom you can remove the 4 functions :
CameraFrontUpdate()
CameraLeftUpdate()
CameraTopUpdate()
Camera3DUpdate()

Then simply add all your others routines in the function :
LogicUpdate()

Last edited 2012


RemiD(Posted 2012) [#10]

Left view : the camera is positionned at 30X,0Y,0Z and looks at the origine.



There is an error. For the left view, the camera has to be positionned at -30X,0Y,0Z and has to look at the origine.
You can change this is easily.


atv(Posted 2012) [#11]
it seems to be set ok ?

PositionEntity(CameraLeft,30,0,0)