Thursday, April 21, 2011

Unfortunately, I haven't had a chance to start porting any of the OpenGL SuperBible classes to either Java or Clojure yet.  Hopefully now that I've got a long weekend, I can focus some on it.  Also, my priorities have shifted a little somewhat.

At my work, I've been tinkering with the possibility of using JBoss's netty project to implement some of the things we need to do.  As it so happens, my little game project will also need some networking capability.  For example, it must have the ability to let connected users chat in a room, and also to update "figures" on a map.  I also think it would be useful for getting updates on the software itself.

Netty, though a well documented project, also has a bit of a learning curve.  One of the things I'd like to be able to do is to launch a subprocess in Java, capture that process's stdout, and redirect it to a Netty channel.  Hopefully, I can do this in clojure.  This of course also means I need to build my own version of clojure.contrib.shell-out, since that doesn't seem to do exactly what I need (though I probably should take a look at the source).

In other words, there's a lot of stuff I need to do:


  1. Reimplement my own version of clojure.contrib.shell-out
  2. Create a netty based async IO system I can use for:
    • Chatting between players/GM
    • Launch subprocess and redirect stdout to the netty channel
    • Update figures on a map
    • Download updates to the project
  3. Create a subprocess launcher that I have access to the stdout
  4. Start wrapping the SuperBible libraries (probably straight to clojure via lwjgl)

I've actually begun updating the sojourner project again, though none of the bullet points listed above are anywhere close to done.  I also want to lay down a little general project planning for sojourner rather than just tackle things on the fly like I have been doing.  Having a roadmap helps enforce goals and you can see where you are heading and how much farther you have to go.


Saturday, April 9, 2011

Start porting some of OpenGL SuperBible classes to clojure

My hard copy book of The Joy of Clojure finally came in, and I feel reinvigorated to start programming in my spare time.  As I've been reading the SuperBible book, I realize that it is very much geared towards using its built in classes.  For example, it uses a GLShaderManager class to handle a lot of the shader programs, and it has its own library that deals with a lot of the math basics.

Since I want to do this in clojure, I'm going to have to port or at least heavily borrow from these C++ classes in order to use them.  It's also been about 2 months since I've touched any clojure code, and it's already gotten quite rusty.  For example, while reading through the Joy of Clojure book, I already forgot the differences between map, apply and reduce.  So I hopefully porting the C++ classes to clojure will be a good way to refreshen my clojure.

I'm still not sure what the best way to do this will be.  On one hand, it might be better to port it to Java first, and then make clojure wrappers around it.  If I do that, more people will be able to benefit from it, and I could also in theory use jython or Scala with it if I wanted (not that I know Scala that well).  On the other hand, I think that will not only be more work, there might be stuff lost in translation.  Just like how the Bible got corrupted by going from Aramaic to Latin to English, so too I think would going from C++ to Java to clojure.  That being said, I'll have to deal with Java at some level since I will be using lwjgl as my interface to OpenGL.

So starting today, I'll start work on this port in my sojourner project.  Which by the way, I've totally forgotten how to use leiningen with.  I started looking at cake, but its Windows support seems to be a little iffy, even though I dislike Windows.  Speaking of which, I might also throw Chakra on my main desktop, as its a more user-friendly version of Arch linux.  My other option is throwing Sabayon.  I really want to use a rolling-release style of linux rather than point release versions since upgrading almost inevitably winds up breaking something pretty badly.

Wednesday, March 23, 2011

OpenGL one step at a time

After realizing that Oracle was going to move to JavaFX, and that JavaFX could do hardware acceleration, I decided I may as well learn 3d programming.  Since I'm a believer in cross-platformness, I am going to use OpenGL for my programming.

From my initial foray into learning OpenGL, it appears that starting with version 3.0 of OpenGL, the use of GLSL is kind of mandatory (at least if you use the core profile, I am a bit confused about the compatibility spec).  But basically, the old fixed function pipeline is being deprecated and should eventually go the way of the dodo.

