Generating VRML Using ASPs

Scott Parent - November 14, 1999

Combining VRML and ASP technologies goes a long way towards server-side automation of a virtual world. The advantages ASP bring to VRML include server-side variables, control structures, session management, COM objects, Database Access and, potentially, background supplication of virtual world detail.

ASP scripting languages offer a nice vehicle for adding conditional and iterative generation of model elements. The following ASP Code segment generates a wall using the x, y, z and rot parameters. The conditional logic in the middle uses a global variable, wallDefined, to decide whether to generate the VRML code for defining the shape called, "Wall," or using the existing definition. As with any programming task, subroutines, parameters and conditional logic all contribute towards making reusable and readable elements.

Sub MakeWall( x, y, z, rot)
%>
	Transform {
		translation <%=x & " " & y & " " & z%>
		rotation 0 1 0 <%=rot%>
		children [
<%			if wallDefined then %>
				USE Wall
<%			else %>
				DEF Wall Shape {
					appearance USE WallAppearance
					geometry Box { size 8 4 1 }
				}
<%
				wallDefined = true
			end if 
%>
			]
		}
<%
End Sub

 

COM Objects

The ability to encapsulate behaviors programmed in C++, VB or Java--and to expose the public attributes and methods of those behaviors--is of immense value. Not only does it promote reusability of these behaviors, but the tools used to develop them offer potentially significant performance and quality advantages and feature-richness when compared to ASPs scripting languages. When employed by one of these scripting languages, however, COM objects also offer very nice semantics.

In the following example, the COM object called, theLabyrinth, represents a dynamically generated maze built out of isometric Rooms. Every Room has at most three walls--leaving at least one opening. The Labyrinth's RoomAt method returns the room from it's maze at the given row, column and floor arguments: x, y, and 0, respectively, in this example. This segment also makes use of the MakeWall subroutine, shown above.

For x = 0 To mWidth - 1
	For y = 0 To mHeight - 1
		set r = theLabyrinth.RoomAt( x, y, 0)
		roomTop = y * roomHeight
		roomLeft = x * roomWidth
		If r.portal(North) Is Nothing Then 'draw north wall
			MakeWall roomLeft + 4, 0, roomTop, 0
		End If
		If r.portal(East) Is Nothing Then 'draw east wall
			MakeWall roomLeft, 0, roomTop+4, 1.571
		End If
	Next
Next

The variable, r, in this example is, of course, a Room object. The portal( direction) method of room returns the special VBScript value, Nothing, when a wall exists on the side of the Room indicated by the direction parameter.

Another advantage of ASP being COM-enabled is that a vast and ever-growing library of components exists for all kinds of tasks. One package of particular interest is ActiveX Data Objects--or, ADO. Simply stated, ADO provides a set of COM objects for database access. For server-side, virtual world automation, ADO could be used to persist pieces of the world. Inversely--for data visualization experiences--the world could be generated based on a set of real world data: numeric and financial measurements, projections, geographical attributes, etc.

Discoveries

Before I successfully used ASP to generate a virtual world that I could navigate, I encountered a variety of obstacles. For some of these issues, I simply could not find any substantial documentation or examples to work from. For others, the published information is simply wrong. In the end though, I believe I have arrived at a list of ingredients for a working combination.

For starters, the browser cannot navigate directly to an ASP generating VRML content--at least with the two VRML browsers I experimented with. The problems that arose were different from browser to browser--and from HTTP/1.0 to HTTP/1.1. In all cases, however, the browser appeared unable to make sense of IIS' encoding of VRML generated by an ASP.

Instead, the connection must be made through an EMBED tag. That is, the browser must navigate to document of type text/html--either an HTML document or another ASP--and this document must contain an EMBED tag such as the following:

<EMBED height=100% width=100% src=labyrinth1.asp name=world type="x-world/x-vrml">

Specifying the MIME type in the EMBED tag is a critical part of this formula. A beneficial side-effect of this approach, however, is that one can look at the VRML code generated by the ASP. That is, the embeded URL can be invoked directly to show a plain-text version of the VRML code. This is particularly useful in the event of a failure that causes the VRML viewer to crash--such as a server error message.

Example: Labyrinth Generator

