Accessing PixelBox buffer in Mogre

Kodachi_Garou

21-12-2006 17:42:32

Hello everyone. I'm trying to port some code I had in OgreDotNet to dynamically write to a texture buffer some byte[]. The original code was basically this:


using System;
using OgreDotNet;
using System.Runtime.InteropServices;
using System.Drawing;

namespace Rendering
{
public class FrameBufferTexture
{
private Texture mTexture;
private HardwarePixelBuffer mBuffer;

public FrameBufferTexture(string materialName, string textureName, string groupName)
{
TexturePtr texturePtr = TextureManager.Instance.CreateManual(
textureName, groupName, TextureType.TEX_TYPE_2D,
256, 256, 0, PixelFormat.PF_BYTE_BGRA);

mTexture = texturePtr.Get();
HardwarePixelBufferSharedPtr bufferSharedPtr = mTexture.getBuffer();
mBuffer = bufferSharedPtr.Get();

ResourcePtr resourcePtr = MaterialManager.Instance.Create(materialName, groupName);
MaterialPtr materialPtr = new MaterialPtr(ResourcePtr.getCPtr(resourcePtr).Handle, false);
TextureUnitState tState = materialPtr.Get().GetTechnique(0).getPass(0).createTextureUnitState(textureName);
tState.SetTextureAddressingMode(TextureUnitState.TextureAddressingMode.TAM_CLAMP);
}

~FrameBufferTexture()
{
//TODO: Free OGRE resources here
}

public void WriteFrame(byte[] frame)
{
mBuffer.Lock(HardwareBuffer.LockOptions.HBL_NORMAL);

PixelBox pBox = mBuffer.getCurrentLock();
MemoryDataStream mds = new MemoryDataStream(pBox.data, 256 * 256 * 4, false);
mds.SetBuffer(frame, 0, (uint)frame.Length);

mBuffer.Unlock();
}
}
}


I tried to convert this into Mogre structures but I had some doubts figuring out the right data structures to use. I did manage to put up some code which compiles and executes without errors, but however, it displays nothing on screen:

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using Mogre;

namespace Rendering
{
public class FrameBufferTexture
{
private Texture mTexture;
private HardwarePixelBuffer mBuffer;

public FrameBufferTexture(string materialName, string textureName, string groupName)
{
TexturePtr texturePtr = TextureManager.Singleton.CreateManual(
textureName, groupName, TextureType.TEX_TYPE_2D,
256, 256, 0, PixelFormat.PF_BYTE_BGRA);

mTexture = texturePtr.Target;
HardwarePixelBufferSharedPtr bufferSharedPtr = mTexture.GetBuffer();
mBuffer = bufferSharedPtr.Target;

MaterialPtr materialPtr = (MaterialPtr)MaterialManager.Singleton.Create(materialName, groupName);
TextureUnitState tState = materialPtr.Target.GetTechnique(0).GetPass(0).CreateTextureUnitState(textureName);
tState.SetTextureAddressingMode(TextureUnitState.TextureAddressingMode.TAM_CLAMP);
}

~FrameBufferTexture()
{
//TODO: Free OGRE resources here
}

public void WriteFrame(byte[] frame)
{
unsafe
{
mBuffer.Lock(HardwareBuffer.LockOptions.HBL_NORMAL);

PixelBox pBox = mBuffer.CurrentLock;
Marshal.Copy(frame, 0, pBox.data, frame.Length);

mBuffer.Unlock();
}
}
}
}


Can someone point out what I'm doing wrong here? It's a bit difficult to navigate through any of the wrappers internals when it comes to lower level data access.

Thanks for all your help,

G

Kodachi_Garou

22-12-2006 15:07:42

On further analysis, it turned out that it was not this component's fault, but rather some other piece of converted code. The above conversion is indeed valid, and can be used, if someone's interested.