123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!DOCTYPE html>
<html>

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

varying vec4 fColor;
varying vec2 fTexCoord;

uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;

void main() 
{
    gl_Position = projectionMatrix*modelViewMatrix*vPosition;
    fColor = vColor;
    fTexCoord = vTexCoord;
} 
    </script>
    <script id="fragment-shader" type="x-shader/x-fragment">

precision mediump float;

varying vec4 fColor;
varying  vec2 fTexCoord;

uniform sampler2D texture;

void
main()
{
    gl_FragColor = fColor * 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>
  </head>

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

<br />
Move: 
<button id = "Button3">Farther</button><span id="distance"></span>
<button id = "Button4">Closer</button>
<button id = "Button6">Up</button><span id="theta"></span>
<button id = "Button5">Down</button>
<button id = "Button8">Left</button><span id="phi"></span>
<button id = "Button7">Right</button>

<div>
<input type="checkbox" id="perspective" name="perspective"
         checked>
  <label for="perspective">Use perspective</label>
</div>
  </body>

  <img crossorigin="anonymous" id="texImage" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2999896/Brooklyn_College_Seal.png" hidden></img>

</html>
            
          
!
            
              var gl;
var program;

var points = [];  // All vertices to be drawn (including duplicates)
var colors = [];  // Colors of all vertices to be drawn (including duplicates)
var texCoords = []; // Texture coordinates for each vertex

var near = 0.001;
var far = 1000.0;
var radius = 3.0;
var theta  = 60 * Math.PI/180;
var phi    = 30 * Math.PI/180;
var dr = 5.0 * Math.PI/180.0;

var  fovy = 45.0;  // Field-of-view in Y direction angle (in degrees)
var  aspect;       // Viewport aspect ratio

var modelViewMatrix, projectionMatrix;
var modelViewMatrixLoc, projectionMatrixLoc;
var eye;
const at = vec3(0.0, 0.0, 0.0);
const up = vec3(0.0, 1.0, 0.0);

var usePerspective = true;

var texture;

var texCoordsBase = [
    vec2(0, 0),
    vec2(1, 0),
    vec2(1, 1),
    vec2(0, 1)
];

var vertices = [
    vec3( -0.5, -0.5,  0.5 ),
    vec3( -0.5,  0.5,  0.5),
    vec3(  0.5,  0.5,  0.5),
    vec3(  0.5, -0.5,  0.5),
    vec3( -0.5, -0.5, -0.5),
    vec3( -0.5,  0.5, -0.5),
    vec3(  0.5,  0.5, -0.5),
    vec3(  0.5, -0.5, -0.5)
];

var vertexColors = [
    vec4( 1.0, 1.0, 1.0, 1.0 ),  // white
    vec4( 1.0, 0.0, 0.0, 1.0 ),  // red
    vec4( 1.0, 1.0, 0.0, 1.0 ),  // yellow
    vec4( 0.0, 1.0, 0.0, 1.0 ),  // green
    vec4( 0.0, 0.0, 1.0, 1.0 ),  // blue
    vec4( 1.0, 0.0, 1.0, 1.0 ),  // magenta
    vec4( 0.0, 0.0, 0.0, 1.0 ),  // black
    vec4( 0.0, 1.0, 1.0, 1.0 )   // cyan
];    


function makeCube( )
{
    makeCubeFace(0,3,2,1);
    makeCubeFace(2,3,7,6);
    makeCubeFace(3,0,4,7);
    makeCubeFace(1,2,6,5);
    makeCubeFace(4,5,6,7);
    makeCubeFace(5,4,0,1);
}

function makeCubeFace(a, b, c, d)
{
   var inds = [ a, b, c, a, c, d ];
   var texInds = [0, 1, 2, 0, 2, 3];
   for ( var i = 0; i < inds.length; ++i ) {
			points.push( vertices[inds[i]]);
      
      // Color by vertex
      //colors.push( vertexColors[inds[i]] );
            
      // Color by face
      colors.push(vertexColors[a]);
     
      // Texture
      texCoords.push(texCoordsBase[texInds[i]]);
   }
}


function checkerboardImage(texSize) {
  var image1 = new Array()
  for (var i =0; i<texSize; i++) { image1[i] = new Array(); }
  for (var i =0; i<texSize; i++) {
      for ( var j = 0; j < texSize; j++) {
        image1[i][j] = new Float32Array(4);
      }
  }
  for (var i =0; i<texSize; i++) for (var j=0; j<texSize; j++) {
      var c = (((i & 0x8) == 0) ^ ((j & 0x8)  == 0));
      image1[i][j] = [c, c, c, 1];
  }

  // Convert floats to ubytes for texture
  var image2 = new Uint8Array(4*texSize*texSize);
  for ( var i = 0; i < texSize; i++ ) {
    for ( var j = 0; j < texSize; j++ ) {
      for(var k =0; k<4; k++) {
        image2[4*texSize*i+4*j+k] = 255*image1[i][j][k];
      }
    }
  }
  
  return image2;
}


function configureTexture( image, isImage, texSize ) {
    texture = gl.createTexture();
    gl.bindTexture( gl.TEXTURE_2D, texture );
    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
    if (isImage) {
      gl.texImage2D( gl.TEXTURE_2D, 0, gl.RGB, 
           gl.RGB, gl.UNSIGNED_BYTE, image ); 
    } else {
      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texSize, texSize, 0, 
        gl.RGBA, gl.UNSIGNED_BYTE, image);
    } 
    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.uniform1i(gl.getUniformLocation(program, "texture"), 0);
}


function updateDisplays() {
  document.getElementById("distance").innerText = radius.toFixed(2);
	document.getElementById("theta").innerText = (theta * 180 / Math.PI).toFixed(2);
	document.getElementById("phi").innerText = (phi * 180 / Math.PI).toFixed(2);
}

function init() {
  var canvas = document.getElementById("gl-canvas");
  gl = WebGLUtils.setupWebGL(canvas);
  if (!gl) {
    alert("WebGL isn't available");
  }

  updateDisplays();
  document.getElementById("Button3").onclick = function(){ radius *= 5/4; updateDisplays(); };
  document.getElementById("Button4").onclick = function(){ radius *= 4/5; updateDisplays();};
  document.getElementById("Button5").onclick = function(){ theta += dr; updateDisplays(); };
  document.getElementById("Button6").onclick = function(){ theta -= dr; updateDisplays(); };
  document.getElementById("Button7").onclick = function(){ phi += dr; updateDisplays(); };
  document.getElementById("Button8").onclick = function(){ phi -= dr; updateDisplays(); };
  document.getElementById("perspective").onclick = function(){ usePerspective = document.getElementById("perspective").checked; };

  //  Configure WebGL
  gl.viewport(0, 0, canvas.width, canvas.height);
  gl.clearColor(1.0, 1.0, 1.0, 1.0);
  gl.enable(gl.DEPTH_TEST);  // <--- New

  // Set aspect ratio of canvas
  aspect =  canvas.width/canvas.height;

  //  Load shaders and initialize attribute buffers
  program = initShaders(gl, "vertex-shader", "fragment-shader");
  gl.useProgram(program);

	// Populate vertices and colors
  makeCube();  

  // Load vertex data into the GPU
  var bufferId = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, bufferId);
  gl.bufferData(gl.ARRAY_BUFFER, flatten(points), gl.STATIC_DRAW);

  // Associate vertex shader variables with data buffer
  var vPosition = gl.getAttribLocation(program, "vPosition");
  gl.vertexAttribPointer(vPosition, 3, gl.FLOAT, false, 0, 0);
  gl.enableVertexAttribArray(vPosition);


  // Load color data into the GPU
  bufferId = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, bufferId);
  gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);

  // Associate color shader variables with our data buffer
  var vColor = gl.getAttribLocation(program, "vColor");
  gl.vertexAttribPointer(vColor, 4, gl.FLOAT, false, 0, 0);
  gl.enableVertexAttribArray(vColor);
 

  // Configure texture buffers
  var tBuffer = gl.createBuffer();
  gl.bindBuffer( gl.ARRAY_BUFFER, tBuffer );
  gl.bufferData( gl.ARRAY_BUFFER, flatten(texCoords), gl.STATIC_DRAW );
    
  var vTexCoord = gl.getAttribLocation( program, "vTexCoord" );
  gl.vertexAttribPointer( vTexCoord, 2, gl.FLOAT, false, 0, 0 );
  gl.enableVertexAttribArray( vTexCoord );

  // Option 1: Texture from hard-coded URL
  // var image = new Image();
  // image.onload = function() { 
  //    configureTexture( image, 1, image.width );
  // }
  // image.crossOrigin = "anonymous";
  // image.src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/2999896/brickwork-texture.jpg"
  // From: http://www.bricksntiles.com/textures/
  
  // Option 2: Texture from hidden <img> element
  var image = document.getElementById("texImage");
  window.onload = function() { configureTexture( image, 1, image.width ); };
  
  // Option 3: Generate texture image procedurally
  // var image2 = checkerboardImage(256);
  // configureTexture( image2, 0, 256 )
  
  // Handles to send matrices
  modelViewMatrixLoc = gl.getUniformLocation( program, "modelViewMatrix" );
  projectionMatrixLoc = gl.getUniformLocation( program, "projectionMatrix" );
 
  
  // Go
  render();
}


function render(){
    gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
            
    eye = vec3(
        radius*(0.05 + Math.sin(theta)*Math.sin(phi)), 
        radius*Math.cos(theta),
        radius*Math.sin(theta)*Math.cos(phi)
        );
    modelViewMatrix = lookAt(eye, at, up);
    if (usePerspective) {
    	projectionMatrix = perspective(fovy, aspect, near, far);
    } else {
    	projectionMatrix = ortho(-radius/3, radius/3, -radius/3, radius/3, near, far);
		}

    gl.uniformMatrix4fv( modelViewMatrixLoc, false, flatten(modelViewMatrix) );
    gl.uniformMatrix4fv( projectionMatrixLoc, false, flatten(projectionMatrix) );
            
    gl.drawArrays( gl.TRIANGLES, 0, points.length );
    requestAnimFrame(render);
}


init();

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console