March 28, 2024, 10:56:45 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


3d beginner

Started by WayneA, January 08, 2010, 06:15:32 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

WayneA

Did anyone write their first 3d projects with Ebasic? If so, how did you get started and what resources did you use? I'm interested in using Ebasic for some 3d projects however I've never done any work with 3d graphics.
99 little bugs in the code,
99 bugs in the code,
Fix one bug,
Compile again,
104 little bugs in the code...

All code I post is in the public domain.

ZeroDog

There are a few options for 3D in EBasic.  You can use OpenGL or DirectX, or a graphics library or engine such as the 3Impact engine.   I found Creative Basic's 3D commands fun and easy to use.

I started work on some D3D9 tutorials for the 3D command pak for EBasic, but got sidetracked before I got anything really done.   This was the first tutorial in the series I was putting together, and its 'mostly' finished:
'ZeroDog's EBasic Tutorials
'Fun With DirectX9 3D
'Part 1 - Getting started
'*******************************

'In this tutorial, we will learn how to setup a DX9 3D screen and how to create
'a few different primitive shapes.


'***** Setting up the D3DScreen *****
'First thing we need to do is set up our DX9 3D screen.
'For this we will need to define a variable named SCREEN as a C3DScreen object.
def screen as C3DScreen

'The variable SCREEN is now a C3DScreen object, which has different methods that it can access.
'We will now use one of two methods in the C3DScreen object to create our 3D screen.
'The CreateFullScreen method will create the screen in fullscreen mode with the specified size.
'The CreateWindowed method will create the screen within a window, with the specified attributes.
'In this case, we will be creating the screen in a window.  If you wish to test it in
'fullscreen mode, uncomment the next line, and comment out the line after.

'screen.CreateFullScreen(800,600,32,1,&winproc)
screen.CreateWindowed(0,0,800,600,@CAPTION|@SIZE,"ZDEBT-FWD3D-1:Getting Started",NULL,1,&winproc)

'Next we will need to create a scene object to place objects into.
'For this we will define a variable named SCENE as a C3DObject object.
def scene as C3DObject

'The variable SCENE is now a C3DObject, which contains different methods to manipulate it.
'We will create the scene within the screen using the CreateScene method.
scene.CreateScene(screen)

'***** Setting up the camers *****
'We now need a camera to render from.
'We will define a variable named CAMERA as a C3DCamera object.
def camera as C3DCamera

'The variable CAMERA is now a C3DCamera object with different methods to manipulate it.
'We will create the camera for the screen using the Create method.
camera.Create(screen)
'We can position the CAMERA using the POSITION method of the object at location x,y,z
camera.position(0,0,0)
'To orient the CAMERA we use the ORIENT method of the object, to orient it at dx,dy,dz,  ux,uy,uz
'The z direction of the camera will be pointing at dx,dy,dz
'The y direction of the camera will be pointing at ux,uy,uz
'So in this case, the z direction will be pointing straight ahead (0,0,1)
'And the y direction will be pointing straight up (0,1,0)
camera.Orient(0,0,1, 0,1,0)

'At this point, the screen, scene and camera are all initialized and created so it is
'time to start adding things to the scene.

'***** Setting up lighting *****
'We need some lighting for our scene or all the objects will be rendered as black.
'First we will define a variable named LIGHT as a C3DLight object.
def light as C3DLight

'We will use the Create method of the C3DLight object to create the light.
'We can create the light as LIGHT_POINT, LIGHT_SPOT, or LIGHT_DIRECTIONAL.
'In this case, we will be using LIGHT_POINT and giving the light an index of 1.
light.Create(screen,LIGHT_POINT,1)
'We can use the POSITION method of the object to place the light at location (x,y,z)
light.Position(0,20,-50)
'And finally we will add the light as a child of the scene object using the AddChild method.
scene.AddChild(light)

'***** Setting up Primitive shapes *****
'Now we need some objects in our scene
'We will define a variable named BOX as a C3DMesh object.
def box as C3DMesh
'We will use the CreateBox method of the object to create a cube mesh on the screen object.
'The cube will have a width of 1(x direction),a height of 2(y direction),a depth of 3(z direction).
'The last variable used in the method is the inside flag for the mesh. If this flag is 1 then
'the mesh will be created with sides facing inwards. If it is 9 then the sides will be drawn
'facing outwards.  In this case we want the sides facing outwards, so we set the flag to 0.
box.CreateBox(screen,1,2,3, 0)
'We can position the mesh we just created using the POSITION method of the object at location x,y,z
box.Position(-1,0,5)
'Finally we add the box mesh to the scene object using the AddChild method.
scene.AddChild(box)

