GWEN/GWork Irrlicht Renderer

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: GWEN Irrlicht Renderer

Post by kklouzal »

Yeah the color pickers have a huge impact on performance, I think I see what set you down this path ;)

I was playing with SetRenderOffset() and AddRenderOffset() it seems to 'improve' things slightly, its very hard to describe, it's hard to describe what happened when I removed all the translate calls also, the controls just didn't quite draw properly, things weren't in the correct position and it just looked terrible.. I'm going to take one more crack at it then let it rest for a while, I've got other issues to sort out hehe

EDIT: SetRenderOffset() looks promising, I cant figure out what to set, I tried setting it to the negative value of the control's position and the positive value of the controls position. The negative values make the control stay drawn in the top left corner of the screen but as you move the control away the image starts to get cut off. so that might be some place to start.
Dream Big Or Go Home.
Help Me Help You.
beasthacker
Posts: 15
Joined: Fri Jul 25, 2014 3:51 pm

Re: GWEN Irrlicht Renderer

Post by beasthacker »

Hey just a quick update for you.

As far as I can tell we shouldn't have to comment out calls to Translate after all.

When Gwen::Controls::Base::DoCacheRender() is called it will save the previous RenderOffset and then call SetRenderOffset(Gwen::Point(0,0)); Once the cached control is finished drawing it will reset the RenderOffset back to its last value.

I think the issue here is with clipping somewhere. I also moved the colorpicker control to (0,0) and it works fine. If I move it to (10,10), the texture is drawn but the top/left of it is clipped? So the texture itself is drawn to the correct place but the control that was drawn inside the texture is being clipped at (0,0).

Here's a crappy diagram to show what is occurring with me.

------------------ <- Edge of window (0,0) )
|
| -------------- <-- Origin where control cache texture is being drawn (10,10) (If I clear cache texture to black then this area is black)
| | ---------- <-- Where visibly clipped control drawing starts (20,20)
| | |
| | |
| | |

So I think that our Clip region is getting messed up so that when we are drawing to the Cache texture it is using the normal clip region used during regular drawing and clipping controls at this point?
beasthacker
Posts: 15
Joined: Fri Jul 25, 2014 3:51 pm

Re: GWEN Irrlicht Renderer

Post by beasthacker »

SUCCESS!

Dude. Its a freaking bug in Gwen. I wasted a bunch of time on the assumption that it was my code...

The problem is in Controls/Base.cpp:

in Gwen::Controls::Base::DoCacheRender() implementation

Code: Select all

 
    // Here is the issue
    if (this != pMaster)
    {
        render->AddRenderOffset(GetBounds());
        render->AddClipRegion(GetBounds());
    }
    else
    {
        render->SetRenderOffset(Gwen::Point(0, 0));
//        render->SetClipRegion(GetBounds()); // This was the original
        render->SetClipRegion(Gwen::Rect(0, 0, GetBounds().w, GetBounds().h)); // This is what it should be!
    }
 
 
Because the clip region's origin was set to the origin of the control, having the control at (0,0) worked fine. If you moved the control away lets say (250,250) and the control's size is smaller than (250,250) then it won't be visible at all!

After this one line change in gwen source everything works fine!

Frame rate has about doubled for the color picker from 70fps to 140fps when using the control and back up to 300fps like other controls when its not in use!
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: GWEN Irrlicht Renderer

Post by kklouzal »

Ah you're the man! Committing changes now!

I have two bad habits, well three, 1st I assume if it's crashing it's their fault, 2nd I assume if it's not working it's my fault, 3rd I assume. :P

Seems like cached control's don't update when they lose focus.
Dream Big Or Go Home.
Help Me Help You.
beasthacker
Posts: 15
Joined: Fri Jul 25, 2014 3:51 pm

Re: GWEN Irrlicht Renderer

Post by beasthacker »

Hey,

Question for you if you've got a minute. My program is done in a resizable window. I've noticed that when resizing the window that gwen's mouse input seems to be off? I was wondering if you've experienced this on your end with Gwen/Irrlicht on Windows? I'm trying to figure out if it has to do with Irrlicht's OSX code or with Gwen. If you have any ideas I'd appreciate it :D
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: GWEN Irrlicht Renderer

Post by kklouzal »

Hmm, I would suggest make sure your resizing the PCanvas along with the Irrlicht window.
Dream Big Or Go Home.
Help Me Help You.
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: GWEN Irrlicht Renderer

Post by kklouzal »

@beasthacker did you solve the issue of resizing cached controls?
Dream Big Or Go Home.
Help Me Help You.
beasthacker
Posts: 15
Joined: Fri Jul 25, 2014 3:51 pm

Re: GWEN Irrlicht Renderer

Post by beasthacker »

Unfortunately not yet. As far as I can tell its not on the Irrlicht renderer side of things but we'd have to implement something in Gwen's source. AFAIK this was never actually done. I think that was the intended purpose of the CCT::UpdateControlCacheTexture() method. I suppose we'd just have to ensure its called in gwen source and then implement it in the renderer. Doesn't sound too hard but I haven't gotten around to it yet.
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Re: GWEN Irrlicht Renderer

Post by Virion »

Looks good. :)
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: GWEN Irrlicht Renderer

Post by kklouzal »

I did some deep searching around on GitHub (since I'm a dummy and deleted my repo some time ago) and finally found my GWEN renderer for Irrlicht!

GWEN/irrlicht.h:

Code: Select all

#pragma once
 
#ifndef GWEN_RENDERERS_IRRLICHT_H
#define GWEN_RENDERERS_IRRLICHT_H
 
#include "Gwen\Gwen.h"
#include "Gwen\BaseRender.h"
 
#include <Irrlicht.h>
 
namespace Gwen
{
    namespace Renderer
    {
        //
        //  Irrlicht ICacheToTexture
        class IrrlichtCTT;
 
        //
        //  Irrlicht Renderer
        class Irrlicht : public Gwen::Renderer::Base
        {
            IrrlichtCTT* m_CTT;
            irr::video::IVideoDriver* Driver;
            irr::video::SColor DrawColor;
            irr::core::rect<irr::s32> ClipRect;
            irr::gui::IGUIFont* Text;
 
        public:
            Irrlicht(irr::IrrlichtDevice* Device);
            ~Irrlicht();
 
            /*void Init();
            void Begin();
            void End();*/
 
            void SetDrawColor(Gwen::Color color);
 
            void DrawFilledRect(Gwen::Rect rect);
 
            void StartClip();
            void EndClip();
 
            void LoadTexture(Gwen::Texture* pTexture);
            void FreeTexture(Gwen::Texture* pTexture);
 
            void DrawTexturedRect(Gwen::Texture* pTexture, Gwen::Rect pTargetRect, float u1 = 0.0f, float v1 = 0.0f, float u2 = 1.0f, float v2 = 1.0f);
            //void DrawMissingImage(Gwen::Rect pTargetRect);
 
            Gwen::Color PixelColour(Gwen::Texture* pTexture, unsigned int x, unsigned int y, const Gwen::Color & col_default = Gwen::Color(255, 255, 255, 255));
 
            ICacheToTexture* GetCTT();
 
            void LoadFont(Gwen::Font* pFont);
            void FreeFont(Gwen::Font* pFont);
            void RenderText(Gwen::Font* pFont, Gwen::Point pos, const Gwen::UnicodeString & text);
            //Gwen::Point MeasureText(Gwen::Font* pFont, const Gwen::UnicodeString & text);
 
            void DrawLinedRect(Gwen::Rect rect);
            void DrawPixel(int x, int y);
 
        };
    }
}
#endif
GWEN/Irrlicht.cpp:

Code: Select all

#include "Gwen/Renderers/Irrlicht.h"
#include "Gwen/Texture.h"
 
#pragma comment(lib, "Irrlicht.lib")
 
#include <iostream>
 
namespace Gwen
{
    namespace Renderer
    {
        //
        //  Irrlicht ICacheToTexture
        class IrrlichtCTT : public Gwen::Renderer::ICacheToTexture
        {
            irr::video::IVideoDriver* m_Driver;
            std::map<Gwen::Controls::Base*, irr::video::ITexture*> m_TextureCache;
 
        public:
            IrrlichtCTT(irr::video::IVideoDriver* Driver) :
            m_Driver(Driver) {}
            ~IrrlichtCTT()
            {
                //
                //  Cleanup all cached textures
                for (auto it : m_TextureCache)
                {
                    m_Driver->removeTexture(it.second);
                }
            }
 
            void Initialize() {}
            void ShutDown() {}
 
            //
            //  Setup Irrlicht to cache this control to a texture
            void SetupCacheTexture(Gwen::Controls::Base* control)
            {
                std::map<Gwen::Controls::Base*, irr::video::ITexture*>::iterator it = m_TextureCache.find(control);
                if (it != m_TextureCache.end())
                {
                    m_Driver->setRenderTarget((*it).second, true, true, irr::video::SColor(0,0,0,0));
                }
            }
 
            //
            //  Revert Irrlicht's render target
            void FinishCacheTexture(Gwen::Controls::Base* control)
            {
                m_Driver->setRenderTarget(NULL, false, false);
            }
 
            //
            //  Draw this controls cached texture
            void DrawCachedControlTexture(Gwen::Controls::Base* control)
            {
                std::map<Gwen::Controls::Base*, irr::video::ITexture*>::iterator it = m_TextureCache.find(control);
                if (it != m_TextureCache.end())
                {
                    m_Driver->draw2DImage((*it).second,
                        irr::core::vector2di(control->X(), control->Y()),
                        irr::core::rect<irr::s32>(irr::core::dimension2d<irr::s32>(0, 0),
                        (*it).second->getSize()),
                        NULL,
                        irr::video::SColor(255, 255, 255, 255),
                        true);
                }
            }
 
            //
            //  Create this controls cached texture if it doesn't already exist
            void CreateControlCacheTexture(Gwen::Controls::Base* control)
            {
                if (m_TextureCache.find(control) == m_TextureCache.end())
                {
                    const Gwen::Rect Bounds = control->GetBounds();
                    irr::video::ITexture* RTT = m_Driver->addRenderTargetTexture(irr::core::dimension2d<irr::u32>(Bounds.w, Bounds.h));
                    m_TextureCache.insert(std::pair<Gwen::Controls::Base*, irr::video::ITexture*>(control, RTT));
                }
            }
 
            void UpdateControlCacheTexture(Gwen::Controls::Base* control) {}
 
            void SetRenderer(Gwen::Renderer::Base* renderer) {}
        };
 
        //
        //  Irrlicht Renderer
        Irrlicht::Irrlicht(irr::IrrlichtDevice* Device) :
            m_CTT(new IrrlichtCTT(Device->getVideoDriver())), Driver(Device->getVideoDriver())
        {
            DrawColor.set(255, 255, 255, 255);
            ClipRect = irr::core::rect<irr::s32>();
            Text = Device->getGUIEnvironment()->getBuiltInFont();
        }
 
        Irrlicht::~Irrlicht()
        {
            delete m_CTT;
        }
 
        /*void Irrlicht::Init() {}
        void Irrlicht::Begin() {}
        void Irrlicht::End() {}*/
 
        void Irrlicht::SetDrawColor(Gwen::Color color)
        {
            DrawColor.set((irr::u32)color.a, (irr::u32)color.r, (irr::u32)color.g, (irr::u32)color.b);
        }
 
        void Irrlicht::DrawFilledRect(Gwen::Rect rect)
        {
            Translate(rect);
            Driver->draw2DRectangle(DrawColor,
                irr::core::rect<irr::s32>(rect.x, rect.y, rect.x + rect.w, rect.y + rect.h),
                &ClipRect);
        }
 
        void Irrlicht::StartClip()
        {
            const Gwen::Rect rect = ClipRegion();
            ClipRect = irr::core::rect<irr::s32>(rect.x*Scale(), rect.y*Scale(), (rect.x + rect.w)*Scale(), (rect.y + rect.h)*Scale());
        }
 
        void Irrlicht::EndClip()
        {
            ClipRect = irr::core::rect<irr::s32>();
        }
 
        void Irrlicht::LoadTexture(Gwen::Texture* pTexture)
        {
            irr::video::ITexture* NewTex = Driver->getTexture(pTexture->name.c_str());
            if (!NewTex) {
                pTexture->failed = true;
                return;
            }
 
            const irr::core::dimension2d<irr::u32> TexSize = NewTex->getSize();
            pTexture->width = TexSize.Width;
            pTexture->height = TexSize.Height;
            pTexture->data = NewTex;
        }
 
        void Irrlicht::FreeTexture(Gwen::Texture* pTexture)
        {
            if (!pTexture->FailedToLoad())
            {
                Driver->removeTexture((irr::video::ITexture*)pTexture->data);
            }
        }
 
        void Irrlicht::DrawTexturedRect(Gwen::Texture* pTexture, Gwen::Rect pTargetRect, float u1, float v1, float u2, float v2)
        {
            if (!pTexture->FailedToLoad())
            {
                const unsigned int w = pTexture->width;
                const unsigned int h = pTexture->height;
 
                Translate(pTargetRect);
                
                Driver->draw2DImage((irr::video::ITexture*)pTexture->data,
                    irr::core::rect<irr::s32>(pTargetRect.x, pTargetRect.y, pTargetRect.x + pTargetRect.w, pTargetRect.y + pTargetRect.h),
                    irr::core::rect<irr::s32>(u1*w, v1*h, u2*w, v2*h),
                    &ClipRect,
                    0,
                    true);
            }
        }
 
        //void Irrlicht::DrawMissingImage(Gwen::Rect pTargetRect) {}
 
        Gwen::Color Irrlicht::PixelColour(Gwen::Texture* pTexture, unsigned int x, unsigned int y, const Gwen::Color & col_default)
        {
            if (pTexture->FailedToLoad())
            {
                return col_default;
            }
 
            irr::video::ITexture* Texture = (irr::video::ITexture*)pTexture->data;
 
            irr::video::SColor PixelColor;
            const irr::u32 pitch = Texture->getPitch();
            const irr::video::ECOLOR_FORMAT format = Texture->getColorFormat();
            static const irr::u32 bytes = irr::video::IImage::getBitsPerPixelFromFormat(format) / 8;
 
            unsigned char* buffer = (unsigned char*)Texture->lock();
            if (buffer)
            {
                PixelColor = irr::video::SColor(*(unsigned int*)(buffer + (y * pitch) + (x*bytes)));
                Texture->unlock();
                return Gwen::Color(PixelColor.getRed(), PixelColor.getGreen(), PixelColor.getBlue(), PixelColor.getAlpha());
            }
 
            return col_default;
        }
 
        ICacheToTexture* Irrlicht::GetCTT()
        {
            return m_CTT;
        }
 
        void Irrlicht::LoadFont(Gwen::Font* pFont)
        {}
 
        void Irrlicht::FreeFont(Gwen::Font* pFont)
        {}
 
        void Irrlicht::RenderText(Gwen::Font* pFont, Gwen::Point pos, const Gwen::UnicodeString & text)
        {
            Translate(pos.x, pos.y);
            Text->draw(text.c_str(),
                irr::core::rect<irr::s32>(pos.x, pos.y, pos.x, pos.y),
                DrawColor,
                false,
                false,
                &ClipRect);
        }
 
        /*Gwen::Point Irrlicht::MeasureText(Gwen::Font* pFont, const Gwen::UnicodeString & text)
        {
        Gwen::Point NewPoint;
        NewPoint.x = 3;
        NewPoint.y = 8;
        return NewPoint;
        }*/
 
        void Irrlicht::DrawLinedRect(Gwen::Rect rect)
        {
            Translate(rect);
            Driver->draw2DRectangleOutline(irr::core::rect<irr::s32>(rect.x, rect.y, rect.x + rect.w, rect.y + rect.h), DrawColor);
        }
 
        void Irrlicht::DrawPixel(int x, int y)
        {
            Driver->drawPixel(x, y, DrawColor);
        }
 
    }
}
I'm working to get it integrated with the only repository managing to keep making progress on improving GWEN (https://github.com/billyquith/GWork)
Will post an update here when I have something worth mentioning!
Dream Big Or Go Home.
Help Me Help You.
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: GWEN Irrlicht Renderer

Post by kklouzal »

Anyone and everyone who would like to see the Irrlicht renderer adopted to GWork (the active developed GWEN fork) please comment this issue letting the developer know you want it (the dev requested this himself) https://github.com/billyquith/GWork/issues/12

Even if you're not going to use GWork but would like to see it available for Irrlicht please make a comment!
Thank you guys
Dream Big Or Go Home.
Help Me Help You.
chronologicaldot
Competition winner
Posts: 684
Joined: Mon Sep 10, 2012 8:51 am

Re: GWEN Irrlicht Renderer

Post by chronologicaldot »

I seemed to have overlooked this. Seems like a decent looking GUI system and would be nice to have. Can it load and render UTF8 fonts of multiple languages (or just "UTF-8 compliant", i.e. render ASCII, not whine with everything else)?

The docs aren't very helpful.
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: GWEN Irrlicht Renderer

Post by kklouzal »

It has full support for full unicide strings inside the library however it can only render what it's parent engine is capable of rendering. Currently I slapped together text rendering to use IGUI default font. A more appropriate approach would be to use freetype and integrate that into the renderer. I don't know enough about Irrlicht currently to do this.

Currently I'm having a display bug where transparency shows as a bright yellow color. I *think* this is specific to using the opengl driver but I haven't tested with directx yet. Back when I originally created the renderer I remember this very same bug but don't remember what I did to remedy it..
Image
Dream Big Or Go Home.
Help Me Help You.
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: GWEN Irrlicht Renderer

Post by kklouzal »

Everything is working again!
Image
Please show your support: https://github.com/billyquith/GWork/issues/12
Dream Big Or Go Home.
Help Me Help You.
Post Reply