• Welcome to Dark Bit Factory & Gravity.
 

Defining/using Open-GL Camera paths

Started by da_fatstuff, August 29, 2007

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

da_fatstuff

Hi again,

So far I've only used static views and moved everything in the scenes by various formula...now I'm thinking of something that needs specific predefined camera control around a 3D scene, and for the movement of the camera to be reasonably fluid...currently I'm thinking of some sort of array of movements, with a movement being presented with these properties:

fx, fy, fz = viewing from co-ords
tx, ty, tz = viewing to co-ords
ticks = duration to utilise these co-ords for
smoothing = number of ticks to use graduating from previous co-ords to current co-ords

As the camera movements definition will have to be a manual process in code I'm hoping the smoothing will enable a relatively small set of defined paths to look less cheap (removing the jerky movements from one to the next).

Anyone got any tips/routines to use predefined demo camera paths for use in OpenGL, or does the above seem ok to y'all?

Cheers!

Shockwave

I think you should look into bezier splines.

If you want exact contro to be sure that your camera path goes through control points, look into catmul rom splines.

There may even be a catmul example somewhere on this forum somewhere.
Shockwave ^ Codigos
Challenge Trophies Won:

da_fatstuff

Cheers - that was quick!

I've found a forum link for a Freebasic implementation:
http://dbfinteractive.com/index.php?topic=2098.0

I hope it's ok if I also post this link that I found explained it (has the C code) for anyone else looking:
http://www.lighthouse3d.com/opengl/maths/index.php?catmullrom

Thanks again.

Shockwave

looking forward to what you come up with :)
Shockwave ^ Codigos
Challenge Trophies Won:

taj

One last thing to consider is to use d3dx (not d3d) for all its maths routines. It has catmull rom splines for "free". Plus a huge set of other stuff like a complete vector and maths lib, geometry subdivision, image loading etc. Its perfectly compatible with Opengl and I'm unsure why more people dont use it.

Chris
Challenge Trophies Won:

da_fatstuff

Cheers Chris - D3DX is definately worth me looking at as you say (the specific Catmull-Rom link below for anyone else looking):

http://msdn.microsoft.com/archive/en-us/directx9_c_Dec_2005/D3DXVec2CatmullRom.asp

I guess OpenGL users might have a portability consideration/intention that prevents them using this - but I don't so it's great, thanks!

Jim

To start with you might just want to slide the camera along a line.
If you have x0,y0,z0 and x1,y1,z1 as the start and end points and a value t between 0 and 1, then you can slide the camera along by

cx = x0 + (x1-x0)*t
cy = y0 + (y1-y0)*t
cz = z0 + (z1-z0)*t

The you can use look-at code to make the camera point where you want, there's a sample of that here
http://dbfinteractive.com/index.php?topic=1874.0, and I think D3DX has functions for doing it too.

Once you've done a straight line, curves will be easier - they nearly always take a 't' parameter to position you along them.

Jim
Challenge Trophies Won:

da_fatstuff

#7
I've knocked-up a quick VS2005 project using what's been discussed here to test it out - and it seems to work -I've attached it here for anyone's future reference - obviously the scene being viewed is just for testing's sake (so not pretty), and the camera movements are largely just random values.

Basically the misc.h contains CAMERA_PATTERNS to specify how many camera movements there are, and camera_moves has these movements defined as the array that I initially described.  The Main.cpp's PositionCamera method then uses the described D3DX D3DXVec3CatmullRom spline method to transition from one camera position to the next...

...I think I've used this correctly anyway, with the 2nd/3rd vectors being the from-to transitions I'm interested in, and the 1st/4th being the historic/future transitions either side.

Note: original post I put here defined fromcam_pos twice instead of prevcam_pos then fromcam_pos, so corrected this here.

Shockwave

Hmm.. Not working here, do you have a safe version perhaps? I'd love to see it!
Shockwave ^ Codigos
Challenge Trophies Won:

da_fatstuff

#9
The debug exe should be in there too - did you try that one?  Not sure why I included the release one actually  :-\

PS It's nothing exciting to see (I haven't got that far)  - just really to demonstrate how I intend to use camera's as guided by the above- I think the important bits of code are:

misc.h

struct CAMERA_POS {
float fx;
float fy;
float fz;
float tx;
float ty;
float tz;
unsigned int frames;
unsigned int smoothing;
};