'We will define a variable named SPHERE as a C3DMesh object.
def sphere as C3DMesh
'We will use the CreateSphere method of the object to create a sphere mesh on the screen object.
'The sphere will have 36 bands (or 36 points)and a radius of 0.5 .
'The last variable used in the method is the inside flag for the mesh. If this flag is 1 then
'the mesh will be created with sides facing inwards. If it is 9 then the sides will be drawn
'facing outwards.  In this case we want the sides facing outwards, so we set the flag to 0.
sphere.CreateSphere(screen,36,0.5, 0)
'We can position the mesh we just created using the POSITION method of the object at location x,y,z
sphere.Position(0.5,0,5)
'Finally we add the sphere mesh to the scene object using the AddChild method.
scene.AddChild(sphere)

'We will define a variable named CYLINDER as a C3DMesh object.
def cylinder as C3DMesh
'We will use the CreateCylinder method of the object to create a cylinder mesh on the screen object.
'The cylinder will have a height of 2, 32 segments (or 32 points), and a radius of 0.5 .
'The last variable used in the method is the inside flag for the mesh. If this flag is 1 then
'the mesh will be created with sides facing inwards. If it is 9 then the sides will be drawn
'facing outwards.  In this case we want the sides facing outwards, so we set the flag to 0.
cylinder.CreateCylinder(screen,2,32,0.5, 0)
'We can position the mesh we just created using the POSITION method of the object at location x,y,z
cylinder.Position(2,0,5)
'Finally we add the cylinder mesh to the scene object using the AddChild method.
scene.AddChild(cylinder)

'***** The Rendering Loop *****
run=1
do
'First thing we will do is to set the background color of the screen to blue.
'We will do this using the Clear method.  The color is in RGBA format, so we use the RGBA function
'to specify the value (red, green, blue, alpha)
screen.Clear( RGBA(0,0,255 ,255) )

screen.BeginScene(camera)
scene.Draw()
screen.RenderScene()
wait 1
until run=0
    scene.Free()
end



sub winproc
    if @message = @IDCREATE then CENTERWINDOW screen.m_win
    if @message = @IDCLOSEWINDOW then run=0
return
ENDSUB

RG

I didn't do my first 3D in ebasic but have done several programs. Best approach is to start small and write some easy programs, relying on the examples that come with ebasic or that have been posted to the forum. How you go about it also depends on whether you want to do game development, create custom meshes, etc. Post your images and I can make some more specific recos.

Rich

A-One

ZeroDog,

Thank you for the 1st example,
I have looked at the 3D commands, but got very confused; the comments in your example is just what I needed.

How do you place an object that is at an angle?

For example, create a cylinder that is rotated 15 degrees about the x-axis and 30 degrees about the z-axis?

Thank you.

aurelCB

Zero when i first time compile and run this example program simply crush.
So i change color depth from 16bit to 32bit colors and works.
I think that not would be bad to tell which color program need. ;)

ZeroDog

QuoteHow do you place an object that is at an angle?

For example, create a cylinder that is rotated 15 degrees about the x-axis and 30 degrees about the z-axis?

You can do this one of a few ways.  You can use the object's Orient method, the object's LookAt method or the MatrixRotation function.

For the LookAt method, you simply specify a point in 3D space for the object to face.
mesh2.LookAt(10,5,15)

for the Orient method you specify the directional and up vectors for the object.
mesh1.Orient(0,0,1, 0,1,0)

for the MatrixRotation function it goes something like this:
'create a MATRIX4 UDT called mat
MATRIX4 mat 
'create a matrix identity using the mat UDT
MatrixIdentity(mat)
'set the rotation 15 degrees x, 30 degrees y
MatrixRotation(mat, 15 * 0.01745, 0, 30 * 0.01745)
'apply the new matrix to the mesh object.
mesh1.SetMatrix(mat)


A-One

February 21, 2010, 03:11:50 PM #6 Last Edit: February 21, 2010, 04:41:06 PM by A-One
Zerodog,

Where can I find a more detailed description of these commands?

Is it possible to setup multiple viewports in the same window to view the objects from different angles?

Thank you for the help on this, I am slowly getting this.

ZeroDog

the 6 numbers represent the dx, dy, zy ( the direction vector ) and the ux, uy, uz (the up vector)
generally these numbers are between -1.0 and 1.0, however, they do not have to be.

Think of it this way,  if you were a 3D object, by default your dx, dy, dz would be 0,0,1 , meaning that you are looking 0 units in the x direction, 0 units in the y, and 1 unit in the z direction (looking directly forwards).  Your ux, uy, uz would be 0,1,0 meaning your head is pointing 0 units in x direction, 1 units in the y direction, and 0 units in the z direction (pointing straight up).