April 28, 2024, 09:15:37 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


3D starfield

Started by Ionic Wind Support Team, August 08, 2006, 04:26:37 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ionic Wind Support Team

Quote from: John S on August 08, 2006, 10:36:34 PM
When I use the A & S controls to move the camera left or right after I rotate (using the Z & X controls) the camera does not slide left or right, but goes off in the global x and -x directions.  Is this a bug?
...

Yep it was a bug, or at least an omission.  Forgot to recalculate the Y axis when moving left/right. Fixed for Beta 1.
Ionic Wind Support Team

John S

Thanks Paul,
I really jazzed your example up.  I already brute forced the work around for Move and the Scalar function.  I'm going to add in that scalar function you wrote to shorten the code.

   IF di.KeyDown(DIK_A)      // move/slide camera left
   {   c_np.x = c_ps.x + c_rt.x*(-.5f * fAdjust);
      c_np.y = c_ps.y + c_rt.y*(-.5f * fAdjust);
      c_np.z = c_ps.z + c_rt.z*(-.5f * fAdjust);
      c.Position(c_np.x,c_np.y,c_np.z);
      c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
   };

You should include the scalar function in the next release.

Keep up the outstanding work
John Siino, Advanced Engineering Services and Software

Ionic Wind Support Team

Anything that makes it a better engine is a good thing ;)

Just added a Reset method to C3DScreen.  For resizing the rendering surface when a window is resized.


MyScreen::OnSize(int nType,int cx,int cy),int
{
Reset(cx,cy,0,false);
float aspect = cx/(cy+0f);
c.SetAspectRatio(aspect);
}


Reset will also allow switching from windowed to fullscreen modes eventually.
Ionic Wind Support Team

John S

I tried using the new sub and Vec3Add and the program gave me goofy results.  It worked fine with the brute force method,

New Code:

sub Vec3Scale( VECTOR3 v, float s ), VECTOR3
{
      VECTOR3 ret = v;
       v.x *= s;
       v.y *= s;
       v.z *= s;
       return ret;
}

I will post the code that worked next

...

   IF di.KeyDown(DIK_A)      // move/slide camera left
   {   c_np = Vec3Scale(c_rt, -.5f * fAdjust);
      c_np = Vec3Add(c_ps, c_np);
      c.Position(c_np.x,c_np.y,c_np.z);
      c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
   };

   IF di.KeyDown(DIK_S)      // move/slide camera right
   {   c_np = Vec3Scale(c_rt, .5f * fAdjust);
      c_np = Vec3Add(c_ps, c_np);
      c.Position(c_np.x,c_np.y,c_np.z);
      c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
   };

   IF di.KeyDown(DIK_E)      // move/slide camera up
   {   c_np = Vec3Scale(c_up, .5f * fAdjust);
      c_np = Vec3Add(c_ps, c_np);
      c.Position(c_np.x,c_np.y,c_np.z);
      c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
   };

   IF di.KeyDown(DIK_D)      // move/slide camera right
   {   c_np = Vec3Scale(c_up, -.5f * fAdjust);
      c_np = Vec3Add(c_ps, c_np);
      c.Position(c_np.x,c_np.y,c_np.z);
      c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
   };
John Siino, Advanced Engineering Services and Software

John S

August 09, 2006, 12:07:50 AM #29 Last Edit: August 09, 2006, 11:49:29 AM by John S
O.K.  I learned a lot this evening!    Here is my final attempt at this one. 

I changed a few more things and added some more stuff.
Changed initial position and orientation of camera and some of the controls and speed of rotations.
Corrected the A & S controls for sliding camera left and right
Added E & D controls for sliding camera up and down

List of controls:

Up Arrow = rotate camera up
Dn Arrow = rotate camera down
Left Arrow = rotate camera left
Right Arrow = rotate camera right

V = Move Camera Forward
C = Move Camera Backward
Z = Roll Camera Counter Clockwise
X = Roll Camera Clockwise
A = Slide Camera Left
S = Slide Camera Right
E = Slide Camera Up
D = Slide Camera Down

R = Turn Camera towards Planet

Added some additional camera methods including:
GetPosition(), GetUpVector(), GetDirection(), Vec3Cross(c_up, c_hd)



New Code:

// 3dstarfield.src
// An example using a point list to draw a starfield.
// Arrow keys to move camera.
// PgUp/PgDn to pan up and down.
// Z and X to rotate on Z axis.

declare import,timeGetTime(),int;

//Our FVF structure for points
struct point_vertex{
VECTOR3 pos;
DWORD color;        // The vertex colour.
};

VECTOR3 c_ps, c_hd, c_up, c_rt, c_np; // camera position, heading, etc

#define point_fvf D3DFVF_XYZ|D3DFVF_DIFFUSE
#define num_points 20000
#define fieldwidth 1000

global sub main()
{
C3DScreen s;
C3DCamera c;
C3DMesh m;
C3DMesh planet;
C3DObject scene;
C3DLight light;
CDirectInput di;

float fadjust,fTarget;

//target 60FPS for camera movement
fTarget = 1.0f / /*FPS*/60.0f * 1000.0f;
fAdjust = 1.0f;

s.CreateWindowed(0,0,640,480,AWS_CAPTION|AWS_VISIBLE|AWS_SIZE|AWS_CENTERED,0,"3D Points - ESC exits",NULL,false);
di.Init(s);

c.Create(s);
c.Position(0,-50,0);
c.Orient(0,1,0,0,0,1);
c.SetBackPlane(1.75*fieldwidth);

s.Clear(RGBA(0,0,0,255));
s.BeginScene(c);
s.RenderText(0,10,"Loading objects...",RGBA(255,255,0,255));
s.RenderScene();

//create a point mesh with 'num_points' points.
m.CreateMeshEx(s,D3DPT_POINTLIST,num_points,num_points,num_points,point_fvf);

//Lock the vertex and index buffers so we can write to them.
point_vertex *v = m.LockVertexBuffer();
word *idx = m.LockIndexBuffer();
for(int x=0;x < num_points;x++)
{
*idx = x;
v->pos.x = rnd(-fieldwidth,fieldwidth);
v->pos.y = rnd(-fieldwidth,fieldwidth);
v->pos.z = rnd(-fieldwidth,fieldwidth);
v->color = RGBA(rnd(140,255),rnd(140,255),rnd(140,255),255);
v+=len(point_vertex);
idx+=len(word);
}

m.UnlockVertexBuffer();
m.UnlockIndexBuffer();
m.EnableLighting(false);
m.UseVertexColor(true);
m.SetVisible(true);

planet.CreateSphere(s,25,5.0,false);
planet.LoadTexture(0,GetStartPath()+"media\\tile3.tga",0);

light.Create(s,LIGHT_POINT,1);
light.Position(950,0,0);
light.SetAttenuation(0,1/2000.0,0);
light.SetSpecular(.5,.5,.5,1);
light.SetAmbient(.15,.15,.15,1);

scene.CreateScene(s);
scene.AddChild(light);
scene.AddChild(m);
scene.AddChild(planet);

int fps = 0;
int startTime;

do
{
startTime = timeGetTime();

//  Position, Heading & Up Vectors of Camera
c_ps = c.GetPosition();
c_up = c.GetUpVector();
c_hd = c.GetDirection();
c_rt = Vec3Cross(c_up, c_hd);

//check for keys and move camera accordingly
IF di.KeyDown(DIK_V)
c.Move(0.0f,.75f * fAdjust);

IF di.KeyDown(DIK_C)
c.Move(0.0f,-.75f * fAdjust);

IF di.KeyDown(DIK_RIGHT)
c.Rotate(.0174533 * fAdjust/2,0,0);
IF di.KeyDown(DIK_LEFT)
c.Rotate(-.0174533 * fAdjust/2,0,0);
IF di.KeyDown(DIK_Z)
c.Rotate(0,0,-.0174533 * fAdjust/2);
IF di.KeyDown(DIK_X)
c.Rotate(0,0,.0174533 * fAdjust/2);

IF di.KeyDown(DIK_UP)
c.Rotate(0,-.0174533 * fAdjust/2,0);
IF di.KeyDown(DIK_DOWN)
c.Rotate(0,.0174533 * fAdjust/2,0);

IF di.KeyDown(DIK_R)
c.LookAt(0,0,0);

IF di.KeyDown(DIK_A)      // move/slide camera left
// c.Move(-.5f * fAdjust, 0.0f);
{ c_np.x = c_ps.x + c_rt.x*(-.5f * fAdjust);
c_np.y = c_ps.y + c_rt.y*(-.5f * fAdjust);
c_np.z = c_ps.z + c_rt.z*(-.5f * fAdjust);
c.Position(c_np.x,c_np.y,c_np.z);
c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
};

IF di.KeyDown(DIK_S)      // move/slide camera right
// c.Move( .5f * fAdjust, 0.0f);
{ c_np.x = c_ps.x + c_rt.x*(.5f * fAdjust);
c_np.y = c_ps.y + c_rt.y*(.5f * fAdjust);
c_np.z = c_ps.z + c_rt.z*(.5f * fAdjust);
c.Position(c_np.x,c_np.y,c_np.z);
c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
};

IF di.KeyDown(DIK_E)      // move/slide camera up
{ c_np.x = c_ps.x + c_up.x*(.5f * fAdjust);
c_np.y = c_ps.y + c_up.y*(.5f * fAdjust);
c_np.z = c_ps.z + c_up.z*(.5f * fAdjust);
c.Position(c_np.x,c_np.y,c_np.z);
c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
};

IF di.KeyDown(DIK_D)      // move/slide camera right
{ c_np.x = c_ps.x + c_up.x*(-.5f * fAdjust);
c_np.y = c_ps.y + c_up.y*(-.5f * fAdjust);
c_np.z = c_ps.z + c_up.z*(-.5f * fAdjust);
c.Position(c_np.x,c_np.y,c_np.z);
c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
};

planet.Rotate(0,0,startTime / 6000.0f);

s.Clear(RGBA(0,0,0,255));
s.BeginScene(c);
scene.Draw();

s.RenderText(10,10,"FPS: "+NumToStr(fps),RGBA(255,255,0,255));

s.RenderText(85,10,"Position:  "+NumToStr(c_ps.x)+",  "+NumToStr(c_ps.y)+",  "+NumToStr(c_ps.z),RGBA(255,255,0,255));
s.RenderText(250,10,"Heading:  "+NumToStr(c_hd.x, 3)+",  "+NumToStr(c_hd.y, 3)+",  "+NumToStr(c_hd.z, 3),RGBA(255,255,0,255));

s.RenderText(470,10,"Up:",RGBA(255,255,0,255));
s.RenderText(500,10,NumToStr(c_up.x, 2)+",  "+NumToStr(c_up.y, 2)+",  "+NumToStr(c_up.z, 2),RGBA(255,255,0,255));

s.RenderText(470,30,"Rt:",RGBA(255,255,0,255));
s.RenderText(500,30,NumToStr(c_rt.x, 2)+",  "+NumToStr(c_rt.y, 2)+",  "+NumToStr(c_rt.z, 2),RGBA(255,255,0,255));

s.RenderText(10,425,"Use arrow keys, A, S, E, D, Z, X, C, V, R to move around and escape key to quit",RGBA(255,255,0,255));

fps = s.RenderScene();
fAdjust = (timeGetTime() - startTime) / fTarget;
}until di.KeyDown(DIK_ESCAPE);

Scene.Free();
}

John Siino, Advanced Engineering Services and Software

Ionic Wind Support Team

Sorry I wrote that sub without testing it.  Surprised you didn't spot the error ;)

sub Vec3Scale(VECTOR3 v, float s),VECTOR3
{
      VECTOR3 ret = v;
       ret.x *= s;
       ret.y *= s;
       ret.z *= s;
       return ret;
}
Ionic Wind Support Team

John S

August 09, 2006, 10:55:24 AM #31 Last Edit: August 09, 2006, 10:57:08 AM by John S
Quote from: Paul Turley on August 09, 2006, 12:27:07 AM
... Surprised you didn't spot the error ;)

It was getting late.  I will wrote thas sub in and it works great.  Thanks

This weekend, I'm going to take this file and transform the planet into a sun and add a planet in orbit with a moon orbitting the planet.
John Siino, Advanced Engineering Services and Software

John S

August 09, 2006, 11:03:45 AM #32 Last Edit: August 09, 2006, 11:48:40 AM by John S
Here is my final-final attempt at this one.

Corrected the sub Vec3Scale() and used Vec3Add()

List of controls:

Up Arrow = rotate camera up
Dn Arrow = rotate camera down
Left Arrow = rotate camera left
Right Arrow = rotate camera right

V = Move Camera Forward
C = Move Camera Backward
Z = Roll Camera Counter Clockwise
X = Roll Camera Clockwise
A = Slide Camera Left
S = Slide Camera Right
E = Slide Camera Up
D = Slide Camera Down

R = Turn Camera towards Planet


// 3d_starfield.src
// An example using a point list to draw a starfield.
// Arrow keys to move camera.
// PgUp/PgDn to pan up and down.
// Z and X to rotate on Z axis.

declare import,timeGetTime(),int;

sub Vec3Scale( VECTOR3 v, float s ), VECTOR3
{
      VECTOR3 ret = v;
       ret.x *= s;
       ret.y *= s;
       ret.z *= s;
       return ret;
}

//Our FVF structure for points
struct point_vertex{
VECTOR3 pos;
DWORD color;        // The vertex colour.
};

VECTOR3 c_ps, c_hd, c_up, c_rt, c_np; // camera position, heading, etc

#define point_fvf D3DFVF_XYZ|D3DFVF_DIFFUSE
#define num_points 20000
#define fieldwidth 1000

global sub main()
{
C3DScreen s;
C3DCamera c;
C3DMesh m;
C3DMesh planet;
C3DObject scene;
C3DLight light;
CDirectInput di;

float fadjust,fTarget;

//target 60FPS for camera movement
fTarget = 1.0f / /*FPS*/60.0f * 1000.0f;
fAdjust = 1.0f;

s.CreateWindowed(0,0,640,480,AWS_CAPTION|AWS_VISIBLE|AWS_SIZE|AWS_CENTERED,0,"3D Points - ESC exits",NULL,false);
di.Init(s);

c.Create(s);
c.Position(0,-50,0);
c.Orient(0,1,0,0,0,1);
c.SetBackPlane(1.75*fieldwidth);

s.Clear(RGBA(0,0,0,255));
s.BeginScene(c);
s.RenderText(0,10,"Loading objects...",RGBA(255,255,0,255));
s.RenderScene();

//create a point mesh with 'num_points' points.
m.CreateMeshEx(s,D3DPT_POINTLIST,num_points,num_points,num_points,point_fvf);

//Lock the vertex and index buffers so we can write to them.
point_vertex *v = m.LockVertexBuffer();
word *idx = m.LockIndexBuffer();
for(int x=0;x < num_points;x++)
{
*idx = x;
v->pos.x = rnd(-fieldwidth,fieldwidth);
v->pos.y = rnd(-fieldwidth,fieldwidth);
v->pos.z = rnd(-fieldwidth,fieldwidth);
v->color = RGBA(rnd(140,255),rnd(140,255),rnd(140,255),255);
v+=len(point_vertex);
idx+=len(word);
}

m.UnlockVertexBuffer();
m.UnlockIndexBuffer();
m.EnableLighting(false);
m.UseVertexColor(true);
m.SetVisible(true);

planet.CreateSphere(s,25,5.0,false);
planet.LoadTexture(0,GetStartPath()+"media\\tile3.tga",0);

light.Create(s,LIGHT_POINT,1);
light.Position(950,0,0);
light.SetAttenuation(0,1/2000.0,0);
light.SetSpecular(.5,.5,.5,1);
light.SetAmbient(.15,.15,.15,1);

scene.CreateScene(s);
scene.AddChild(light);
scene.AddChild(m);
scene.AddChild(planet);

int fps = 0;
int startTime;

do
{
startTime = timeGetTime();

//  Position, Heading & Up Vectors of Camera
c_ps = c.GetPosition();
c_up = c.GetUpVector();
c_hd = c.GetDirection();
c_rt = Vec3Cross(c_up, c_hd);

//check for keys and move camera accordingly
IF di.KeyDown(DIK_V)
c.Move(0.0f,.75f * fAdjust);

IF di.KeyDown(DIK_C)
c.Move(0.0f,-.75f * fAdjust);

IF di.KeyDown(DIK_RIGHT)
c.Rotate(.0174533 * fAdjust/2,0,0);
IF di.KeyDown(DIK_LEFT)
c.Rotate(-.0174533 * fAdjust/2,0,0);
IF di.KeyDown(DIK_Z)
c.Rotate(0,0,-.0174533 * fAdjust/2);
IF di.KeyDown(DIK_X)
c.Rotate(0,0,.0174533 * fAdjust/2);

IF di.KeyDown(DIK_UP)
c.Rotate(0,-.0174533 * fAdjust/2,0);
IF di.KeyDown(DIK_DOWN)
c.Rotate(0,.0174533 * fAdjust/2,0);

IF di.KeyDown(DIK_R)
c.LookAt(0,0,0);

IF di.KeyDown(DIK_A)      // move/slide camera left
{ c_np = Vec3Scale(c_rt, -.5f * fAdjust);
c_np = Vec3Add(c_ps, c_np);
c.Position(c_np.x,c_np.y,c_np.z);
c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
};

IF di.KeyDown(DIK_S)      // move/slide camera right
{ c_np = Vec3Scale(c_rt, .5f * fAdjust);
c_np = Vec3Add(c_ps, c_np);
c.Position(c_np.x,c_np.y,c_np.z);
c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
};

IF di.KeyDown(DIK_E)      // move/slide camera up
{ c_np = Vec3Scale(c_up, .5f * fAdjust);
c_np = Vec3Add(c_ps, c_np);
c.Position(c_np.x,c_np.y,c_np.z);
c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
};

IF di.KeyDown(DIK_D)      // move/slide camera right
{ c_np = Vec3Scale(c_up, -.5f * fAdjust);
c_np = Vec3Add(c_ps, c_np);
c.Position(c_np.x,c_np.y,c_np.z);
c.Orient(c_hd.x,c_hd.y,c_hd.z,c_up.x,c_up.y,c_up.z);
};

planet.Rotate(0,0,startTime / 6000.0f);  // spin the planet

s.Clear(RGBA(0,0,0,255));
s.BeginScene(c);
scene.Draw();

s.RenderText(10,10,"FPS: "+NumToStr(fps),RGBA(255,255,0,255));

s.RenderText(85,10,"Position:  "+NumToStr(c_ps.x)+",  "+NumToStr(c_ps.y)+",  "+NumToStr(c_ps.z),RGBA(255,255,0,255));
s.RenderText(250,10,"Heading:  "+NumToStr(c_hd.x, 3)+",  "+NumToStr(c_hd.y, 3)+",  "+NumToStr(c_hd.z, 3),RGBA(255,255,0,255));

s.RenderText(470,10,"Up:",RGBA(255,255,0,255));
s.RenderText(500,10,NumToStr(c_up.x, 2)+",  "+NumToStr(c_up.y, 2)+",  "+NumToStr(c_up.z, 2),RGBA(255,255,0,255));

s.RenderText(470,30,"Rt:",RGBA(255,255,0,255));
s.RenderText(500,30,NumToStr(c_rt.x, 2)+",  "+NumToStr(c_rt.y, 2)+",  "+NumToStr(c_rt.z, 2),RGBA(255,255,0,255));

s.RenderText(10,425,"Use arrow keys, A, S, E, D, Z, X, C, V, R to move around and escape key to quit",RGBA(255,255,0,255));

fps = s.RenderScene();
fAdjust = (timeGetTime() - startTime) / fTarget;
}until di.KeyDown(DIK_ESCAPE);

Scene.Free();
}

John Siino, Advanced Engineering Services and Software