Skip to content

Instantly share code, notes, and snippets.

@axjxwright
Last active May 17, 2016 03:10
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 axjxwright/f06e6a64b6464d9f0c7ee245820fe501 to your computer and use it in GitHub Desktop.
Save axjxwright/f06e6a64b6464d9f0c7ee245820fe501 to your computer and use it in GitHub Desktop.
#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
#include "cinder/Rand.h"
#include "cinder/CameraUi.h"
#include "cinder/Perlin.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class PointSpriteApp : public App
{
public:
void setup ( ) override;
void update ( ) override;
void draw ( ) override;
gl::BatchRef _geometry;
CameraPersp _camera;
CameraUi _cameraUi;
gl::Texture2dRef _sprite;
std::vector<vec3> _positions;
int _numPoints{10000};
};
void PointSpriteApp::setup()
{
_camera = CameraPersp ( getWindowWidth(), getWindowHeight(), 60.0f, 0.1f, 1000.0f );
_camera.lookAt ( vec3 ( 0, 0, 5 ), vec3 ( 0 ) );
_cameraUi = CameraUi ( &_camera, getWindow() );
{
Surface8u circle ( 128, 128, true );
for ( int i = 0; i < circle.getWidth(); i++ )
{
for ( int j = 0; j < circle.getHeight(); j++ )
{
ivec2 pixel{i, j};
ColorAf color = glm::distance ( vec2(pixel), circle.getBounds().getCenter() ) <= circle.getWidth() / 2 ? ColorAf::white() : ColorAf ( 1, 1, 1, 0 );
circle.setPixel(pixel, color);
}
}
_sprite = gl::Texture2d::create ( circle, gl::Texture2d::Format().mipmap().minFilter(GL_LINEAR_MIPMAP_LINEAR).magFilter(GL_LINEAR) );
}
auto vs = CI_GLSL(150,
uniform mat4 ciModelViewProjection;
in vec4 ciPosition;
void main ( )
{
gl_Position = ciModelViewProjection * ciPosition;
}
);
auto fs = CI_GLSL(150,
uniform sampler2D uTexture;
out vec4 FinalColor;
void main ( )
{
vec4 color = texture(uTexture, gl_PointCoord);
if ( color.a < 0.3f ) discard;
FinalColor = color;
}
);
auto shader = gl::GlslProg::create ( vs, fs );
shader->uniform ( "uTexture", 0 );
for ( int i = 0; i < _numPoints; i++ )
{
vec3 position { randFloat(-1, 1), randFloat(-1, 1), randFloat(-1, 1) };
_positions.push_back( position * 50.0f );
}
auto mesh = gl::VboMesh::create(_numPoints, GL_POINTS, { gl::VboMesh::Layout().attrib( geom::POSITION, 3 ) } );
mesh->bufferAttrib( geom::POSITION, _positions );
_geometry = gl::Batch::create ( mesh, shader );
gl::enableAlphaBlending();
gl::enableDepth();
gl::pointSize(10.0f);
}
void PointSpriteApp::update()
{
static Perlin kNoise;
for ( int i = 0; i < _geometry->getNumVertices(); i++ )
{
_positions[i].z += std::sin ( getElapsedSeconds() ) * 0.2f * ( _positions[i].z * 0.05 );
}
_geometry->getVboMesh()->bufferAttrib( geom::POSITION, _positions );
}
void PointSpriteApp::draw()
{
gl::clear ( Colorf::gray(0.2f) );
gl::setMatrices( _camera );
{
gl::ScopedTextureBind tex0 ( _sprite, 0 );
_geometry->draw ( );
}
}
CINDER_APP( PointSpriteApp, RendererGl, [] ( App::Settings * settings ) { settings->setWindowSize(1280, 720); } )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment