Search Unity

Question Convert Render Texture to OpenGL TEXTURE 2D

Discussion in 'Scripting' started by Egamorf, Mar 28, 2023.

  1. Egamorf

    Egamorf

    Joined:
    Jul 20, 2017
    Posts:
    1
    Hi everyone ! I'm new with OpenGL,

    I need to convert a RenderTexture from my main Camera to an OpenGL TEXTURE_2D to send it using Spout in a UWP project.


    here is what i tried, but getting error (cannot convert System.IntPtr to uint) :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Spout.Interop;
    5. using System;
    6. using OpenGL;
    7.  
    8. public class SpoutSend : MonoBehaviour
    9. {
    10.     //SpoutSender sender;
    11.     public RenderTexture texture;
    12.     DeviceContext deviceContext;
    13.     SpoutSender sender;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         deviceContext = DeviceContext.Create();
    19.         IntPtr glContext = IntPtr.Zero;
    20.         glContext = deviceContext.CreateContext(IntPtr.Zero);
    21.         deviceContext.MakeCurrent(glContext); // Make this become the primary context
    22.         sender = new Spout.Interop.SpoutSender();
    23.         sender.CreateSender("CsSender", 1920, 1080, 0); // Create the sender
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         var Texturepointer = toTexture2D(texture).GetNativeTexturePtr();
    30.         sender.SendTexture(Texturepointer, Gl.TEXTURE_2D, 1920, 1080, false, 0);
    31.     }
    32.  
    33.     public Texture2D toTexture2D(RenderTexture rTex)
    34.     {
    35.         Texture2D dest = new Texture2D(rTex.width, rTex.height, TextureFormat.RGBA32, false);
    36.         dest.Apply(false);
    37.         Graphics.CopyTexture(rTex, dest);
    38.         return dest;
    39.     }
    40. }