Issues understanding RTShader for GL3Plus

Problems building or running the engine, queries about how to use features etc.
Kalaherty
Gnoblar
Posts: 1
Joined: Sun Feb 09, 2025 12:46 pm

Issues understanding RTShader for GL3Plus

Post by Kalaherty »

Ogre Version: :14.3.2:
Operating System: :Linux Mint:
Render System: :GL3Plus:
Hello,
I've recently been learning Ogre and I've been having issues understanding how to setup the RTShader system for RenderSystem_GL3Plus.
I'm not using OgreBites (though I have been using it for reference) and I'm trying to handle Ogre with SDL2.

So far I've been able to make scenes with animated and textured models using the fixed function RenderSystem_GL, but when I switched over to RenderSystem_GL3Plus; I do not believe that I'm generating shaders correctly. They appear in my shader cache, but the models keep generating two techniques, where one has an attached shader, while the other does not, which seems to halt the renderer.

Since I was not able to sign up due to the current gmail issues (I ended up making a yahoo account), I was trying to piece together things from searching the forums, skimming the documentation, comparing it to the samples and asking ChatGPT, but I think i just confused myself (ChatGPT did a lot of hallucinating about non existent member functions), so I don't think I understand the RTShader system at all.

I'm hoping that someone can point me in the right direction, so I've prepared a small static scene with an ambient light, a point light, camera and 3D model using RenderSystem_GL because it shows my current understanding and how I'm using SDL2 with Ogre. Apart from the somewhat redundant usage of the RTShader generator, I'm hoping I haven't already messed up.

Code: Select all

#include <Ogre.h>
#include <OgreRTShaderSystem.h>
#include <OgreCodec.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
#include <iostream>

void unregisterConflictingCodecs()      //so I can use *.png files
{
    const std::vector<std::string> formatsToUnregister = {
        "jpeg", "jpg", "png", "bmp", "tga", "gif", "hdr", "psd", "pic", "ppm", "pgm"
    };

for (const auto& format : formatsToUnregister)
{
    Ogre::Codec* existingCodec = Ogre::Codec::getCodec(format);
    if (existingCodec)
    {
        Ogre::Codec::unregisterCodec(existingCodec);
        std::cout << "Unregistered conflicting codec for '" << format << "'." << std::endl;
    }
}
}

int main(int argc, char *argv[])
{
    // Initialize SDL2
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0)
    {
        std::cerr << "Failed to initialize SDL2: " << SDL_GetError() << std::endl;
        return -1;
    }

// Create an SDL2 window
SDL_Window* sdlWindow = SDL_CreateWindow(
    "Ogre App with SDL2",
    SDL_WINDOWPOS_CENTERED,
    SDL_WINDOWPOS_CENTERED,
    1280, 720,
    SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL
);

if (!sdlWindow)
{
    std::cerr << "Failed to create SDL2 window: " << SDL_GetError() << std::endl;
    SDL_Quit();
    return -1;
}

// Get the native window handle for Ogre
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(sdlWindow, &wmInfo);
void* nativeWindowHandle = nullptr;

Ogre::String winHandle;
#if defined(_WIN32)
    winHandle = Ogre::StringConverter::toString(reinterpret_cast<size_t>(wmInfo.info.win.window));
#elif defined(__linux__)
    winHandle = Ogre::StringConverter::toString(reinterpret_cast<size_t>(wmInfo.info.x11.window));
#endif

#ifdef _WIN32
    nativeWindowHandle = wmInfo.info.win.window;
#elif __linux__
    nativeWindowHandle = reinterpret_cast<void*>(wmInfo.info.x11.window);
#else
#error Unsupported platform!
#endif

// Initialize Ogre
Ogre::Root* root = new Ogre::Root();
unregisterConflictingCodecs();

root->loadPlugin("Codec_STBI"); // for *.png loading

root->loadPlugin("RenderSystem_GL"); // OpenGL rendering
root->setRenderSystem(root->getAvailableRenderers().at(0));
Ogre::NameValuePairList params;

#if defined(_WIN32)
    params["externalWindowHandle"] = winHandle;
#elif defined(__linux__)
    params["parentWindowHandle"] = winHandle;
#endif

Ogre::RenderWindow* renderWindow = root->initialise(false);
renderWindow = root->createRenderWindow("MainRenderWindow", 1280,720,false, &params);

Ogre::ResourceGroupManager::getSingleton().addResourceLocation("./media/models", "FileSystem", "General");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("./media/textures", "FileSystem", "General");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("./media/materials", "FileSystem", "General");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("./media/Main", "FileSystem", "OgreInternal");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("./media/RTShaderLib", "FileSystem", "OgreInternal");

Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("OgreInternal");

Ogre::SceneManager* sceneMgr = root->createSceneManager();
Ogre::Camera* camera = sceneMgr->createCamera("MainCamera");
camera->setNearClipDistance(5);
camera->setAutoAspectRatio(true);

Ogre::SceneNode* camNode = sceneMgr->getRootSceneNode()->createChildSceneNode();
camNode->attachObject(camera);

Ogre::Viewport* viewport = renderWindow->addViewport(camera);
viewport->setBackgroundColour(Ogre::ColourValue(0.1f, 0.1f, 0.1f));

// Set up the RTShader system
Ogre::RTShader::ShaderGenerator::initialize();
Ogre::RTShader::ShaderGenerator* shadergen = Ogre::RTShader::ShaderGenerator::getSingletonPtr();
shadergen->addSceneManager(sceneMgr); // Register the scene manager with the RTSS

Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("General");

camNode->setPosition(0, 10, 20);
camNode->lookAt(Ogre::Vector3(0, 2, 0), Ogre::Node::TransformSpace::TS_WORLD);


// Set up the lighting
sceneMgr->setAmbientLight(Ogre::ColourValue(0.0f, 0.0f, 0.0f));
sceneMgr->setShadowTechnique(Ogre::ShadowTechnique::SHADOWTYPE_STENCIL_ADDITIVE);

Ogre::Light* light = sceneMgr->createLight("PointLight");
light->setType(Ogre::Light::LT_POINT);
Ogre::SceneNode* myLight = sceneMgr->getRootSceneNode()->createChildSceneNode();
myLight->setPosition(10, 12, 15);
myLight->attachObject(light);

//add 3d model
Ogre::Entity* pent = sceneMgr->createEntity("purp.mesh");
pent->setCastShadows(true);
Ogre::SceneNode* pnode = sceneMgr->getRootSceneNode()->createChildSceneNode();
pnode->attachObject(pent);
pnode->setScale(.5,.5,.5);
pnode->setPosition(0,5,0);

// For main loop
bool running = true;
SDL_Event event;

const int TARGET_FPS = 60; // Your desired frame rate
const int MAX_FRAME_TIME = 1000 / TARGET_FPS; // Maximum time a frame can take in milliseconds

float _dt;                  //for delta time generation
float _oldTime, _newTime;

while (running)
{
    // Process SDL2 events
    while (SDL_PollEvent(&event))
    {
        if (event.type == SDL_QUIT)
        {
            running = false;
        }
        else if (event.type == SDL_KEYDOWN)
        {
            if (event.key.keysym.sym == SDLK_ESCAPE)
            {
                running = false;
            }
        }
    }

    _oldTime = _newTime;        //generate delta time
    _newTime = SDL_GetTicks();
    _dt = (_newTime-_oldTime)/1000.0f;


    root->renderOneFrame(); //Render Ogre scene

    int frameTime = SDL_GetTicks() - _newTime; // for frame skipping
  if (frameTime < MAX_FRAME_TIME)
    {
        SDL_Delay(MAX_FRAME_TIME - frameTime);
    }

}

root->shutdown();

SDL_DestroyWindow(sdlWindow);
SDL_Quit();

return 0;
}

And I'm using Blender2Ogre to export models. I'm unsure if the material export is correct when it comes to the RTShader element, so here is the material I used in the above scene:

Code: Select all

// generated by blender2ogre 0.9.0 on 2025-02-10 12:22:53
material purpmat {
    receive_shadows on
    technique {
        pass {
            diffuse 0.8 0.8 0.8 1.0
            specular 0.709091 0.0 0 0 0

        // additional maps - requires RTSS
        rtshader_system {
            lighting_stage normal_map low.001.png
            lighting_stage metal_roughness
        }

        // - base_color_texture
        texture_unit {
            texture purpskin_01.png
            tex_address_mode wrap
            colour_op modulate
        }

        // Don't know how to export: metallic_texture exbump.png
    }
}
}

My apologies if this post is somewhat scattered. I'm quite introverted and a bit burnt out. I'll be very appreciative for any help provided and I'll do my best to provide any other additional information. Sorry for not posting a current attempt of using RenderSystem_GL3Plus and generating the shaders, but I'm so confused by it; my current attempts are very convoluted and obviously wrong, so I want to try and restart at step one.

paroj
OGRE Team Member
OGRE Team Member
Posts: 2142
Joined: Sun Mar 30, 2014 2:51 pm
x 1151

Re: Issues understanding RTShader for GL3Plus

Post by paroj »

see this for a minimal sample without bites:
https://github.com/OGRECave/ogre/blob/m ... _sample.py