Author Topic: converting an image to bluescale  (Read 5296 times)

0 Members and 1 Guest are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
converting an image to bluescale
« on: December 12, 2006 »
im messing about with a bit of dot3 bump mapping in gl but i cant seem to get the conversion to blue scale right and was wondering if one of you guys rememberd how its done.

the way im trying is adding my color values together and dividing them by five.
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: converting an image to bluescale
« Reply #1 on: December 12, 2006 »
Not quite sure what you mean, I'm messing around with dot3 stuff just now too but i'm using a separate heightmap. If you're trying to generate a heightmap from colours then I wouldn't think there's any right or wrong way but shouldn't you be dividing by 3 (unless you're storing as a float in which case the scale won't matter until you're converting to a normal map).
« Last Edit: December 12, 2006 by Stonemonkey »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: converting an image to bluescale
« Reply #2 on: December 12, 2006 »
yeah im trying to generate a blue scale image off of the origonal image so does the process go something like this.

imager = ( imager + imageg + imageb ) / 3
imageg = ( imager + imageg + imageb ) / 3
imageb = ( imager + imageg + imageb ) / 3

the way im doing my dot 3 is with three texture units a heightmap texture a normals texture and an ordinary texture.
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: converting an image to bluescale
« Reply #3 on: December 12, 2006 »
I'm still not sure, I know there's often different methods for things so I can't say if that's right or not but what I'm doing is quite different.

I start off with a heightmap with single values for the height of each pixel

then for each pixel I use the heights of the 4 pixels around it to calculate the normal for each pixel

Then I write the normal into the normal map texture with:

Code: [Select]

'normalise
  Â  Â  Â  Â  Â  d=1.0/sqr(nx^2+ny^2+nz^2)
  Â  Â  Â  Â  Â  nx*=d
  Â  Â  Â  Â  Â  ny*=d
  Â  Â  Â  Â  Â  nz*=d

'scale and offset for 8 bits per colour channel
  Â  Â  Â  Â  Â  red=(nx+1.0)*127.0
  Â  Â  Â  Â  Â  green=(ny+1.0)*127.0
  Â  Â  Â  Â  Â  blue=(nz+1.0)*127.0

  Â  Â  Â  Â  Â  'write red,green,blue to normal texture(check the format of how you're uploading the texture)

and there's no need to use the heightmap during rendering.
« Last Edit: December 12, 2006 by Stonemonkey »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: converting an image to bluescale
« Reply #4 on: December 13, 2006 »
cheers mate.

im trying somthing like this now to create a normals map.

Code: [Select]
sub GenNormalizationCubeMap( byval size as integer, byval texid as GLuint )
   
        dim ImageData as ubyte ptr

dim as single offset = 0.5f , halfSize = size * 0.5f
dim as double temp(0 to 2) , d
dim as integer bytePtr = 0
   
        ImageData = allocate( size*size*3 )
   
glGenTextures( 1 , @texid )
glBindTexture( GL_TEXTURE_CUBE_MAP , texid )

for j=0 to size
        for i=0 to size

temp(0) = halfSize
temp(1) = j+offset-halfSize
temp(2) = -(i+offset-halfSize)
           
d=1.0/sqr(temp(0)^2+temp(1)^2+temp(2)^2)
                        temp(0)*=d
                        temp(1)*=d
                        temp(2)*=d

                        ImageData[bytePtr] = (temp(0)+1.0) * 127.0f
ImageData[bytePtr+1] = (temp(1)+1.0) * 127.0f
ImageData[bytePtr+2] = (temp(2)+1.0) * 127.0f
           
bytePtr += 3
           
next
next
   
glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X , 0 , GL_RGB8 , size , size , 0 , GL_RGB , GL_UNSIGNED_BYTE , ImageData )

bytePtr = 0
   
for j=0 to size
for i=0 to size
           
temp(0) = -halfSize
temp(1) = j+offset-halfSize
temp(2) = i+offset-halfSize
           
d=1.0/sqr(temp(0)^2+temp(1)^2+temp(2)^2)
                        temp(0)*=d
                        temp(1)*=d
                        temp(2)*=d

ImageData[bytePtr] = (temp(0)+1.0) * 127.0f
ImageData[bytePtr+1] = (temp(1)+1.0) * 127.0f
ImageData[bytePtr+2] = (temp(2)+1.0) * 127.0f

bytePtr+=3
           
next
next
   
glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_X , 0 , GL_RGB8 , size , size , 0 , GL_RGB , GL_UNSIGNED_BYTE , ImageData )

bytePtr = 0
   
for j=0 to size
for i=0 to size
           
temp(0) = i + offset - halfSize
temp(1) = -halfSize
temp(2) = j + offset - halfSize
           
d=1.0/sqr(temp(0)^2+temp(1)^2+temp(2)^2)
                        temp(0)*=d
                        temp(1)*=d
                        temp(2)*=d
           
ImageData[bytePtr] = (temp(0)+1.0) * 127.0f
ImageData[bytePtr+1] = (temp(1)+1.0) * 127.0f
ImageData[bytePtr+2] = (temp(2)+1.0) * 127.0f