So I finally got my OpenGL SuperBible 5th edition book on Friday.  I think I will create a new project under bitbucket which will basically be me following the book's examples, but using Clojure.  How will I manage that since the code examples are all C/C++?  Fortunately, lwjgl (Lightweight Java Game Library) has ported almost all of the OpenGL Core and GLSL API to java.

This will require a bit of mental juggling.  I will have to convert C/C++ to Java, and then from Java to Clojure.  This will also be interesting because OpenGL is in some ways a huge state machine, and Clojure being a functional language, takes a different spin on saving state.  That being said, I don't think there's a Java port for GLSL.  And GLSL is required for shaders.

GLSL is basically a weird version of C with very specific data types and keywords.  For example, GLSL makes a distinction between uniform data flow and varying data flow.  If you have some nested if/else blocks, or switches, then that would be varying data flow, and variables must be noted as such.  Also, GLSL has many specific built-in data types representing vectors and matrices.

What really surprised me is that shader programs are stored as strings and then fed to a compilation unit on the video card.  Basically it's compiling them on the fly.  I did see however that it appears that in OpenGL 4.1, you can store and open binary files (presumably pre-compiled shaders?).  I'll have to study the stock shaders that the book's project comes with.  Although GLSL kind of looks like C code, there are many new keywords that I don't fully understand yet.  For example, invariant, uniform, or varying.

The OpenGL SuperBible weighs in at almost 1000 pages, although a good 300+ pages are Man Pages from OpenGL.  Nevertheless, that leaves around 650 pages just on OpenGL.  It appears that it's mostly about OpenGL itself and not GLSL, though there does appear to be one chapter devoted to it.  Combined with the fact that I am also going to have to refresh my knowledge of linear algebra, this is going to be extremely slow going.

Also, the book is geared towards writing C++ programs, using GLUT or freeglut, and glew.  Furthermore, the book's project comes with a home-grown library called GLTools which has a lot of utility type functions.  In essence, I am going to have to translate all this into Java-speak, and more specifically into lwjgl speak.  For example, lwjgl comes with its own windowing classes that has the same job role as GLUT.  I haven't gotten far enough into the book, but I also wonder if it goes into any controller input issues (for example, getting input from a joystick).

I also want to get in on OpenCL, the open version of Nvidia's CUDA which allows for processing on a GPU's stream units.  The lwjgl project also has ported (most of) OpenCL which means I can use this as well.  Given the vast parallelism given by the GPU, this should be able to make for some interesting AI possibilities.  What will be really interesting as I play with this will be to see the differences between writing concurrent programs for CPU's (which generally speaking are Task oriented in OpenCL lingo), and concurrent programs for GPUs (which are generally Data oriented).

I'm really curious how I can deal with OpenGL's state machine using clojure.  But I don't know enough about the state machine to understand how to best model a clojure representation of it.

Anyhow, this is all pretty interesting, and hopefully within the next week or two, I'll be updating my sorely ignored sojourner project with some new stuff.

Sunday, February 20, 2011

Slow going

I haven't posted too much here recently for a few reasons.

1.  I'm boning up on OpenGL, particularly GLSL
2.  I'm re-learning linear algebra (in order to do matrix multiplication)
3.  I'm getting back to basics with clojure and reading the rest of The Joy of Clojure and Clojure in Action
4.  I'm getting serious about working out again

I will post soon a blog about debugging shared libraries however.

So I haven't had time to work on the sojourner project directly.  I really wish I had a job where I could program in clojure (or lisp) full time.  I also need to get more serious about math again.  I hated how all my math skills rusted away from non-use.

I did have a strange revelation though about math while re-reading Steve Hagan's excellent "Buddhism is not What You Think".  One of the central tenets of Buddhism is impermanence.  Nothing is permanent and everything changes from moment to moment.  This is in direct contrast to functional programming and mathematics in general.  Or at least so I at first thought.

A number is unchanging.  5 has been, is and always shall be 5.  But what exactly is 5?  Is it an abstraction?  A concept?  Buddhism makes it clear that reality is not a concept.  Even mathematicians debate about the "reality" of much of math.  To put it another way, is there truly a pure right angle that exists in nature?  When you say that you have 3 quarters, is 3 a thing, or simply a pointer to reality?

Deep down, I wish I had been a philosopher.  And one day, I will put all this aside and become a mystic.  But for now, I am still beholden to the world.  I do think i have entered the stream, but I am still reluctant to cross to the other side.

Sunday, January 30, 2011

Sound mind in a sound body

Like probably millions of Americans, I decided that one of my New Year's resolutions was to lose weight.  However, I also want to exercise my mind as well as my body.  Although I've been somewhat ok in the getting the body in better shape part, I have (in my opinion) neglected my mind.

On Thursday, I decided to do a "sugar detox".  For at least one week, and I will attempt two, I will have no refined or processed sugars whatsoever.  No desserts, no sodas (gasp!), not even putting jam on muffins, or sugar in my oatmeal.  The only sugar I will allow myself will be from fruits or fruit juices (and even with fruit juices, I will dilute it with water 50/50).  I am also not going to drink any diet soft drinks, as I don't think these are good for your health.  I will be taking some fitness supplements, a whey protein isolate, a branched chain amino acid intra workout drink, and SuperPump 250, a proprietary blend of several things which is meant as a 'pre-workout' energizer.  Most of these have sucralose to make them palatable.  So the fruits, fruit juices and "fake" sugar in the supplements are the only sweets I will be having for the next two weeks.

This is my third day without any sugar, and so far I feel pretty good.  No real cravings or sugar pangs yet.  I even did some grocery shopping the other day, and none of the desserts enticed me, which is very surprising.  Generally, I buy my food day-to-day.  The reason for this is that I often forget to defrost my food, which means I'd have to buy some fast food anyway.  So almost every day or every other day, I go to the supermarket to get my food.  The benefit to this is that my food is fresh.  The downside to this is that I am always tempted to buy bad things (and lord help me....Cadbury Creme eggs are already on sale).  So I was surprised that I didn't really linger in the bakery department, and neither did the aisle containing the Cadbury Creme Eggs.

Having been reasonably fit before I injured my knee, I do know how to lose weight.  The hard part is the discipline, preparation, and energy.  For me, it takes 6 weeks of working out a minimum of 4 days a week to force exercise as a habit.  My body tends to lose fat more quickly with weight workouts than cardio, but cardio is necessary for my body at least 3 days a week.  Preparation is another overlooked part of losing weight.  Trying to eat 5-6 times a day mandates planning your meals.  If you don't prepare your meals in advance, you WILL cheat.  And the last part, having enough energy is also difficult to do.  Having enough energy means that you get 8hrs of solid sleep, eat breakfast, and don't crash on sugar.  Sounds simple, but in practice, it's harder for me than it sounds.

The same rules that apply to training the body apply to training the mind.  It takes discipline, preparation, and energy.  The discipline means that I have to follow through on what I want.  Wanting is not enough.  Just reading or practicing some homework isn't enough.  Preparation means setting aside time for studying, and sticking to it.  If I just try and find free time, usually there isn't any.  And if I don't force myself to study at a given time, I will procrastinate and find something less strenuous to do.  Having energy means having the mental focus to study.  Just like a muscle, if your mind is not clear and focused, you won't get a lot of mental benefit.

In order to help me out in both body and mind, I have decided to create a game plan.  For my physical training, I am going to get back onto BodySpace and report my progress there.  I intend to do the following:

Sat: Cardio
Sun: Cardio
Mon: Upper Body
Tue: Rest
Wed: Lower Body, Cardio
Thu: Arms, Abs
Fri: Rest

