//
//  AugmentedReality.cpp
//  BlockyFroggy
//
//  Created by John Ryland on 16/9/17.
//  Copyright © 2017 John Ryland. All rights reserved.
//

#include "AugmentedReality.h"
#include <OpenGLES/ES2/gl.h>
#include "Dialog.h"


CameraInput::CameraInput()
  : m_capture(cvCreateCameraCapture(0))
//  : m_captureDevice(0)
{
}


CameraInput::~CameraInput()
{
  cvReleaseCapture(&m_capture);
//  m_captureDevice.release();
}


cv::Mat CameraInput::getNextFrame()
{
  cv::Mat mat;

  //IplImage* img =
     cvQueryFrame(m_capture);

  //m_captureDevice >> mat;
  return mat;
}


void DisplayFrame(const cv::Mat& frame)
{
  // DynamicImageShape

  int w = frame.cols;
  int h = frame.rows;
  unsigned char* data = frame.data;
  int w2 = std::min(w, 256);
  int h2 = std::min(h, 256);
  uint32_t idx = 10;
  unsigned char* d = (unsigned char*)malloc(256*256*4);
  for (int j = 0; j < h2; j++)
    for (int i = 0; i < w2; i++)
    {
      d[(j*256 + i) * 4 + 0] = data[(j * w + i) * 3 + 0];
      d[(j*256 + i) * 4 + 1] = data[(j * w + i) * 3 + 1];
      d[(j*256 + i) * 4 + 2] = data[(j * w + i) * 3 + 2];
      d[(j*256 + i) * 4 + 3] = 0x00;
    }
  glTexSubImage2D(GL_TEXTURE_2D, 0, (idx%8)*256, (idx/8)*256, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid *)d);
  free(d);
}