const unsigned short CAMERA_PATTERNS = 8;

CAMERA_POS camera_moves[] = {
0.0f,-20.0f,0.0f,0.0f,-20.0f,-60.0f,450,300,
0.0f,0.0f,0.0f,0.0f,-20.0f,-55.0f,600,300,
-10.0f,-10.0f,-10.0f,0.0f,-15.0f,-55.0f,350,350,
10.0f,-10.0f,-10.0f,-20.0f,-10.0f,-50.0f,350,350,
-10.0f,-10.0f,-10.0f,20.0f,-10.0f,-30.0f,60,60,
-20.0f,-20.0f,-20.0f,0.0f,10.0f,-40.0f,45,45,
0.0f,-0.0f,0.0f,0.0f,-15.0f,-60.0f,900,45,
0.0f,-10.0f,-20.0f,0.0f,15.0f,-40.0f,120,45,
};


Main.cpp

#include <GL/gl.h>
#include <GL/glu.h>
#include <D3dx9math.h>
#include "misc.h"

static CAMERA_POS prevcam_pos;
static CAMERA_POS fromcam_pos;
static CAMERA_POS tocam_pos;
static CAMERA_POS nextcam_pos;
static float sstep = 1.0f;
static unsigned int frames = 0;
static unsigned short cam_index = -1;

static void PositionCamera() {
frames++;
if ((!&tocam_pos)||frames>tocam_pos.frames) {
// Increment camera view and store previous ones
cam_index++;
if (cam_index>=CAMERA_PATTERNS)
cam_index = 0;
tocam_pos = camera_moves[cam_index];
if (cam_index==1)
prevcam_pos = camera_moves[CAMERA_PATTERNS-1];
else if (cam_index==0)
prevcam_pos = camera_moves[CAMERA_PATTERNS-2];
else
prevcam_pos = camera_moves[cam_index-2];
if (cam_index==0)
fromcam_pos = camera_moves[CAMERA_PATTERNS-1];
else
fromcam_pos = camera_moves[cam_index-1];
if (cam_index==(CAMERA_PATTERNS-1))
nextcam_pos = camera_moves[0];
else
nextcam_pos = camera_moves[cam_index+1];

frames = 0;
lasts = 0.0f;
sstep = 1.0f/tocam_pos.smoothing;
}
// Default position to defined camera position
float sfx=tocam_pos.fx,sfy=tocam_pos.fy,sfz=tocam_pos.fz,stx=tocam_pos.tx,sty=tocam_pos.ty,stz=tocam_pos.tz;
// Movement smoothing achieved by Catmull-Rom splines,
// recommended by Shockwave & Chris highlighted the existing D3DX library functions
if ((&fromcam_pos)&&(tocam_pos.smoothing > frames)) {
// We're here so we're smoothing the movement to our required destination from a previous one
// lasts will be the 0-1 position along the smoothing spline
lasts += sstep;
// View-from smoothing
D3DXVECTOR3 fprev = D3DXVECTOR3(prevcam_pos.fx,prevcam_pos.fy,prevcam_pos.fz);
D3DXVECTOR3 ffrom = D3DXVECTOR3(fromcam_pos.fx,fromcam_pos.fy,fromcam_pos.fz);
D3DXVECTOR3 fto = D3DXVECTOR3(tocam_pos.fx,tocam_pos.fy,tocam_pos.fz);
D3DXVECTOR3 fnext = D3DXVECTOR3(nextcam_pos.fx,nextcam_pos.fy,nextcam_pos.fz);
D3DXVECTOR3 fsmoothed;
D3DXVec3CatmullRom(&fsmoothed,&fprev,&ffrom,&fto,&fnext,lasts);
// Overwrite default 'from' co-ords with smoothed ones
sfx = fsmoothed.x;
sfy = fsmoothed.y;
sfz = fsmoothed.z;
// View-to smoothing
D3DXVECTOR3 tprev = D3DXVECTOR3(prevcam_pos.tx,prevcam_pos.ty,prevcam_pos.tz);
D3DXVECTOR3 tfrom = D3DXVECTOR3(fromcam_pos.tx,fromcam_pos.ty,fromcam_pos.tz);
D3DXVECTOR3 tto = D3DXVECTOR3(tocam_pos.tx,tocam_pos.ty,tocam_pos.tz);
D3DXVECTOR3 tnext = D3DXVECTOR3(nextcam_pos.tx,nextcam_pos.ty,nextcam_pos.tz);
D3DXVECTOR3 tsmoothed;
D3DXVec3CatmullRom(&tsmoothed,&tprev,&tfrom,&tto,&tnext,lasts);
// Overwrite default 'to' co-ords with smoothed ones
stx = tsmoothed.x;
sty = tsmoothed.y;
stz = tsmoothed.z;
}
// Position camera
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity();
gluLookAt(sfx,sfy,sfz,stx,sty,stz,0.0,1.0,0.0);
}



