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-shader" type="x-shader/x-vertex">

varying vec3 R;
attribute vec4 vPosition;
attribute vec4 vNormal;
attribute float vDoReflect;

uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat3 normalMatrix;

void main()
{     
    //vec3 eyePos  = normalize((modelViewMatrix*vPosition).xyz);
    vec3 eyePos  = (modelViewMatrix*vPosition).xyz;

    vec3 N = normalize(normalMatrix*vNormal.xyz);
    //vec3 N = normalMatrix*vNormal.xyz;
    
    if (vDoReflect >= 0.5) {
      gl_Position = projectionMatrix*modelViewMatrix*vPosition;
      R = reflect(eyePos, N);
    } else {
      gl_Position = projectionMatrix*vPosition;
      R = vec3(vNormal.x, vNormal.y, -vNormal.z);
    } 
}
</script>

<script id="fragment-shader" type="x-shader/x-fragment">

precision mediump float;

varying vec3 R;
uniform samplerCube texMap;

void main()
{
    vec4 texColor = textureCube(texMap, R);

    gl_FragColor = texColor;
}
</script>

<p> </p>
<button id = "Button2">Increase theta</button>
<button id = "Button3">Decrease theta</button>
<button id = "Button4">Increase phi</button>
<button id = "Button5">Decrease phi</button>
<p> </p>



<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 program;

var numVertices  = 36;

var dr = 5.0 * Math.PI/180.0;
var radius = 3.0;
var theta  = -6.0 * dr;
var phi    = -6.0 * dr;

var near = 0.001;
var far = 1000.0;
var  fovy = 120.0;  // Field-of-view in Y direction angle (in degrees)
var  aspect;       // Viewport aspect ratio


var flag = true;

var red = new Uint8Array([255, 0, 0, 255]);
var green = new Uint8Array([0, 255, 0, 255]);
var blue = new Uint8Array([0, 0, 255, 255]);
var cyan = new Uint8Array([0, 255, 255, 255]);
var magenta = new Uint8Array([255, 0, 255, 255]);
var yellow = new Uint8Array([255, 255, 0, 255]);

var cubeMap;

var pointsArray = [];
var normalsArray = [];
var reflectArray = [];

var vertices = [
    vec4( -0.5, -0.5,  0.5, 1.0 ),
    vec4( -0.5,  0.5,  0.5, 1.0 ),
    vec4( 0.5,  0.5,  0.5, 1.0 ),
    vec4( 0.5, -0.5,  0.5, 1.0 ),
    vec4( -0.5, -0.5, -0.5, 1.0 ),
    vec4( -0.5,  0.5, -0.5, 1.0 ),
    vec4( 0.5,  0.5, -0.5, 1.0 ),
    vec4( 0.5, -0.5, -0.5, 1.0 )
];


function configureCubeMap() {

    cubeMap = gl.createTexture();

    gl.bindTexture(gl.TEXTURE_CUBE_MAP, cubeMap);
    gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X ,0,gl.RGBA,
       1,1,0,gl.RGBA,gl.UNSIGNED_BYTE, red);
    gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_X ,0,gl.RGBA,
       1,1,0,gl.RGBA,gl.UNSIGNED_BYTE, green);
    gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Y ,0,gl.RGBA,
       1,1,0,gl.RGBA,gl.UNSIGNED_BYTE, blue);
    gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Y ,0,gl.RGBA,
       1,1,0,gl.RGBA,gl.UNSIGNED_BYTE, cyan);
    gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_Z ,0,gl.RGBA,
       1,1,0,gl.RGBA,gl.UNSIGNED_BYTE, yellow);
    gl.texImage2D(gl.TEXTURE_CUBE_MAP_NEGATIVE_Z ,0,gl.RGBA,
       1,1,0,gl.RGBA,gl.UNSIGNED_BYTE, magenta);
    

    gl.texParameteri(gl.TEXTURE_CUBE_MAP,gl.TEXTURE_MAG_FILTER,gl.NEAREST);
    gl.texParameteri(gl.TEXTURE_CUBE_MAP,gl.TEXTURE_MIN_FILTER,gl.NEAREST);
}

function scaleVertex(scaling, vertex) {
  scaledVertex = scale(scaling, vertex);
  scaledVertex[3] = 1.0;
  return scaledVertex;
}

function quad(a, b, c, d, scaling, doReflect) {

     var t1 = scale(scaling, subtract(vertices[b], vertices[a]));
     var t2 = scale(scaling, subtract(vertices[c], vertices[a]));
     var normal = cross(t1, t2);
     normal[3] = 0.0;

  console.log(scaling);
  console.log(vertices[a]);
  console.log(scaleVertex(scaling, vertices[a]));
     pointsArray.push(scaleVertex(scaling, vertices[a])); 
     normalsArray.push(normal); 
     reflectArray.push(doReflect);

     pointsArray.push(scaleVertex(scaling, vertices[b])); 
     normalsArray.push(normal);
     reflectArray.push(doReflect);

     pointsArray.push(scaleVertex(scaling, vertices[c])); 
     normalsArray.push(normal);
     reflectArray.push(doReflect);
    
     pointsArray.push(scaleVertex(scaling, vertices[a])); 
     normalsArray.push(normal);
     reflectArray.push(doReflect);

     pointsArray.push(scaleVertex(scaling, vertices[c])); 
     normalsArray.push(normal);
     reflectArray.push(doReflect);

     pointsArray.push(scaleVertex(scaling, vertices[d])); 
     normalsArray.push(normal);
     reflectArray.push(doReflect);   
}

function colorCube(scaling, doReflect)
{
    quad( 1, 0, 3, 2, scaling, doReflect );
    quad( 2, 3, 7, 6, scaling, doReflect );
    quad( 3, 0, 4, 7, scaling, doReflect );
    quad( 6, 5, 1, 2, scaling, doReflect );
    quad( 4, 5, 6, 7, scaling, doReflect );
    quad( 5, 4, 0, 1, scaling, doReflect );
}

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

    gl.viewport( 0, 0, canvas.width, canvas.height );
    gl.clearColor( 1.0, 1.0, 1.0, 1.0 );
    
    gl.enable(gl.DEPTH_TEST);

    // 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 );
    
    colorCube(1.0, 1.0);
    colorCube(-10.0, 0.0);

    var nBuffer = gl.createBuffer();
    gl.bindBuffer( gl.ARRAY_BUFFER, nBuffer);
    gl.bufferData( gl.ARRAY_BUFFER, flatten(normalsArray), gl.STATIC_DRAW );
    
    var vNormal = gl.getAttribLocation( program, "vNormal");
    gl.vertexAttribPointer( vNormal, 4, gl.FLOAT, false, 0, 0);
    gl.enableVertexAttribArray( vNormal);

    var vBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, flatten(pointsArray), gl.STATIC_DRAW);
    
    var vPosition = gl.getAttribLocation( program, "vPosition");
    gl.vertexAttribPointer(vPosition, 4, gl.FLOAT, false, 0, 0);
    gl.enableVertexAttribArray(vPosition);
    
    var rBuffer = gl.createBuffer();
    gl.bindBuffer( gl.ARRAY_BUFFER, rBuffer);
    gl.bufferData( gl.ARRAY_BUFFER, flatten(reflectArray), gl.STATIC_DRAW );
    
    var vDoReflect = gl.getAttribLocation( program, "vDoReflect");
    gl.vertexAttribPointer( vDoReflect, 1, gl.FLOAT, false, 0, 0);
    gl.enableVertexAttribArray( vDoReflect);

    //var projectionMatrix = ortho(-2, 2, -2, 2, -10, 10);
    var projectionMatrix = perspective(fovy, aspect, near, far);

    gl.uniformMatrix4fv( gl.getUniformLocation( program, "projectionMatrix" ), false, flatten(projectionMatrix) );

    configureCubeMap();
    gl.activeTexture( gl.TEXTURE0 );
    gl.uniform1i(gl.getUniformLocation(program, "texMap"),0); 
    
    document.getElementById("Button2").onclick = function(){theta += dr;};
    document.getElementById("Button3").onclick = function(){theta -= dr;};
    document.getElementById("Button4").onclick = function(){phi += dr;};
    document.getElementById("Button5").onclick = function(){phi -= dr;};

    render();
}

var render = function(){
    gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    
    var eye = vec3(radius*Math.sin(theta)*Math.cos(phi), 
        radius*Math.sin(theta)*Math.sin(phi), radius*Math.cos(theta));
    var at = vec3(0.0, 0.0, 0.0);
    var up = vec3(0.0, 1.0, 0.0);
    
    var modelViewMatrix = lookAt(eye, at, up);
  
    gl.uniformMatrix4fv( gl.getUniformLocation(program, "modelViewMatrix"), false, flatten(modelViewMatrix) );
            
    var normalMatrix = [
        vec3(modelViewMatrix[0][0], modelViewMatrix[0][1], modelViewMatrix[0][2]),
        vec3(modelViewMatrix[1][0], modelViewMatrix[1][1], modelViewMatrix[1][2]),
        vec3(modelViewMatrix[2][0], modelViewMatrix[2][1], modelViewMatrix[2][2])
    ];
    
    gl.uniformMatrix3fv(gl.getUniformLocation(program, "normalMatrix"), false, flatten(normalMatrix) );

    gl.drawArrays( gl.TRIANGLES, 0, pointsArray.length );
    requestAnimFrame(render);
}

init();
              
            
!
999px

Console