Skip to content

Instantly share code, notes, and snippets.

Created April 28, 2015 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/259ecb29ab0442fcf313 to your computer and use it in GitHub Desktop.
Save anonymous/259ecb29ab0442fcf313 to your computer and use it in GitHub Desktop.
include "MyProject.h"
#include "VideoCapture.h"
#include "Engine/Engine.h"
 
// Sets default values
// inherits from actor
AVideoCapture::AVideoCapture()
{
      // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
       PrimaryActorTick.bCanEverTick = true;
 
       Capture = CreateDefaultSubobject<USceneCaptureComponent2D>("CaptureComponent");
       RootComponent = Capture;
       //TextureRenderTarget2D'/Game/Textures/RenderTarget.RenderTarget'
       staticConstructorHelpers::FObjectFinder<UTextureRenderTarget2D> renderTarget(TEXT("/Game/Textures/RenderTarget.RenderTarget"));
 
       if (renderTarget.Succeeded())
       {
             UE_LOG(LogClass, Log, TEXT("OMG! YES!"));
             Capture->TextureTarget = renderTarget.Object;
            
             VideoTexture = Cast<UTexture>(Capture->TextureTarget);
 
             // pixel format of 10 is RGBA
 //tarray of uint8
             VideoBuffer.Init(VideoTexture->Resource->GetSizeX() * VideoTexture->Resource->GetSizeY() * 4);
       }
 
      
 
}
 
// Called when the game starts or when spawned
void AVideoCapture::BeginPlay()
{
       Super::BeginPlay();
      
}
 
// Called every frame
void AVideoCapture::Tick( float DeltaTime )
{
       Super::Tick( DeltaTime );
 
       uint32 Stride = 0;
       FRHITexture2D* Texture2D = VideoTexture->Resource->TextureRHI->GetTexture2D();
      
       uint8* TextureBuffer = (uint8*)RHILockTexture2D(Texture2D, 0,RLM_WriteOnly, Stride, false);
 
       FMemory::Memcpy(TextureBuffer, VideoBuffer.GetData(), VideoBuffer.Num());
 
       RHIUnlockTexture2D(Texture2D, 0, false);
      
       /*
       from mediatextureresource.cpp in unreal engine source code
 
       if (CurrentFrame.IsValid())
       {
             // draw the latest video frame
             if (CurrentFrameTime != LastFrameTime)
             {
                    uint32 Stride = 0;
                    FRHITexture2D* Texture2D = TextureRHI->GetTexture2D();
                    uint8* TextureBuffer = (uint8*)RHILockTexture2D(Texture2D, 0, RLM_WriteOnly, Stride, false);
 
                    FMemory::Memcpy(TextureBuffer, CurrentFrame->GetData(), CurrentFrame->Num());
                    RHIUnlockTexture2D(Texture2D, 0, false);
 
                    LastFrameTime = CurrentFrameTime;
                    Cleared = false;
             }
       }
 
 
      
      
       */
}
 
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment