The importance of shading

Porin molecule, No Ambient Occlusion

Introducing… a flat, boring porins protein, this is how most scientific vistualization would show it.

What’s wrong with the image?  Well technically nothing, it’s scientifically accurate, each atom is color coded to represent an element, and it even rotates so that you can see the overall structure of protein.  But can we do better?  You bet!

Click here to see the exact same porins protein, but with a much advanced shading model, I think it’s clear why this one is superior.  With ambient occlusion, in which the model is shaded based on how likely an area is occluded because of surrounding objects, the spatial structure of the protein is much easier to understand.  And with today’s super fast GPUs, doing a screen-space ambient occlusion is virtually instant.

And if anyone is curious, the software I used to generate the graph is CuteMol, an

interactive, high quality molecular visualization system. QuteMol exploits the current GPU capabilites through OpenGL shaders to offers an array of innovative visual effects. QuteMol visualization techniques are aimed at improving clarity and an easier understanding of the 3D shape and structure of large molecules or complex proteins.

Very simple 2 player networking in Python

A while ago, I posted a simple demo I made in the Blender game engine that let’s 2 player interact with each other over the internet, all it is required is a really simple networking script and the IP address of the two players.  Here is as youtube video of it in action, mouse-over the video to see some caption text.

The laptop on the left is physically separated from the desktop to the right, linked only by a wireless internet connection. And as you can see, input made to the desktop computer is sent across the network to the laptop, in realtime.

And here is the script, I’ll try to explain everything as best I can:

# Simple Python UDP networking demo Created by Mike Pan
# Todo: auto-self IP detection would be nice

# the following line loads the game module used by Python
# to access the Blender Game Engine API, and assigns it the alias G
import GameLogic as G
# load the network socket python module, we'll need it later
import socket

# define own IP address, and stores it as a property under GameLogic
# essentially making it accessible as a global variable
G.ownIP = "xxx.xxx.xxx.xxx"
# define peer IP address, stores it as a property under GameLogic
# again, essentially making it accessible as a global variable
G.peerIP = "xxx.xxx.xxx.xxx"
# define the port number (arbitrary), and the socket buffer size
# (4096 is a good starting point)
G.port = 4000
G.buffer = 4096

# create an IPV4 address with the pre-defined IP and the port number
addr = (G.peerIP, G.port)

# We'll add this line to all our communication, so when debugging,
# we won't get confused as to who send the packets.
signature = "Sent from " + G.ownIP

def init():
	# create an UDP socket for **sending** datagram
	G.sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	# time out is 10ms, pretty short for realworld application,
	#but enough for debugging over local network
	G.sender.settimeout(0.01)
	# this is important! by making this socket object a
	# non-blocking operation, all network stuff is run on
	# a separate thread, thus any network delay will not
	# cause the game to slow down
	G.sender.setblocking(0)

	# creates UDP socket for **receiving** datagram
	G.listener = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	# same as above, time out is 10ms
	G.listener.settimeout(0.01)
	# same as above, we also need to make sure network operations
	# will not slow down the main application
	G.listener.setblocking(0)

	# Win32 needs the socket binded:
	try:
	 G.listener.bind((G.ownIP, G.port))
	except:
	 print "Binding Listener failed"

def comm():
	# get the object that is attached to this script
	own = G.getCurrentController().owner

 	# receive from peer
	try:
		data, port = G.listener.recvfrom(G.buffer)
		# data contains all the data that came through,
		# it's a string, and we can do whatever we want
		# like print it out
		print data.split(',')
	# if the receiving fails, print something to notify the user
	except:
		print "Receive failed"

	# send to peer
	# generate data to be sent here,
	# send the position data across the network:
	string = str(own.worldPosition)+ ',' + signature

	# going to try to send the data
	try:
		G.sender.sendto(string, addr)
		# if sending fails, notify with error message
	except:
		print "Send failed"

That’s it! To use this script, we simply need to call init() once at the start of the game, and then comm() everytime you wish to send/receive some data (perhaps every 1/30th of a second). As mentioned, here is no ‘server’ involved, the two peers connect direction to each other via a UDP socket. All the user has to do is specify the two IP addresses.

Compile Blender 2.45 – a how to guide

This guide was written for Blender 2.45.  I have an updated guide for building Blender 2.5 available HERE.

1. Acquire Blender source code and libraries

- Download and install tortoisesvn from: tortoisesvn.tigris.org
-created a folder named “blender” under c:\build, select the folder and click on SVN checkout, and use the following location:

https://svn.blender.org/svnroot/bf-blender/trunk/blender

-created a folder named “windows” under c:\build\lib, select the folder and click on SVN checkout, and use the following location:

https://svn.blender.org/svnroot/bf-blender/trunk/lib/windows

Once the above 3 steps are done, you will have the most up-to-date blender source code and library in the world.

OPTIONAL INFO: If you want to build an official release, you can grab the more stable source code (~10MB, library files are not included) from blender.org/download/source-code. Simply delete everything under C:\build\blender and then extract the tarball to the same directory. Building from the official release ensures the Blender you compiled is stable and relatively bug-free.

2. Acquire compiling softwares

