Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<!DOCTYPE html>
<html>

<script id="vertex-shader1" type="x-shader/x-vertex">
attribute  vec4 vPosition;
uniform mat4 rotation;

void main() 
{
    gl_Position = rotation * vPosition;
} 
</script>

<script id="vertex-shader2" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec2 vTexCoord;

varying vec2 fTexCoord;

uniform mat4 rotation;
  
void main()
{
  gl_Position = rotation * vPosition;
  fTexCoord = vTexCoord;
}
</script>

<script id="fragment-shader1" type="x-shader/x-fragment">
precision mediump float;

void main()
{
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
</script>
<script id="fragment-shader2" type="x-shader/x-fragment">
precision mediump float;

varying  vec2 fTexCoord;

uniform sampler2D texture;

void main() 
{ 
    gl_FragColor = texture2D( texture, fTexCoord);
} 
</script>

<script type="text/javascript" src="https://www.cs.unm.edu/~angel/WebGL/7E/Common/webgl-utils.js"></script>
<script type="text/javascript" src="https://www.cs.unm.edu/~angel/WebGL/7E/Common/InitShaders.js"></script>
<script type="text/javascript" src="https://www.cs.unm.edu/~angel/WebGL/7E/Common/MV.js"></script>

<body>
<canvas id="gl-canvas" width="512" height="512">
Oops ... your browser doesn't support the HTML5 canvas element
</canvas>
</body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                var canvas;
var gl;
var theta = 0.0;

textureSize = 64;

// quad texture coordinates
var texCoord = [
  vec2(0, 0),
  vec2(0, 1),
  vec2(1, 1),
  vec2(1, 1),
  vec2(1, 0),
  vec2(0, 0)
];

// quad vertices
var quadPoints = [
  vec2(-0.5, -0.5),
  vec2(-0.5, 0.5),
  vec2(0.5, 0.5),
  vec2(0.5, 0.5),
  vec2(0.5, -0.5),
  vec2(-0.5, -0.5)
];

// triangle vertices
var trianglePoints = [vec2(-0.5, -0.5), vec2(0, 0.5), vec2(0.5, -0.5)];

var program1, program2;
var texture1;
var rotationLoc;
var vPosition1, vPosition2;

var framebuffer, renderbuffer;

window.onload = function init() {
  canvas = document.getElementById("gl-canvas");

  gl = WebGLUtils.setupWebGL(canvas);
  if (!gl) {
    alert("WebGL isn't available");
  }

  // Create an empty texture
  texture1 = gl.createTexture();
  gl.activeTexture(gl.TEXTURE0);
  gl.bindTexture(gl.TEXTURE_2D, texture1);
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
  gl.texImage2D(
    gl.TEXTURE_2D,
    0,
    gl.RGBA,
    textureSize,
    textureSize,
    0,
    gl.RGBA,
    gl.UNSIGNED_BYTE,
    null
  );
  gl.generateMipmap(gl.TEXTURE_2D);
  gl.texParameteri(
    gl.TEXTURE_2D,
    gl.TEXTURE_MIN_FILTER,
    gl.LINEAR_MIPMAP_LINEAR
  );
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);

  gl.bindTexture(gl.TEXTURE_2D, null);

  // Allocate a frame buffer object
  framebuffer = gl.createFramebuffer();
  gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);

  renderbuffer = gl.createRenderbuffer();
  gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer);

  // Attach texture to renderbuffer as color buffer
  gl.framebufferTexture2D(
    gl.FRAMEBUFFER,
    gl.COLOR_ATTACHMENT0,
    gl.TEXTURE_2D,
    texture1,
    0
  );

  // check for completeness
  var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
  if (status != gl.FRAMEBUFFER_COMPLETE) alert("Frame Buffer Not Complete");

  //
  //  Load shaders and initialize attribute buffers
  //
  program1 = initShaders(gl, "vertex-shader1", "fragment-shader1");
  program2 = initShaders(gl, "vertex-shader2", "fragment-shader2");


  gl.useProgram(program1);
  
  // Create and initialize a buffer object with triangle vertices
  var buffer1 = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer1);
  gl.bufferData(gl.ARRAY_BUFFER, flatten(trianglePoints), gl.STATIC_DRAW);

  // Initialize the vertex position attribute from the vertex shader
  vPosition1 = gl.getAttribLocation(program1, "vPosition");
  gl.vertexAttribPointer(vPosition1, 2, gl.FLOAT, false, 0, 0);
  gl.enableVertexAttribArray(vPosition1);

  // Bind to window system frame buffer, unbind the texture
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  // gl.bindRenderbuffer(gl.RENDERBUFFER, null);

  

  
  // Setup program 2
  gl.useProgram(program2);

  gl.activeTexture(gl.TEXTURE0);
  gl.bindTexture(gl.TEXTURE_2D, texture1);

  // send data to GPU for normal render
  var buffer2 = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer2);
  gl.bufferData(gl.ARRAY_BUFFER, new flatten(quadPoints), gl.STATIC_DRAW);

  vPosition2 = gl.getAttribLocation(program2, "vPosition");
  gl.vertexAttribPointer(vPosition2, 2, gl.FLOAT, false, 0, 0);
  gl.enableVertexAttribArray(vPosition2);

  var buffer3 = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer3);
  gl.bufferData(gl.ARRAY_BUFFER, flatten(texCoord), gl.STATIC_DRAW);

  var vTexCoord = gl.getAttribLocation(program2, "vTexCoord");
  gl.vertexAttribPointer(vTexCoord, 2, gl.FLOAT, false, 0, 0);
  gl.enableVertexAttribArray(vTexCoord);

  gl.uniform1i(gl.getUniformLocation(program2, "texture"), 0);

  
  gl.bindBuffer(gl.ARRAY_BUFFER, null);

  
  render();
};

function render() {
  gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  gl.useProgram(program1);
  
  // Render one triangle
  gl.viewport(0, 0, textureSize, textureSize);
  gl.clearColor(0.5, 0.5, 0.5, 1.0);
  gl.clear(gl.COLOR_BUFFER_BIT);
  
  theta += 1;
  var rotation = rotate(theta, vec3(0.0, 0.0, 1.0));
  gl.uniformMatrix4fv(
    gl.getUniformLocation(program1, "rotation"),
    false,
    flatten(rotation)
  );

  gl.enableVertexAttribArray(vPosition1);
  gl.drawArrays(gl.TRIANGLES, 0, 6);
  gl.disableVertexAttribArray(vPosition1);

  
  // render quad with texture

  // Bind to window system frame buffer, unbind the texture
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  //gl.bindRenderbuffer(gl.RENDERBUFFER, null);

  gl.useProgram(program2);

  gl.activeTexture(gl.TEXTURE0);
  gl.bindTexture(gl.TEXTURE_2D, texture1);

  var rotation = rotate(-theta, vec3(0.0, 0.0, 1.0));
  gl.uniformMatrix4fv(
    gl.getUniformLocation(program2, "rotation"),
    false,
    flatten(rotation)
  );

  gl.clearColor(1.0, 1.0, 1.0, 1.0);
  gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
  gl.clearColor(0.0, 0.0, 1.0, 1.0);
  gl.clear(gl.COLOR_BUFFER_BIT);
  
  gl.enableVertexAttribArray(vPosition2);
  gl.drawArrays(gl.TRIANGLES, 0, 6);
  gl.disableVertexAttribArray(vPosition2);

  requestAnimFrame(render);
}

              
            
!
999px

Console