setting cache metadata on files in s3
visualizing data structures with svg
so i found dropping SVG to disk is an easy and fun way to visualize or debug data structures. here’s all it takes in C:
FILE * fp = fopen("kdtree.svg", "w");
fprintf(fp, "<svg version=\"1.1\" width=\"1024\" height=\"1024\" xmlns=\"http://www.w3.org/2000/svg\">\n");
fprintf(fp, "<line stroke=\"#%06x\" stroke-width=\".1\" x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\"/>\n", ...);
fprintf(fp, "</svg>");
what’s nice is then you can load the file up in firefox or inkscape and zoom in. also you can easily draw in other data like triangles and circles or put nodes you’re interested in in different colors and what not.
quick maya curves export script
i needed a quick script tonight to export a slew of curves outta maya and google failed me so now that i’ve hacked a quick script for it i figure i’ll post it for future lazy coders :) here you go - a simple python script to export curves from maya ;)
from maya.cmds import *
import maya.OpenMaya as OpenMaya
def findNode (name):
selectionList = OpenMaya.MSelectionList()
selectionList.add(name)
node = OpenMaya.MObject()
selectionList.getDependNode(0, node)
return node
f = open("c:/curves.xml", "w")
f.write("<curves>\n")
curveNames = ls(type='nurbsCurve')
for i in xrange(len(curveNames)):
f.write(" <curve>\n")
curveFn = OpenMaya.MFnNurbsCurve(findNode(curveNames[i]))
points = OpenMaya.MPointArray()
curveFn.getCVs(points)
for j in xrange(points.length()):
pos = points[j]
f.write("
<point value=\"(%g,%g,%g)\" />\n" % (pos.x, pos.y, pos.z))
f.write(" </curve>\n")
f.write("</curves>\n")
f.close()
splines
writing binary files with maxscript
recently i've written a bunch of maxscripts to export test data from 3dsmax for various things. doing a c++ exporter would be waaay overkill when i just need a quick spline or mesh. thankfully writing binary data from max is trivial.
maxscript:
f = fopen filename "wb"
count = numKnots $ 1
writeLong f count
for i = 1 to count do (
p = getKnotPoint $ 1 i
writeFloat f p.x
writeFloat f p.y
writeFloat f p.z
)
fclose f
circular harmonics
i was playing with circular harmonics and having quite some fun and since i don't see a lot about them on the web i wrote up a quick text: circular harmonics. they're nice and very simple basis functions.
photoshop blend modes
formulas for the photoshop blend modes. this is probably useful to others as well
so i'll post it here. www.pegtop.net/delphi/articles/blendmodes/. here's a summary so you don't have to navigate through all the links to collect em all.
multiply a * b screen 1 - (1 - a) * (1 - b) darken min(a, b) lighten max(a, b) transparency
sorting alpha blended objects back to front gets transparency something like 95% correct
in games. that last 5% is a real pain though and happens when you have
objects with bounds that overlap so that no matter what order you draw them in
you're going to get errors. sorting the triangles for a transparent object
isn't difficult, but sorting the triangles between objects when those objects
have different materials and the two sets of triangles interpenetrate each other
is awful and doesn't map efficiently to current graphics APIs and hardware.quick macros
quick macros is a killer feature in devstudio i use all the time that not many other people seem to know about or use. it's perfect for quickly automating repetitive changes. for example if you want to take a large enum and turn it into a table of strings that's a lot of typing! instead, using a quick macro just go to the first line, hit ctrl+shift+r then add your quotes, delete any enum prefix, ctrl+u after the first character to get some nice lower case, then finish it off with the cursor on the next line. now just mash ctrl+shift+p and watch your productivity soar. in some cases you can accomplish the same thing with regular expression find and replace, but i find it way simpler and faster to just do what i want done to one line then let devstudio repeat that for me.
maxscript
i'm in love with maxscript. if you don't know, it's the scripting
system for 3dsmax. i had some good times with it like five years ago,
but we parted ways when when i started using maya. maya has its own
scripting system called mel, but i never really hit it off with mel and
instead just stuck with c++ plugins.one nice thing with maxscript is that you can write quick exporters
with it. it is also incredibly simple to build ui to make using your
script easy. but the best thing is the bizarre language itself and how
that maps to dealing with 3d objects and animations. for example:
shed some bytes
hex edit a shader compiled with the HLSL compiler and you'll see it has added a bunch of extra data. it has an embedded constant table and a big fat string saying the compiler version. if you are making an intro this is wasting bytes! here's the easiest way to strip that out. compile in two steps instead of one:
fxc /nologo /EPixelShader /Tps_1_1 shaders.fx > shaderPs.psh
psa /FhshaderPs.h /Vns_psData shaderPs.pshhlsl compiler
microsoft's HLSL compiler is nice. it's amazing that you can do the math
behind the ps1.1 texm3x3vspec instruction and the compiler recognizes it and
emits a single instruction. when it does that you get the feeling that
the compiler is a sentient genius carefully studying and optimizing your
code. but of course cruel reality eventually steps in and spits in your
face! here are some some examples of the HLSL compiler making baby jesus cry:
add in parallel
add to 64 3-bit numbers in parallel using mmx intrinsics:
__m64 plane0, plane1, plane2; // source bit planes
__m64 out0, out1, out2; // result bit planes
__m64 x; // bits to add
__m64 carry; // temp
out0 = _mm_xor_si64(x, plane0);
carry = _mm_and_si64(x, plane0);
out1 = _mm_xor_si64(carry, plane1);
carry = _mm_and_si64(carry, plane1);
out2 = _mm_xor_si64(carry, plane2);mmx makes me happy :)
filtering trick
here's how you cut down the number of texture samples needed for a
filter. instead of sampling two adjacent pixels, scaling by a weight,
and adding, sample at a point in between the pixel and use a different
weight. to calculate the offset between the pixels and the new weight
just set the two equations equal.
p0 * w0 + p1 * w1 = (p0 * (1 - offset) + p1 * offset) * newWeight
p0 * w0 + p1 * w1 = p0 * ((1 - offset) * newWeight) + p1 * (offset * newWeight)
w0 = (1 - offset) * newWeight
w1 = offset * newWeight
offset = w1 / (w0 + w1)
newWeight = w0 + w1orthonormalization
gram-schmidt orthonormalization is really elegant, but i haven't had occasion to use it in practice. a common situation i run into requires creating an orthonormal basis from three 3d vectors which start pretty close to orthogonal. for example, say you have a forward vector and also approximate right and up vectors and you want them all orthonormal. sounds like a perfect job for gram-schmidt orthonormalization right? sure, but it's more efficient to just do two cross products.
cross product method:plain text code
writing computer programs in plain text files is ridiculously old fashioned.
are documents created in plain text? no. there are WYSIWYG
editors and rich document formats. are images created in plain text?
no. there are great painting tools where you can visualize and manipulate
them in 2D. are three dimensional models created in plain text? again, no.
there are tools that visualize and manipulate them in the 3D domain. so are computer
programs the 1D stream of characters which plain text files efficiently
represent? NO! so why are they created and represented in such a restrictive,
incapable format? hardware has advanced so much faster than software
because of this insistence of expressing software in plain text.wish list
here are some random things i want for c++ and devstudio:
- regular expressions built into the c++ syntax. not some library implementation
of regular expressions like boost or the regex in c#, but easy syntax like in
perl or ruby.- source code styles sheets. instead of putting a line of //===... above every
function, just add that as a rule to your style sheet and all code you view gets
that automatically. if you like class members prefixed with m_ or _ or suffixed with _
or whatever just set that as a style sheet rule. like wise with k/r braces vs ms braces.- compile to xml. the compiler should be able to dump the syntax tree out to a simple xml file.
this way you can write all sorts of great code manipulation tools without having to write a
whole parser for c++. you could just use ruby or xslt or whatever you like to manipulate the xml.- eliminate the need for forward declarations from c++. it's the year 2005, why do functions
still have to be forward declared? we have the technology!- the stl implementation with devstudio needs an option for range checking on vectors.
and it shouldn't throw an exception, it should assert.- the crt needs a real assert. one with a cool ui and options to generate a crash
report and email it to the developer.- compiler error messages are still really stupid. if you miss a semicolon it doesn't
say "hey dude, you forgot a semicolon." instead you mostly get something totally random.
it only takes a second to notice the missing semicolon, but come on... it's 2005.- region support like c#. not only the #region #endregion but also the ability to collapse
all functions to their prototypes and collapse class declarations and implementations.- compiler intrinsics for vectors and matrices. it should be able to map operations
with them to simd instructions.blargh
devstudio kills me when i edit html. i'll format my html source just perfectly the way i want it then at some point i'll switch to the design tab to preview it and when i switch back devstudio has mangled the file! it will strip extra lines and move my tags around relative to the text they contain. there's no reason for this and it's just cruel and unusual. i'd like to meet the guy responsible for this buggy behavior so i can punch him in the stomach. you know, you'd stop doing stupid careless stuff if someone punched you in the stomach every time!crappy book
my new bathroom book is andrei alexandrescu's modern c++ design. it's fitting because like me when i'm on the can, alexandrescu is full of crap.
test scenes
here's a great way to make simple test scenes:
- write a maya translater to export to xml
- use ruby and rexml to process the xml into a c header
- #include
sse is a pain
mmx is great. sse is not. mmx fits all the uses i try it with just great (64 bit stuff, rgba pixels, etc). everytime i use sse i end up wasting so many cycles shuffling data in and out of memory and between the registers. a horizontal add instruction would help a lot (which sse3 has i think).
texts
i've written some texts. they cover creating cellular textures, making lens flare textures, finding good normals for metaballs, the pq-torus knot, an easy point in triangle test, and a method of packing lightmaps into larger textures. you can read them here