<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="/static/xsl/demos.xsl"?>
<demo title="Triangle Islands : opengl + haxe">
<wiki>
= Introduction =

I was looking around for a neat demo app to show off [/static/demos/random/archetype.haxe.xml Haxe Archetype]'s new [http://xinf.org/wiki/NekoOpenGL opengl] template when I came across [http://userpages.umbc.edu/~dhood2/courses/cmsc435/fall2007/?page=Blog&amp;tag=OpenGL this really nice description of the olde triangle mountain algorithm].

The basic idea is to subdivide triangles and randomly move vertices up or down like this really nice diagram shows:

</wiki>

<diagram src="/static/images/demos/triangle.island/mountain.png"/>

<wiki>

= [http://www.youtube.com/watch?v=7TQByv_xkuc Splitting up Triangles and Making them Mountains] =

OK... so maybe that looks a little confusing... but the text makes it really clear: split each triangle into 4 subtriangles. Sometimes, like [http://www.groo.com/ Groo], I'm a little slow of mind, so I drew a little picture to help me out:

</wiki>

<diagram src="/static/images/demos/triangle.island/split_the_triangle_4_ways.png"/>

<wiki>
From this, my brain could pretty directly come up with:
</wiki>

<code>
 t[ 0 ] =  pt1, mid1, mid3
 t[ 1 ] = mid1,  pt2, mid2
 t[ 2 ] = mid3, mid2,  pt3
 t[ 3 ] = mid1, mid2, mid3
</code>

<wiki>

It looks like [http://code.google.com/p/brianin3d-triangle-mountain/source/browse/trunk/us/versus/them/triangle_mountain/Triangle.hx#16 this] in [http://haxe.org haxe].

= Avoid the Gaps =

So far so good! Split up the triangles! Raise/Lower those vertices! But...

'''BEWARE!'''

If you just start jacking around willy-nilly (or even willy+nilly wiki_esc[untested with willy&#177;nilly]), you might not notice that when it's time to split ''t[1]'' and and ''t[3]'' they are '''both''' gunna split the segment that connects ''mid1'' and ''mid2''.

Lawd help you if you raise that pt in one and lower it in the other... or even raise it by different amounts: you will gap yourself up good and you won't like it.

It will look something like this:

/static/images/demos/triangle.island/gapped_up_island.jpg

and noone will think you are cool...

My solution is pretty uncool actually: [http://code.google.com/p/brianin3d-triangle-mountain/source/browse/trunk/us/versus/them/triangle_mountain/MidPointBuddy.hx MidPointBuddy] has a '''getMidPoint''' which returns the midpoint for any segment and keeps track of them in a hash table.

Simple, but effective...

= Some minor openGL points =

If you know me or if you have read any of this you probaly know that I completely and shamelessly rip off everyone all the time and never do my own work. 

Occassionaly I might throw together some crappy third rate ascii art:
</wiki>

<code>
/*  p1              p2
      .------------. 
       \` .         \
        \   ` .      \
         \     ` .    \
          \        ` . \
        p4 .------------. p3  */
var p1 = new Pt3( -f, 0, -f );
var p2 = new Pt3(  f, 0, -f );
var p3 = new Pt3(  f, 0,  f );
var p4 = new Pt3( -f, 0,  f );
</code>

<wiki>

but mostly I just rip everything off from the internet and never make any sort of contributions to anything. 

This is the case with the [http://code.google.com/p/brianin3d-triangle-mountain/source/browse/trunk/us/versus/them/triangle_mountain/App.hx app] that drives this mess. "Borrowed" it from the [http://svn.xinf.org/xinf/trunk/libs/opengl/test/App.hx xinf gang].

Here is another example... I used to have some code like this a billion years ago, but I have no idea where it is now... probably on some thumb drive...

Luckily, there is no point in knowing of remembering anything anymore and with the help of [http://www.sjbaker.org/steve/omniv/opengl_lighting.html Steve Baker] (who I've never met, but I hear is very nice), I cobbed something like [http://code.google.com/p/brianin3d-triangle-mountain/source/browse/trunk/us/versus/them/triangle_mountain/App.hx#160 this] together:
</wiki>

<code>
GL.shadeModel( GL.FRONT_AND_BACK );
GL.enable( GL.LIGHTING );
GL.enable( GL.COLOR_MATERIAL );
GL.colorMaterial( GL.FRONT_AND_BACK, GL.AMBIENT_AND_DIFFUSE );
GL.enable( GL.LIGHT0 );
</code>

<wiki>
Put that together with a [http://code.google.com/p/brianin3d-triangle-mountain/source/browse/trunk/us/versus/them/triangle_mountain/Triangle.hx#28 little]:
</wiki>

<code>
e1 = p1 - p2;
e2 = p3 - p2;
normal.cross( e1, e2 ).normalize();
...
cross(e1,e2) {
	this.x = ( e1.y * e2.z - e1.z * e2.y );
	this.y = ( e1.z * e2.x - e1.x * e2.z );
	this.z = ( e1.x * e2.y - e1.y * e2.x );
	return this;
}
</code>

<wiki>
sprinkle in a little:

 GL.normal3( x, y, z );

to taste and off it goes.
</wiki>


<wiki>

= Go get it and run it! I'm not charging you for it! =

I know you have already [http://haxe.org/download download and installed haxe] and must have subversion installed... 

</wiki>

<code>
 % svn checkout http://brianin3d-triangle-mountain.googlecode.com/svn/trunk/ brianin3d-triangle-mountain-read-only  
 % cd brianin3d-triangle-mountain-read-only
 % haxe compile.hxml
 % ./app
</code>

<wiki>

I apologize for the controls: x,y,z,q,r

Check it out and see how easy it is to have a lot of fun with [http://xinf.org/wiki/NekoOpenGL opengl] and [http://haxe.org haxe]. 

Got a better idea for a project? Fire it up with [/static/demos/random/archetype.haxe.xml Haxe Archetype]'s new flash tip:

 haxelib run archetype create -type=opengl -artifactId=triangle_mountain -packageName=us.versus.them

Holler

= Links =

* [http://code.google.com/p/brianin3d-triangle-mountain/ the code]
* [http://userpages.umbc.edu/~dhood2/courses/cmsc435/fall2007/?page=Blog&amp;tag=OpenGL dhood's blog]
* [/static/demos/random/archetype.haxe.xml Haxe Archetype]
* [http://xinf.org/wiki/NekoOpenGL opengl for Haxe]
* [http://www.sjbaker.org/steve/omniv/opengl_lighting.html Basic OpenGL Lighting]

= Saddy Faces? =

Sometimes things don't work out like you'd like... I know... it's a bummer...

== XF86VidModeQueryExtension ==

This happens on my workstation, but not on my desktop:
</wiki>

<code>
Called from opengl/GLU.hx line 271
Called from GLFW__impl.hx line 16
Uncaught exception - load.c(232) : Failed to load library : /usr/lib/haxe/lib/opengl/0,2,0/ndll/Linux/opengl.ndll (/usr/lib/haxe/lib/opengl/0,2,0/ndll/Linux/opengl.ndll: undefined symbol: XF86VidModeQueryExtension)
</code>

they are both running Ubuntu 8.10, both with NVidia drivers... The problem '''only''' occurs on the workstation.

<wiki>
</wiki>

</demo>

