SlideShare a Scribd company logo
Modify code to change cube into pyramid.
Javascript:
"use strict";
var shadedCube = function() {
var canvas;
var gl;
var numPositions = 36;
var positionsArray = [];
var normalsArray = [];
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)
];
var lightPosition = vec4(1.0, 1.0, 1.0, 0.0);
var lightAmbient = vec4(0.2, 0.2, 0.2, 1.0);
var lightDiffuse = vec4(1.0, 1.0, 1.0, 1.0);
var lightSpecular = vec4(1.0, 1.0, 1.0, 1.0);
var materialAmbient = vec4(1.0, 0.0, 1.0, 1.0);
var materialDiffuse = vec4(1.0, 0.8, 0.0, 1.0);
var materialSpecular = vec4(1.0, 0.8, 0.0, 1.0);
var materialShininess = 100.0;
var ctm;
var ambientColor, diffuseColor, specularColor;
var modelViewMatrix, projectionMatrix;
var viewerPos;
var program;
var xAxis = 0;
var yAxis = 1;
var zAxis = 2;
var axis = 0;
var theta = vec3(0, 0, 0);
var thetaLoc;
var flag = false;
function quad(a, b, c, d) {
var t1 = subtract(vertices[b], vertices[a]);
var t2 = subtract(vertices[c], vertices[b]);
var normal = cross(t1, t2);
normal = vec3(normal);
positionsArray.push(vertices[a]);
normalsArray.push(normal);
positionsArray.push(vertices[b]);
normalsArray.push(normal);
positionsArray.push(vertices[c]);
normalsArray.push(normal);
positionsArray.push(vertices[a]);
normalsArray.push(normal);
positionsArray.push(vertices[c]);
normalsArray.push(normal);
positionsArray.push(vertices[d]);
normalsArray.push(normal);
}
function colorCube()
{
quad(1, 0, 3, 2);
quad(2, 3, 7, 6);
quad(3, 0, 4, 7);
quad(6, 5, 1, 2);
quad(4, 5, 6, 7);
quad(5, 4, 0, 1);
}
window.onload = function init() {
canvas = document.getElementById("gl-canvas");
gl = canvas.getContext('webgl2');
if (!gl) alert( "WebGL 2.0 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);
//
// Load shaders and initialize attribute buffers
//
program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
colorCube();
var nBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, nBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(normalsArray), gl.STATIC_DRAW);
var normalLoc = gl.getAttribLocation(program, "aNormal");
gl.vertexAttribPointer(normalLoc, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(normalLoc);
var vBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(positionsArray), gl.STATIC_DRAW);
var positionLoc = gl.getAttribLocation(program, "aPosition");
gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(positionLoc);
thetaLoc = gl.getUniformLocation(program, "theta");
viewerPos = vec3(0.0, 0.0, -20.0);
projectionMatrix = ortho(-1, 1, -1, 1, -100, 100);
var ambientProduct = mult(lightAmbient, materialAmbient);
var diffuseProduct = mult(lightDiffuse, materialDiffuse);
var specularProduct = mult(lightSpecular, materialSpecular);
document.getElementById("ButtonX").onclick = function(){axis = xAxis;};
document.getElementById("ButtonY").onclick = function(){axis = yAxis;};
document.getElementById("ButtonZ").onclick = function(){axis = zAxis;};
document.getElementById("ButtonT").onclick = function(){flag = !flag;};
gl.uniform4fv(gl.getUniformLocation(program, "uAmbientProduct"),
ambientProduct);
gl.uniform4fv(gl.getUniformLocation(program, "uDiffuseProduct"),
diffuseProduct );
gl.uniform4fv(gl.getUniformLocation(program, "uSpecularProduct"),
specularProduct );
gl.uniform4fv(gl.getUniformLocation(program, "uLightPosition"),
lightPosition );
gl.uniform1f(gl.getUniformLocation(program,
"uShininess"), materialShininess);
gl.uniformMatrix4fv( gl.getUniformLocation(program, "uProjectionMatrix"),
false, flatten(projectionMatrix));
render();
}
var render = function(){
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
if(flag) theta[axis] += 2.0;
modelViewMatrix = mat4();
modelViewMatrix = mult(modelViewMatrix, rotate(theta[xAxis], vec3(1, 0, 0)));
modelViewMatrix = mult(modelViewMatrix, rotate(theta[yAxis], vec3(0, 1, 0)));
modelViewMatrix = mult(modelViewMatrix, rotate(theta[zAxis], vec3(0, 0, 1)));
//console.log(modelView);
gl.uniformMatrix4fv(gl.getUniformLocation(program,
"uModelViewMatrix"), false, flatten(modelViewMatrix));
gl.drawArrays(gl.TRIANGLES, 0, numPositions);
requestAnimationFrame(render);
}
}
shadedCube();
HTML:
Rotate XRotate YRotate ZToggle Rotation
Oops ... your browser doesn't support the HTML5 canvas element

More Related Content

PDF
1. Modify code to change cube into pyramid.2. Make the pyramid int.pdf
PDF
Learning WebGLで学ぶWebGL入門
PDF
WebGL - 3D in your Browser
PDF
The fallowing program shows the simple transformation #define GLEW.pdf
PDF
The Ring programming language version 1.7 book - Part 63 of 196
PDF
A Novice's Guide to WebGL
PDF
The Ring programming language version 1.5.4 book - Part 59 of 185
DOCX
Programa de objetos 3 d wire
1. Modify code to change cube into pyramid.2. Make the pyramid int.pdf
Learning WebGLで学ぶWebGL入門
WebGL - 3D in your Browser
The fallowing program shows the simple transformation #define GLEW.pdf
The Ring programming language version 1.7 book - Part 63 of 196
A Novice's Guide to WebGL
The Ring programming language version 1.5.4 book - Part 59 of 185
Programa de objetos 3 d wire

Similar to Modify code to change cube into pyramid.Javascriptuse strict.pdf (20)

PDF
Building Native Apps- A Digital Canvas for Coders and Designers with Walter Luh
PPT
HTML5 Canvas
PDF
How do I modify this code in order to create a rotating pyramid instea.pdf
PDF
JS Experience 2017 - Animações simples com o three.js
PDF
Webgl para JavaScripters
PPTX
Bs webgl소모임004
PPTX
Introduction to open gl in android droidcon - slides
KEY
Leaving Flatland: getting started with WebGL
KEY
The Canvas API for Rubyists
PDF
Exploring Canvas
PDF
Marat-Slides
PDF
I Can't Believe It's Not Flash
PDF
How to build a html5 websites.v1
PPTX
фабрика Blockly
PDF
The Ring programming language version 1.8 book - Part 65 of 202
PDF
Javascript: the important bits
PDF
Richard Salter: Using the Titanium OpenGL Module
PPTX
JavaFX 2.0 With Alternative Languages [Portuguese]
PDF
Безопасность интернет-приложений осень 2013 лекция 10
Building Native Apps- A Digital Canvas for Coders and Designers with Walter Luh
HTML5 Canvas
How do I modify this code in order to create a rotating pyramid instea.pdf
JS Experience 2017 - Animações simples com o three.js
Webgl para JavaScripters
Bs webgl소모임004
Introduction to open gl in android droidcon - slides
Leaving Flatland: getting started with WebGL
The Canvas API for Rubyists
Exploring Canvas
Marat-Slides
I Can't Believe It's Not Flash
How to build a html5 websites.v1
фабрика Blockly
The Ring programming language version 1.8 book - Part 65 of 202
Javascript: the important bits
Richard Salter: Using the Titanium OpenGL Module
JavaFX 2.0 With Alternative Languages [Portuguese]
Безопасность интернет-приложений осень 2013 лекция 10
Ad

More from saxenaavnish1 (20)

PDF
Mire el documental Am�rica se convierte en una potencia mundial, lea.pdf
PDF
Mini Case identificaci�n de los clientes adecuados para el ka de Fo.pdf
PDF
Millie Larsen es una mujer de 84 a�os que vive sola en una casa pequ.pdf
PDF
Miles, a 20 year old male, is seeking counseling because his paterna.pdf
PDF
Mike s 3, p 2Leo s 2 , p 2Don s 1 , p 2April s 3 , p 1.pdf
PDF
Miguel is the managing general partner of MAR and owns a 40 interes.pdf
PDF
Michael Porter de Harvard ha propuesto la cadena de valor como una h.pdf
PDF
Microbiology Describe the relationship between DNA and RNA con.pdf
PDF
Michael leases a Lucerne farm to Boaz for a term of three years. Aft.pdf
PDF
me quedan 35 munites por favor ayudenme Art�culo 1 En el caso .pdf
PDF
Mendel logr� descubrir las leyes b�sicas de la herencia, mientras qu.pdf
PDF
MCQ Select the correct answer.1. What does SQL stand fora.pdf
PDF
MCQ Pls Help ACC495 Corporate Governance and Risk Management Which .pdf
PDF
Megan se gradu� de la universidad hace tres a�os y ha estado trabaja.pdf
PDF
media luna pura Sarah Ryan, vicepresidenta de marketing de Portlan.pdf
PDF
my code doesnt work can you help me please the whole idea of is it i.pdf
PDF
Must be in C++ programming The main objective of this experiment i.pdf
PDF
MUS monetary unit samplingA) What is the planned sample sizeB).pdf
PDF
Multifactor authentication (MFA) requires users to authenticate thei.pdf
PDF
MTCR es una empresa l�der en el desarrollo y fabricaci�n de una ampl.pdf
Mire el documental Am�rica se convierte en una potencia mundial, lea.pdf
Mini Case identificaci�n de los clientes adecuados para el ka de Fo.pdf
Millie Larsen es una mujer de 84 a�os que vive sola en una casa pequ.pdf
Miles, a 20 year old male, is seeking counseling because his paterna.pdf
Mike s 3, p 2Leo s 2 , p 2Don s 1 , p 2April s 3 , p 1.pdf
Miguel is the managing general partner of MAR and owns a 40 interes.pdf
Michael Porter de Harvard ha propuesto la cadena de valor como una h.pdf
Microbiology Describe the relationship between DNA and RNA con.pdf
Michael leases a Lucerne farm to Boaz for a term of three years. Aft.pdf
me quedan 35 munites por favor ayudenme Art�culo 1 En el caso .pdf
Mendel logr� descubrir las leyes b�sicas de la herencia, mientras qu.pdf
MCQ Select the correct answer.1. What does SQL stand fora.pdf
MCQ Pls Help ACC495 Corporate Governance and Risk Management Which .pdf
Megan se gradu� de la universidad hace tres a�os y ha estado trabaja.pdf
media luna pura Sarah Ryan, vicepresidenta de marketing de Portlan.pdf
my code doesnt work can you help me please the whole idea of is it i.pdf
Must be in C++ programming The main objective of this experiment i.pdf
MUS monetary unit samplingA) What is the planned sample sizeB).pdf
Multifactor authentication (MFA) requires users to authenticate thei.pdf
MTCR es una empresa l�der en el desarrollo y fabricaci�n de una ampl.pdf
Ad

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Lesson notes of climatology university.
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Pharma ospi slides which help in ospi learning
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Cell Types and Its function , kingdom of life
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Computing-Curriculum for Schools in Ghana
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Cell Structure & Organelles in detailed.
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Institutional Correction lecture only . . .
STATICS OF THE RIGID BODIES Hibbelers.pdf
Lesson notes of climatology university.
O5-L3 Freight Transport Ops (International) V1.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Pharma ospi slides which help in ospi learning
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Cell Types and Its function , kingdom of life
102 student loan defaulters named and shamed – Is someone you know on the list?
Computing-Curriculum for Schools in Ghana
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
VCE English Exam - Section C Student Revision Booklet
PPH.pptx obstetrics and gynecology in nursing
GDM (1) (1).pptx small presentation for students
Cell Structure & Organelles in detailed.
Supply Chain Operations Speaking Notes -ICLT Program
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Institutional Correction lecture only . . .

Modify code to change cube into pyramid.Javascriptuse strict.pdf

  • 1. Modify code to change cube into pyramid. Javascript: "use strict"; var shadedCube = function() { var canvas; var gl; var numPositions = 36; var positionsArray = []; var normalsArray = []; 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) ]; var lightPosition = vec4(1.0, 1.0, 1.0, 0.0); var lightAmbient = vec4(0.2, 0.2, 0.2, 1.0); var lightDiffuse = vec4(1.0, 1.0, 1.0, 1.0); var lightSpecular = vec4(1.0, 1.0, 1.0, 1.0); var materialAmbient = vec4(1.0, 0.0, 1.0, 1.0); var materialDiffuse = vec4(1.0, 0.8, 0.0, 1.0); var materialSpecular = vec4(1.0, 0.8, 0.0, 1.0); var materialShininess = 100.0;
  • 2. var ctm; var ambientColor, diffuseColor, specularColor; var modelViewMatrix, projectionMatrix; var viewerPos; var program; var xAxis = 0; var yAxis = 1; var zAxis = 2; var axis = 0; var theta = vec3(0, 0, 0); var thetaLoc; var flag = false; function quad(a, b, c, d) { var t1 = subtract(vertices[b], vertices[a]); var t2 = subtract(vertices[c], vertices[b]); var normal = cross(t1, t2); normal = vec3(normal); positionsArray.push(vertices[a]); normalsArray.push(normal); positionsArray.push(vertices[b]); normalsArray.push(normal); positionsArray.push(vertices[c]); normalsArray.push(normal); positionsArray.push(vertices[a]); normalsArray.push(normal); positionsArray.push(vertices[c]); normalsArray.push(normal); positionsArray.push(vertices[d]);
  • 3. normalsArray.push(normal); } function colorCube() { quad(1, 0, 3, 2); quad(2, 3, 7, 6); quad(3, 0, 4, 7); quad(6, 5, 1, 2); quad(4, 5, 6, 7); quad(5, 4, 0, 1); } window.onload = function init() { canvas = document.getElementById("gl-canvas"); gl = canvas.getContext('webgl2'); if (!gl) alert( "WebGL 2.0 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); // // Load shaders and initialize attribute buffers // program = initShaders(gl, "vertex-shader", "fragment-shader"); gl.useProgram(program); colorCube(); var nBuffer = gl.createBuffer();
  • 4. gl.bindBuffer(gl.ARRAY_BUFFER, nBuffer); gl.bufferData(gl.ARRAY_BUFFER, flatten(normalsArray), gl.STATIC_DRAW); var normalLoc = gl.getAttribLocation(program, "aNormal"); gl.vertexAttribPointer(normalLoc, 3, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(normalLoc); var vBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer); gl.bufferData(gl.ARRAY_BUFFER, flatten(positionsArray), gl.STATIC_DRAW); var positionLoc = gl.getAttribLocation(program, "aPosition"); gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(positionLoc); thetaLoc = gl.getUniformLocation(program, "theta"); viewerPos = vec3(0.0, 0.0, -20.0); projectionMatrix = ortho(-1, 1, -1, 1, -100, 100); var ambientProduct = mult(lightAmbient, materialAmbient); var diffuseProduct = mult(lightDiffuse, materialDiffuse); var specularProduct = mult(lightSpecular, materialSpecular); document.getElementById("ButtonX").onclick = function(){axis = xAxis;}; document.getElementById("ButtonY").onclick = function(){axis = yAxis;}; document.getElementById("ButtonZ").onclick = function(){axis = zAxis;}; document.getElementById("ButtonT").onclick = function(){flag = !flag;}; gl.uniform4fv(gl.getUniformLocation(program, "uAmbientProduct"), ambientProduct); gl.uniform4fv(gl.getUniformLocation(program, "uDiffuseProduct"), diffuseProduct ); gl.uniform4fv(gl.getUniformLocation(program, "uSpecularProduct"), specularProduct );
  • 5. gl.uniform4fv(gl.getUniformLocation(program, "uLightPosition"), lightPosition ); gl.uniform1f(gl.getUniformLocation(program, "uShininess"), materialShininess); gl.uniformMatrix4fv( gl.getUniformLocation(program, "uProjectionMatrix"), false, flatten(projectionMatrix)); render(); } var render = function(){ gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); if(flag) theta[axis] += 2.0; modelViewMatrix = mat4(); modelViewMatrix = mult(modelViewMatrix, rotate(theta[xAxis], vec3(1, 0, 0))); modelViewMatrix = mult(modelViewMatrix, rotate(theta[yAxis], vec3(0, 1, 0))); modelViewMatrix = mult(modelViewMatrix, rotate(theta[zAxis], vec3(0, 0, 1))); //console.log(modelView); gl.uniformMatrix4fv(gl.getUniformLocation(program, "uModelViewMatrix"), false, flatten(modelViewMatrix)); gl.drawArrays(gl.TRIANGLES, 0, numPositions); requestAnimationFrame(render); } } shadedCube();
  • 6. HTML: Rotate XRotate YRotate ZToggle Rotation Oops ... your browser doesn't support the HTML5 canvas element