Skip to content

Instantly share code, notes, and snippets.

@kybr
Created November 22, 2018 22:14
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 kybr/e6eb8886f8ae143e512b1d5c76b4e624 to your computer and use it in GitHub Desktop.
Save kybr/e6eb8886f8ae143e512b1d5c76b4e624 to your computer and use it in GitHub Desktop.
#include "al/core.hpp"
using namespace al;
using namespace std;
#define N (1000)
#define CLOUD_WIDTH (5.0)
string fullPathOrDie(string fileName, string whereToLook = ".") {
SearchPaths searchPaths;
searchPaths.addSearchPath(whereToLook);
string filePath = searchPaths.find(fileName).filepath();
if (filePath == "") {
fprintf(stderr, "FAIL\n");
exit(1);
}
return filePath;
}
struct AlloApp : App {
ShaderProgram shader;
Mesh pointMesh;
Texture texture;
void onCreate() override {
pointMesh.primitive(Mesh::POINTS);
for (int i = 0; i < N; i++) {
pointMesh.vertex(
Vec3f(rnd::uniformS(), rnd::uniformS(), rnd::uniformS()) *
CLOUD_WIDTH);
pointMesh.color(HSV(rnd::uniform(), 1.0, 1.0));
}
// Generate a texture with an alpha channel for transparency
texture.create2D(256, 256, Texture::RGBA8);
int Nx = texture.width();
int Ny = texture.height();
// prepare vector for pixel data
std::vector<Colori> pixel; // byte color: rgba, [0:255]
pixel.resize(Nx * Ny);
for (int j = 0; j < Ny; ++j) {
float y = float(j) / (Ny - 1) * 2 - 1;
for (int i = 0; i < Nx; ++i) {
float x = float(i) / (Nx - 1) * 2 - 1;
float m = exp(-13 * (x * x + y * y));
Color c = RGB(m);
c.a = m;
pixel[j * Nx + i] = c;
}
}
texture.submit(pixel);
// these shaders are from the _OpenGL 4.0 Shading Language Cookbook_
shader.compile(
R"(
#version 400
layout (location = 0) in vec3 VertexPosition;
uniform mat4 al_ModelViewMatrix;
uniform mat4 al_ProjectionMatrix;
void main() {
gl_Position = al_ModelViewMatrix * vec4(VertexPosition, 1.0);
}
)",
R"(
#version 400
in vec2 TexCoord;
uniform sampler2D SpriteTex;
layout( location = 0 ) out vec4 FragColor;
void main() {
FragColor = texture(SpriteTex, TexCoord);
}
)",
R"(
#version 400
layout( points ) in;
layout( triangle_strip, max_vertices = 4 ) out;
uniform float Size2; // Half the width of the quad
uniform mat4 al_ProjectionMatrix;
out vec2 TexCoord;
void main() {
mat4 m = al_ProjectionMatrix; // Reassign for brevity
gl_Position = m * (vec4(-Size2,-Size2,0.0,0.0) + gl_in[0].gl_Position);
TexCoord = vec2(0.0,0.0);
EmitVertex();
gl_Position = m * (vec4(Size2,-Size2,0.0,0.0) + gl_in[0].gl_Position);
TexCoord = vec2(1.0,0.0);
EmitVertex();
gl_Position = m * (vec4(-Size2,Size2,0.0,0.0) + gl_in[0].gl_Position);
TexCoord = vec2(0.0,1.0);
EmitVertex();
gl_Position = m * (vec4(Size2,Size2,0.0,0.0) + gl_in[0].gl_Position);
TexCoord = vec2(1.0,1.0);
EmitVertex();
EndPrimitive();
}
)");
}
void onAnimate(double dt) override {}
void onDraw(Graphics& g) override {
g.clear(0.23);
// g.texture();
// g.meshColor();
g.depthTesting(false);
g.blending(true);
g.blendModeTrans();
texture.bind();
g.shader(shader);
g.shader().uniform("Size2", 0.1);
g.draw(pointMesh);
texture.unbind();
}
};
int main() { AlloApp().start(); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment