Irrlicht Arrays

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
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Irrlicht Arrays

Post by Vectrotek »

Funny thing..

Code: Select all

 
 
 // So.. I've got this class called "Smart Shader"..
 // It allows you to add Shader Parameters like Ints, FLoats etc to and instance of it at will
 // so that ulitimately the Callback feeds only the relevant shader what is contained in the relevant "Smart Shader" class..
 // Something similar to the "shader flags" used in the XML based post processor (water thing) form 2014..
 // Only this time I don't use XML to add them, but rather in the code itself..
 
 
 // I have a function like this..
 // It "pushes_back" the String of the Parameter Name to an "const char*" array and also "pushes back"
 // the Initial Value of the Variable (in this case float) to ANOTHER FLOAT array..
 // I hope to keep these indices parrallel so that I can get a name from an index and a value from a name etc..
 
 u32 SmartShader::AddVertexFloatParameter   (const char* ParamName, f32 InitialVal) 
  {RunningVertexFloatIndex++;
   VertexFloatParamNames.push_back(ParamName); // The resulting DROPPED INDEX should match the FOLLOWING push..
   VertexFloatValues.push_back(InitialVal);    // The resulting DROPPED INDEX should match the PREVIOUS push..
   return RunningVertexFloatIndex - 1;
  }
 
 // So I go (as an example)..
 SmartShaderCollection.AccessShader (2)-> AddVertexFloatParameter  ("WaterYPosition", 0.0);
 SmartShaderCollection.AccessShader (2)-> AddVertexFloatParameter  ("WaterRed",       0.1);
 SmartShaderCollection.AccessShader (2)-> AddVertexFloatParameter  ("WaterGreen",     0.7);
 SmartShaderCollection.AccessShader (2)-> AddVertexFloatParameter  ("WaterBlue",      0.4);
 
 // O.K.
 
 //  Now, I have a function..
   u32   SmartShader::GetVertexFloatParamValueByName       (const char* Name) 
    {
     s32 AquiredIndex = VertexFloatParamNames.binary_search(Name);
     if(AquiredIndex != -1) 
     return VertexFloatValues[AquiredIndex]; // Because The "Dropped Index" should be the same as for the "Names" ..
    }
 
 
 // Now I want to know what the value for "WaterBlue" is by using the the function..
 
 f32 AquiredBlue = SmartShaderCollection.AccessShader (2)->GetVertexIntParamValueByName ("WaterBlue");
 
 // Now "AquiredBlue" shoudl be "0.4" correct?
 // Well It now is "0.0" the value of "WaterYPosition" as if the Indices somehow got REVERSED??
 
 // Whats wrong..
 
 // The main aim here is to to get the VALUE for a given parameter if I supply its NAME..
 
 // The Smart Shaders CONTAINER looks like this..
 
 class ShadersContainer
  {public:
   
   ShadersContainer();
   ~ShadersContainer();
   void AddShader (char* VertFilename, char* FragFilename);
   SmartShader* AccessShader(u32 Index);
 
   private:
   core::array <SmartShader> ShadersArray;
   SmartShader TempShader;
  };
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   ShadersContainer:: ShadersContainer() {      }
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   ShadersContainer::~ShadersContainer() {      }
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   void ShadersContainer::AddShader (char* VertFilename, char* FragFilename)
    {
     TempShader.SetVertexProgramFileName(VertFilename);
     TempShader.SetFragmentShaderFileName(FragFilename);
     ShadersArray.push_back(TempShader);
    }
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 // This seems to work but I aint sure it is the best way..
 SmartShader* ShadersContainer::AccessShader(u32 Index)
  {return &ShadersArray[Index];
  }
  
CuteAlien
Admin
Posts: 9629
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Irrlicht Arrays

Post by CuteAlien »

Question about arrays and you forgot to give us the actual place where you define your array *sigh*.
So just guessing here... is your VertexFloatParamNames using <const char*> as template parameter maybe?
In which case it can't work because you pass it pointers which are only valid while passing them to the function, but invalid immediately afterwards. So your array is filled with invalid pointers which can point to really anything (whatever is on the stack on the moment probably).If VertexFloatParamNames is working with something like irr::core::stringc instead please excuse my rambling - in that case there's probably some other problem (have to read code then again).
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
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Irrlicht Arrays

Post by Vectrotek »

Thanks..

Funny thing..
Your suspicion was right..
My code.. (the Integer version)

Code: Select all

 
   core::array <const char*> VertexIntParamNames;      // I've seen this somewhere so thought it is O.K.
   core::array <const char*> VertexFloatParamNames;   // I thought the Array would smartly make them unique.. 
 
.. This, does really seem to somehow, store these strings and then have them available later?!?
But.. Like you said I now realize that these probably "should" not survive.. (I don't know why they do)

Anyway, I did what you said, this.. (also the integer version)..

Code: Select all

 
   core::array <core::stringc> VertexIntParamNamesXX;   // New names appended with XX as a temporary measure..
   core::array <core::stringc> VertexFloatParamNamesXX;  // (for floats, ignore)
 
I then acquire the Index (via the name) similar to the previous code, but like this..

Code: Select all

 
   u32   SmartShader::GetVertexIntParamValueByName       (const char* Name) 
    {
     // s32 AquiredIndex = VertexIntParamNames.binary_search(Name);  // Old one, with "const char*" which I also feel "should" be wrong.
     s32 AquiredIndex = VertexIntParamNamesXX.binary_search(Name);  // Search the new "core::stringc"'s..  (array finds a stringc with const char* Name?) O.K.
     if(AquiredIndex != -1) 
      return VertexIntegerValues[AquiredIndex];
     else return 1;
    }
 
Which feels better for me (and for you), but still has the same result with seemingly "reversed" indices..
I could force the search to work backwards, but no sleep if I don't understand why?
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Irrlicht Arrays

Post by Vectrotek »

Example: I do this..

Code: Select all

 
 SmartShaderCollection.AccessShader (2)-> AddVertexIntegerParameter  ("FirstName", 111);
 SmartShaderCollection.AccessShader (2)-> AddVertexIntegerParameter  ("SecondName", 222);
 SmartShaderCollection.AccessShader (2)-> AddVertexIntegerParameter  ("ThirdName", 333);
 SmartShaderCollection.AccessShader (2)-> AddVertexIntegerParameter  ("FourthName", 444);
 SmartShaderCollection.AccessShader (2)-> AddVertexIntegerParameter  ("FifthName", 555);
 SmartShaderCollection.AccessShader (2)-> AddVertexIntegerParameter  ("SixthName", 666);
 SmartShaderCollection.AccessShader (2)-> AddVertexIntegerParameter  ("SeventhName", 777);
 SmartShaderCollection.AccessShader (2)-> AddVertexIntegerParameter  ("EighthName", 888);
 SmartShaderCollection.AccessShader (2)-> AddVertexIntegerParameter  ("NinthName", 999);
 
To test the values I did this..

Code: Select all

 
  // Value from Name..
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += (u32)SmartShaderCollection.AccessShader (2)->GetVertexIntParamValueByName ("FirstName"); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += (u32)SmartShaderCollection.AccessShader (2)->GetVertexIntParamValueByName ("SecondName"); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += (u32)SmartShaderCollection.AccessShader (2)->GetVertexIntParamValueByName ("ThirdName"); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += (u32)SmartShaderCollection.AccessShader (2)->GetVertexIntParamValueByName ("FourthName"); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += (u32)SmartShaderCollection.AccessShader (2)->GetVertexIntParamValueByName ("FifthName"); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += (u32)SmartShaderCollection.AccessShader (2)->GetVertexIntParamValueByName ("SixthName"); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += (u32)SmartShaderCollection.AccessShader (2)->GetVertexIntParamValueByName ("SeventhName"); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += (u32)SmartShaderCollection.AccessShader (2)->GetVertexIntParamValueByName ("EighthName"); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += (u32)SmartShaderCollection.AccessShader (2)->GetVertexIntParamValueByName ("NinthName"); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
 // Name from Index..
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += SmartShaderCollection.AccessShader (2)->GetVertexIntParamNameByIndex (0); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += SmartShaderCollection.AccessShader (2)->GetVertexIntParamNameByIndex (1); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += SmartShaderCollection.AccessShader (2)->GetVertexIntParamNameByIndex (2); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += SmartShaderCollection.AccessShader (2)->GetVertexIntParamNameByIndex (3); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += SmartShaderCollection.AccessShader (2)->GetVertexIntParamNameByIndex (4); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += SmartShaderCollection.AccessShader (2)->GetVertexIntParamNameByIndex (5); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += SmartShaderCollection.AccessShader (2)->GetVertexIntParamNameByIndex (6); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += SmartShaderCollection.AccessShader (2)->GetVertexIntParamNameByIndex (7); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
   OurStTxt = TheDevice->getGUIEnvironment()->getRootGUIElement()->getElementFromId (CurrentGuiUI);
   if (OurStTxt)  {stringw OurStr = L":    "; OurStr += SmartShaderCollection.AccessShader (2)->GetVertexIntParamNameByIndex (8); OurStTxt->setText(OurStr.c_str());}
   CurrentGuiUI++;
 
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Irrlicht Arrays

Post by Vectrotek »

Screenshot of the output..
Image
The order??
CuteAlien
Admin
Posts: 9629
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Irrlicht Arrays

Post by CuteAlien »

binary_search does sort the array. For unsorted arrays you can only use linear_search.

edit: If you want to work with key-value pairs you can also use maps. Those do exactly that - you have a key (for example a string) and you can add any kind of other value to it. Theoretically even faster (thought in practical terms you can beat the speed usually with arrays because usual map implementations do mess up the processor cache). But map is easier to use for this kind of stuff.
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
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Irrlicht Arrays

Post by Vectrotek »

Here is the ADD function..
I might be converting from char* to stringc wrong..

Code: Select all

 
 u32 SmartShader::AddVertexIntegerParameter   (const char* ParamName, u32 InitialVal) 
  {// VertexIntParamNames.set_sorted (false);
   // VertexIntegerValues.set_sorted (false);
   RunningVertexIntIndex++;
   // VertexIntParamNames.push_back(ParamName); // Commented.. (edit)..
   VertexIntegerValues.push_back(InitialVal);
   core::stringc TestString (ParamName);     // conversion (deep copy??)..
   VertexIntParamNamesXX.push_back(TestString);
   return RunningVertexIntIndex - 1;
  }
Last edited by Vectrotek on Thu Aug 11, 2016 7:21 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9629
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Irrlicht Arrays

Post by CuteAlien »

Conversion is fine. Btw - const char* pointers maybe worked above because compiler buffered strings somewhere. But in general you can only use const char* when you ensure the memory they point to will stay valid (for example if you had that text in static strings it would be guaranteed to work).
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
CuteAlien
Admin
Posts: 9629
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Irrlicht Arrays

Post by CuteAlien »

Also noticing right now - if binary_search sorts or not depends on if the const or non-const version of it is called. But generally - don't use binary_search on unsorted arrays (makes no sense).
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
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Irrlicht Arrays

Post by Vectrotek »

Trying it right now..
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Irrlicht Arrays

Post by Vectrotek »

Cute Alien.. You're a genius!!!

I stayed away from Linear Search because the text keeps saying "very slow",
but for a few non-cyclic parameters it just fine!

Thanks Pal..

Shot..
Image
Smart Shaders coming up..
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Irrlicht Arrays

Post by Vectrotek »

[SOLVED]
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Irrlicht Arrays

Post by Vectrotek »

Mmm.. You're right about the static text.
I think I'll stick with Arrays for now.
Also, thanks for the info on Maps..
(I was just about to try that route)
Cheers!
Post Reply