SharedPtr SWIG bindings issues

Problems building or running the engine, queries about how to use features etc.
User avatar
Arthurfernandes
Kobold
Posts: 37
Joined: Mon Oct 28, 2024 10:50 pm
x 5

SharedPtr SWIG bindings issues

Post by Arthurfernandes »

Ogre Version: 14.3.4
Operating System: Windows 10
Render System: DirectX 11

So, I'm trying to use ShadowCameraSetupPtr in C#, but it's not working. In Mogre, the way it is used is like this:

Code: Select all

PSSMShadowCameraSetup pssmSetup = new PSSMShadowCameraSetup();
float vNearClipDistance = mDefaultCamera.NearClipDistance;
pssmSetup.SplitPadding = 20;
pssmSetup.CalculateSplitPoints(3, vNearClipDistance, 1000);
pssmSetup.SetOptimalAdjustFactor(0, 2);
pssmSetup.SetOptimalAdjustFactor(1, 1);
pssmSetup.SetOptimalAdjustFactor(2, 0.5f);

ShadowCameraSetup c = pssmSetup;

mPSSMSetup = new ShadowCameraSetupPtr(c)

But when I try to use it in Ogre with the SWIG binding, there is no constructor in ShadowCameraSetupPtr that accepts ShadowCameraSetup.
In Mogre, the code to use the method is:

Code: Select all

public unsafe ShadowCameraSetupPtr(ShadowCameraSetup obj)
{
    //IL_0033: Expected I, but got I8
    _native = obj._native;
    ((object)this)._002Ector();
    try
    {
        SharedPtr_003COgre_003A_003AShadowCameraSetup_003E* ptr = (SharedPtr_003COgre_003A_003AShadowCameraSetup_003E*)global::_003CModule_003E.@new(32uL);
        SharedPtr_003COgre_003A_003AShadowCameraSetup_003E* sharedPtr;
        try
        {
            sharedPtr = ((ptr == null) ? null : global::_003CModule_003E.Ogre_002ESharedPtr_003COgre_003A_003AShadowCameraSetup_003E_002E_007Bctor_007D_003Cclass_0020Ogre_003A_003AShadowCameraSetup_003E(ptr, (Ogre.ShadowCameraSetup*)obj._native, (SharedPtrFreeMethod)0));
        }
        catch
        {
            //try-fault
            global::_003CModule_003E.delete(ptr);
            throw;
        }

    _sharedPtr = sharedPtr;
    return;
}
catch
{
    //try-fault
    base.Dispose(A_0: true);
    throw;
}
}

I tried to change a few things in the .i file, but with no success. This problem happens with all SharedPtr types, not just ShadowCameraSetupPtr.

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

Re: SharedPtr SWIG bindings issues

Post by paroj »

did you try PSSMShadowCameraSetup.create()?

User avatar
Arthurfernandes
Kobold
Posts: 37
Joined: Mon Oct 28, 2024 10:50 pm
x 5

Re: SharedPtr SWIG bindings issues

Post by Arthurfernandes »

paroj wrote: Thu Mar 06, 2025 11:32 pm

did you try PSSMShadowCameraSetup.create()?

Yeah, this works.

But if I use SHADOWTYPE_TEXTURE_MODULATIVE_INTEGRATED, it works fine, but the texture doesn’t receive shadows from other entities. And if I use SHADOWTYPE_TEXTURE_ADDITIVE I get an error.

This is how I initialize shadows in the project:

Code: Select all

private void InitShadows()
{
    scnMgr.setShadowTechnique(ShadowTechnique.SHADOWTYPE_TEXTURE_ADDITIVE);
    //scnMgr.setShadowTechnique(ShadowTechnique.SHADOWTYPE_TEXTURE_MODULATIVE_INTEGRATED);
    scnMgr.setShadowTextureCountPerLightType(Light.LightTypes.LT_DIRECTIONAL, 3);
    scnMgr.setShadowDirLightTextureOffset(0);
    scnMgr.setShadowTextureCount(3);
    scnMgr.setShadowTextureSettings(1024, 3, PixelFormat.PF_FLOAT32_R);
    var mat = MaterialManager.getSingleton().getByName("PSSM/shadow_caster");
    scnMgr.setShadowTextureCasterMaterial(mat);

var pssmSetup = PSSMShadowCameraSetup.create();
scnMgr.setShadowCameraSetup(pssmSetup);

scnMgr.setShadowFarDistance(3000);
scnMgr.setShadowCasterRenderBackFaces(false);
scnMgr.setShadowTextureSelfShadow(true);
}

SHADOWTYPE_TEXTURE_MODULATIVE_INTEGRATED:

Image

SHADOWTYPE_TEXTURE_ADDITIVE:

Image

One more thing, I was searching for something that could help use shared_ptr in SWIG bindings, and I found WeakReference (https://learn.microsoft.com/en-us/dotne ... ew=net-9.0). I don’t know if this could help with memory management between C++ and C#.

[EDIT]

So, my problem is the terrain. When I disable the terrain, I can use any shadow type, but with the terrain on, I get an error.

Code: Select all

public void CreateTerrain()
{
    #region CamNode position

pivotNode.setPosition(mTerrainPos + new Vector3(0, 100, 0));

#endregion

#region Terrain Light

Light light = scnMgr.createLight("DirectionalLight");
light.setType(Light.LightTypes.LT_DIRECTIONAL);
light.setDiffuseColour(new ColourValue(1, 1, 1));
light.setSpecularColour(new ColourValue(0.4f, 0.4f, 0.4f));
SceneNode lightNode = scnMgr.getRootSceneNode().createChildSceneNode();
lightNode.setDirection(new Vector3(0.55f, -0.3f, 0.75f).normalisedCopy());
lightNode.attachObject(light);

#endregion

#region Terrain

mTerrainGroup = new TerrainGroup(scnMgr, Terrain.Alignment.ALIGN_X_Z, 1025, 12000);
mTerrainGlobals = new TerrainGlobalOptions();

mTerrainGroup.setOrigin(new Vector3(0, 0, 0));

ConfigureTerrainDefaults(light);

// Define o material padrão antes de carregar o terreno
SetDefaultMaterial();

mTerrainGroup.defineTerrain(0, 0, -1);
mTerrainGroup.loadAllTerrains(true);
mTerrainGroup.freeTemporaryResources();

mainTerrain = mTerrainGroup.getTerrain(0, 0);

MaterialPtr mat = mainTerrain.getMaterial();
shaderGen.createShaderBasedTechnique(mat.getTechnique(0), Variaveis.MSN_SHADERGEN);
RenderState rs = shaderGen.getRenderState(Variaveis.MSN_SHADERGEN, mat.__deref__(), 0);
SubRenderState srs = shaderGen.createSubRenderState(Variaveis.SRS_NORMALMAP);
rs.addTemplateSubRenderState(srs);

srs.setParameter("height_scale", "0.1");

#endregion
}

private void ConfigureTerrainDefaults(Light light)
{
    // Configuração global do terreno
    mTerrainGlobals.setMaxPixelError(0);
    mTerrainGlobals.setCompositeMapDistance(12000);
    mTerrainGlobals.setUseRayBoxDistanceCalculation(false);

// Influencia na velocidade do terreno
mTerrainGlobals.setLightMapSize(256);

// Configura iluminação global do terreno
mTerrainGlobals.setLightMapDirection(light.getDerivedDirection());
mTerrainGlobals.setCompositeMapAmbient(scnMgr.getAmbientLight());
mTerrainGlobals.setCompositeMapDiffuse(light.getDiffuseColour());

// Ajuste de altura do skirt do terreno
mTerrainGlobals.setSkirtSize(10);
}

private void SetDefaultMaterial()
{
    Terrain.ImportData defaultimp = mTerrainGroup.getDefaultImportSettings();

// Configuração do terreno
defaultimp.worldSize = 12000;
defaultimp.minBatchSize = 33;
defaultimp.maxBatchSize = 65;

// Adicionando camada de textura ao terreno
defaultimp.layerList.Add(new Terrain.LayerInstance());
defaultimp.layerList.Add(new Terrain.LayerInstance());
defaultimp.layerList.Add(new Terrain.LayerInstance());

defaultimp.layerList[0].worldSize = 200;
defaultimp.layerList[0].textureNames.Add("Ground37_diffspec.dds");
defaultimp.layerList[0].textureNames.Add("Ground37_normheight.dds");
defaultimp.layerList[1].worldSize = 200;
defaultimp.layerList[1].textureNames.Add("Ground23_diffspec");
defaultimp.layerList[1].textureNames.Add("Ground23_normheight.dds");
defaultimp.layerList[2].worldSize = 400;
defaultimp.layerList[2].textureNames.Add("Rock20_diffspec.dds");
defaultimp.layerList[2].textureNames.Add("Rock20_normheight.dds");

Image combined = new Image();
combined.loadTwoImagesAsRGBA("Ground23_col.jpg", "Ground23_spec.png", "General");
TextureManager.getSingleton().loadImage("Ground23_diffspec", "General", combined);
}

This is how I initialize the terrain. I'm getting strange errors with the terrain, like this:

Image

Sometimes the terrain just glitches like this, but on other computers, it runs just fine.