- Download and install Python 2.5.1 (9MB) from www.python.org
- Download Mingw 5.1.3 (~10MB) from www.mingw.org . Run the installer, Pick any mirror, Select Current and install packages MinGW BASETOOLS and G++ COMPILER.
- Download and install SCons 0.97 Windows (300KB) from www.scons.org
- Download the experimental GCC4.x for MinGW package from tdragon.net. Extract it to the place where you installed MinGW, and this will serve as a ‘drop in’ replacement compiler. From my own testing, GCC4 is faster than the one that comes with MinGW, but can cause trouble when compiling OpenAL.

3. Environment setup

- Go to Start Menu >> Control Panel >> System >> Advanced (Tab) >> Envoronmant Variables (button) >> System Variables scroll area double click on the PATH item
- Add the following line to the end: ;C:\Python25;C:\mingw\bin

4. Python stuff…

Copy libpython25.a from C:\Python25\libs to C:\build\lib\windows\python\lib


5. Customization (Enable/disable features, add speed optimizations)

- Create a new file named user-config.py under C:\build\blender\
- Open user-config.py with notepad and copy the following text into it.

WITH_BF_GAMEENGINE=’true’
WITH_BF_BULLET = ‘true’
WITH_BF_ODE = ‘true’
WITH_BF_OPENEXR = ‘true’
WITH_BF_FTGL = ‘true’
WITH_BF_FMOD = ‘true’
WITH_BF_FFMPEG = ‘true’
WITH_BF_QUICKTIME = ‘false’
WITH_BF_OPENAL = ‘false’
WITH_BF_SDL = ‘false’
BF_PYTHON_VERSION = ’2.5′

CCFLAGS.extend( ['-march=i686', '-ftracer', '-fomit-frame-pointer', '-finline-functions', '-ffast-math'])
CXXFLAGS.extend(['-march=i686', '-ftracer', '-fomit-frame-pointer', '-finline-functions', '-ffast-math'])

REL_CFLAGS = [ '-O3' ]
REL_CCFLAGS = [ '-O3' ]

‘-march=’ is probably the most important flag in this config file. Newer Intel CPU (Pentium D, Core 2) owners are recommended to use ‘-march=nocona’, AMD (A64, X2) owners can try ‘-march=k8′.
‘-mmmx’, ‘-msse’, ‘-msse2′, ‘-msse3′ are usually implied by using a valid march flag, therefore they are not included in the config file.
‘-ffast-math’ are in general stable enough for daily use (I’ve never seen a crash), but if you are running a real production server or can not tolorate crashes, remove this flag. It will make the binary a little slower.

This is how the directory should look like:
build2

6. Compile!

- Open a command prompt window by click on Start >> Run >> type in “cmd” and hit OK.
- Navigate to where the Blender source code is by typing this into the command prompt cd C:\build\blender
- Start the building process: scons BF_TOOLSET=mingw BF_BUILDDIR=c:\install -j4
- The above command should take about 5-10 minutes to process, and it helps if you have a dual-core processor!
- When it finishes, (Hopefully without error), you can nagivate to C:\install\win32-mingw\ and find a complete Blender installation ready to be zipped up and distributed!

Where to go from here?

You might want to rid the blender.exe of extra useless debug data by entering the following command into the command line:
strip C:\install\win32-mingw\blender.exe
This will reduce the size of the executable by 8mb or so without having any negative impact

The above ‘user-config.py’ file will result in a full-featured Blender that is optimized for speed and should run on almost all modern processors. To speed up the building process, you can chose to disable some of the features by replacing ‘true’ with ‘false’ (this will give you a blender binary without certain feaures such as gameengine, audio, etc…)
scons clean BF_TOOLSET=mingw BF_BUILDDIR=C:\install
can be used to clean the build data and getting ready for a new build


I also recommend: How to build Blender using Visual C++ Express Edition 2008 By Eugene (etr9j), MSVC produces faster binary, but takes a lot longer to setup for the first time.

Compile Blender 2.45 – an easy how to guide

This guide was written for Blender 2.45.  I have an updated guide for building Blender 2.5 available HERE.

1. Acquire Blender source code and libraries

- Download and install tortoisesvn from: tortoisesvn.tigris.org
-created a folder named “blender” under c:\build, select the folder and click on SVN checkout, and use the following location:

https://svn.blender.org/svnroot/bf-blender/trunk/blender

-created a folder named “windows” under c:\build\lib, select the folder and click on SVN checkout, and use the following location:

https://svn.blender.org/svnroot/bf-blender/trunk/lib/windows

2. Acquire compiling softwares

- Download and install Python 2.5.1 (9MB) from www.python.org
- Download Mingw 5.1.3 (~10MB) from www.mingw.org . Run the installer, Pick any mirror, Select Current and install packages MinGW BASETOOLS and G++ COMPILER.
- Download and install SCons 0.97 Windows (300KB) from www.scons.org

3. Environment setup

- Go to Start Menu >> Control Panel >> System >> Advanced (Tab) >> Envoronmant Variables (button) >> System Variables scroll area double click on the PATH item
- Add the following line to the end: ;C:\Python25;C:\mingw\bin
- Copy libpython25.a from C:\Python25\libs to C:\build\lib\windows\python\lib

