Curious about cube mapping

BlitzMax Forums/OpenGL Module/Curious about cube mapping

ImaginaryHuman(Posted 2009) [#1]
I'm looking to use OpenGL 1.3 texture combiners and cube maps for a variety of reasons. One reason is to use a `normal map texture` to specify which texels from a cube map will be output. It doesn't need to turn it into a bumpmap or anything, I just need to know what commands cause/imply the reading of a texel from the normal map texture, used AS the normal to find a texel from the cube map.

e.g. in the normal map texture, a texel's RGB values are used to store x/y/z vector information (not normalized). How do I simply use this normal, per pixel, as `input` to the lookup of a cube map? ie if the texel's normal says `point over there`, the cube map is then indexed `over there` and returns a cubemap texel??????

Despite all the stuff I've read online and in the OpenGL superbible/red/blue books I just can't seem to figure out what it is that triggers the normals in the texture to be used as normals for indexing the cubemap. Any ideas?


ImaginaryHuman(Posted 2009) [#2]
This is the article I'm trying to understand:

http://nehe.gamedev.net/data/articles/article.asp?article=20

I think I have a better idea of it now. Bumpmap normals are NOT passed to the cube map - they are passed in by the multitexcoord calls, which are per-vertex not per pixel. They get interpolated across the quad. It represents the angle from the light to the quad surface. If the light is positioned directly in front of a vertex the vector would point straight down the z axis (ie have no effect). Then this is dot-producted against the per-pixel bump map AFTER that, to find a new angle and turn it into a range 0..1. I think I get it.

But I'm still looking for a way to feed per-pixel normals into a cube map lookup, without using shaders. I'm hoping there is a way using texture combiners. ?


ImaginaryHuman(Posted 2009) [#3]
Or alternatively, is there a way to just offset a regular 2d texture coordinate based on values in another texture, without shaders?


_JIM(Posted 2009) [#4]
It should be possible as I've seen it done (look for Environment-Mapped BumpMapping - EMBM)

Not sure how to implement this within BMAX though (or anywhere else for that matter).

I only know the shader way :)

Or, alternatively, do it in software (but it's going to be slow). I did this already and you actually helped me optimize it :)

SetGraphicsDriver GLMax2DDriver()
Graphics(800, 600)

Global testImg:TImage = LoadImage("concept_A.jpg")
Global normalImg:TImage = LoadImage("normalmap.jpg")
Global tempPix:TPixmap
Global frameTime:Int

tempPix:TPixmap = CreatePixmap(normalImg.width, normalImg.Height, PF_BGRA8888)

SetBlend ALPHABLEND
Global DeformPix:TPixmap

While (Not KeyHit(KEY_ESCAPE))
	frameTime = MilliSecs()
	DrawImage(testImg, 0, 0)
	
	DeformPix = GrabPixmap(MouseX(), MouseY(), normalImg.width, normalImg.Height)
	DrawImageDeform(DeformPix, normalImg, MouseX(), MouseY())
	
	DrawText(MilliSecs() - frameTime, 0, 0)
	
	Flip 0
Wend

Function DrawImageDeform(pixSrc:TPixmap, imgNormal:TImage, xD:Float, yD:Float)
	Local pixNrm:TPixmap = LockImage(imgNormal)

	Local x, y
	
	Local n:Int
	Local xS:Float
	Local yS:Float
	
	Local r:Int, g:Int, b:Int
	
	Local w:Int, h:Int
	w = imgNormal.Width
	h = imgNormal.Height

	Local p2:Int ptr = Int ptr(PixmapPixelPtr(pixNrm))
	Local d2:Int ptr = Int ptr(PixmapPixelPtr(tempPix, x, y))
	
	For y = 0 To h - 1
		For x = 0 To w - 1
			n:Int = p2[0]
			
			If (n = $FF808080)
				d2[0] = $00000000
			Else
				r = (n Shr 16) & $ff
				b = n & $ff
				
				xS:Float = x + ((r - 128.0) / 8.0)
				yS:Float = y + ((b - 128.0) / 8.0)
				
				If (xS < 0) Then xS = 0
				If (xS > pixSrc.width - 1) Then xS = pixSrc.width - 1
				If (yS < 0) Then yS = 0
				If (yS > pixSrc.Height - 1) Then yS = pixSrc.Height - 1
	
				d2[0] = ReadPixel(pixSrc, Int(xS), Int(yS))
			EndIf
			
			p2:+1
			d2:+1
		Next
	Next
	DrawImage(LoadImage(tempPix), xd, yd)
End Function


This is what I came up with. Not sure if there's anything left to optimize in there (except if you're doing this with a static image, with nothing moving "behind the distortion"), but if there is and you find it, please share it :)


ImaginaryHuman(Posted 2009) [#5]
Um yah I would need to do it in hardware not with the cpu. It would be applied full-screen every frame so it needs to use something like texture combiners or whatever.

ATI actually did come up with a bump mapping texture combiner extension which did exactly what I need to do - ie use offsets in one texture to displace texture coordinate lookups in another texture... but it seems it never really caught on and I haven't seen it supported on any machines. I guess most people who wanted to do bump mapping turned to things like normal maps and shaders. I know you could displace the coords in a shader simple enough but I'd like something that works in ... say, OpenGL 1.4/1.5ish with no shaders.