Gouraud shading in Java 2D

Hi all!

I am working on a gouraud-shading algorithm for Java2D. I found a tip on this forum that the Paint and PaintContext - classes could be extended in order to make a new GradientPaintContext accepting 3 points instead of the standard 2. My problem is that I have not dug this deep into Java yet, and I am having trouble understanding the GradientPaintContext source file (as for when pixels are acctually beeing set, what certain operators does and so on). Does anyone know a good site explaining some of the not-so-common operators as i.e. the shift operator, or declarations like:

int g1 = (rgb1 >> 8) & 0xff;

or could someone here explain these to me?

The >> operator is a logical shift right of eight bits. If your input looked like this -


number = 0x0011100000000000

number >> 6

would produce 0x0000000011100000

That is for a 16 bit value, java uses 32 for most values.

The little snippet you provided extracts the green component from the rgb value. Mathmatically it shifts rgb1 eight bits right and then does a logical AND to drop the high 24 bits off. To extract the red component you’d have the same code, except you’d shift 16 to the right.

Java has a >> and >>> operator. The first keeps the sign of a number, assuming its not a float or double, where shifting would screw everything up. The second will shift 0 in at the top bit.

Here are some links to radial gradients. This should help you on trying to understand how to implement you own gradient.

[link=http://www.oreilly.com/catalog/java2d/chapter/ch04.html]Radial Gradient Example[/link]

[link=http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=2D;action=display;num=1088018351;start=1#1]Another Radial Gradient[/link]

HTH -
Dr. A>

Thanks a million! The only thing that still troubles me now is the notation 0xf00f, or similar. I understand that it is a way of writing integer numbers, but how should it be interpreted?

Its hexadecimel, each digits (character) represents 4 bits (i.e. 0-15). A=10, B=11, etc… So 0xF00F is a 32 bit number with the top 4 bits set and the bottom 4 bits set.

Kev

[quote]The >> operator is a logical shift right
[/quote]
Actually it is an arithmetic shift right. The >>> operator is the logical shift.

[quote]Its hexadecimel, each digits (character) represents 4 bits (i.e. 0-15). A=10, B=11, etc… So 0xF00F is a 32 bit number with the top 4 bits set and the bottom 4 bits set.

Kev
[/quote]
If every character represents 4 bits each, shouldn’t 0xF00F be a 16 bit number (4*4 bits)? That is:

0xF00F = 1111 0000 0000 1111 ?

Another thing is the operations & and |.

j = 0011 1111 1111 1111 1111 1111 1111 1110 & 0xF00F;

What does this one acctually do? Thanks for the answers!

I Googled this: (with “Java bit operations boolean”)

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html

Cheers,
Mikael

You’re absoloutely right, and I’m uh… a blathering idiot that can’t type… no no, it was a test right?

Kev

swpalmer - True true. I wasn’t paying close attention when I posted. Silly moi!

elak - There are 2 things to know when doing all this bit shifting/and-ing/or-ing, etc. One is what is going on with the numbers. The second is why the operations are being done. Once you get used to how to manipulate the numbers, you will start getting an intuitive sense of why things are being done.

For example - Anytime you see a shift right with an AND operation say 8/16, you know the goal is to extract some number that was stored/packed into the upper bits of a number. An easy example is RGB numbers. Normally each color component is represented as an 8 bit value. Rather than have 3 ints, it takes less memory to use 1 int and pack the values.

Division and multiplication can also be done using shifting. Shift left by 1 and you have multiplied your original number by 2.


int a = 10;   (0x0a  same as 0b00001010)
int b;
b = a << 1;  (0x14 same as 0b00010100)

The link mgrev2 posted should help too.

HTH,
Dr. A>

Hehe, I’ll promise to tell no one =) But it feels good that I got some clue about what I am about to do. Thank you all!