Bitmap to Material

nataz

13-08-2007 16:31:55

Hi,

i am currently trying to get a Picture from my Webcam, into an Mogre Texture or Material. I am getting the Pictures from my Webcam as System.Drwaing.Bitmap images.

so what i currently do is the following:


public void initCapture(int Width, int Height )
{
inPtr = TextureManager.Singleton.CreateManual("input", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, TextureType.TEX_TYPE_2D, 800, 600, 1, Mogre.PixelFormat.PF_R8G8B8);
MaterialPtr inMat = MaterialManager.Singleton.Create("inputMat", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME);
inMat.GetTechnique(0).GetPass(0).CreateTextureUnitState("input");
sceneMgr.GetEntity("Plane01").SetMaterialName("inputMat");
}

public void updateCaptureTexture(Bitmap bmp)
{
inBmp = bmp;
unsafe
{
try
{
PixelBox pb2 = new PixelBox((uint)bmp.Width, (uint)bmp.Height, 1, Mogre.PixelFormat.PF_R8G8B8, (IntPtr)inBmp.GetHbitmap().ToPointer());
inPtr.GetBuffer().BlitFromMemory(pb2);
}
catch (Exception ex) { }
}
}


But the plane remains Black, there is no texture shown (I show the Bitmap in an Picturebox, so the original Bitmap is definately not empty).
Anyone another idea how to get the Bitmap into a Texture?

Thanks in advance

Sweenie

14-08-2007 07:15:28

This is VB code but it should be easy enough to convert...
It's maybe not the most optimal solution but it works for me...
Been using it to render my gui.



Dim txPtr As TexturePtr = TextureManager.Singleton.CreateManual("guiTex", "General", TextureType.TEX_TYPE_2D, Width, Height, 0, PixelFormat.PF_BYTE_RGBA, 6)
Dim txBuffer As HardwarePixelBufferSharedPtr = txPtr.GetBuffer(0, 0)


Dim pb As PixelBox = txBuffer.Lock(New Box(0, 0, Width, Height), HardwareBuffer.LockOptions.HBL_DISCARD)
'Create a GDI+ bitmap that points directly to the Ogre texture
Dim bm As New Bitmap(Width, Height, Width * 4 + pb.RowSkip * 4, Imaging.PixelFormat.Format32bppArgb, pb.data)
'Create a graphics context for the bitmap
Dim gr As Graphics = Graphics.FromImage(bm)
'gr.CompositingMode = Drawing2D.CompositingMode.SourceCopy

'Do any fancy GDI+ stuff here
gr.DrawImage(myGDIbitmapofchoice, 0, 0)


'Dispose the graphics and bitmap
gr.Dispose()
bm.Dispose()

'Unlock the Ogre texture
txBuffer.Unlock()


nataz

14-08-2007 08:12:39

Hi, thanks for the Answer, but i guess you are converting a Texture (RTT maybe) TO a Bitmap, but i need to convert a Bitmap into a texture, so the other way round, any solution anyone?

nataz

14-08-2007 08:57:26

I heared there was a C# version of Theora, but the file is not online anymore (at wreckedgames). Could anyone please upload the file again? I think there is a solution on how to do Bitmap -> texture

Thnx in advance

nataz

14-08-2007 11:32:53

Okay,

sorted it out:

code:

unsafe
{
System.IO.MemoryStream memstr = new System.IO.MemoryStream();
bmp.Save(memstr, ImageFormat.Bmp);
byte[] bmpbyte = memstr.ToArray();
HardwarePixelBufferSharedPtr pixelBuffer = inPtr.GetBuffer();
pixelBuffer.Lock(HardwareBuffer.LockOptions.HBL_NORMAL); // for best performance use HBL_DISCARD!
PixelBox pixelBox = pixelBuffer.CurrentLock;
Marshal.Copy(memstr.ToArray(), 0, pixelBox.data, bmpbyte.Length);
pixelBuffer.Unlock();

}

Sweenie

17-08-2007 11:51:31

Hi, thanks for the Answer, but i guess you are converting a Texture (RTT maybe) TO a Bitmap, but i need to convert a Bitmap into a texture, so the other way round, any solution anyone?

Nope, I actually draw TO the Ogre Texture.

I make a GDI+ Graphics object point directly to an Ogre RTT, that way I can draw bitmaps to the texture, but it also let me draw other stuff as well.
Text, boxes, lines, arcs etc...

Here is a screenshot of my .Net rendered gui

ahs

12-06-2008 12:29:27

Hello,

My app needs syncronize GDI calls over a MOGRE RenderWindow with flicker-free rendering.

In a FrameListener event I update a GDI Bitmap and then update a RenderTarget via a RTT.

It is very slow (~40fps).

Your app with your .NET Renderer GUI update every frame?

Can you help me?
Thanks in avance

Neurose

11-08-2008 17:42:55

Hello everybody,

I have portated your code to C#:



public void bla(Bitmap picture){
uint Width = (uint)picture.Width;
uint Height = (uint)picture.Height;
TexturePtr txPtr = TextureManager.Singleton.CreateManual("BackgroundTexture", "General", TextureType.TEX_TYPE_2D, Width, Height, 0, PixelFormat.PF_BYTE_RGBA, 6);
HardwarePixelBufferSharedPtr txBuffer = txPtr.GetBuffer(0, 0);
PixelBox pb = txBuffer.Lock(new Box(0, 0, Width, Height), HardwareBuffer.LockOptions.HBL_DISCARD);
//Create a GDI+ bitmap that points directly to the Ogre texture
Bitmap bm = new Bitmap((int)Width, (int)Height, (int)(Width * 4 + pb.RowSkip * 4), System.Drawing.Imaging.PixelFormat.Format32bppArgb, pb.data);
//Create a graphics context for the bitmap
Graphics gr = Graphics.FromImage(bm);
//gr.CompositingMode = Drawing2D.CompositingMode.SourceCopy

//Do any fancy GDI+ stuff here
gr.DrawImage(picture, 0, 0);


//Dispose the graphics and bitmap
gr.Dispose();
bm.Dispose();

//Unlock the Ogre texture
txBuffer.Unlock();
}


and now I get an AccessViolationException in the CreateManual()- method, has anyone an idea what the problem is?

An other problem is, that I don't know how to put this Texture on a MaterialPtr.....

Can anyone help me?

Greetings

fletch27502

04-06-2010 15:39:09

How are you getting the image from the webcam? I've tried several things including using a couple of the samples from the C# DirectShow implementations. I've got lots of different ways of getting a bmp to a texture including code that copies the bmp directly to the existing Ogre texture(which is much faster).

Scott

CodeKrash

29-12-2010 01:09:03

I have the webcam/vid input device solution if you still want it!

Beauty

30-12-2010 13:54:37

Some months ago user smiley80 created a demo application which shows the webcam stream.
Maybe this helps you.

Here is the related forum thread:
viewtopic.php?f=8&t=12812

A download link is in this post:
viewtopic.php?p=72548#p72548

And here the source code:
http://code.google.com/p/mogresdk/sourc ... WebcamDemo

Please give a feedback in the forum topic of the webcam demo and tell us if it works well or causes problems.
In the future we want to add the demo into the MogreSDK.