bytePtr += 3
           
next
next
glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Y , 0 , GL_RGB8 , size , size , 0 , GL_RGB , GL_UNSIGNED_BYTE , ImageData )

bytePtr = 0
for j=0 to size
for i=0 to size
           
temp(0) = i+offset-halfSize
temp(1) = halfSize
temp(2) = -(j+offset-halfSize)
           
d=1.0/sqr(temp(0)^2+temp(1)^2+temp(2)^2)
                        temp(0)*=d
                        temp(1)*=d
                        temp(2)*=d

ImageData[bytePtr] = (temp(0)+1.0) * 127.0f
ImageData[bytePtr+1] = (temp(1)+1.0) * 127.0f
ImageData[bytePtr+2] = (temp(2)+1.0) * 127.0f

bytePtr += 3
           
next
next
glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y , 0 , GL_RGB8 , size , size , 0 , GL_RGB , GL_UNSIGNED_BYTE , ImageData )

bytePtr = 0
for j=0 to size
for i=0 to size
           
temp(0) = i+offset-halfSize
temp(1) = (j+offset-halfSize)
temp(2) = halfSize
           
d=1.0/sqr( (temp(0)^2) + (temp(1)^2) + (temp(2)^2) )
                        temp(0)*=d
                        temp(1)*=d
                        temp(2)*=d

ImageData[bytePtr] = (temp(0)+1.0) * 127.0f
ImageData[bytePtr+1] = (temp(1)+1.0) * 127.0f
ImageData[bytePtr+2] = (temp(2)+1.0) * 127.0f

bytePtr += 3
           
next
next
   
glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_Z , 0 , GL_RGB8 , size , size , 0 , GL_RGB , GL_UNSIGNED_BYTE , ImageData )

bytePtr = 0
for j=0 to size
for i=0 to size
           
temp(0) = -(i+offset-halfSize)
temp(1) = (j+offset-halfSize)
temp(2) = -halfSize
           
d=1.0/sqr(temp(0)^2+temp(1)^2+temp(2)^2)
                        temp(0)*=d
                        temp(1)*=d
                        temp(2)*=d
           
ImageData[bytePtr] = (temp(0)+1.0) * 127.0f
ImageData[bytePtr+1] = (temp(1)+1.0) * 127.0f
ImageData[bytePtr+2] = (temp(2)+1.0) * 127.0f

bytePtr += 3
           
next
next

glTexImage2D( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z , 0 , GL_RGB8 , size, size , 0, GL_RGB, GL_UNSIGNED_BYTE, ImageData )

glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)
   
end sub

but when i display the texture it creates its all white ie it wont do anything when i try and run a light over it.
« Last Edit: December 13, 2006 by ninogenio »
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: converting an image to bluescale
« Reply #5 on: December 13, 2006 »
heres a link to the exe and source it all works apart from the lighting when i change my light positions it has no effect on the texture.

http://www.4shared.com/file/7332918/9d6343a0/dot3bumpmap.html
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: converting an image to bluescale
« Reply #6 on: December 13, 2006 »
I get complete white from that stage too, tried my own cubemap generation in there and get the same result. I'm having a look but can't see what's wrong yet.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: converting an image to bluescale
« Reply #7 on: December 13, 2006 »
Took a while:

pass texid to the GenNormalizationCubeMap function byref.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: converting an image to bluescale
« Reply #8 on: December 13, 2006 »
ahh thanks mate have some karma.

i cant belive it was a little mistake like that.

btw apart from gening a normal map at run time is this how dot3 is done in the wild as to speak or is there better ways of doing it.
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: converting an image to bluescale
« Reply #9 on: December 13, 2006 »
I'm not sure if there's other ways to do this although there are slightly different ways to achieve it as atm I am using a single texture unit with the light vector passed as a colour value instead of a vector and I'm not renormalising with a cubemap although I'll probably put that option back in.
There are also other types of bumpmapping which I've not looked into yet.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: converting an image to bluescale
« Reply #10 on: December 13, 2006 »
DOT3 is ideal because it's the full diffuse lighting calculation on every pixel, instead of just the corners.  There are ways of creating normal maps from ordinary textures which kind of simulate what the normals on that surface might look like, or some art packages let you draw normal maps in the same way they let you draw colour (texture) maps.  Alternatively, you can use a constant normal for the lighting instead of having a normal map for each pixel.

There are a number of other bumpmap types available, which are all approximations to DOT3.
The two in D3D are
D3DTOP_BUMPENVMAP
D3DTOP_BUMPENVMAPLUMINANCE
but I don't know if people are still using those now that DOT3 is actually fast enough.  I remember trying DOT3 on a GeForce 256 and it sucked for performance, about 10 to 100x slower than just ordinary textures.

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: converting an image to bluescale
« Reply #11 on: December 14, 2006 »
One that I do when needing just the blue ( 0-255 ) for palette indexed effects within Blitz and Freebasic is simply, AND the colour value by 255 ( Or $HFF )
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: