[SOLVED] Clipplanes in opengl

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
tbw
Posts: 59
Joined: Sat Jan 15, 2011 9:51 am
Location: Germany

[SOLVED] Clipplanes in opengl

Post by tbw »

I've got a strange problem and crawling the forum did not lead me to a solution...
Currently I'm working on a water scene node that incorporates depth information. Everything is fine so far but when I use clip planes in opengl I get the following:
Image

In d3d the same scene looks like this:
Image

I put a debug output of the reflection/refraction and depth rtt into the top.

As you can see the reflection rtt in opengl as well as the refraction rtt are different.

My code for creation the reflection map looks like this:

Code: Select all

void CWaterSceneNode::updateReflectionMap()
{
        //get current camera
        scene::ICameraSceneNode* currentCamera = SceneManager->getActiveCamera();
 
        // get the video driver
        video::IVideoDriver* videoDriver = SceneManager->getVideoDriver();
 
        // render to reflection
        videoDriver->setRenderTarget(ReflectionMap, true, true, video::SColor(0,0,0,0)); 
    
        // set the FOV and far value
        Camera->setNearValue(currentCamera->getNearValue());
        Camera->setFarValue(currentCamera->getFarValue());
        Camera->setFOV(currentCamera->getFOV());
    
        // set the position of the water camera
        core::vector3df position = currentCamera->getAbsolutePosition();
        position.Y = -position.Y + 2.0f*RelativeTranslation.Y; 
        Camera->setPosition(position);
 
        // set the target of the water camera
        core::vector3df target = currentCamera->getTarget();
        target.Y = -target.Y+2.0f*RelativeTranslation.Y;
        Camera->setTarget(target);
 
        // set the reflection camera as active camera
        SceneManager->setActiveCamera(Camera); 
 
        // enable reflection clip plane
        core::plane3d<f32> reflectionClipPlane(
                0, RelativeTranslation.Y-ReflectionClipPlaneOffet, 0, 
                0, 1, 0);
        videoDriver->setClipPlane(0, reflectionClipPlane, true);
 
        // draw the scene
        SceneManager->drawAll(); 
 
        // disable reflection clip plane
        videoDriver->enableClipPlane(0, false);
 
        // reset active camera
        SceneManager->setActiveCamera(currentCamera);
}
... basically the algo used in other realistic waterscene nodes

My question is now: Do clipplanes work different in opengl and d3d or what could be the reason fot the difference.
Thanks in advance for your help...
Last edited by tbw on Tue Mar 13, 2012 6:53 pm, edited 1 time in total.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: Clipplanes in opengl

Post by REDDemon »

Taking a fast look into the drivers implementation there is no extra code for handling the different coordinate systems of OpenGL and D3D for clipplanes.I'll try to make a simple patch. tell me if it works. I think that Irrlicht is left-handed so I will patch OpenGL drivers impl.

EDIT:
try with this:

"COpenGLDriver.cpp" ---- line: 3945

Code: Select all

 
void COpenGLDriver::uploadClipPlane(u32 index)
{
        // opengl needs an array of doubles for the plane equation
        double clip_plane[4];
        clip_plane[0] = UserClipPlanes[index].Plane.Normal.X;
        clip_plane[1] = UserClipPlanes[index].Plane.Normal.Y;
        clip_plane[2] = -UserClipPlanes[index].Plane.Normal.Z;//flip Z to compensate opengl right handed coordinate system.
        clip_plane[3] = UserClipPlanes[index].Plane.D;
        glClipPlane(GL_CLIP_PLANE0 + index, clip_plane);
}
 
this should enough... maybe. Just try, if it didn't work you have just to restore original code.

Code: Select all

 
void COpenGLDriver::uploadClipPlane(u32 index)
{
        // opengl needs an array of doubles for the plane equation
        double clip_plane[4];
        clip_plane[0] = UserClipPlanes[index].Plane.Normal.X;
        clip_plane[1] = UserClipPlanes[index].Plane.Normal.Y;
        clip_plane[2] = UserClipPlanes[index].Plane.Normal.Z;
        clip_plane[3] = UserClipPlanes[index].Plane.D;
        glClipPlane(GL_CLIP_PLANE0 + index, clip_plane);
}
 
if it works i'll submit the patch.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Clipplanes in opengl

Post by hybrid »

Maybe also create a simple test app for our regression suite. I was always planning for such a thing, but clip planes are not used in my projects so far, so I always forget about it. Maybe just a simple sphere with some cut-off sides. But I think I checked the general function of this implementation, as it had some problems with the different application of transformations inside the opengl low-level driver. IIRC, d3d does not take into account the current view matrix, while opengl does (or something like that).
tbw
Posts: 59
Joined: Sat Jan 15, 2011 9:51 am
Location: Germany

Re: Clipplanes in opengl

Post by tbw »

Thank you for your replies!
Unfortunately the patch didn't fix the problem. Actually the behavour is just the same (what I find very surprising).
As the screnshots show, the clipping works correctly for the terrain scene node, also (not clearly visible in the screenshot) the quake scene node is clipped correctly.
The skybox and the animated mesh (hud weapon) are not clipped correctly.
I therefore assume that the clip plane equation is correct and that the viewmatrix is the "bad guy".
I will try to find out a little bit more regarding this issue ....
Thanks again or your hints
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Clipplanes in opengl

Post by hybrid »

Ok, I was testing out these things as well, in order to provide the test app I wrote about. Unfortunately, on my side the d3d clip planes do not seem to work at all. Are you sure that d3d is working correctly?
tbw
Posts: 59
Joined: Sat Jan 15, 2011 9:51 am
Location: Germany

Re: Clipplanes in opengl

Post by tbw »

The only thing I can say is that the clipplane in d3d is doing exactly what I expect (cutting off reflection in the reflection pass and refraction in the refraction pass).
I use only one clip plane, so I can't say what happens in case of using more than one clipplane (with normals in multiple directions).
I compiled an example with the water node and the core components (pp framework for depthpass). you can find it here:

http://www.van-helsing-band.de/irrlicht/water.zip

Maybe you have got the time to have a look on it (the debug output of the rtts refracted reflected and depth is already included)
The initial coordinate of the fps camera is not really clever chosen but flying through the scene gives a good overview of the effect (watching the debug rtt's)
I hope you can run the sample, because it makes heavy use of (unoptimized) shaders...
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Clipplanes in opengl

Post by hybrid »

I could run the app, however the opengl shaders did not work. In two cases the automatic propagation from int to float did not work, so maybe simply replace all your constants with floating point numbers (IIRC, it was 0 and 20 in the water shader).
But, this still didn't help me much. I couldn't get the rendering process behind those images, and also why sometimes the skybox or other things were completely missing. Seems also that some parts are not properly refreshed and cleaned, so at certain parts of the image the result is simply an overrendering mess. I'll try further with my test app.
BTW: Could also be a problem with RTTs. Maybe try to render one of the parts directly into the framebuffer.
tbw
Posts: 59
Joined: Sat Jan 15, 2011 9:51 am
Location: Germany

Re: Clipplanes in opengl

Post by tbw »

Thank you hybrid for your investigation and your hints.
I will work it out and give input when its done ....
tbw
Posts: 59
Joined: Sat Jan 15, 2011 9:51 am
Location: Germany

Re: Clipplanes in opengl

Post by tbw »

It seems that the clipplanes work as they should do.
If I replace the skybox scenenode with a skydome, the reflection and the refraction are correct in d3d aswell as in opengl, so I sassume that the skyboxnode with its translation (centered around the camera) is the reason for the effect.

Image

Thanks again for your hints and your investigation!!!
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: [SOLVED] Clipplanes in opengl

Post by hybrid »

I'm still working on the test case, as the clip planes require different setup resp. have different effects depending on when they are added to the scene node (or updated). I'm not sure yet where the actual bug lies, but there are still some issues which should be resolved in the future.
ACE247
Posts: 704
Joined: Tue Mar 16, 2010 12:31 am

Re: [SOLVED] Clipplanes in opengl

Post by ACE247 »

Did anyone say thanks for the nice water code ;) Thanks!
Can we use it?
tbw
Posts: 59
Joined: Sat Jan 15, 2011 9:51 am
Location: Germany

Re: [SOLVED] Clipplanes in opengl

Post by tbw »

Of course you can use it but the code I posted is definitly work in progress.
I'll post it in the code snippets section when it is finished (specular reflections, wave calculations in vertex shader an so on).
:)
ACE247
Posts: 704
Joined: Tue Mar 16, 2010 12:31 am

Re: [SOLVED] Clipplanes in opengl

Post by ACE247 »

Super, die scenenode(und post processing system) koentest du dann beim IrrExt repo zufuegen :)
Beste Wasser sceneNode bis jetzt.
Hab festgestellt das du aus Deutschland kommst und wohl deutsch bist wie ich ;)
Sinsemilla
Posts: 38
Joined: Mon Jan 09, 2012 5:07 pm

Re: [SOLVED] Clipplanes in opengl

Post by Sinsemilla »

Hi,

Rally nice work :D, the water looks amazing. And also thanks for posting the code. Unfortunately i get some compiler errors if i try to compile the code with codeblocks on Linux, respectively GCC. Here is the compiler-output:

irrMap.h||In member function ‘irr::core::map<const irr::core::string<char>, irr::video::ITexture*>::AccessClass& irr::core::map<const irr::core::string<char>, irr::video::ITexture*>::AccessClass::operator=(const irr::core::map<const irr::core::string<char>, irr::video::ITexture*>::AccessClass&)’:|
irrMap.h|487|error: non-static reference member ‘irr::core::map<const irr::core::string<char>, irr::video::ITexture*>& irr::core::map<const irr::core::string<char>, irr::video::ITexture*>::AccessClass::Tree’, can’t use default assignment operator|
irrMap.h|487|error: non-static reference member ‘const irr::core::string<char>& irr::core::map<const irr::core::string<char>, irr::video::ITexture*>::AccessClass::Key’, can’t use default assignment operator|
PostProcessManager.cpp||In member function ‘void CPostProcessManager::SwapAuxBuffers()’:|
PostProcessManager.cpp|53|note: synthesized method ‘irr::core::map<const irr::core::string<char>, irr::video::ITexture*>::AccessClass& irr::core::map<const irr::core::string<char>, irr::video::ITexture*>::AccessClass::operator=(const irr::core::map<const irr::core::string<char>, irr::video::ITexture*>::AccessClass&)’ first required here |
PostProcessManager.cpp||In member function ‘void CPostProcessManager::render(E_POSTPROCESS_EFFECT)’:|
PostProcessManager.cpp|78|error: ‘const stringc’ has no member named ‘empty’|
PostProcessManager.cpp|82|error: ‘const stringc’ has no member named ‘empty’|
PostProcessManager.cpp||In member function ‘void CPostProcessManager::loadEffectConfig(const char*)’:|
PostProcessManager.cpp|389|error: ‘irr::core::stringc’ has no member named ‘empty’|
PostProcessManager.cpp|401|error: ‘irr::core::stringc’ has no member named ‘empty’|
||=== Build finished: 7 errors, 0 warnings ===|
Last edited by Sinsemilla on Tue Nov 18, 2014 12:44 am, edited 1 time in total.
Sinsemilla
Posts: 38
Joined: Mon Jan 09, 2012 5:07 pm

Re: [SOLVED] Clipplanes in opengl

Post by Sinsemilla »

Perhaps this information helps a little bit.
The GCC version i used: gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 on Linux Mint 12 x86_64
IDE: Codeblocks 10.05
Post Reply