Skip to content

Instantly share code, notes, and snippets.

@companje
Created July 19, 2012 18:26
Show Gist options
  • Save companje/3145826 to your computer and use it in GitHub Desktop.
Save companje/3145826 to your computer and use it in GitHub Desktop.
Earth with Clouds AlphaBlending Shader
//testApp.cpp
#include "ofMain.h"
class testApp : public ofBaseApp {
public:
ofShader shader;
ofImage earth,clouds;
ofEasyCam cam;
GLUquadricObj *quadric; //for gluSphere
void setup() {
//setup window
ofSetWindowShape(500,500);
ofSetWindowPosition(0,0);
ofBackground(0);
ofSetFrameRate(20);
//load textures with normalized texcoords (0..1)
ofDisableArbTex();
earth.loadImage("earth2k.jpg");
clouds.loadImage("clouds.jpg");
//load the vertex and fragment shaders
shader.load("shaders/blend");
//initialize sphere
quadric = gluNewQuadric();
gluQuadricTexture(quadric, GL_TRUE);
gluQuadricNormals(quadric, GLU_SMOOTH);
}
void draw() {
//start camera and flip y-axis
cam.begin();
ofScale(1,-1);
//start shader en set uniform variables
shader.begin();
shader.setUniformTexture("earth", earth, 1);
shader.setUniformTexture("clouds", clouds, 2);
shader.setUniform1f("blendAlpha", sin(ofGetElapsedTimef())/2+.5); //fade clouds
//draw sphere
glEnable(GL_DEPTH_TEST);
gluSphere(quadric, 150, 400, 400);
//that's all
shader.end();
cam.end();
}
};
//////////////////////////////////////////////////////////////
//blend.frag
uniform sampler2D earth;
uniform sampler2D clouds;
uniform float blendAlpha;
void main(void) {
vec2 texcoord = gl_TexCoord[0].xy;
vec4 background = texture2D(earth, texcoord);
vec4 foreground = texture2D(clouds, texcoord);
gl_FragColor = foreground * blendAlpha + background;
}
//////////////////////////////////////////////////////////////
//blend.vert
void main() {
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment