[Solved]Stencil buffer from freeglut and render to target.

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!
Post Reply
lvgeng
Posts: 38
Joined: Fri Aug 14, 2015 4:35 pm

[Solved]Stencil buffer from freeglut and render to target.

Post by lvgeng »

Something I cannot understand well...

I included freeglut to use features of stencil buffer, while using irrlicht to draw. It works fine when rendering directly.
But when I add the render to target feature, strange thing happened.
It seems that stencil buffer test just doesn't work while rendering to target. (Because I can change the color of the area I was supposed to set stencil buffer) Any idea what is going on and how to fix it?

Image
Image
My code.

Code: Select all

#include <irrlicht.h>
#include <GL/freeglut.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
 
int main()
{
    IrrlichtDevice *device =
        createDevice(video::EDT_OPENGL, dimension2d<u32>(800, 600), 16,
            false, true, true, 0);
    if (!device)
        return 1;
    device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();
 
    IAnimatedMesh* mesh = smgr->getMesh("../media/f14/f14d.obj");
    if (!mesh)
    {
        device->drop();
        return 1;
    }
    IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);
 
    if (node)
    {
        node->setMaterialFlag(EMF_LIGHTING, true);
        node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
 
        node->setScale(core::vector3df(3,3,3));
        node->setRotation(core::vector3df(0,135,0));
    }
 
    smgr->addLightSceneNode(0, core::vector3df(2000, 2000, 2000), video::SColorf(1.0f, 1.0f, 1.0f), 20000);
    smgr->addLightSceneNode(0, core::vector3df(2000, 2000, -2000), video::SColorf(1.0f, 1.0f, 1.0f), 20000);
    smgr->setAmbientLight(video::SColorf(0.3f, 0.3f, 0.3f));
 
    scene::ICameraSceneNode* currentCamera = smgr->addCameraSceneNodeFPS();
    currentCamera->setFarValue(20000.f);
    currentCamera->setPosition(core::vector3df(0, 0, -70));
    currentCamera->setTarget(core::vector3df(0, 30, 0));
 
    //===============================================
    video::ITexture* rt = 0;
    rt = driver->addRenderTargetTexture(core::dimension2d<u32>(800, 600), "RTT1");  
    // add fixed camera
    
 
 
    while (device->run())
    {
        //driver->beginScene(true, true, SColor(255, 100, 101, 140));
        driver->setRenderTarget(rt, true, true, SColor(255, 100, 101, 140));
        
        glClearStencil(0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glEnable(GL_STENCIL_TEST);
 
        glStencilFunc(GL_NEVER, 0x1, 0x1);
        glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
 
        driver->draw2DRectangle(
            video::SColor(255, 255, 0, 0),
            core::rect<s32>(0,0,400,600)
            );
 
        glStencilFunc(GL_EQUAL, 0x1, 0x1);
        glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
 
        smgr->drawAll();
        
        driver->setRenderTarget(0, true, true, 0);
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        glDisable(GL_STENCIL_TEST);
        driver->draw2DImage(rt, core::position2d<s32>(10, 10),
            core::rect<s32>(0, 0, 800, 600), 0,
            video::SColor(255, 255, 255, 255), true);
 
 
        guienv->drawAll();
        driver->endScene();
    }
    device->drop();
    return 0;
}
=========================================================================
Solution: Use the trunk rather than the release version.

=========================================================================
Last edited by lvgeng on Thu Jan 28, 2016 5:14 pm, edited 1 time in total.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Stencil buffer from freeglut and render to target.

Post by hendu »

RTTs do not have a stencil attachment by default. You can enable this either in shader-pipeline or my fork.
lvgeng
Posts: 38
Joined: Fri Aug 14, 2015 4:35 pm

Re: Stencil buffer from freeglut and render to target.

Post by lvgeng »

hendu wrote:RTTs do not have a stencil attachment by default. You can enable this either in shader-pipeline or my fork.
Er... would you mind to give some more detailed advice? I can still understand shader-pipeline(although without knowing what to do exactly), bit I have no idea about 'my fork'. Would you kindly give me some advices?
lvgeng
Posts: 38
Joined: Fri Aug 14, 2015 4:35 pm

Re: Stencil buffer from freeglut and render to target.

Post by lvgeng »

hendu wrote:RTTs do not have a stencil attachment by default. You can enable this either in shader-pipeline or my fork.
http://irrlicht.sourceforge.net/forum/v ... =2&t=50294

According to the discussion it seems that the support for stencil buffer have been added. Does it means that actually I can render plenty of different stencil buffer and then use them later? And another problem is that I am not so sure which part is for shader... it seems that I am using the default shader. I modified the helloworld example to my project, how can I access the shader control?
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Stencil buffer from freeglut and render to target.

Post by hendu »

My fork of irr is here:
https://github.com/clbr/seirr/tree/shipping

In it, you would add "true" to the end of your addRenderTargetTexture call. I haven't used the new interface in shader-pipeline (or trunk, I guess). Stencil and depth are shared between all RTTs, if you want separate ones you'll need to use the new interface on very latest irr.
lvgeng
Posts: 38
Joined: Fri Aug 14, 2015 4:35 pm

Re: Stencil buffer from freeglut and render to target.

Post by lvgeng »

hendu wrote:My fork of irr is here:
https://github.com/clbr/seirr/tree/shipping

In it, you would add "true" to the end of your addRenderTargetTexture call. I haven't used the new interface in shader-pipeline (or trunk, I guess). Stencil and depth are shared between all RTTs, if you want separate ones you'll need to use the new interface on very latest irr.
Thank you I will check. Currently I am using 1.8.3, is that late enough...? Or I should pull from the repository for beta version?
PS: about the link you gave... how to use it?
Replace the old ones in the source code of irrlicht and then compile the source code for .dll and .lib (and .a for linux?)?
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: Stencil buffer from freeglut and render to target.

Post by Nadro »

@lvgeng
In trunk stencil textures for render targets are available too -> ECF_D24S8. You can check example no. 13 in trunk for more details.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
lvgeng
Posts: 38
Joined: Fri Aug 14, 2015 4:35 pm

Re: Stencil buffer from freeglut and render to target.

Post by lvgeng »

Nadro wrote:@lvgeng
In trunk stencil textures for render targets are available too -> ECF_D24S8. You can check example no. 13 in trunk for more details.
Just one problem.... how to get trunk?
I found this: http://irrlicht.sourceforge.net/forum/v ... =1&t=47538

But it's a relatively long time ago. Just in case, is that what I should do to get trunk? From that SVN repository?
CuteAlien
Admin
Posts: 9665
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Stencil buffer from freeglut and render to target.

Post by CuteAlien »

You need a svn client (tortoise svn on Windows, on Linux install some subversion package). Then you can download it from svn://svn.code.sf.net/p/irrlicht/code/trunk

(so address has changed a little bit since the last post, otherwise it's the same).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Stencil buffer from freeglut and render to target.

Post by mongoose7 »

Well, the link is not working now, but from memory you can download a ZIP file without needing a SVN client.
Post Reply