Spline Update

July 12th, 2010 admin No comments

From the feedback I got it seems people aren’t keen on doing things themselves. So I’ve uploaded a full solution:

url: http://www.daksystems.net/svn/public/Spline/
Login: Guest
password: guest

As always you can access this via an SVN client or by simply using your browser, your choice. I’ve modified things slightly to use a nehe demo that includes arcball rotation for moving your scene around to view the spline. Points are still hardcoded, I’m not going to write you lot an importer. I’ve added a keystroke for switching between Bezier(‘b’) and Catmull-Rom(‘c’) splines. This will give you an idea of how these two curves are different.

It was noted that my implementation doesn’t properly include end points. That is sort of true, to connect to the start and end curve points just double them up. IE, have 2 copies of your start and 2 copies of your end.

As an aside I’d like to thank Makeeva for the wonderful private comment that reads as follows:

“Dont post exampels if you dont have everything there!!! ive spent a hour now and havent gotten it working. you’re program is like you, dumb.”

Allow me to take this moment to apologize for not providing the proper hand holding to go along with my 4:00a.m. post. As proper punishment for my transgression I will hand convert all of my code to assembly before recompiling my next example.

.model small
.stack
.data
message   db "Not bloody likely", "$"
.code
main   proc
   mov   ax,seg message
   mov   ds,ax
 
   mov   ah,09
   lea   dx,message
   int   21h
 
   mov   ax,4c00h
   int   21h
main   endp
end main
Categories: Uncategorized Tags:

Late night splining

July 5th, 2010 admin No comments

I love coding, I really do. I’m not saying I want to spend the rest of my life writing code but there is something cathartic about the process. Our lives are filled with so many instabilities that it is nice to sit down w/ a system based purely on logic and know that following the rules will lead to an expected result.

Some posts back I made a reference to catmull-rom splines but the code was messed up during the copy process to the blog. Here today I’m going to attach a single .cpp file that will create a spline and render it in OpenGL. Easy right?

Splines.cpp

95% of this code is NeHe’s wonderfully simplistic OpenGL tutorial. That’s how little code it takes to spline. Given that it is 3:30am. you’ll have to forgive my lack of comments in the Spline.cpp file. But these should suffice:

 
 
///catmull-rom spline (t should range from 0-1)
double catmullrom(double t, double p0,double p1,double p2,double p3){
	double t2 = t*t;
	double t3 = t2 * t;
	return (0.5 *(    	(2 * p1) + (-p0 + p2) * t +(2*p0 - 5*p1 + 4*p2 - p3) * t2 +(-p0 + 3*p1- 3*p2 + p3) * t3));
}
 
///create a dummy point vector, points here are chosen @ random
void FillPointVector(){
	pointList.push_back(vector3(-500,700,10));
	pointList.push_back(vector3(-300,12,10));
	pointList.push_back(vector3(-300,12,10));
	pointList.push_back(vector3(-100,12,10));
	pointList.push_back(vector3(-50,45,0));
	pointList.push_back(vector3(0,0,20));
	pointList.push_back(vector3(50,0,15));
	pointList.push_back(vector3(200,350,30));
	pointList.push_back(vector3(350,0,20));
	pointList.push_back(vector3(356,35,60));
	pointList.push_back(vector3(400,67,15));
	pointList.push_back(vector3(424,122,0));
	pointList.push_back(vector3(450,1,0));
	pointList.push_back(vector3(500,0,0));
}
 
///Build full path;
void BuildPath(){
	FillPointVector();
 
	printf("Point vector filled: %d\n",pointList.size());
	float granularity = 0.1f;  // This refers to the step size between points along a spline.
	printf("Filled line list...");
 
	for(int c = 0; c < pointList.size()-3; c++){
		vector3 p0,p1,p2,p3;
		p0 = pointList[c];
		p1 = pointList[c+1];
		p2 = pointList[c+2];
		p3 = pointList[c+3];
		for(float i = 0; i <= 1; i+=granularity){
			vector3 pt;
			pt.x = catmullrom(i,p0.x,p1.x,p2.x,p3.x);
			pt.y = catmullrom(i,p0.y,p1.y,p2.y,p3.y);
			pt.z = catmullrom(i,p0.z,p1.z,p2.z,p3.z);
 
                        ///ignore this crap
			if(i==0){
				minPt = maxPt = pt;
			} else {
				minPt = vector3::Min(minPt,pt);
				maxPt = vector3::Max(maxPt,pt);
			}
			lineList.push_back(pt);
		}
		printf(".");
	}
	printf("Complete[%d]\n",lineList.size());
 
	printf("Sample: %f %f %f\n",lineList[0].x,lineList[0].y,lineList[0].z);
 
        //make sure line list is EVEN or we'll run in to problems w/ OpenGL.
        if(lineList.size() % 2 != 1 && lineList.size() != 0)lineList.pop_back();
}

Take note that splines are made up of 4 control points and we merge these to form one cohesive path.

Categories: Uncategorized Tags:

Poor man’s A/C

June 22nd, 2010 Jerdak No comments

Lifehacker brings this interesting article about staying cool w/o the need for air conditioning (…. no thank you) but the first item on their list made me smile.

Back in college our “air conditioner” was a bucket/bag of ice sitting in front of a fan. The “peak” cooling time was when the ice had somewhat melted and the fan began blowing a tiny mist throughout the room.

Categories: Uncategorized Tags:

Sad, but in a crotch kicking way

June 17th, 2010 admin No comments

I read this on CNN today and it made me sad, very sad:

He singled out this sentence from Obama as unfortunate: “That is why just after the rig sank, I assembled a team of our nation’s best scientists and engineers to tackle this challenge — a team led by Dr. Steven Chu, a Nobel Prize-winning physicist and our nation’s secretary of energy.”

“A little less professorial, less academic and more ordinary,” Payack recommended. “That’s the type of phraseology that makes you (appear) aloof and out of touch.”

This in reference to Obama’s speech regarding the oil spill. Apparently it was written for 10th graders according to this Paul J.J. Payack, president of Global Language Monitor. I’m not up on Payack’s qualifications and I dearly hope he is just some unqualified hack. The alternative to Payack being a crank is that American’s are so fuck stupid they can’t follow the sentence above. I’d like to know what elements of the sentence might throw those w/ a lesser reading level.

Lets look some possible problems:

  1. # of words:  There are roughly 41 words.  I haven’t added up all the characters but there is a good chance that wouldn’t fit on twitter or in a text.  Obama needs to dumb it down a notch.  Use monosyllabic words only:  “Rig sank, I make team, clean bad stuff.”  Done and done bitches, post it!
  2. Nobel prize winning“:  We know from our republican “pundits” that American’s don’t like that fancy edumacation crap.  We want our leaders borderline retarded and their scientists no less so.  We want Joe Everyman, who didn’t need fancy schoolin’ to teach him the hard rules of life, who dropped out of school at the age of 6 to work in the coal mine, who barely understands how to actually count his age on his hand.  Besides, “scientist” is really just another word for elitist prick.  One step below someone who adopts a fake British accent, eh govna.
  3. physicist“:  Alright, to be fair to Payack, there is a good chance most American’s think this word describes a high school gym teacher and who wants that guy running the show?
  4. The “,”:  I see 2 comma’s in that sentence to break up the thought.  Obama, during recitation, should have simply rambled w/ nary a pause for breath.  How else are American’s supposed to get all that he says while managing to ignore their children and txt their friends about the latest Jersey Shore developments?

It’s a sad fucking state of affairs when our president needs to dumb it down from easily understandable to …. whatever the fuck comes below that.  I wonder if, in the years to come, we’ll have a president who talks to the “average American” and the other 70% of us are left in the cold trying to decipher what can only be described as “drooled ramblings.”

