3D GRAPHICS RENDERING, PART 1 · Hacker@alphalink.com.au

Intro:

These tutorials are mainly THEORY work with very little code making them very easy to convert to a diffrent language. I will assume that you allready know how to create a .TGA file (and how to use it), This is because it is one of the best (FREE) formats to use, Full possible color usage, no attempts at 'compression' and reletivily easy to understand format. This tutorial also requires a BIT of knowledge about 3D and RGB.

 

The average(Mean) of colors:

OK, lets say that we have a image. A small 15*15 square, the image can be any size with any content you want. Lets say that the image is a smily face (color 255 255 255) on a black background (color 0 0 0) now lets say we want to superimpose another picture over ours (colored light is just that with a straight color), We wont call a 3D card API unless we want it done in real time in large ammounts. So we get the Average of the overlay color and the image color. (A compromise of both). There is also a less CPU intensive way of doing the same thing. Its called Striple Alpha (Hope thats the spelling) It works by puting one pixel of the original image then one of the overlay. This looks rubbish under lower resolutions but under hi-res, low color, it looks ok.

(Note the code example WONT work in basic. It is just an example that most basic programmers will be able to catch onto. Maybe with some modification it MIGHT work, but I have some other things to do. Hey thats a challenge for all you people out there)

DEFINE SUB graphicput (x,y,color)
'This routine Dosent exist, Probbably never will. I just use it as a example
'You would place your .TGA writing routine here, Maybe a Hi-res,Hi-color
'pixel plot. 
DIM R.image(16,16) ' Red Pigment, Original Image
DIM G.image(16,16) ' Blue Pigment, Original Image
DIM B.image(16,16) ' Green Pigment, Original Image

DIM R.overlay(16,16) ' Red Pigment, Overlay
DIM G.overlay(16,16) ' Green Pigment, Overlay
DIM B.overlay(16,16) ' Blue Pigment, Overlay

'Reads All DATA statements and puts it into correct Array's

for x = 1 to 15
        for y = 1 to 15
                read r.image(x,y)
        next y
next x
for x = 1 to 15
        for y = 1 to 15
                read g.image(x,y)
        next y
next x
for x = 1 to 15
        for y = 1 to 15
                read b.image(x,y)
        next y
next x

for x = 1 to 15
        for y = 1 to 15
                read r.overlay(x,y)
        next y
next x
for x = 1 to 15
        for y = 1 to 15
                read g.overlay(x,y)
        next y
next x
for x = 1 to 15
        for y = 1 to 15
                read b.overlay(x,y)
        next y
next x

for x = 1 to 15
        for y = 1 to 15
' Next 3 lines are a cheap HACK. This gets rid of the division by 0 error
                IF r.overlay(x, y) = 0 AND r.image(x, y) = 0 THEN r.overlay(x, y) = 0.1
                IF g.overlay(x, y) = 0 AND g.image(x, y) = 0 THEN g.overlay(x, y) = 0.1
                IF b.overlay(x, y) = 0 AND b.image(x, y) = 0 THEN b.overlay(x, y) = 0.1 
                r = (r.overlay(x, y) + r.image(x, y)) / 2 ' Averages Red pigment's
                g = (g.overlay(x, y) + g.image(x, y)) / 2 ' Averages Green pigment's
                b = (b.overlay(x, y) + b.image(x, y)) / 2 ' Averages Blue pigment's
                graphicput (x,y,[R G B]) ' Place color [R G B] at x,y
        next y
next x
' The folowing DATA statements contain the images. Its ok to screw around
' with numbers

' Original Image CODE.
'Red
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,255,255,0,0,0,0,0,255,255,0,0,0
data 0,0,0,255,255,0,0,0,0,0,255,255,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,255,0,0,0,0,0,0,0,255,0,0,0
data 0,0,0,255,255,255,255,255,255,255,255,255,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
'Green
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,255,255,0,0,0,0,0,255,255,0,0,0
data 0,0,0,255,255,0,0,0,0,0,255,255,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,255,0,0,0,0,0,0,0,255,0,0,0
data 0,0,0,255,255,255,255,255,255,255,255,255,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
'Blue
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,147,147,0,0,0,0,0,147,147,0,0,0
data 0,0,0,147,147,0,0,0,0,0,147,147,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,147,0,0,0,0,0,0,0,147,0,0,0
data 0,0,0,147,147,147,147,147,147,147,147,147,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
' OverLay image code
'Red
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
data 147,147,147,147,147,147,147,147,147,147,147,147,147,147,147
'Green
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
'Blue
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
data 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255

Err.. Thats a bit of code (150 lines approx.). I havent run this I just typed this in as it came to me. It isnt designed to work, Basic wont be able to use a variable's lined together like [R G B] but a .TGA routine could work on CHR$(R) + CHR$(G) + CHR$(B). Hmm. This started out as a theory article. I might have time to do another article but I have a lot of other stuff to do. But because you are all code hungry freaks. (Except that girl on the left... And what did you say your number was again?) Here is a bit of code that I might do the next article about, Effects. All it is is a simple circle. that has a 3d finish. It isnt TOO well done and it is a PALLETTE hog. Its not commented at all. Here is a thought change the RGB value of the large SMEER (color 1 or color 64) to 0 then use that entire area as a Overlay. It looks a lot better then a straight red and It gives that lightbulb feel.

SCREEN 13: y = 64 FOR x = 1 TO 63: y = y - 1: PALETTE x, y: NEXT x FOR x = 1 TO 63: CIRCLE (160, 100), x, x: NEXT x FOR x = 1 TO 320: FOR y = 1 TO 200 z = POINT(x, y) IF z = 0 AND z <> 63 THEN PSET (x, y), POINT(x - 1, y) IF z = 63 THEN PSET (x, y), 0 NEXT y: NEXT x DO: LOOP UNTIL INKEY$ <> "": PALETTE

- Hacker@alphalink.com.au





This article originally appeared in The BASIX Fanzine Issue 13 from December 1998.