4. Compile!

Before you start, this is how the everything should look like:

build2

- Open a command prompt window by click on Start >> Run >> type in “cmd” and hit OK.

- Navigate to where the Blender source code is by typing this into the command prompt cd C:\build\blender

- Start the building process by:
scons BF_BUILDDIR=c:\install -j4

- The above command should take about 5-10 minutes to process, and it helps if you have a dual-core processor!

- When it finishes, (Hopefully without error), you can nagivate to C:\install\win32-mingw\ and find a complete Blender installation ready to be zipped up and distributed!

Where to go from here?

You might want to rid the blender.exe of extra useless debug data by entering the following command into the command line:
strip C:\install\win32-mingw\blender.exe
This will reduce the size of the executable by 8mb or so without having any negative impact

scons clean BF_BUILDDIR=C:\install
can be used to clean the build data and getting ready for a new build

Once you have successfully compiled Blender, you can try more customization by following the Advanced Guide.

Blender Game networking test

Now that I am working on a game, using Python cPickle and Socket, i was able to create a peer to peer, symmetrical network implementation for the multiplayer aspect of the project.   Using some 30 lines of Python, each player would ‘trade’ data with its peers.

I am using UDP, so it’s naturally faster than TCP/IP, but delivery is not guranteed.  But for realtime games latency is the main concern, so UDP is definitely the way to go.  The video shows me controlling a cube on the desktop computer (monitor to the right), it’s position data is sent across the internet* to the laptop which displays a similar cube.

Close to the end of the video, I turned off the Wi-Fi on the laptop, killing the network connection.  As expected, the cube stops updating, when I turn it back on, it takes a few second to reconnect, and then the cube starts moving again.

*I route the network traffic through another computer somewhere in town to simulate a more realistic network delay.

Blender, Memory issues and 64bit

RAMs are cheap, and that blender project you’ve been working on is taking up too much memory.  So you did the obvious: you bought 2GB of DDR2 memory and put it in the computer, fire up Blender and expect everyting to work as usual, right?

Maybe, maybe not.

Well, if you are still stuck on 32bit for some reason, chances are, you might run into a few memory related problems.  If you are running a 64bit Operating System with 64bit Blender,then you can safely ignore the rest of this post.  In a pure 64bit environment (meaning 64bit CPU, 64bit OS, 64bit app), the memory addressing space is virtually unlimited.   So the more memory you have, the better, 64bit can use it all.

Theory

Addressing Space can be seen as the maximum amount of memory and virtual memory the operating system can utilize. It is independent from the actual amount of RAM a computer has. For 32bit Operating System, the total Addressing space is ALWAYS 4GB, or (2^32 bytes). For 64bit, this number is a LOT bigger: 2^64 bytes is about 16000000 Terabyte.

Think of 32bit as a city where the phone number is 5 digit long.  In such a city, there is approximately 90,000 unique phone numbers, because of this, the number of phones(analogous to physical memory) can not exceed 90,000, it doesn’t make sense to have more phones than phone numbers, since there will be no way of reaching these phones.  Now, in a 64bit city, their phone number is 10 digit long, that gives them about 9 billion unique phone numbers.

32bit Limitations

32bit Windows is designed so that EACH application can only utilize up to half of the 4GB addressing space, or 2GB. (the other half is reserved for the system). Again, this is a software limit inherent to the architecture of the operating system, and has nothing to do with how much RAM is actually installed. This does not cause a problem for day-to-day applications because they usually stay well below this limit. But when working with Blender, especially baking fluids, or rendering high resolution images with composite node, a single application can easily reach 2GB of memory usage. From my testing, Blender crashes at the 1.8GB mark. Remember this is a software limitation due to the design of Windows, you can install 4GB of physical RAM on your computer, and set-aside a 4GB swap file, Blender will still crash at the 1.8GB mark.

Fix

Thankfully there is a fix for the problem:

The fix forces the system to give up 1GB of additional addressing space to the application.  This way, each application can get up to 3GB, while the OS reserves 1GB for itself.

Once you have completed teh above, find a version of Blender that is compiled with the LAA (Large Address Aware) flag. The LAA flag tells the Windows operating system that the application is aware and capable of handling the extra large address space. ‘Official’ Blender releases are now compiled with LAA.

So that’s it. With the above 2 steps, your 32bit Blender should be able to utilize slightly more memory than before.

FAQ
Q: I only have 2GB of physical RAM (or less), should I worry about LAA and /3GB or go 64bit?

A: The short answer is no. It is unlikely that you will truly hit the 1.8GB barrier, since the OS takes up some memory as well, by the time Blender is using 1.8GB of RAM, your computer is probably going very slow, due to the performance hit of using the swap file.  What you should be doing is either optimize the scene or get more physical memory.

Q: Does blender do [rendering, baking, sculpting] faster with more memory?

A: It depends, more memory helps only if you are running out of it.  If Blender is only using 200mb, adding another 4GB of memory will not speed up anything.

Q: I hate you for this lengthy and yet still confusing post!

A: I know.