shaders

Zie ook [[OpenGL]]

shader toy

vertex shader art

GLSL preview in Atom

https://atom.io/packages/glsl-preview

texOffset in Processing

"texOffset uniform. This uniform is set automatically by Processing and contains the vector (1/width, 1/height), with width and height being the resolution of the texture. "

Tutorials

"There are several resources, such as online tutorials and forums, books, and web coding sandboxes (for example Shader Toy, GLSL Sandbox, and Vertex Shader Art), that can be recommended for learning GLSL programming."

The Book of Shaders

Online NormalMap creator

get shader language version

glGetString(GL_SHADING_LANGUAGE_VERSION)
color = light * mix(texture2D(night, textureCoord), 
   texture2D(day, textureCoord), 
   smoothstep(-0.25, 0.25, dot(N, L)));

Shaders on Mac

Links

What does gl_TexCoord.st mean?

st is a vec2 containing the x,y tex coord. It is exactly the same as xy or rg. It just returns the first two components of a float[4]. The letters are there for semantic reasons.

ftransform is depricated

Ftransform is equivalent to this:

gl_Position = ftransform();
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;

v will be a coordinate in projection (screen) space.

Displacement with diffuse light

uniform sampler2D colormap;
uniform sampler2D bumpmap;
varying vec2  TexCoord;
varying vec3 vertex_light_position;
varying vec3 norm;

void main(void) {
   vec3 normal = normalize( norm );
   float diffuse_value = max(dot(normal, vertex_light_position), 0.0);
   gl_FragColor = diffuse_value * texture2D(colormap, TexCoord);
}
uniform sampler2D colormap;
uniform sampler2D bumpmap;
varying vec2 TexCoord;
uniform int maxHeight;
varying vec3 vertex_light_position;
varying vec3 norm;

void main(void) {
    TexCoord = gl_MultiTexCoord0.st;
    vec4 bumpColor = texture2D(bumpmap, TexCoord);
    float df = 0.30*bumpColor.x + 0.59*bumpColor.y + 0.11*bumpColor.z;
    vec4 newVertexPos = vec4(gl_Normal * df * float(maxHeight), 0.0) + gl_Vertex;
    gl_FrontColor = gl_Color;
    vertex_light_position = normalize(gl_LightSource[0].position.xyz);
    norm = -1.0 * gl_NormalMatrix * gl_Normal;
    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position = gl_ModelViewProjectionMatrix * newVertexPos;

openFrameworks:

#include "testApp.h"
#include "ofxExtras.h"

//--------------------------------------------------------------
void testApp::setup(){
    ofSetWindowShape(1280,800);
    ofSetWindowPosition(0,0);
    ofBackground(0,0,0);
    ofDisableArbTex();
    ofEnableAlphaBlending();
    ofSetFrameRate(60);
    ofSetVerticalSync(true);
    glEnable(GL_DEPTH_TEST);

    colormap.loadImage("earth8k.jpg");
    bumpmap.loadImage("earth-bumps.png");
    //normalmap.loadImage("earth-normals.png");

    quadratic = gluNewQuadric();
    gluQuadricTexture(quadratic, GL_TRUE);
    gluQuadricNormals(quadratic, GLU_SMOOTH);

    shader.load("shaders/displace");
    material.setShininess(200);
}

void testApp::draw(){
    ofSetColor(255);
    light.enable(); 
    light.setPosition(ofGetWidth()-ofGetMouseX(),ofGetHeight()-ofGetMouseY(),200); //ofxGetCenter().x,ofxGetCenter().y,cam.getP);
    ofEnableLighting();
    material.begin();
    cam.begin();

    shader.begin();
    shader.setUniformTexture("colormap", colormap, 1); 
    shader.setUniformTexture("bumpmap", bumpmap, 2);
    shader.setUniform1i("maxHeight",20); //sin(ofGetElapsedTimef())*50-25);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_FRONT);
    ofScale(-1, 1);
    gluSphere(quadratic, 250, 400, 400);

    shader.end();
    colormap.unbind();
    bumpmap.unbind();
    material.end(); 
    light.disable();
    ofDisableLighting();    
}