Discovered this as my wife and I were packing for a trip.

Discovered this as my wife and I were packing for a trip.

Setting up the Java stencil buffer

In OpenGL, the stencil buffer is used to mask pixels on the screen. However, if you notice that your stencil test is always passing (i.e. your stencil bits are always returns as zero), then you probably haven’t initialized the stencil bits.

This isn’t very well documented. It took a few days of searching to find it. In C, one would be using GLUT, so calling glutInitDisplayMode() would be he function to call that requests stencil bits. In Java, this is in the GLCapabilities object.

So, to request stencil bits, in your init() function put


GLCapabilities cap = new GLCapabilities();
cap.setStencilBits(8);
GLCanvas canvas = new GLCanvas(cap);


Basically, this initializes a new GLCapabilities object, and assigns it an 8-bit stencil buffer. Pass it into the GLCanvas as a parameter and you’re gold. 

One use for this buffer is to render shadow volumes:

 

Java’s forever loop

If you want to loop forever in Java, just type


for(;;) { }


Coincidentally, “for(;;)” and “forever” have the same number of characters…

Enabling Apache and PHP on a Mac 10.6 (Snow Leopard)

The Apache web server and PHP come preinstalled on Macs. All you have to do is enable it. Unfortunately, there is no “Enable Apache and PHP” switch, but the process is relatively simple.

To first enable PHP5, using a Terminal navigate to “/etc/apache2/”


cd /etc/apache2


and sudo open up the httpd.conf file using your editor of choice.

Next, find this line:


#LoadModule php5_module        libexec/apache2/libphp5.so


and uncomment it by removing the ‘#’. Save and close. Now PHP5 is enabled.

Next, we need to turn on the Apache web server (or reboot it if it’s already running). Open up System Preferences from the Apple menu. Click on Sharing. Under the list of options in the toolbar on the left, check (or uncheck-then check) the “Web Sharing” option. This will effectively activate the apache web server.

That’s it! Now open up a web browser and type http://localserver/ and it should give you a happy “It Works!” confirmation that Apache is up. Your home directory is located at http://localserver/~username and you can navigate to your websites and run PHP scripts.

Using scp on a directory/file with spaces

The protocol when copying/moving a file that has spaces in the filename in a terminal window is to use “\ “, or


cp ~/some\ directory/some\ thing.zip ~/destination/


Oddly enough, scp doesn’t employ the same protocol.  Instead, I’ve found that this works:


scp 'myname@location:"~/some directory/some thing.zip" ' ~/destination/


That is, wrap the entire call after scp in single quotes, and the path in double quotes

Or an easy solution: don’t put spaces in filenames or directories

Editing your Vim parameters: .vimrc and .gvimrc

Vim is my editor of choice because it allows me to swiftly move through my code without lifting my fingers from the keyboard. That is, it is a mouse-less text editor. There’s a bit of a learning curve, so if you haven’t jumped into an editor like Vim or Emacs before just hang in there.

If you’re running linux, you can grab it through apt-get, if you’re on a Mac, then check out MacVim, and Windows users should go through the Vim website.  

Now that you have your GVim editor (Graphical Vim), you can edit some of its properties using two files: .vimrc and .gvimrc. The first file should already exist within your home directory, and the latter you may need to create (within your home directoy as well).

There are a number of settings you can manually set.  For my .gvimrc, for example, I have this:


set lines=40
set columns=78
set guioptions-=m
set guioptions-=T
set guifont=Monaco:h13


This sets the number of lines and columns and eliminates that annoying toolbar so you’re left with a clean window for coding.

The .vimrc controls most of the parameters for your vim editing (how to handle tabs, keybindings, etc). I won’t post an entire tutorial here, but instead here are useful links to get you on your way.

A script that updates all your subversion repositories

I have several subversion repositories that I continually need to update. Here is a convenient script I wrote that automatically updates all of them. 

For bash (also sh,ksh)


#! /bin/bash

# Subversion repositories to be updated
svnlist=( \
"cae" \
"jtk" \
"llm" \
"idh" \
)

svnbase="/Users/Chris/Home/box/"

# Number of subversion repositories.
listnum=${#svnlist[@]}

for ((i=0;i<$listnum;i++))
do
  echo; echo ${svnlist[${i}]}"..."; echo
  cd $svnbase${svnlist[${i}]}
  svn update
done


Vimtutor

Did you know there’s a really great built-in unix tutorial on how to use vim? Just type vimtutor in any terminal window and it will begin the tutorial. It’s written by Michael Pierce and Robert Ware from the Colorado School of Mines (which just so happens to by my alma mater).

Scala: I can overload operators again!

Overloading operators is a neat feature that languages like C++ have, where the programmer can redefine a limited set of algorithmic operators. Why would you want to do this? This could come in handy if you’re defining a new object (a complex number, say) and you want to be able to specify what happens when you add two complex numbers together. 

I say it’s a neat feature because it has limited functionality. The programmer may only redefine algorithmic operators (+,-,*,/,…). Some would even argue that operator overloading makes code almost impossible to maintain. (By “some”, I mean Sun Microsystems, now Oracle). It is primarily for this reason that there is no operator overloading in Java.

So I was very surprised to learn that Scala supports operator overloading. It not only allows you to redefine operators, but also define completely new operators. Let’s give it a whirl. Let’s construct a Matrix class.


class Matrix(var a: Array[Array[Float]]) {
  var m = a.length    // rows
  var n = a(0).length // cols

  def +=(b: Matrix) {
    for (i <- 0 until m)
      for (j <- 0 until n)
        a(i)(j) += b.get(i,j)
  }

  def get(i: Int, j: Int): Float = {
    return a(i)(j)
  } 

  def override toString() = {
    a.map(_.mkString(" ")).mkString("\n")
  }
}


This class defines a new Matrix object that can get any entry given the row/column, and can add another matrix to it. It’s primitive, but demonstrates how simple operator overloading can be. We’ve also overridden a function toString() which will allow us to check out answer. We’ve defined the operator (+=), and the other parameter to the operator (Matrix b). 

We can test this in the console.


scala> var a = Array.ofDim[Float](2,2)
a: Array[Array[Float]] = Array(Array(0.0, 0.0), Array(0.0, 0.0))

scala> var b = Array.ofDim[Float](2,2)
b: Array[Array[Float]] = Array(Array(0.0, 0.0), Array(0.0, 0.0))

scala> a(0)(0) = 1   

scala> a(1)(1) = 1

scala> b(0)(1) = 2

scala> b(1)(0) = 2

scala> var m1 = new Matrix(a)
m1: Matrix = 
1.0 0.0
0.0 1.0

scala> var m2 = new Matrix(b)
m2: Matrix = 
0.0 2.0
2.0 0.0

scala> m1+=m2

scala> m1   
res5: Matrix = 
1.0 2.0
2.0 1.0


Voila! We can now go crazy writing a more intuitive Matrix class than one written in Java that uses methods like “plusEquals()” or “times()”. In Scala, operators are merely functions. Note that creating our += operator is done exactly the same way as our get() function. Hence, m1+=m2 can also be called as m1.+=(m2).

This concept is even extended to numbers (which are also objects in Scala)! That is, 1+2 is equivalent to saying 1.+(2)

In this respect we are given more freedom on defining operators.