I’m creating a custom control in Visual Basic 6.0 which takes any image – still or moving – and superimposes a 3d object on top of it.
Surprisingly, that’s not as easy as I first assumed it would be.
I first started down the path of leveraging OpenGL programming to do this.
Here’s my 3d triangle, rotated 30 degrees:
Looks decent right? Thank you Nehe for the tutorial on hot to do parts of this in OpenGL.
So the goal from there was to place this 3d model over a 2d image such as this:
But there’s a problem, right? See the black area around my pyramid? Ugly, right?
So what I set out to do with this control was to remove that black area.
There’s an easy way to ‘fake it’ by setting something called a transparency color.
Put specifically, I would say to Windows ‘hey, for everything that’s black, ignore the black on the 3d image and instead show me the background image – let the ‘background image’ bleed through.
But I immediately vetoed that idea for too many reasons to list, not the least of which is – it’s just plain lazy.
Naw. Real reason’s simpler than that. Later, when I get into texturing the image or low light conditions, there may be parts of my 3d model which, with lighting applied, actually become black.
So if I set the black to transparent, parts of my model itself would expose the background.
You’ve seen it on green screens, right? Where the person wears a color that’s close to the green and pieces of their clothing actually become transparent?
So to avoid that with my models, my goal was to paint the pyramid and pyramid ONLY on the background.
Without first drawing that ugly black box.
SO FIRST. I would copy the part of the background I wanted to use onto something called an off-screen buffer.
SECOND I was going to draw my model – the pyramid – onto this offscreen buffer AFTER the background had been copied into it.
This would have – in theory – created a superimposed model. No transparent colors necessary.
AND THEN I would paste that model to the screen.
This is referred to as ‘double buffering’, where an off screen memory buffer is used to created the composite image and then is swapped to the screen using a call to the Windows API Swapbuffers command.
So as I chugged down this path. I hit problem after problem after problem.
And then a roadblock.
What I learned was – OPENGL – the 3d modelling ?language? / API I was using – cannot actually render INTO a 2D buffer. Now it CAN render TO a 2d buffer, which basically overwrites anything that was in that buffer to begin with, but it cannot write into that buffer.
This presents me with a problem.
I have to learn Matrix Math – and specifically- 3d transformations and build my own renderer which I can then use to render my objects directly into the scenes and avoid the black discolorations.
Back when I worked at Orbital in Chandler, I worked alongside Guidance and Navigation folks and engineers who were awesome with 3d math, it was something they had to be keenly aware of to make sure rockets didn’t hit the side of buildings and would instead make it to space..
I suppose I ‘put it to the universe’ at that time that I wanted to learn that math.
I didn’t have a valid reason for it at the time.
Seeing as I am in part programming my own senses to render 3d objects and lovers in my own visual space without the silly square black backgrounds,
While simultaneously teaching a computer to render objects in 3d space…
Be careful what you wish for.
Reality has a habit of giving what’s asked for.