Categories: Uncategorized Tags:

Beauty of Unity3D

June 15th, 2010 admin No comments

Previously I touched a bit on adding 3D content to a page using the, as yet, unfinalized WebGL standard. The page I made was simple and displayed a non-shaded bspline. But what if you aren’t savvy or maybe you lack the time to wrangle the oddities of OpenGL? The best option that I’ve found so far is Unity3D, a free(for the Indie version) game engine.

One of my most popular posts covers merging Ogre3D w/ Havok physics. That project actually continued long after the page I posted was up (sadly the code couldn’t be updated as it became an official project) and I was having a blast. The problem was I never had enough time. As the only developer on the project I had to handle every piece of the engine. I could pull from many open source options but integration was never fast and easy, like a Yeti. On top of that there wasn’t any sort of scripting framework, all changes had to be written in raw C++ and recompiled. Scripting was always planned but kept taking a back seat to graphics, networking, and physics.


Why am I blathering (BLATHERING BLATHERSKITE!…. Anyone? DuckTales?) on about time and 3D engines? Unity3D pretty much does everything you need and then some w/o the hassle of coding an entire engine. The beauty of their engine isn’t necessarily the tech., it’s the editor. Simple to understand, easy to use, and extremely friendly on novice users. Indeed, everything is tied to an impressive C#/Java scripting framework that makes accessing existing core functionality a snap. Coupled w/ an active community and some decent examples Unity3D is very easy to pick up.

Scripts are compiled on the fly in to a sort of pseudo bytecode(also known as jit compiled, or just in time compiled).

It is around 20 times faster than traditional javascript and around 50% slower than native C++ code.

The only real downside is the lack of control over the core at the C++ level. Adding in features is definitely possible but if you are someone that needs complete control over every aspect of his/her engine you’ll be disappointed. All this means is you have to be clever with the tools you are given in order to create a unique game. Additionally if you are looking to make something other than a generic RTS or FPS you’ll need the pro version which offers full shader support and RTT.

To see an example, click below to download the Unity plugin. After which the 3D image of a toy gun should appear. This is a VERY simple model viewer I threw together in maybe 10 minutes? It took me longer to write this post.


Left/Right Arrow Keys – Rotate around Y-Axis (Spin)
Up/Down Arrow Keys – Rotate around X-Axis (Tilt)

And this is the only script in that scene:

var rotateSpeed : float = 1.0;
var center : Vector3 = Vector3.zero;
var prefab : Transform;
 
 
private var windowRect : Rect = new Rect(0,0,150,50);
 
function Awake(){
	///calculate model center;
	var mesh : Mesh = GetComponent(MeshFilter).mesh;
	var bounds = mesh.bounds;
 
	///Be sure to apply all transformations current present in the model.
	center = mesh.bounds.center;
	center = transform.rotation * center;
	center = center * transform.localScale.x;
	center += transform.position;
 
	///Draw a dummy object to the scene, not necessary but helps to debug odd centers.
	print("Center: " + center.x + "," + center.y + "," + center.z);
	Instantiate(prefab,center, Quaternion.identity);
}
 
function OnGUI () {
	windowRect = GUI.Window (0, windowRect, SpeedGUI, "Speed Control");
}
function SpeedGUI(id : int){
	rotateSpeed = GUI.HorizontalSlider (Rect (25, 25, 100, 30), rotateSpeed, 1.0, 25.0);
}
function Update () {
 
	//Key left/right & up/down key movements
	var rotZ = Input.GetAxis("Vertical");
	var rotY = Input.GetAxis("Horizontal");
 
	//Rotate model around it's center.
	transform.RotateAround(center, Vector3.up,-rotY * rotateSpeed);
	transform.RotateAround(center, Vector3.right,rotZ * rotateSpeed);
}
Categories: Coding, Technology Tags: , , , ,