Game Development Community

fun with donuts

by Orion Elenzil · 11/17/2009 (8:49 am) · 1 comments

i've been spending the last couple partial-days getting some real 3D stuff going on the iPhone,
both to get a feel for the 3D and math capabilities of the device, and as a way to become more familioar
with OpenGL ES and Objective C and iPhone development in general.

i have to say i'm fairly impressed.

i've got a spinning donut with vertices, normals, colors, and shading all being
re-computed each frame in CPU (ie, i'm doing the shading myself, not OGL ES)
on about 2400 vertices and the framerate seems pretty good.
the computations are even a bit ornate, involving a handful of trig functions
per vertex, as well as two stages of normal computation.

unfortunately, thanks to Apple's awesome totalitarianism,
i can't share this with you until i put it into the app store
and then wait the requisite two weeks.

good things:
* it's performant

frustrations:
* since OpenGL takes vertex buffers of the format X1, Y1, Z1, X2, Y2, Z2, ...
it's inconvenient to use anything derived from NSObject, since that introduces
an extra four bytes on top of whatever members the object has.
ie, no vector class for you!
this is okay, i just made a vector typedef and a little library of operations.

* Objective C still feels like a primitive language to me, compared to C++.
apparently i could be writing all this in C++, but part of the point here
is to bring myself up to speed on Objective C.


This is sort of meaningless without a video or app,
but i can give some screenshots. The object in question is continuously in motion,
sort of rolling around the major axis like a smoke-ring, changing color, shape,
and the nature of its shape, to a degree.

Here's the sort of geometry-manipulation cascade, for the interested:

setup:
* make a set of base vertices which are in the shape of a donut.
I use just a single vec3f* array, and do my own stride calculations and such to navigate it.
* ditto normals and color
* ditto an array of theta & phi for each vertex
* construct an index array that represents a bunch of triangle strips into the vertices.
the tricky part here is wrapping around at the edges.
* for each vertex, store the index to each of its four neighbors.
(again, taking care at the wrap-around)

each frame:
* perturb each vertex along its normal by some function of its theta and phi coords.
don't trash the base vertices.
(ie, fun with sines and cosines)
* calculate the normals at each vertex
this is basically the cross-product of two vectors: left neighbor -> right neighbor & top neighbor -> bottom neighbor.
this is why we pre-calculate those indices for each vertex, so our inner loop is as tight as possible.
* extrude each vertex along its new normal, and re-calculate the normals.
you can do this as often as you like. it yields a nice organic look.
* calculate the final color for each vertex based on the dot-product of the normal and some light vector
and the base color for that vertex.
* for each triangle strip, call glDrawElements().

again, the donut thing is sort of coiling around and smoothly transitioning between these various appearances,
it's 2400 vertices, and the framerate seems pretty good. 30fps or so, i would guess.

elenzil.com/progs/iphone/donut1/images/don2_1.pngelenzil.com/progs/iphone/donut1/images/don2_2.pngelenzil.com/progs/iphone/donut1/images/don2_3.pngelenzil.com/progs/iphone/donut1/images/don2_4.png

#1
11/18/2009 (1:21 pm)
Very impressive stuff Orion. I'm struggling with a simple List View on iPhone, let alone OGL ES :D