Canvas 3d JS Library

WebGL made easy!
  • rss
  • What is C3DL?
  • Download
  • Tutorials
    • Tutorial #1: WebGL Browsers
    • Tutorial #2: A simple scene
    • Tutorial #3: Callback
    • Tutorial #4: Models
    • Tutorial #5: Light effects
    • Tutorial #6: Picking
    • Tutorial #7: Materials
    • Tutorial #8: Particle Systems
    • Tutorial #9: Camera Basics
    • Tutorial #10: Advanced FreeCamera
    • Tutorial #11: OrbitCamera
    • Tutorial #12: Advanced Camera Functions
  • Development News
  • Documentation
  • Community
  • Resources
  • Contact
  • About

Tutorial #12: Advanced Camera Functions

In the last several tutorials we’ve dealt with how cameras move and how the user can interact with them and control them in different ways. To finish off the chain of camera tutorials we’ll examine several functions (common to both types of camera’s) that allow you to control how much the camera can see. Like real cameras, the cameras in C3DL cannot see everything around them. They are both limited by a field of view. In addition, FreeCamera and OrbitCamera can choose to not see things that are very far away (why waste time drawing something that is so small you can barely see it), or very very close (because that might block your view and prevent you from seeing anything else). The distance at which near objects stop being drawn is known as the near clipping plane, while the distance at which far objects stop being drawn is called the far clipping plane. These three factors (field of view, near clipping plane, far clipping plane) are combined to determine what actually gets displayed to the user. An object must be inside the field of view, beyond the near clipping plane and closer than the far clipping plane in order to be seen. Anything outside of this will still be tracked by the program, but will not be drawn. For traditional photography, this would be outside the frame. The object is still there, you just can’t see it (or maybe can only see part of it).

These can be controlled with the functions setFieldOfView, setNearClippingPlane and setFarClippingPlane. The current values can also be queried using getFieldOfView, getNearClippingPlane and getFarClippingPlane.

The following code uses these functions to allow the user to control these settings in a scene. It requires the same duck model and texture we’ve been using in previous tutorials.

The HTML document

<html>
    <head>
        <title>Canvas3D tutorial #12: Advanced Camera Functions</title>
        <script type="application/javascript" src="../canvas3dapi/c3dapi.js" ></script>
        <script type="application/javascript" src="tutorial12.js"></script>
    </head>
    <body>
        <!-- Add a canvas element to the page. It is scripted by using its id -->
        <canvas id="tutorial" style="border: 2px solid blue" width="500" height="500"></canvas>
    </body>
</html>

The JavaScript Code

You can press the arrow keys to move the camera around in the same was as tutorial 11. Instead of rotation, this tutorial includes controls for the clipping planes and field of view. The w and s keys move the far clipping plane (farther away and closer respectively), changing how far you can see ducks in the distance. The q and a buttons move the near clipping plane (farther away and closer respectively), changing how close you can see ducks in the foreground. the e key will expand the vertical field of view, and d will contract it. If you expand the field of view you are displaying more of the scene in the same size canvas, so things will be squished towards the middle of the canvas. If you reduce the field of view, you are displaying less of the scene in the same canvas, so objects in the middle will get stretched, while objects around the top and bottom may be moved off the canvas entirely.

// Tutorial 12

c3dl.addMainCallBack(canvasMain, "tutorial");
c3dl.addModel("duck.dae");

var lastKey = -1;

function down(event)
{
	var cam = scn.getCamera();
    	var vel = [0,0,0];
    	if(event.keyCode != lastKey) {
        	lastKey = event.keyCode;
            	var mov = [0,0,0];
        	vel = cam.getLinearVel();
    		switch(event.keyCode) {//determine the key pressed
         	       	case 37://left arrow
                		mov = c3dl.multiplyVector(cam.getLeft(),1,mov);
        		break;
        		case 39://right arrow
                    		mov = c3dl.multiplyVector(cam.getLeft(),-1,mov);
                	break;
                	case 40://down arrow
            			mov = c3dl.multiplyVector(cam.getDir(),-1,mov); //move the camera 'back' (towards the user)
        		break;
                	case 38://up arrow
                		mov = c3dl.multiplyVector(cam.getDir(),1,mov);//move the camera 'forward' (into the scene)
        		break;
    		}
            	cam.setLinearVel([vel[0]+mov[0],vel[1]+mov[1],vel[2]+mov[2]]);
	}
}

