<commit_before>// multiple homomorphic materials blended with a material map
#include <sh/sh.hpp>
#include <sh/shutil.hpp>
#include <iostream>
#include "Shader.hpp"
#include "Globals.hpp"

using namespace SH;
using namespace ShUtil;

#include "util.hpp"

class JeweledShader : public Shader {
public:
  JeweledShader();
  ~JeweledShader();

  bool init();

  ShProgram vertex() { return vsh;}
  ShProgram fragment() { return fsh;}

  ShProgram vsh, fsh;

  static JeweledShader instance;
};

JeweledShader::JeweledShader()
  : Shader("Jeweled")
{
}

JeweledShader::~JeweledShader()
{
}

bool JeweledShader::init()
{
  ShAttrib2f texture_scale(1.0,1.0);
  texture_scale.name("Texture Scale");
  texture_scale.range(0.0,10.0);

  vsh = SH_BEGIN_PROGRAM("gpu:vertex") {
    ShInputPosition4f ipos;// model-space position
    ShInputNormal3f inorm; // model-space normal
    ShInputTexCoord2f tc;  // texture coordinate (passed through)
    ShInputVector3f itan;  // model-space tangent
    
    ShOutputPosition4f opos; // Position in NDC
    ShOutputVector3f light;  // direction to light (surface frame)
    ShOutputVector3f view;   // direction to eye (surface frame)
    ShOutputTexCoord2f otc;  // tex coords passed through
    ShOutputVector3f reflv;  // reflection vector

    otc = tc * texture_scale;
    opos = Globals::mvp | ipos; // Compute NDC position
    ShNormal3f n = Globals::mv | inorm; // Compute view-space normal
    ShNormal3f t = Globals::mv | itan; // Compute view-space normal
    n = normalize(n);
    t = normalize(t);

    ShPoint3f posv = (Globals::mv | ipos)(0,1,2); // Compute view-space position
    ShVector3f lightv = normalize(Globals::lightPos - posv); // Compute light direction
    ShVector3f viewv = -normalize(posv); // Compute view vector
    reflv = reflect(viewv,n);  // view-space reflection vector

    // compute local surface frame (in view space)
    // ShVector3f t = normalize(itan - (itan|n)*n);
    ShVector3f s = normalize(cross(t,n));
    
    // project view and light vectors onto local surface frame
    view(0) = (t|viewv);
    view(1) = (s|viewv);
    view(2) = (n|viewv);

    light(0) = (t|lightv);
    light(1) = (s|lightv);
    light(2) = (n|lightv);  // if positive, is irradiance scale
  } SH_END;

  ShImage image;

#define NMATS 3
  ShTexture2D<ShColor3f> ptex[NMATS];
  ShTexture2D<ShColor3f> qtex[NMATS];

  image.loadPng(SHMEDIA_DIR "/brdfs/satin/satinp.png");
  ptex[0].size(image.width(), image.height());
  ptex[0].memory(image.memory());
  // ptex[0].name("Satin p texture");

  image.loadPng(SHMEDIA_DIR "/brdfs/satin/satinq.png");
  qtex[0].size(image.width(), image.height());
  qtex[0].memory(image.memory());
  // ptex[0].name("Satin q texture");
  
  image.loadPng(SHMEDIA_DIR "/brdfs/mystique/mystique64_0.png");
  ptex[1].size(image.width(), image.height());
  ptex[1].memory(image.memory());
  // ptex[1].name("Mystique q texture");

  image.loadPng(SHMEDIA_DIR "/brdfs/mystique/mystique64_1.png");
  qtex[1].size(image.width(), image.height());
  qtex[1].memory(image.memory());

  image.loadPng(SHMEDIA_DIR "/brdfs/garnetred/garnetred64_0.png");
  ptex[2].size(image.width(), image.height());
  ptex[2].memory(image.memory());
  // ptex[1].name("Garnet red q texture");

  image.loadPng(SHMEDIA_DIR "/brdfs/garnetred/garnetred64_1.png");
  qtex[2].size(image.width(), image.height());
  qtex[2].memory(image.memory());

  // Specular highlight (to be added when needed...)
  image.loadPng(SHMEDIA_DIR "/brdfs/specular.png");
  ShTexture2D<ShColor3f> stex(image.width(), image.height());
  stex.memory(image.memory());
  // stex.name("Specular highlight texture");
  
  // Cube map for mirror reflection
  std::string imageNames[6] = {"left", "right", "top", "bottom", "back", "front"};
  ShTextureCube<ShColor4f> env(image.width(), image.height());
  {
    for (int i = 0; i < 6; i++) {
      ShImage image;
      image.loadPng(std::string(SHMEDIA_DIR "/envmaps/aniroom/") + imageNames[i] + ".png");
      env.memory(image.memory(), static_cast<ShCubeDirection>(i));
    }
  }
  
  ShWrapRepeat2D< ShTexture2D<ShColor3f> > mat[NMATS];

  // Material map, channel 0
  image.loadPng(SHMEDIA_DIR "/mats/goldknot_ch1.png");
  // image.loadPng(SHMEDIA_DIR "/mats/colourknot_ch1.png");
  mat[0].size(image.width(), image.height());
  mat[0].memory(image.memory());
  // mat[0].name("Filgiree material map");
  
  // Material map, channel 1
  image.loadPng(SHMEDIA_DIR "/mats/colourknot_ch2_ch4.png");
  mat[1].size(image.width(), image.height());
  mat[1].memory(image.memory());
  // mat[1].name("Base material map");
  
  // Material map, channel 2
  image.loadPng(SHMEDIA_DIR "/mats/colourknot_ch3.png");
  mat[2].size(image.width(), image.height());
  mat[2].memory(image.memory());
  // mat[2].name("Knot interior material map");

  // Scale factors
  ShColor3f alpha[NMATS];
  ShAttrib1f diffuse[NMATS];
  ShAttrib1f specular[NMATS];<commit_msg>Fix JeweledShader for new Sh fixes<commit_after>// multiple homomorphic materials blended with a material map
#include <sh/sh.hpp>
#include <sh/shutil.hpp>
#include <iostream>
#include "Shader.hpp"
#include "Globals.hpp"

using namespace SH;
using namespace ShUtil;

#include "util.hpp"

class JeweledShader : public Shader {
public:
  JeweledShader();
  ~JeweledShader();

  bool init();

  ShProgram vertex() { return vsh;}
  ShProgram fragment() { return fsh;}

  ShProgram vsh, fsh;

  static JeweledShader instance;
};

JeweledShader::JeweledShader()
  : Shader("Jeweled")
{
}

JeweledShader::~JeweledShader()
{
}

bool JeweledShader::init()
{
  ShAttrib2f texture_scale(1.0,1.0);
  texture_scale.name("Texture Scale");
  texture_scale.range(0.0,10.0);

  vsh = SH_BEGIN_PROGRAM("gpu:vertex") {
    ShInputPosition4f ipos;// model-space position
    ShInputNormal3f inorm; // model-space normal
    ShInputTexCoord2f tc;  // texture coordinate (passed through)
    ShInputVector3f itan;  // model-space tangent
    
    ShOutputPosition4f opos; // Position in NDC
    ShOutputVector3f light;  // direction to light (surface frame)
    ShOutputVector3f view;   // direction to eye (surface frame)
    ShOutputTexCoord2f otc;  // tex coords passed through
    ShOutputVector3f reflv;  // reflection vector

    otc = tc * texture_scale;
    opos = Globals::mvp | ipos; // Compute NDC position
    ShNormal3f n = Globals::mv | inorm; // Compute view-space normal
    ShNormal3f t = Globals::mv | itan; // Compute view-space normal
    n = normalize(n);
    t = normalize(t);

    ShPoint3f posv = (Globals::mv | ipos)(0,1,2); // Compute view-space position
    ShVector3f lightv = normalize(Globals::lightPos - posv); // Compute light direction
    ShVector3f viewv = -normalize(posv); // Compute view vector
    reflv = reflect(viewv,n);  // view-space reflection vector

    // compute local surface frame (in view space)
    // ShVector3f t = normalize(itan - (itan|n)*n);
    ShVector3f s = normalize(cross(t,n));
    
    // project view and light vectors onto local surface frame
    view(0) = (t|viewv);
    view(1) = (s|viewv);
    view(2) = (n|viewv);

    light(0) = (t|lightv);
    light(1) = (s|lightv);
    light(2) = (n|lightv);  // if positive, is irradiance scale
  } SH_END;

  ShImage image;

#define NMATS 3
  ShTexture2D<ShColor3f> ptex[NMATS];
  ShTexture2D<ShColor3f> qtex[NMATS];

  image.loadPng(SHMEDIA_DIR "/brdfs/satin/satinp.png");
  ptex[0].size(image.width(), image.height());
  ptex[0].memory(image.memory());
  // ptex[0].name("Satin p texture");

  image.loadPng(SHMEDIA_DIR "/brdfs/satin/satinq.png");
  qtex[0].size(image.width(), image.height());
  qtex[0].memory(image.memory());
  // ptex[0].name("Satin q texture");
  
  image.loadPng(SHMEDIA_DIR "/brdfs/mystique/mystique64_0.png");
  ptex[1].size(image.width(), image.height());
  ptex[1].memory(image.memory());
  // ptex[1].name("Mystique q texture");

  image.loadPng(SHMEDIA_DIR "/brdfs/mystique/mystique64_1.png");
  qtex[1].size(image.width(), image.height());
  qtex[1].memory(image.memory());

  image.loadPng(SHMEDIA_DIR "/brdfs/garnetred/garnetred64_0.png");
  ptex[2].size(image.width(), image.height());
  ptex[2].memory(image.memory());
  // ptex[1].name("Garnet red q texture");

  image.loadPng(SHMEDIA_DIR "/brdfs/garnetred/garnetred64_1.png");
  qtex[2].size(image.width(), image.height());
  qtex[2].memory(image.memory());

  // Specular highlight (to be added when needed...)
  image.loadPng(SHMEDIA_DIR "/brdfs/specular.png");
  ShTexture2D<ShColor3f> stex(image.width(), image.height());
  stex.memory(image.memory());
  // stex.name("Specular highlight texture");
  
  // Cube map for mirror reflection
  std::string imageNames[6] = {"left", "right", "top", "bottom", "back", "front"};
  ShTextureCube<ShColor4f> env(image.width(), image.height());
  {
    for (int i = 0; i < 6; i++) {
      ShImage image;
      image.loadPng(std::string(SHMEDIA_DIR "/envmaps/aniroom/") + imageNames[i] + ".png");
      env.memory(image.memory(), static_cast<ShCubeDirection>(i));
    }
  }
  
  ShWrapRepeat< ShTexture2D<ShColor3f> > mat[NMATS];

  // Material map, channel 0
  image.loadPng(SHMEDIA_DIR "/mats/goldknot_ch1.png");
  // image.loadPng(SHMEDIA_DIR "/mats/colourknot_ch1.png");
  mat[0].size(image.width(), image.height());
  mat[0].memory(image.memory());
  // mat[0].name("Filgiree material map");
  
  // Material map, channel 1
  image.loadPng(SHMEDIA_DIR "/mats/colourknot_ch2_ch4.png");
  mat[1].size(image.width(), image.height());
  mat[1].memory(image.memory());
  // mat[1].name("Base material map");
  
  // Material map, channel 2
  image.loadPng(SHMEDIA_DIR "/mats/colourknot_ch3.png");
  mat[2].size(image.width(), image.height());
  mat[2].memory(image.memory());
  // mat[2].name("Knot interior material map");

  // Scale factors
  ShColor3f alpha[NMATS];
  ShAttrib1f diffuse[NMATS];
  ShAttrib1f specular[NMATS];
{'fraction_non_alphanumeric': 0.1660759493670886, 'fraction_numerical': 0.019544303797468354, 'mean_word_length': 3.992922143579373, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7422523', 'n_tokens_mistral': 3743, 'n_tokens_neox': 3635, 'n_words': 706}