Nutrition will be a bit tricky.  I'm going to try the grab bag approach, where I pick a protein source, a carb source, and a fruit/veggie source for each meal.  I also bought some food containers that should help portion out the meal.  Some fitness gurus advocate the "portion size" method, and some say that you must actually count calories, as "eyeballing" it can lead to over or under estimations of caloric food intake.  For now, I'm going to go with the "eyeball" approach, and have about a palm sized portion of protein, a palm sized portion of carbs, and a fist sized portion of fruit or veggies (since I'm not a huge veggie person, I'll probably be eating more fruit than veggies...and no, carrots and potatoes don't count as veggies...but broccoli, cauliflower, spinach, most greens, and legumes do count).

For the mental training, I am going to devote an hour a day every weekday to academic training.  In the beginning, this is mostly going to consist of relearning a lot of my math.  I've always been fascinated by math, but my weakness in the basics always hampered me.  To be honest, I usually only did some of my math homework in school, just enough to understand the concept, but not enough to reinforce it as 2nd nature.  I also want to get much better at linear and abstract algebra.  And of course, I need to get back to basics with my calculus.  Once I feel good with my algebras, I need to get better at stochastics, because a lot of interesting problems are random in nature (really, Quantum Mechanics is all about probabilities).  And finally, I want to study Number Theory and Numerical Analysis, as I never did those in college.

Some may wonder why I want to study math, and not more computer science-y things.  Afterall, I've heard several people tell me that you don't really need or use that much math in real world programming.  I think that's a bunch of hooey.  I don't want to be good at "real world" programming...I want to understand computer SCIENCE, not software engineering.  Furthermore, the truly hard problems to solve require a good background in math.  And finally, being better at math will, I think, make me better at programming in general. 

I used to tell people who were interested in going into Computer Science if they were good at math.  It surprised me how many people said that either A) they weren't good at math, or B) weren't interested in math.  I never asked, but I always wondered if these people, like so many, got their interest in programming because they wanted to make computer games.  Well, if you want to make computer games, you better be good at math.  3D programming?  You better know matrix multiplication and therefore linear algebra.  Want to make some cool AI stuff?  You better know bayes theorem for some of the more interesting AI techniques.  For a good network server, understanding queuing theory and the stochastic nature  of processes will come in handy.

Just learning how to program isn't THAT hard.  Yes, a good engineer is more than just a programmer (what I mean by this is that a good software engineer understands things like lifecycle design, revision control, documentation, release engineering, unit testing, etc etc.  And I think being good at math is what can separate a good engineer from a great one.  One of the best quotes I ever heard about the difference between a scientist and an engineer is this:

"Engineers learn in order to build.  Scientists build in order to learn"

By that definition, I consider myself a scientist.  Building things just to build them or make money off them does not interest me.  I build things in order to learn.  Right now, most of my learning has simply been to put a new technology in my pocket (which hopefully will look better on my resume).  I really only learned the fundamentals of databases for a project at work, and the same for eclipse.  I will be learning 3d programming (OpenGL) for my clojure game, and I learned clojure because....well, I like learning languages.

But for me, math isn't just about putting something in my pocket that will make me more attractive to employers.  In fact, it's probably something of a detriment.  But math is the language of the universe.  And just as I like learning programming languages, I want to get better at this universal language called math.

Saturday, January 29, 2011

Reconsidering GUI platform

So I just found out that JavaFX will be ported as a Java API and thus will become a java library.  JavaFX seems to be a more technologically advanced GUI framework than Swing.  What caught my eye on the JavaFX roadmap was that it would be hardware graphics accelerated.  Unlike Swing, which is rooted in AWT, JavaFX will be able to take advantage of OpenGL or DirectX.

In fact, when I originally started on my clojure project, I had considered using lwjgl and nifty-gui (and possibly even the jmonkeyengine).  I had ultimately planned on wanting a 3d view, as I thought this would be a cool feature for the game.  A long time ago, even before I had started programming, I had looked at the OpenRPG engine (written in python).  While I thought OpenRPG was pretty cool, I wished it had a 3d map/world feature.

