Creating a global IrrlichtDevice for IShaderConstantSetCallB

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
runyonave
Posts: 3
Joined: Wed Oct 21, 2020 11:57 pm

Creating a global IrrlichtDevice for IShaderConstantSetCallB

Post by runyonave »

Hello, I have been learning Irrlicht for the past few weeks and currently going through the Shader tutorial. I have a question, specifically more geared towards C++ here.

According to the tutorial, the IShaderConstantSetCallBack uses IrrlichtDevice* device to set some uniform data. In the tutorial the IrrlichtDevice* device is set as a global within the inherited IShaderConstantSetCallBack class. I am not fond of this as it kind of makes it seem a bit messy. I have instead created a utility class that will store the device as a global variable. The class is called in whatever file that uses device (it is set in main.cpp). I am not a fan of using global variables, but I find this more readable.
,
So my question is, is there a better method of setting device? I.e. I would rather set device in main.cpp and send a reference to it to the IShaderConstantSetCallBack. Or is there another way I can set the shader uniforms outside of IShaderConstantSetCallBack? I have more experience with OpenGL than irrlicht, but is there a way to set the current context with the shader ID, and then set the uniforms that way? Thanks!

Header:

Code: Select all

#ifndef UTILITY_VARS_H
#define UTILITY_VARS_H
 
#include <irrlicht.h>
 
class UtilityVars
{
public:
    static irr::IrrlichtDevice* device;
};
 
#endif
Definition:

Code: Select all

#include "UtilityVars.h"
 
irr::IrrlichtDevice* UtilityVars::device;
CuteAlien
Admin
Posts: 9846
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating a global IrrlichtDevice for IShaderConstantSetC

Post by CuteAlien »

Globals should generally be avoided. Irrlicht example just uses them as it leads to shorter examples which makes them a bit easier to understand.

So my solution in this case would be - if a class needs an object, then pass a pointer to it. Or a reference. Both would work in this case.
Meaning MyShaderCallBack can have a IrrlichtDevice* member variable (or even IrrlichtDevice&) and you can initialize it for example in the constructor by passing a device to that. Then you don't need any global anywhere.

If you do globals by using static member functions (which certainly also is a valid option) - please initaliize them to 0 in your definition. c++ doesn't do that for you, so always do that for all pointers.

Some alternative I used sometimes - I have one global class object. I usually call it the application class and the object is called something like APP. I tend to create it with "external" instead of making it a static member. And that contains certain variables like Irrlicht device. Example where I used that: https://github.com/mzeilfelder/hc1/blob ... src/main.h
Not sure if that is clean (I don't really need the global, I could pass pointers everywhere), but it worked out well enough and code stayed readable. And always passing pointers to every class when you _know_ it will always be using the same ones can be a bit annoying. It makes your classes more reusable to pass every pointer ... but maybe you don't plan to have them all reusable, but already know some classes are only meant to be used in a single game.
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
runyonave
Posts: 3
Joined: Wed Oct 21, 2020 11:57 pm

Re: Creating a global IrrlichtDevice for IShaderConstantSetC

Post by runyonave »

This was really helpful, thanks CuteAlien!
Post Reply