<commit_before>/***********************************
   Copyright 2019 <NAME>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
***********************************/

/** \file SkySphere.cpp
 * SkySphere-class function definitions.
 */

#include <OpenFrames/SkySphere.hpp>
#include <osg/BlendFunc>
#include <osg/PointSprite>
#include <osgDB/FileUtils>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sstream>

namespace OpenFrames{

// Implement vertex shader to compute star position/size/color
static const char *OFSkySphere_VertSource = {
  "#version 120\n"
  "uniform mat4 osg_ModelViewProjectionMatrix;\n"

  "void main(void)\n"
  "{\n"
  // Position and color are just passed through, but point size is
  // encoded in the alpha component of color
  "  gl_Position = osg_ModelViewProjectionMatrix*gl_Vertex;\n"
  "  gl_FrontColor = gl_Color;\n"
  "  gl_PointSize = gl_Color.w;\n"
  "}\n"
};

// Implement frament shader to render star according to its size and color
static const char *OFSkySphere_FragSource = {
  "#version 120\n"
  "vec2 v;\n"
  "float alpha;\n"

  // Cutoff radius where fragment alpha starts fading to zero
  "const float r_cutoff = 0.25;\n"
  "const float x = -0.5/(r_cutoff - 0.5);\n"
  "const float y = 0.5/(r_cutoff - 0.5);\n"

  "void main(void)\n"
  "{\n"
  // gl_PointCoord has range (x,y) in [0, 1] each, with y-down
  // Move origin to point center, with extents [-0.5, 0.5]
  "  v = gl_PointCoord - vec2(0.5);\n"

  // Fragment gets star color
  "  gl_FragColor = gl_Color;\n"

  // Fragment alpha is based on distance from point center
  // Fades alpha = 1 -> 0 starting at half of point radius
  "  alpha = mix(x, y, length(v));\n"
  "  alpha = clamp(alpha, 0.0, 1.0);\n"
  "  gl_FragColor.a = alpha;\n"
  "}\n"
};

SkySphere::SkySphere(const std::string &name)
: Sphere(name)
{
  _init();
}

SkySphere::~SkySphere() {}

void SkySphere::_init()
{
  // Initialize star rendering parameters
  _minMag = -2.0;
  _maxMag = 8.0;
  _maxNumStars = 10000;
  _minPixSize = 1.0;
  _maxPixSize = 10.0;

  // Hide Sphere's axes and labels
  showAxes(ReferenceFrame::NO_AXES);
  showAxesLabels(ReferenceFrame::NO_AXES);
  showNameLabel(false);

  // Make sure Sphere follows the eye
  getTransform()->setFollowEye(true);

  // Create new StateSet to specify star-specific OpenGL properties
  osg::StateSet *ss = new osg::StateSet();

  // Create vertex and fragment shaders for stars
  osg::Shader *vertShader = new osg::Shader(osg::Shader::VERTEX, OFSkySphere_VertSource);
  osg::Shader *fragShader = new osg::Shader(osg::Shader::FRAGMENT, OFSkySphere_FragSource);
  osg::Program *program = new osg::Program;
  program->setName("OFSkySphere_ShaderProgram");
  program->addShader(vertShader);
  program->addShader(fragShader);
  ss->setAttribute(program);
  ss->setMode(GL_PROGRAM_POINT_SIZE, osg::StateAttribute::ON);

  // Set up point sprite
  osg::PointSprite *sprite = new osg::PointSprite();
  ss->setTextureAttributeAndModes(0, sprite);

  // Set additive blending for stars
  osg::BlendFunc *fn = new osg::BlendFunc();
  fn->setFunction(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE);
  ss->setAttributeAndModes(fn);

  // Create the geode that will contain all star bins, and add it directly to
  // this frame's transform. This allows the stars to be drawn in this reference
  // frame, while the textured sphere has its own sub-transform (see Sphere::_sphereXform)
  _starGeode = new osg::Geode;
  _starGeode->setName(_name + "_StarBins");
  _xform->addChild(_starGeode);
  
  // Create star bins (osg::Geometries) that will hold sets of stars
  for(unsigned int i = 0; i < _starBinCount; ++i)
  {
    _starBinGeoms[i] = new osg::Geometry;
    _starBinGeoms[i]->setName("StarFieldDrawable"+std::to_string(i));
    _starBinGeoms[i]->setStateSet(ss);
    _starBinGeoms[i]->setUseDisplayList(false);
    _starBinGeoms[i]->setUseVertexBufferObjects(true);
    _starBinGeoms[i]->getOrCreateVertexBufferObject()->setUsage(GL_STATIC_DRAW);
    _starBinGeoms[i]->setVertexArray(new osg::Vec3Array());
    osg::Vec4Array *colors = new osg::Vec4Array;
    colors->setBinding(osg::Array::BIND_PER_VERTEX);
    _starBinGeoms[i]->setColorArray(colors);
    _starGeode->addDrawable(_starBinGeoms[i]);
  }

  // By default draw both texture and starfield
  setDrawMode(TEXTURE + STARFIELD);
}

void SkySphere::setDrawMode(unsigned int drawMode)
{<commit_msg>Stars use GL_MAX blending equation to disappear more nicely into a bright daytime sky<commit_after>/***********************************
   Copyright 2019 <NAME>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
***********************************/

/** \file SkySphere.cpp
 * SkySphere-class function definitions.
 */

#include <OpenFrames/SkySphere.hpp>
#include <osg/BlendEquation>
#include <osg/BlendFunc>
#include <osg/PointSprite>
#include <osgDB/FileUtils>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sstream>

namespace OpenFrames{

// Implement vertex shader to compute star position/size/color
static const char *OFSkySphere_VertSource = {
  "#version 120\n"
  "uniform mat4 osg_ModelViewProjectionMatrix;\n"

  "void main(void)\n"
  "{\n"
  // Position and color are just passed through, but point size is
  // encoded in the alpha component of color
  "  gl_Position = osg_ModelViewProjectionMatrix*gl_Vertex;\n"
  "  gl_FrontColor = gl_Color;\n"
  "  gl_PointSize = gl_Color.w;\n"
  "}\n"
};

// Implement frament shader to render star according to its size and color
static const char *OFSkySphere_FragSource = {
  "#version 120\n"
  "vec2 v;\n"
  "float alpha;\n"

  // Cutoff radius where fragment alpha starts fading to zero
  "const float r_cutoff = 0.25;\n"
  "const float x = -0.5/(r_cutoff - 0.5);\n"
  "const float y = 0.5/(r_cutoff - 0.5);\n"

  "void main(void)\n"
  "{\n"
  // gl_PointCoord has range (x,y) in [0, 1] each, with y-down
  // Move origin to point center, with extents [-0.5, 0.5]
  "  v = gl_PointCoord - vec2(0.5);\n"

  // Fragment alpha is based on distance from point center
  // Fades alpha = 1 -> 0 starting at half of point radius
  "  alpha = mix(x, y, length(v));\n"
  "  alpha = clamp(alpha, 0.0, 1.0);\n"

  // Modulate star color by fragment alpha so that star blends
  // nicely with background sky color
  "  gl_FragColor.rgb = gl_Color.rgb * alpha;\n"
  "  gl_FragColor.a = alpha;\n"
  "}\n"
};

SkySphere::SkySphere(const std::string &name)
: Sphere(name)
{
  _init();
}

SkySphere::~SkySphere() {}

void SkySphere::_init()
{
  // Initialize star rendering parameters
  _minMag = -2.0;
  _maxMag = 8.0;
  _maxNumStars = 10000;
  _minPixSize = 1.0;
  _maxPixSize = 10.0;

  // Hide Sphere's axes and labels
  showAxes(ReferenceFrame::NO_AXES);
  showAxesLabels(ReferenceFrame::NO_AXES);
  showNameLabel(false);

  // Make sure Sphere follows the eye
  getTransform()->setFollowEye(true);

  // Create new StateSet to specify star-specific OpenGL properties
  osg::StateSet *ss = new osg::StateSet();

  // Create vertex and fragment shaders for stars
  osg::Shader *vertShader = new osg::Shader(osg::Shader::VERTEX, OFSkySphere_VertSource);
  osg::Shader *fragShader = new osg::Shader(osg::Shader::FRAGMENT, OFSkySphere_FragSource);
  osg::Program *program = new osg::Program;
  program->setName("OFSkySphere_ShaderProgram");
  program->addShader(vertShader);
  program->addShader(fragShader);
  ss->setAttribute(program);
  ss->setMode(GL_PROGRAM_POINT_SIZE, osg::StateAttribute::ON);

  // Set up point sprite
  osg::PointSprite *sprite = new osg::PointSprite();
  ss->setTextureAttributeAndModes(0, sprite);

  // Blend stars against background by using the brighter of the two
  // This works because star colors have already been alpha-modulated in
  // the fragment shader
  osg::BlendEquation *beq = new osg::BlendEquation(osg::BlendEquation::RGBA_MAX);
  ss->setAttributeAndModes(beq);
  
  // Create the geode that will contain all star bins, and add it directly to
  // this frame's transform. This allows the stars to be drawn in this reference
  // frame, while the textured sphere has its own sub-transform (see Sphere::_sphereXform)
  _starGeode = new osg::Geode;
  _starGeode->setName(_name + "_StarBins");
  _xform->addChild(_starGeode);
  
  // Create star bins (osg::Geometries) that will hold sets of stars
  for(unsigned int i = 0; i < _starBinCount; ++i)
  {
    _starBinGeoms[i] = new osg::Geometry;
    _starBinGeoms[i]->setName("StarFieldDrawable"+std::to_string(i));
    _starBinGeoms[i]->setStateSet(ss);
    _starBinGeoms[i]->setUseDisplayList(false);
    _starBinGeoms[i]->setUseVertexBufferObjects(true);
    _starBinGeoms[i]->getOrCreateVertexBufferObject()->setUsage(GL_STATIC_DRAW);
    _starBinGeoms[i]->setVertexArray(new osg::Vec3Array());
    osg::Vec4Array *colors = new osg::Vec4Array;
    colors->setBinding(osg::Array::BIND_PER_VERTEX);
    _starBinGeoms[i]->setColorArray(colors);
    _starGeode->addDrawable(_starBinGeoms[i]);
  }

  // By default draw both texture and starfield
  setDrawMode(TEXTURE + STARFIELD);
}

void SkySphere::setDrawMode(unsigned int drawMode)
{
{'fraction_non_alphanumeric': 0.1343505399782027, 'fraction_numerical': 0.012087585455266026, 'mean_word_length': 4.240913811007269, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4507813', 'n_tokens_mistral': 3498, 'n_tokens_neox': 3351, 'n_words': 932}