So I will go ahead and complete the java launcher project in Swing, but I will then start looking into lwjgl and nifty-gui from here on out.  Although jmonkeyengine looks great, and I know (almost) nothing about 3d programming, I doubt jmonkeyengine will be very amenable to using clojure.  Besides, this will be an opportunity for me to learn OpenGL from the ground up.


 UPDATE:


I did a little more research, including taking a look at TWL, and a little more into lwjgl, and I think switching to  3d aware GUI platform is the way to go.  

I looked a bit at TWL, but none of their java web start demos worked for me, the wiki leads you to a non-existent page, and the documentation like-wise is missing.  Basically, not a good way to get me to want to use it.  It did however have a scala port and it used mercurial as its VCS system.  Yes, I do tend to see how up-to-date people are with a project.  If they are still using CVS, I get scared (I'm looking at you Eclipse), and if they are still using subversion, I see them as falling behind (or maybe they are just scared of using repository porting tools to convert from subversion to something like git or mercurial).

So while looking at one of the nifty-gui tutorials, it goes into some detail about how to create your own widget.  In other toolkits, this is quite a pain to do, and I thought it was pretty cool to be able to do that.  I did notice that all the java 3d gui toolkits used XML as a declarative language.  I was thinking that it might not be bad to make a clojure port.  

I remember about 10 years ago, on the gamedev.net forum, there was a debate about lisp vs. xml.  Essentially anything you can do in xml, you can do in lisp.  Now that I have a better handle on clojure, I can see why.  In fact, Amit Rathore's book, Clojure in Action tries to dispell the myth that lisp syntax is hard by stripping away parts of xml until it looks like lisp syntax.

Now that I am getting better at clojure, it amazes me that people think Scala is the future for the JVM.  Not to pick on Scala, but its hybrid approach means that while programmers will be more comfortable in learning it from a paradigm perspective, it also means that code will be a mish-mosh of functional and impure code.  I think Scala is the bad way to go, even though it is clearly better than Java itself.  The syntax of clojure was actually pretty easy to get used to, it was just wrapping my brain around functional programming that was hard.  I also still haven't quite understood how or when to use macros, and that's one of the next things I want to tackle, and I think I will need to use macros if I wanted to make a clojure API around nifty-gui.

Thursday, January 20, 2011

be here, now

I was browsing on YouTube the other day and as tends to happen, I ran into several old gems.  I'm not really a big Queen fan, despite having "Another One Bites the Dust" hammered into my ears as a very young lad, but I did find Queen's song for the Highlander movie, "Who Wants to Live Forever".

Maybe because I found that Brian May is now Dr. May (he earned his doctorate degree in Astrophysics recently), I paid more attention to the song.  For the first time, I actually listened to the words...and they struck me as being very buddhist.

If you never saw The Highlander movie (don't bother watching the sequels, though the TV series was pretty good actually), basically it's about a group of mysterious immortals that can only be killed by having their heads cut off.  Imagine for a second being able to live forever, and yet watching those you love around you die.

You don't even really have to be immortal to understand that.  I have a dog named Aiko, and she is very dear to me, and I always thought it a cruel irony that dogs live 1/5th as long as humans do, despite in my opinion, dogs being superior to humans in many ways.  One day, I will have to watch her leave, and the thought always fills me with sadness despite the fact that I know that this is simply the way things are.

So I listened to the lyrics of the song, and basically the recurring theme is, who wants to live forever, if love has to die.  I saw some comments on YouTube from some christians saying that they will live forever one day in a different place.  But buddhists do not ascribe to these beliefs.  For us, this is nothing more than wish-fulfillment, yet another desire that leads to suffering.  Humans have been ingrained to not see things as they are, but as to how we expect them to be, conceive them to be or hope them to be.  Our everyday experience tells us that things are not permanent, and yet we still try to fool ourselves into believing that there is more to life than meets our experiences.

One of the parts of the song says that "forever is our today".  This is exactly what Zen means when it says that we must live now in the present.  The past is gone, and the future is not yet here.  All we truly have is this moment now.  Sometimes, when I take Aiko for a walk, I drop as much baggage as I can, and I am always amazed by two things:

1) how peaceful it is
2) how hard it is to stay that way

Always, some thought or some sensation brings me back to the world of "inner mind", where I think about things past, present or future that have nothing to do with "right now".  Our minds are always scattered and not experiencing what life is presenting to us.  Instead, we think about how much work we have to do, or errands we have to run, or we reminisce about something, etc etc.  But if we could only pay attention to "right now", we would be pretty amazed.

The song also struck me as buddhist in that it shows how pain comes from love.  There is a story about buddhism that I have always liked, because it illustrates how suffering can come from anything...including love.  Most people think that "love burns eternal", or that love saves the day....yadda yadda.  Again, this is wishful thinking.  The truth is that like all things, love changes.  It grows, and it fades away or at least it changes.  The only true lasting love is the kind that accepts the loved for whatever it is, not as you want it to be.  But let me get back to the buddhist story.

King Pasenadi was a king of a neighboring country of the Buddha's own land (Sakya).  He was a nominal Hindu, but his wife had heard of the Buddha's teaching and was enamored of them.  One day, the Buddha was traveling in King Pasenadi's kingdom, and he met a man who was weeping in sorrow.  The Buddha asked the man why he was filled with grief, and the man told the Buddha that his son had died.  The Buddha said that "in love there is suffering", but before the Buddha could explain more, the man angrily retorted that love only brings happiness and joy and then he ran off.

The man soon talked with other people in town, and the other people agreed with the man, saying that it was wrong of the Buddha to claim that love was suffering.  Eventually, word reached King Pasenadi's ears and he told his wife that "this monk called 'The Buddha' may not be as spiritual as you think".  When his wife (Mallika) asked him why, King Pasenadi told her that the Buddha taught that love was suffering.  Later Mallika asked an attendant to ask the Buddha what he had meant to say.  When the attendant (a Brahman) had come back and explained what the Buddha had tried to say, Mallika understood the Buddha's intent.

When King Pasenadi was relaxing, Mallika came to him and asked, "My husband, do you love Princess Vajiri?".  King Pasenadi was somewhat taken aback.  He replied, "Of course I do".  So Mallika continued, "And would you be distressed if something were to befall her?".  The King seemed troubled, for he was wise enough to see where his wife was going with this, and the King resolved to meet the Buddha.


The King asked the Buddha that he could see how pain and suffering could stem from love, but that joy and happiness also sprang from love.  The Buddha's answer is very important, and this answer also applies to meekness.  The Buddha replied that the majority of people in the world conceived of love in terms of "me" or "mine".  Because of this, love was mired in the world of separation and attachments.  When most people think of love, they think only what they will get out of it.  Love that thinks of self, or me, or mine is not true love.  True love only thinks of others or of the whole...never the self.  The Buddha said that there are only two kinds of love; karuna, which is the love that brings happiness to others and maitri which eases the sufferings of others (btw, the future Buddha is said to be Maitreya...the one who ends suffering).  In both karuna and maitri, there is no thought of getting something in return. 
 
 I love this story because it points to the heart of our selfishness.  True love is selfless.  It seeks nothing in return except to share happiness, or to ease suffering.  Nowhere does it say, "in return for loving you, I expect something in return".  Nowhere does it say that love is eternal.

We must love everything.  We must have loving kindness to all things, not just things that we find agreeable to us.  For me, this is one of the hardest aspects to follow in Buddhism.  There are many things in this world that I don't like.  And yet I need to understand how we are all connected, and how we are all under delusion.  It is never a matter of forcing yourself into a belief or practice.  If you force yourself to be celibate while having lustful thoughts, or force yourself to be charitable when giving to others, or give only when expecting to be rewarded...these are false attainments).