matjacques / LightjamsSpout

COM wrapper for Spout - Realtime video sharing framework for Windows - making it usable in c# and .Net in general
8 stars 3 forks source link

My Example is Correct??? #4

Closed crashangelbr closed 8 years ago

crashangelbr commented 8 years ago

Hi, I'm trying to create here a practical example and it looks like I could get some pictures .. but no more than 7fps and the machine's memory fades quickly ... What is missing in my code, please?

`public partial class SpoutReceiver : Form { public SpoutReceiver() { InitializeComponent(); }

    private bool TexturesInitialised = false;
    private float rotation = 0.0f;
    private float Scale = 1;

    private System.Drawing.Imaging.BitmapData gbitmapdata;
    private uint[] gtexture = new uint[1];

    private string senderName;

    private void openGLControl1_OpenGLInitialized(object sender, EventArgs e)
    {
        OpenGL gl = openGLControl1.OpenGL;
        gl.ClearColor(0, 0, 0, 0);
    }

        // --------------------------------------------------------------------
        // UPDATE BITMAPS
        // --------------------------------------------------------------------
    private void InitialiseTexture(ref OpenGL gl, Bitmap gImage1)
    {
        //gImage1 = new Bitmap(@"C:\Users\Unreal\Desktop\teste.bmp");

        //Rectangle rect = new Rectangle(0, 0, gImage1.Width, gImage1.Height);
        Rectangle rect = new Rectangle(0, 0, gImage1.Width, gImage1.Height);
        gbitmapdata = gImage1.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        gImage1.UnlockBits(gbitmapdata);
        gl.GenTextures(1, gtexture);
        gl.BindTexture(OpenGL.GL_TEXTURE_2D, gtexture[0]);
        gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGB8, gImage1.Width, gImage1.Height, 0, OpenGL.GL_BGR_EXT, OpenGL.GL_UNSIGNED_BYTE, gbitmapdata.Scan0);
        uint[] array = new uint[] { OpenGL.GL_NEAREST };
        gl.TexParameterI(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, array);
        gl.TexParameterI(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, array);
        TexturesInitialised = false;

    }

    private void openGLControl1_OpenGLDraw(object sender, SharpGL.RenderEventArgs args)
    {
        //  Get the OpenGL object.
        OpenGL gl = openGLControl1.OpenGL;

        //  Clear the color and depth buffer.
        gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

        //  Load the identity matrix.
        gl.LoadIdentity();

        string name = ""; int w =0, h=0;
        if (!TexturesInitialised)
        {
            var receiver = new LightjamsSpoutLib.LightjamsSpoutReceiver();
            int nbSenders = receiver.NbSenders();

            for (int t = 0; t < nbSenders; ++t)
            {
                receiver.GetSenderInfo(t, out name, out w, out h);
                senderName = name;
            }
        }

        int m_width = w;
        int m_height = h;

        if (!TexturesInitialised)
        {
            LightjamsSpoutLib.GLContext m_glContext = new LightjamsSpoutLib.GLContext();
            m_glContext.Create();
        }

        LightjamsSpoutLib.LightjamsSpoutReceiver m_receiver = new LightjamsSpoutLib.LightjamsSpoutReceiver();

        m_receiver.Connect(senderName, out m_width, out m_height);

        const int bytesPerPixel = 3;    // RGB
        int stride = 4 * ((m_width * bytesPerPixel + 3) / 4);
        byte[] m_buffer = new byte[stride * m_height];

        Bitmap m_bitmap = new Bitmap(m_width, m_height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        var data = m_bitmap.LockBits(new Rectangle(0, 0, m_width, m_height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        m_receiver.ReceiveImageIntPtr((long)data.Scan0, LightjamsSpoutLib.EPixelFormat.BGR);
        m_bitmap.UnlockBits(data);

        // --------------------------------------------------------------------
        // SENDING BITMAP TO POST IN TEXTUREIMAGE2D
        // --------------------------------------------------------------------
        if (!TexturesInitialised)
        {
            InitialiseTexture(ref gl, m_bitmap);
        }

        gl.Enable(OpenGL.GL_TEXTURE_2D);
        gl.BindTexture(OpenGL.GL_TEXTURE_2D, gtexture[0]);
        gl.Color(1.0f, 1.0f, 1.0f, 0.1f);
        gl.Begin(OpenGL.GL_QUADS);
        gl.TexCoord(1.0f, 1.0f);
        gl.Vertex(m_bitmap.Width, m_bitmap.Height, 1.0f);
        gl.TexCoord(0.0f, 1.0f);
        gl.Vertex(0.0f, m_bitmap.Height, 1.0f);
        gl.TexCoord(0.0f, 0.0f);
        gl.Vertex(0.0f, 0.0f, 0.0f);
        gl.TexCoord(1.0f, 0.0f);
        gl.Vertex(m_bitmap.Width, 0.0f, 1.0f);
        gl.End();
        gl.Disable(OpenGL.GL_TEXTURE_2D);

        gl.Flush();
    }

    private void openGLControl1_Resized(object sender, EventArgs e)
    {
        OpenGL gl = openGLControl1.OpenGL;
        gl.MatrixMode(OpenGL.GL_PROJECTION);
        gl.LoadIdentity();
        gl.Perspective(60.0f, (double)Width / (double)Height, 0.01, 10000.0);
        gl.LookAt(0, 0, -500, 0, 0, 0, 0, 1, 0);
        gl.MatrixMode(OpenGL.GL_MODELVIEW);
    }
}`
matjacques commented 8 years ago

Hi,

From what I see, openGLControl1_OpenGLDraw is called to draw each frame. If this is the case, the problem is creating a spout receiver and a bitmap each time. Try creating the receiver and the bitmap only once and reuse them. Then at the end, dispose the bitmap.

crashangelbr commented 7 years ago

Hi, Im trying developer receiver in OpenGL Context your component and my code is error....

Would you like to post an example or a visual studio project to download, please?