function up(event)
{
	var cam = scn.getCamera();
        var vel;
        lastKey = -1;
	var pos = cam.getPosition();
	vel = cam.getLinearVel();
	switch(event.keyCode) {//determine the key released, if it is one of the linear velocity keys, set linear velocity to 0
		case 37://left arrow
		case 39://right arrow
			cam.setLinearVel([0,vel[1],vel[2]]);
		break;
		case 40://down arrow
		case 38://up arrow
			cam.setLinearVel([vel[0],vel[1],0]);
		break;
		case 87://w key - move far clipping plane away
			cam.setFarClippingPlane(cam.getFarClippingPlane() + 1000);
		break;
		case 83://s key - move far clipping plane closer
			cam.setFarClippingPlane(cam.getFarClippingPlane() - 1000);			
		break;
		case 81://q key - move near clipping plane farther away
			cam.setNearClippingPlane(cam.getNearClippingPlane() + 1000);
		break;
		case 65://a key - move near clipping plane closer
			cam.setNearClippingPlane(cam.getNearClippingPlane() - 1000);
		break;
		case 69://e key - increase field of view
			cam.setFieldOfView(cam.getFieldOfView() + 5);
		break;
		case 68://d key - decrease field of view
			cam.setFieldOfView(cam.getFieldOfView() - 5);			
		break;
	}
}

function canvasMain(canvasName){

 scn = new c3dl.Scene();
 scn.setCanvasTag(canvasName);

 renderer = new c3dl.WebGL();
 renderer.createRenderer(this);

 scn.setRenderer(renderer);
 scn.init(canvasName);

 if(renderer.isReady() )
 {
  var things = [];
  for(var i = 0; i < 70; i++) {
    things[i] = new c3dl.Collada();
    things[i].init("duck.dae");
    things[i].setTexture("duck.png");
    things[i].setPosition([0,0,200-(1000*i)]);
    scn.addObjectToScene(things[i]);
  }

  var cam = new c3dl.FreeCamera();
  cam.setPosition([0,400,100]);
  cam.setLookAtPoint([0,400,-100]);
  scn.setCamera(cam);
  
  scn.setKeyboardCallback(up,down);

  scn.startScene();
 }
}

These generally aren’t changed while a scene is running; you set them once at the beginning and leave them that way, otherwise you risk confusing the user. I thought it might be useful to take a look at them, so you can see what they do and learn how to change them if you need to. If you don’t want to change them, they start with a reasonable default.

Videos

Demos

  • Asteroids-3D
  • RTS Prototype
  • Particle Systems Demo
  • Cross-Browser Orbiter
  • Mocap Demo With Spheres
  • Google Maps-3D

C3DL Development News

A spec change that keeps coming back to haunt me

At some point, the way firefox handles keyboard events changed. I’m not sure exactly when it happened, all I know is that it broke how I was dealing with keyboard interaction on almost every demo I’ve written (for example,the mocap demo and MotionView). When I wrote the demos, the keydown event would be fired once, [...]

Release 2.2

The 2.2 Release of the Canvas 3D Library includes a number of new features, updates to old features and fixes for several bugs along with the requisite changes to meet the evolving WebGL spec. Some of the things included (in no particular order) are: Better picking code. The ability to swap textures as a scene [...]

Tutorials

  • Tutorial #1: WebGL Browsers
  • Tutorial #2: A simple scene
  • Tutorial #3: Callback
  • Tutorial #4: Models
  • Tutorial #5: Light effects
  • Tutorial #6: Picking
  • Tutorial #7: Materials
  • Tutorial #8: Particle Systems
  • Tutorial #9: Camera Basics
    • Tutorial9-YawPitchRoll
  • Tutorial #10: Advanced FreeCamera
  • Tutorial #11: OrbitCamera
  • Tutorial #12: Advanced Camera Functions

Documentation

Archives

Archives

C3DL Development News

Recent Comments

  • June 2011
  • March 2011
  • October 2010
  • July 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • A spec change that keeps coming back to haunt me
  • Release 2.2
  • 2.1 Release and things to come
  • Level Up! An Open Web Game Jam
  • Site moved!
  • SceneCreator0.3
  • WWW2010 in Raleigh
  • Motionview
  • On the train to Mountainview
  • C3DL 2.0-WebGL and beyond
  • That depends on what... - peter
  • This application is ... - Haisens
  • I think that example... - peter
  • The above links are ... - Atash
  • Hi there, just wante... - Patrick H. Lauke
  • Firefox 4 was releas... - Cathy Leung
  • In order to access l... - peter
  • I am not able to dis... - preksha
  • "JavaScript can’t di... - Joe Hocking
  • I should point out t... - peter



Canvas 3d JS Library

©2007- 2010 Canvas 3d JS Library

Disclaimer: This website is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Canada License.
The Canvas 3d JS Library and Demos found on this website are licenced under the MIT License

Creative Commons License