Note: Edited this code as original had fromcam_pos assigned twice instead of prevcam_pos then fromcam_pos

Jim

->Shockwave, I see da_fatstuff is using August '07 DirectX SDK which means you should update your DirectX runtimes (yeah, yet again) or get a hold of d3dx9_35.dll from somewhere eg. http://www.m3fe.com/760/, and stick it in the folder with the EXE.  Looks like a side effect of the packing is you lose the warning from Windows about missing dlls.

Jim
Challenge Trophies Won:

taj

Challenge Trophies Won:

slippy

Hey,

really nice to see your 4k stuff here, da_fatstuff ... pretty sure that you'll become a famous intro coder within the next few months ... and thanks again for sharing your stuff to the community - this is gladly seen here ... we need guys like you keeping up the things :)

Looking forward to see more stuff from you in the future ...

The only thing missing right now on your 4k prods is a decent softsynth :)

...

Cheers,
SLiPPY

P.S.: Have some K++ dude

da_fatstuff

Cheers, but I'm a loooooong way from that level - I need to get some proper 3D objects embedded and as you say some sounds going next - that'll take time to master...be a few years before I produce my Micropolis (http://pouet.net/prod.php?which=13017) ;)

Regarding the softsynth I did try Farbrausch's V2 (as they supply the code and it's a great synth) but I can't get it to work with Crinkler yet :( ...not sure how small it'll compact to if I do anyway...and I think MIDI is seen as cheating  :)

slippy

well,

fr's softsynth is supposed for 64k prods only ... you won't fit the replay and track data within a few hundred bytes, though - unless the sound quality of kebby's synth is "kick-ass" IMHO :)

you have to do some low-level coding for that ... I got an example here for you ... it's not mine but I know the author quite good and it's a rather sophisticated tiny synth IMHO  ^^

Just watch the filesize and you'll get the point of what it should be ... aherm nearly forgot to mention ... there's a really good article on sound programming for 4k intros by auld on in4k ... check this link here

Also a good starter would be the "Making-of" article of sprite-o-mat in the Zine #12 magazine ... here's a direct link to the article ... the sources are available as well ...

Perhaps rbraz could give you some tipps or hints as well  :whisper: ... anyway ... :)

Laters,
  SLiPPY

da_fatstuff

#15
Cheers SLiPPY, I'll study this lot - great links, and I'll inflict the forums with any progress I make!

Edit: The In4k site you sent also had this Pouet link to a basic sequencer and playback synth's VC++ source code, where the test song/playback EXE Crinkled down to around 2.54Kb without any code changes (it'll be a good reference for doing this I think):
http://www.pouet.net/prod.php?which=13016


da_fatstuff

#16
Just thought I'd post an update (code etc included) to illustrate what I wanted to do with these cameras better than my initial example did - it's still very much work in progress, but now I've added a (very basic) 3D fractal landscape to demonstrate the camera movements better (and tarted a few other minor things).

It still needs the Dx3dX9_35.dll from the link Jim posted previously...if you do run this - run the debug version, the Release exe was just so I know I still have 1.5K left to tinker with...here's a piccy anyway:

Rbz

@da_fatstuff: that's very good work, some really interesting stuff in there.  :clap:
Are you turning this code into 4k intro ?   If yes, I just can't wait to see the final release :)
Challenge Trophies Won:

Clyde

:clap: Thats just plain amazing, nice work and welldone dude :clap:
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Shockwave

Generate some textures, work on the colours  and include a synth tune, then you have a top rated 4kb intro right there. Seriously, people will be impressed by it.

Really nice work.  :clap:

K+
Shockwave ^ Codigos
Challenge Trophies Won: