Cubemap problem...

BlitzMax Forums/MiniB3D Module/Cubemap problem...

SLotman(Posted 2012) [#1]
So, I finally am trying to do some water reflection using cubemaps. I got it almost working, but there is something wrong with the reflection!

Look at the image below:



My code has nothing special about it:



So... what am I doing wrong? Can anyone help me?

Last edited 2012


AdamRedwoods(Posted 2012) [#2]
does it happen every frame? when the camera moves, is the object in the reflection or does it seem separate?

I notice you're not clearing your color in CamerClsMode(camera, False True) , not sure if you need that or not, try using (camera, true, true).


SLotman(Posted 2012) [#3]
Yeah, every frame... but I just fixed using a clip plane :)


SLotman(Posted 2012) [#4]
I spoke too soon... I'm still having problems:



The 'up' render of the cubemap is wrong... have anyone actually used cubemaps in minib3d without problems???


SLotman(Posted 2012) [#5]
Yup, It's a bug in miniB3D. I Took the "cubemap" sample, altered the teapot to a cube, move above it, and this is what I get:





If somwone knows how to fix it, please help!!!

Two days from now I have to show a small game that will go live in national TV, and I really would like to have a cubemap reflection in the water...

Last edited 2012


SLotman(Posted 2012) [#6]
Running my demo-game in another computer showed even worst results!



Trying the program posted above showed the same strange black bar around the top corner... something is seriously wrong :(

Last edited 2012


SLotman(Posted 2012) [#7]
Just a small bump...

I've been looking at miniB3D sources, but everything appears to be correct... I really have no idea what is causing the problems =(


Kryzon(Posted 2012) [#8]
I don't remember having this problem with cubemaps.

Have you double-checked your rendering code? maybe you're doing something wrong with the camera rotation.

----------------------------------------------------------------
EDIT: Couldn't be, you're running the sample.

I tested that sample the same as you, with a cube, and did not get the artifact. It may be your hardware (unlikely; it wouldn't work at all) OR your MiniB3D module (more likely; everyone has a slightly different version of the original module, you in particular :o) ).

Just as a test, download the official one and try it with that.

Last edited 2012


SLotman(Posted 2012) [#9]
I tried that with the official version! That's the strange part!

But that problem on the screenshot above, in the sample just happens when you move the camera *above* the cube, and look down to it - specially when close to the cube.

It's not hardware related, since I tested it on 2 different computers, and in both I get the same problem.

I will re-download the 0.54 version anyway and see how it goes...

Edit: Yeah, problem still happening.

Edit2: Strange - it seems related to window mode in the sample (in my game test it happens also on fullscreen, don't know why)

Last edited 2012


SLotman(Posted 2012) [#10]
Ouch, more problems... I thought I had the first problem (shown in the first screenshot) solved, but I guess I was wrong.

What is happening: everything that goes through the cubemap, is being reflected upside down - for example: the player underwater, is being reflected above it.

So I setup a clip plane, to clip everything under the cubemap plane - which seems to be the way to do this. This is the code I'm using:

	Local nx:Double=0
	Local ny:Double=1
	Local nz:Double=0
	Local clip_vector:TVector = TVector.Create(EntityX(entity,1), EntityY(entity,1), EntityZ(entity,1))
	Local nd:Double= -clip_vector.dot(TVector.Create(nx,nz,ny))

	Local clip_plane1:Double[]=[nx,ny,nz,nd]
    glClipPlane(GL_CLIP_PLANE0, Varptr(clip_plane1[0]))
    glEnable(GL_CLIP_PLANE0)


But I don't know why, this isn't working! The clip plane is all wrong, and everything below the plane is still being reflected... have anyone made a (working) clip plane in miniB3D before?


Kryzon(Posted 2012) [#11]
I don't think your values are correct. You have to multiply the normal components A, B and C with the XYZ point that defines the plane, not just supply them raw.

Ax + By + Cz + D -> The four values glClipPlane() takes.

'Normal values:
Const A:Double=0
Const B:Double=1  'Flip this value if it doesn't work.
Const C:Double=0

'D is formed with 'another' point you *know* is on the plane. 
'Since the plane is horizontal, we can use the entity's position slightly offset in the X and Z axes:
Const Offset:Double = 5
Local D:Double = - ( (EntityX(entity,1)+Offset)*A + EntityY(entity,1)*B + (EntityZ(entity,1)+Offset)*C ) 

Local clip_equation:Double[] = [EntityX(entity,1)*A, EntityY(entity,1)*B, EntityZ(entity,1)*C, D]
glClipPlane(GL_CLIP_PLANE0, Varptr(clip_equation[0]))
glEnable(GL_CLIP_PLANE0)
References:
http://www.songho.ca/math/plane/plane.html
http://gamedev.stackexchange.com/questions/7308/ray-intersecting-plane-formula-in-c-directx

Last edited 2012


Kryzon(Posted 2012) [#12]
I don't think your values are correct. You have to multiply the normal components A, B and C with the XYZ point that defines the plane, not just supply them raw.

Ax + By + Cz + D -> The four values glClipPlane() takes.

'Normal values:
Const A:Double=0
Const B:Double=1  'Flip this value if it doesn't work.
Const C:Double=0

'D is formed with 'another point you *know* is on the plane'. 
'Since the plane is horizontal, we can use the entity's position slightly offset in the X and Z axes:
Const Offset:Double = 5
Local D:Double = - ( (EntityX(entity,1)+Offset)*A + EntityY(entity,1)*B + (-EntityZ(entity,1)-Offset)*C ) 

Local clip_equation:Double[] = [EntityX(entity,1)*A, EntityY(entity,1)*B, -EntityZ(entity,1)*C, D]
glClipPlane(GL_CLIP_PLANE0, Varptr(clip_equation[0]))
glEnable(GL_CLIP_PLANE0)

'PS: The ENTITY Z's are reversed because we're dealing with raw OpenGL commands here. 
'In OpenGL the Z axis is reversed - MiniB3D hides this fact from us to behave like Blitz3D. 
References:
http://www.songho.ca/math/plane/plane.html
http://gamedev.stackexchange.com/questions/7308/ray-intersecting-plane-formula-in-c-directx

Last edited 2012


SLotman(Posted 2012) [#13]
hmmm... all I get is the same with my own method - the screen seems to be splitted in half; and if I 'flip' B, nothing happens, which means the clip plane is still wrong :(



It should clip everything below the water plane - but you can see it isn't, since you can see the player below the water, and it's reflection just above in the cubemap...
(And the clip plane is appearing like that all the time, even when I'm in much higher ground, which is very, very strange, to say the least)

Last edited 2012


SLotman(Posted 2012) [#14]
Found the problem!

Somehow the clip plane MOVES with the main camera.

So it was a matter of putting the camera at 0,0,0 - and then creating the clip plane, rendering the cubemap, and to finish, return the main camera to it's position.


Kryzon(Posted 2012) [#15]
Good job. Just for reference, what entity were you supplying as 'entity' in that block?


SLotman(Posted 2012) [#16]
A plane I made in 3d max (the water in the screenshot above)

Last edited 2012