//
//  TexturedCube.cpp
//  TexturedCube
//
//  Created by John Ryland on 2/10/17.
//  Copyright © 2017 John Ryland. All rights reserved.
//

#include "../Framework/Framework.h"


DECLARE_PROGRAM_UNIFORMS(TexturedProgram)
  DECLARE_UNIFORM(mat4, modelViewProjectionMatrix)
  DECLARE_UNIFORM(sampler2D, textureId)
DECLARE_PROGRAM_UNIFORMS_END


DECLARE_VERTEX(TexturedVertex)
  DECLARE_ATTRIB(vec3, pos, GL_FALSE)
  DECLARE_ATTRIB(vec3, uv,  GL_FALSE)
DECLARE_VERTEX_END


const char* s_texturedVertexShader =
R"(
  void main()
  {
    gl_TexCoord[0] = vec4(uv, 1.0);
    gl_Position = modelViewProjectionMatrix * vec4(pos, 1.0);
  }
)";


const char* s_texturedFragmentShader =
R"(
  void main()
  {
    gl_FragColor = texture2D(textureId, gl_TexCoord[0].xy);
  }
)";


class TexturedCube : public DemoContext
{
public:
  ~TexturedCube() override;
  void prepare() override;
  void update(float a_seconds) override;
  void draw() override;
  void onResize(const vec2f& a_shape) override;

private:
  ProgramContext2<TexturedProgram, TexturedVertex> m_demoModelContext;
  float m_projectionMatrix[16];
};


TexturedCube::~TexturedCube()
{
  m_demoModelContext.shutdown();
}


void TexturedCube::prepare()
{
  OptionsType options;
  m_demoModelContext.setup(s_texturedVertexShader, s_texturedFragmentShader, Medium, options, 1);

  std::vector<vec3f> vertexes;
  CreateTexturedCube(vertexes);
  m_demoModelContext.m_vertexArray.m_vertexData.clear();
  m_demoModelContext.m_vertexArray.m_vertexData.reserve(vertexes.size() / 2);
  bool odd = false;
  vec3f last;
  for (const vec3f& v : vertexes)
  {
    if (odd)
      m_demoModelContext.m_vertexArray.m_vertexData.push_back(TexturedVertex{ last.x, last.y, last.z, v.x, v.y, v.z });
    else
      last = v;
    odd = !odd;
  }

  m_demoModelContext.update();
  m_demoModelContext.setFlag(clearColor | clearDepth | enableCullFace | enableDepthTest);
  m_demoModelContext.setBackgroundColor(0.0f, 0.0f, 0.0f);

  std::vector<uint8_t> rawImageData;
  std::vector<uint8_t> decodedImageData;
  uint32_t imageW, imageH;
  loadFile("cube-map-test-no-border.png", rawImageData);
  decodePNG(decodedImageData, imageW, imageH, rawImageData.data(), rawImageData.size());
  m_demoModelContext.m_uniforms.m_textureId = 0;
  m_demoModelContext.setTextureData(0, imageW, imageH, decodedImageData.data());
}


void TexturedCube::onResize(const vec2f& a_shape)
{
  Math::makePerspectiveMatrix4x4(m_projectionMatrix, Math::degreesToRadians(45.0f), a_shape.x / a_shape.y, 0.1f, 100.0f);
}


void TexturedCube::update(float a_seconds)
{
  float trans[3] = { 0.0f, 0.0f, -20.0f };
  float rotate[3] = { fmod(a_seconds*23.f, 360.0f), fmod(a_seconds*37.f, 360.0f), fmod(a_seconds*46.0f, 360.0f)};
  float baseModelViewMatrix[16];
  Math::translationRotationScaleToMatrix4x4(baseModelViewMatrix, trans, rotate, 5.0f);

  Math::multiplyMatrix4x4(m_demoModelContext.m_uniforms.m_modelViewProjectionMatrix.m[0], m_projectionMatrix, baseModelViewMatrix);
}


void TexturedCube::draw()
{
  m_demoModelContext.draw();
}


REGISTER_DEMO_CONTEXT("Textured Cube", TexturedCube)

