OpenGL in C++
Spring Sphere Demo: springSphere.zip
Out of curiousity I became interested in simulating 3d elastic objects
via some sort of spring system. My initial Google searches didn't yield
much in the way of tutorials on how to implement the springs properly.
There is some code and useful formulas out there, but most of it is poorly
explained or not quite applicable to what I was trying to do. So I'm
writing my own tutorial and release
the code under the GPL (as soon as I clean it up a little). Maybe I'll
see if NeHe or some other tutorial site wants it.
In short, the spring is easy to do (F = -kx), but the dampening is the
really critical part. After that there's the proper selection of spring
and dampening constants. Without dampening, the spring will oscillate out
of control (like flubber!) as round-off errors accumulate and energy
conservation is ignored... My system currently only damps along the spring
axes, but I need to dampen rotational and linear velocities as well,
via drag probably. Rotational velocity is the real problem, since in the
cartesian coordinate system roundoff errors will accumulate and an
imparted rotation may speed up wildly and blow up.
In the future I'll explore Range-Kutta solutions to imparting velocities,
perhaps these will reduce the need for dampening.
With a bit of coding and some examination of some tutorial source code, I put together a .obj loader and a very simple terrain generator.
The .obj loader was a little daunting initially, not having much experience with parsing in files in C, but actually quite easy. I ran into a couple of problems trying to load in textures, so I haven't finished with the uv coordinate reading part of the .obj loader, and hence the plain objects in the picture.
I made the spaceship and person shown in the picture (the ship is about 500 polys and the woman about 3000) using the extremely nice 3d modellor Wings 3d. Pressing tab cycles through the two objects, the camera, and the light, and allows them to be positioned via the mouse and keyboard.
Since I generated the terrain in-program I also had to generate vertex normals (as opposed to simply loading them in an .obj file), which was accomplished by averaging triangle normals together, which in turn are generated by taking the cross product of two of the triangle edges.
Proper Camera and Object Orientation
Using OpenGL calls exclusively for orienting the camera and positioning objects in the screen is possible, but not very useful for specific needs. Instead it's necessary to implement a matrix class that can do rotations and translations and then pass in the results with glMultMatrix() when it's time to draw the screen. It's not possible to do that without understanding how OpenGL stores its matrices, which I explain in the above link.
Simple Direct Layer
Move the mouse around
to change the view orientation, press number pad keys to move, hold them
down to accelerate. The galaxy distribution turns out wrong in windows,
it's a hack I guess and maybe differences between gcc and VC or the math
libraries are to blame? Also, it runs slower in windows.
SDL.dll required for windows to use these
programs...I didn't make this in windows and perhaps there's a way around
this- also, this dll may not work elsewhere, you might need to make your
own. the SDL site will tell how in the
faq section on windows.
These are two different compiles of the same program, but with different
distributions of stars. I used the SDL
libraries because they're fast and portable. OpenGL may be a
possibility when I want to do more than render circles...
On the keypad 8 and 2 for up and down, 4 and 6 to 'strafe' left
and right, 5 and 0 to move forward and back. The mouse controls
orientation, hold the right button down and drag left or right to rotate
along the forward axis. ESC exits.
There are no relativistic effects yet and nothing is to scale. I'm
using a P400: with 10000 star objects your speed may vary.
See below for some much more primitive java incarnations.
This is a very simple paint program, there's no load or save. I did it
to exercise inversion, Bresenham's line drawing algorithm, and
convolution(though this is not currently working correctly).
Copyright © 2002-2003 Lucas W