The example code presented above stems from a test project that I put together for my research. This project uses active server pages to present a dynamically created labyrinth to the user. The VRML browser, then, allows the user to navigate around this maze.

Components

The project uses two active server pages and a COM object written in Java using Microsoft J++. The COM object exposes two classes: Environment and Room. The Environment object--instantiated as theLabyrinth in the example ASP code--knows how to randomly generate a maze of given dimensions. The pieces of this maze are cubicle sections: Room objects.

Though interface provisions are made for multi-level mazes, this version of the component supports only a single level.

Segments of code from both ASPs are shown in the examples above. One ASP, Labyrinth1.asp, instantiates an Environment object, has that object generate a maze and converts that maze into a VRML representation.

The other ASP, Labyrinth0.asp, simply provides the EMBED tag for Labyrinth1.asp. To begin the process, the browser is directed to the URL for Labyrinth0.asp.

Screen Shots

Upon navigating the Labyrinth0.asp, the server generates the maze and specifies two viewpoints. A VRML viewpoint is the combined position and orientation from which the user views the virtual world. The default viewpoint, shown above, is labeled as, "from within."

The second viewpoint, shown below, is labeled, "The Top." Both viewpoints are calculated by the ASP based on the center and size of the maze.

Active Server Page Code - Labyrinth0.asp

<%@ Language=VBScript %>
<%
	Response.Expires = -1 ' always regenerate this page
%>
<HTML>
<HEAD>
<TITLE>VRML Test 2</TITLE>
</HEAD>
<BODY>
<EMBED height=100% width=100% src=labyrinth1.asp name=world type="x-world/x-vrml">
</BODY>
</HTML>

Labyrinth1.asp

#VRML V2.0 utf8
<%
Sub MakeWall( x, y, z, rot)
%>
	Transform {
		translation <%=x%> 0 <%=z%>
		rotation 0 1 0 <%=rot%>
		children [
<%			if wallDefined then %>
				USE Wall
<%			else %>
				DEF Wall Shape {
					appearance USE WallAppearance
					geometry Box { size 8 4 1 }
				}
<%
				wallDefined = true
			end if 
%>
			]
		}
<%
End Sub
	' Script starts here -------------
	set theLabyrinth = 
		Server.CreateObject("Labyrinth.Environment")
	const North = 0
	const East = 2
	mWidth = 20
	mHeight = 20
	theLabyrinth.BuildSpace mWidth, mHeight, 1
	wallDefined = false
%>
	Background {
		skyColor 1 1 1
	}
	DirectionalLight { 
		direction -0.33887 0.049 -0.93956 
		color 0.24706 0.24706 0.49804 
		intensity 1 
		on TRUE 
	}
	DEF WallAppearance Appearance {
		texture ImageTexture { url "stone7.jpg" }
	}
<%
	roomWidth = 8
	roomHeight = 8
	For x = 0 To mWidth - 1
		For y = 0 To mHeight - 1
			set r = theLabyrinth.RoomAt( x, y, 0)
			roomTop = y * roomHeight
			roomLeft = x * roomWidth
			If r.portal(North) Is Nothing Then 
				'draw north wall
				MakeWall roomLeft + 4, 0, roomTop, 0
			End If
			If r.portal(East) Is Nothing Then 
				'draw east wall
				MakeWall roomLeft, 0, roomTop+4, 1.571
			End If
		Next
	Next
	middleX = ( 4 + mWidth * roomWidth / 2)
	middleZ = ( 4 + mHeight * roomHeight / 2)
%>
	DEF FromWithin Viewpoint { 
		position <%=middleX & " 0 " & middleZ%>
		description "From Within" 
	}
	DEF TheTop Viewpoint { 
		position <%=middleX & " " %> 
			<%=( mWidth * roomWidth * 2) & " "%> 
			<%=middleZ %> 
		orientation 1 0 0 -1.571 
		description "The Top" 
	}

Generated World

If your browser is fitted with a VRML viewer, you should be able to navigate this scene. Though this model was created using the above ASP code, the generated code has been saved as static VRML.

Media Credits

The image file used to texture the labyrinth walls in this model is "stone7.jpg". This image was shipped with a modeling tool called, "VRCreator" from a company called "VREAM."