Web Info & Tutorials

November 17th, 2007

DOG WALKING/PET SITTING ( MONTRéAL ) MEILLEURE OFFRE

For Nuns Island Only, canid framework / pet biddy on call. Pour l’Île des Soeurs seulement : stroll de chien et gardiennage d’animal de compagnie à domicile. (sur appel)

November 17th, 2007
November 17th, 2007

3D CANVAS IN OPERA

Tim Johansson is talking about Opera's support for a 3d Canvas which differs from Mozilla's in that it doesn't map directly to OpenGL, which they did because:

  • It makes it easier to implement on non-OpenGL platforms (such as D3D)
  • We wanted to have some form of collision detection available

What can you do? Here is the interface that you get to work with:

JAVASCRIPT:
  1.  
  2. interface CanvasRenderingContextOpera3D {
  3.  
  4.   // state
  5.   void save(); // push state on state stack
  6.   void restore(); // pop state stack and restore state
  7.  
  8.   // scene/frame
  9.   void beginScene(); // start rendering a new frame
  10.   void endScene(); // finish rendering of the scene and present the result
  11.  
  12.   // transformations
  13.   void translate(in float x, in float y, in float z);
  14.   void scale(in float x, in float y, in float z);
  15.   void rotateX(in float rotation);
  16.   void rotateY(in float rotation);
  17.   void rotateZ(in float rotation);
  18.  
  19.   // rendering operation
  20.   void drawTriangle(in float x1, in float y1, in float z1, in float tex_s1, in float tex_t1,
  21.       in float x2, in float y2, in float z2, in float tex_s2, in float tex_t2,
  22.       in float x3, in float y3, in float z3, in float tex_s3, in float tex_t3);
  23.   void draw3DModel(in Canvas3DModel model);
  24.  
  25.   // create objects
  26.   CanvasTexture createTexture(in Image img);
  27.   Canvas3DModel create3DModel();
  28.  
  29.   // collision detection
  30.   string checkIntersection(in float x, in float y, in float z, in float radius, in Canvas3DModel model);
  31.  
  32.   // rendering state
  33.   attribute CanvasTexture texture; // current texture or null for no texture, default is null
  34.   attribute string color; // current color, default is transparent black
  35.   attribute float fov; // field of view of the scene in degrees, default is 45
  36.   attribute float nearPlane; // distance to the near clipping plane, default is 0.1
  37.   attribute float farPlane; // distance to the far clipping plane, default is 100
  38.   attribute string ztest; // "none", "less", "lessequal", "greater", "greaterequal", "equal", "notequal". Default is "lessequal"
  39.   attribute string blend; // "replace", "add", "srcalpha", "multiply". Default is "replace"
  40. };
  41.  
  42. interface Canvas3DModel {
  43.   void addVertex(in float x, in float y, in float z, in float s, in float t);
  44.   void addTriangle(in integer vertex1, in integer vertex2, in integer vertex3);
  45. };
  46.  
  47. interface CanvasTexture{
  48. };
  49.  

And, here is an example of a rotating cube (which you can see if you are using a new Opera build).

HTML:
  1.  
  2. <canvas id="canvas" width="200" height="200">
  3.   Canvas not supported!
  4. </canvas>
  5.  
  6.   var canvas;
  7.   var context3d;
  8.   var rotation;
  9.   var texture;
  10.   var cube;
  11.   function render(){
  12.     context3d.beginScene();
  13.     context3d.translate(0,0,-5);
  14.     context3d.rotateY(rotation);
  15.     context3d.rotateX(rotation);
  16.     rotation += 2;
  17.     context3d.color = "white";
  18.     context3d.draw3DModel(cube);
  19.     context3d.endScene();
  20.   }
  21.   function onTick(){
  22.     render();
  23.   }
  24.   function onload(){
  25.     canvas = document.getElementById("canvas");
  26.     context3d = canvas.getContext("opera-3d");
  27.     if (!context3d)
  28.     {
  29.       alert("3d canvas not supported");
  30.       return;
  31.     }
  32.     logo = new Image();
  33.     logo.src = "operalogo.png";
  34.     texture = context3d.createTexture(logo);
  35.     context3d.texture = texture;
  36.  
  37.     cube = context3d.create3DModel();
  38.     cube.addVertex(-1, 1, 1, 0, 0);
  39.     cube.addVertex(1, 1, 1, 1, 0);
  40.     cube.addVertex(-1, -1, 1, 0, 1);
  41.     cube.addVertex(1, -1, 1, 1, 1);
  42.     cube.addVertex(-1, 1, -1, 1, 1);
  43.     cube.addVertex(1, 1, -1, 0, 1);
  44.     cube.addVertex(-1, -1, -1, 1, 0);
  45.     cube.addVertex(1, -1, -1, 0, 0);
  46.  
  47.     cube.addTriangle(0,1,2);
  48.     cube.addTriangle(2,1,3);
  49.     cube.addTriangle(4,5,6);
  50.     cube.addTriangle(6,5,7);
  51.     cube.addTriangle(0,4,2);
  52.     cube.addTriangle(2,4,6);
  53.     cube.addTriangle(1,5,3);
  54.     cube.addTriangle(3,5,7);
  55.     cube.addTriangle(0,4,1);
  56.     cube.addTriangle(1,4,5);
  57.     cube.addTriangle(2,6,3);
  58.     cube.addTriangle(3,6,7);
  59.  
  60.     setInterval(onTick, 10);
  61.   }
  62.   document.onload = onload();
  63. </script>
  64.