Lighting Bitmaps in Papervision

1 Comment

This is a technical article explaining how to code a light source into your Papervision 3D scene. Papervision is an open source 3d library for the Flash platform.

Lighting in Papervision is a very useful tool. It helps add realism to your 3D project. Often times, you will have a scene or 3D objects that have textures on them using bitmaps. You can easily add lighting effects to these textures.

First you’ll need to add the following import statements:
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.materials.shaders.PhongShader;
import org.papervision3d.materials.shaders.ShadedMaterial;

Phong Shading is a set of 3D rendering techniques which includes a model for reflecting light onto surfaces. It was invented by Bui Tuong Phong at the University of Utah, who published them in his 1973 Ph.D. dissertation.

Where you set up your scene, just create a light source
light = new PointLight3D();

In the part where you create your 3D objects that you want to light, you will create a PhongShader based on the light source and a ShadedMaterial based on your BitmapMaterial and your PhongShader. Please note that it HAS to be a BitmapMaterial and not a BitmapFileMaterial.

var earthBitmap:Bitmap = new earthAsset() as Bitmap;
var mat:BitmapMaterial = new BitmapMaterial(earthBitmap.bitmapData, true);

// create a shader using the light source
var phongShader:PhongShader = new PhongShader(light, 0xFFFFFF, 0x000000, 10);

// create the material based on the Phong shader
var phongShaderMat:ShadedMaterial = new ShadedMaterial(mat, phongShader);

globe = new Sphere(phongShaderMat, 200, 20, 20);

view.scene.addChild( globe );

The project source code is available for download here.

Popularity: 13% [?]

Issues With Embedding Flash SWF into Flex

3 Comments

Working on a project where I have a highly interactive Flash SWF that needed to be embedded in a Flex project. The thing would not work. Here’s what I did to get it to successfully embed.

  1. Use SWFLoader. If your Flash SWF is self contained and doesn’t access any outside resources, you’re pretty much set.
  2. For SWFLoader, be sure to specify the minWidth and minHeight properties.
  3. Remove/change the loaderInfo statement. stage.loaderInfo.parameters["someValue"]
    I was trying to get flashvars from the getgo and it was failing right here. Commenting out that line made it work.
  4. Here’s the kicker though – don’t embed your SWF. Load it dynamically. So this statement:
    <mx:SWFLoader width="100%" height="100%" source="@Embed(source='FlashAS3.swf')"
    scaleContent="false" autoLoad="true" minWidth="500" minHeight="400" />

    should look like this:

     <mx:SWFLoader width="100%" height="100%" source="FlashAS3.swf"
    scaleContent="false" autoLoad="true" minWidth="500" minHeight="400" /> 

    For some reason, when you embed the SWF, it doesn’t work.

  5. Finally, be sure to update all your paths if your SWF accesses resources such as XML and images dynamically.

Popularity: 22% [?]

Flex AS3 Swf to Swf Communication via LocalConnection

1 Comment

I needed to get A Flex application to have a 2 way communication to an embedded Flex app. The examples I found on the interweb were either 1 way or focused on Flash. Also, some of the samples on the net kept giving me this error –

Error #2082: Connect failed because the object is already connected.

Here’s the basics:

Sending a message is pretty easy. I’ve instantiated a LocalConnection called connection. To send messages, I call the send method. The arguments are the connection name, the method in the child swf that will be invoked, and the data to pass.

// receivedParentPingMethod is a function defined in the Child swf
connection.send("fromParent", "receivedParentPingMethod", txtSend.text);

To receive stuff from the child, you need to do 2 things:

//open a connection and listen to it
connection.connect("fromChild");

Create a function on the parent

//define a function that will be called from the child swf
// receivedChildPingMethod will get called from the Child
public function receivedChildPingMethod(msg:String):void {
        txtResult.text += "Received ping from child:\n"+msg+"\n\n";
}

You then do the same for the child. This is the caveat however. You can’t listen on the same connection for the child because that will throw said error. You’ll have to create a new connection and listen in on that. Something like this:

// listen to events from the parent
connection.connect("fromParent");

This part wasn’t obvious to me and was throwing me off.

Anywho, here’s the project. Enjoy.

Popularity: 12% [?]

Ruby On Rails 2.0 Tutorial For Dummies

2 Comments

I just started learning Ruby On Rails. Since I like to be on the bleeding edge, I decided to go 2.0. What I found was that although there were a lot of tutorials on Rails 1.2, there was hardly anything on 2.0. To make matters worse, just enough had changed to make the tutorials on the web not very helpful to me. Fortunately my good friend Josh Kahn helped walk me through the basics and so I decided to take notes. (Note: this is for a Windows installation.) Yeah, I know, I said I was a Mac guy, but this is for work.

Curt Hibbs has already wrote a good intro to Ruby on Rails and some of the prerequisites to installing Rails, so I won’t rewrite that. Following his lead, I installed:

Installation

  1. Install Ruby using the One-Click Ruby Installer for Windows. (Version 1.8.6-26 as of April 28, 2008).
  2. Install Rails. The Windows installer comes with RubyGems package manager which lets you install the latest version of Rails. Go into the command prompt and run the command
    gem install rails –remote

    Answer ‘y’ to each question.

  3. Install MySQL using the Windows Essentials version. (Version 4.1.22 as of April 28, 2008). Go through the configuration wizard and accept all the settings till you get to Security Settings. Uncheck “Modify Security Settings.”
  4. Install a MySQL front end. I used HeidiSQL, a free open-source one, but there’s no reason you can’t use something else.

The Fun Stuff

This being the web and all, I’m not gonna regurgitate what someone else wrote. I found this pretty easy to follow tutorial by the guy who wrote the ONLamp Ruby on Rails tutorial, but for 2.02. Here’s the Cliff’s notes version for those lazy people like me along with some extra steps that were essential but not covered in the tutorial.

  1. Create a new Rails application called bookstore. In the command prompt, type:
    rails bookstore
  2. Create a scaffold for the bookstore. For this simple example, the bookstore has books with properties title and description. In the command prompt, type:
    ruby script/generate scaffold Book title:string description:text
  3. Configure your database. In your bookstore/config folder, open database.yaml. Your development configuration should look like this:
    development:
      adapter: mysql
      database: bookstore
      host: localhost
      username: root
      password:
      timeout: 5000
  4. Run the database migration script. Now that you’ve configured the database properly, go to the command prompt and type:
    rake db:migrate

    This will create the necessary tables.

  5. Start the server. In your command prompt, navigate to the bookstore directory and type:
    ruby script/server

That’s it. In your browser, navigate to http://127.0.0.1:3000/books/ and you should see your first Ruby On Rails 2.0 app. If you found this post useful, leave a comment. Feedback is appreciated as well.

Popularity: 8% [?]