SlideShare a Scribd company logo
How do I modify this code in order to create a rotating pyramid instead of a rotating cube?
HTML :
<!DOCTYPE html>
<html>
<script id="vertex-shader" type="x-shader/x-vertex">
#version 300 es
in vec4 aPosition;
in vec4 aColor;
out vec4 vColor;
uniform vec3 uTheta;
void main()
{
// Compute the sines and cosines of theta for each of
// the three axes in one computation.
vec3 angles = radians(uTheta);
vec3 c = cos(angles);
vec3 s = sin(angles);
// Remeber: thse matrices are column-major
mat4 rx = mat4(1.0, 0.0, 0.0, 0.0,
0.0, c.x, s.x, 0.0,
0.0, -s.x, c.x, 0.0,
0.0, 0.0, 0.0, 1.0);
mat4 ry = mat4(c.y, 0.0, -s.y, 0.0,
0.0, 1.0, 0.0, 0.0,
s.y, 0.0, c.y, 0.0,
0.0, 0.0, 0.0, 1.0);
mat4 rz = mat4(c.z, s.z, 0.0, 0.0,
-s.z, c.z, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
vColor = aColor;
gl_Position = rz * ry * rx * aPosition;
gl_Position.z = -gl_Position.z;
}
</script>
<script id="fragment-shader" type="x-shader/x-fragment">
#version 300 es
precision mediump float;
in vec4 vColor;
out vec4 fColor;
void
main()
{
fColor = vColor;
}
</script>
<script type="text/javascript" src="initShaders.js"></script>
<script type="text/javascript" src="MV.js"></script>
<script type="text/javascript" src="RLSlProj4.js"></script>
<script type="text/javascript" src="webgl-utils.js"></script>
<body>
<canvas id="gl-canvas" width="512"" height="512">
Oops ... your browser doesn't support the HTML5 canvas element
</canvas>
<br/>
<button id= "xButton">Rotate X</button>
<button id= "yButton">Rotate Y</button>
<button id= "zButton">Rotate Z</button>
</body>
</html>
Javascript :
"use strict";
var canvas;
var gl;
var numPositions = 36;
var positions = [];
var colors = [];
var xAxis = 0;
var yAxis = 1;
var zAxis = 2;
var axis = 0;
var theta = [0, 0, 0];
var thetaLoc;
window.onload = function init()
{
canvas = document.getElementById("gl-canvas");
gl = canvas.getContext('webgl2');
if (!gl) alert("WebGL 2.0 isn't available");
colorCube();
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
//
var program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
var cBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
var colorLoc = gl.getAttribLocation( program, "aColor" );
gl.vertexAttribPointer( colorLoc, 4, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray( colorLoc );
var vBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(positions), 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, "uTheta");
//event listeners for buttons
document.getElementById( "xButton" ).onclick = function () {
axis = xAxis;
};
document.getElementById( "yButton" ).onclick = function () {
axis = yAxis;
};
document.getElementById( "zButton" ).onclick = function () {
axis = zAxis;
};
render();
}
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);
}
function quad(a, b, c, d)
{
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 vertexColors = [
vec4(0.0, 0.0, 0.0, 1.0), // black
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, 1.0, 1.0, 1.0), // cyan
vec4(1.0, 1.0, 1.0, 1.0) // white
];
// We need to parition the quad into two triangles in order for
// WebGL to be able to render it. In this case, we create two
// triangles from the quad indices
//vertex color assigned by the index of the vertex
var indices = [a, b, c, a, c, d];
for ( var i = 0; i < indices.length; ++i ) {
positions.push( vertices[indices[i]] );
//colors.push( vertexColors[indices[i]] );
// for solid colored faces use
colors.push(vertexColors[a]);
}
}
function render()
{
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
theta[axis] += 2.0;
gl.uniform3fv(thetaLoc, theta);
gl.drawArrays(gl.TRIANGLES, 0, numPositions);
requestAnimationFrame(render);
}

More Related Content

PDF
WebGL - 3D in your Browser
PDF
Im looking for coding help I dont really need this to be explained.pdf
PDF
Modify code to change cube into pyramid.Javascriptuse strict.pdf
PDF
The fallowing program shows the simple transformation #define GLEW.pdf
PDF
Internal workshop es6_2015
PDF
Famo.us: From Zero to UI
PPTX
Fact, Fiction, and FP
PDF
I need help with this assignment Ive gotten abit stuck with the cod.pdf
WebGL - 3D in your Browser
Im looking for coding help I dont really need this to be explained.pdf
Modify code to change cube into pyramid.Javascriptuse strict.pdf
The fallowing program shows the simple transformation #define GLEW.pdf
Internal workshop es6_2015
Famo.us: From Zero to UI
Fact, Fiction, and FP
I need help with this assignment Ive gotten abit stuck with the cod.pdf

Similar to How do I modify this code in order to create a rotating pyramid instea.pdf (20)

PDF
Exploring Canvas
PPT
HTML5 Canvas
KEY
openFrameworks 007 - 3D
PDF
OpenGL L06-Performance
PPTX
Introduction to open gl in android droidcon - slides
PDF
A Novice's Guide to WebGL
PDF
Kamil witecki asynchronous, yet readable, code
DOCX
ITS 630 – Residency Project Circuit City was an American c.docx
PDF
Swift - One step forward from Obj-C
PPSX
Javascript variables and datatypes
PPTX
Workshop 1: Good practices in JavaScript
PDF
Marat-Slides
PDF
Five things for you - Yahoo developer offers
PPTX
golang_getting_started.pptx
PDF
HTML5 & The Open Web - at Nackademin
KEY
Leaving Flatland: getting started with WebGL
PDF
INTERVIEW QUESTIONS_Verilog_PART-2.pdf
PDF
Learning WebGLで学ぶWebGL入門
DOCX
Programa de objetos 3 d wire
Exploring Canvas
HTML5 Canvas
openFrameworks 007 - 3D
OpenGL L06-Performance
Introduction to open gl in android droidcon - slides
A Novice's Guide to WebGL
Kamil witecki asynchronous, yet readable, code
ITS 630 – Residency Project Circuit City was an American c.docx
Swift - One step forward from Obj-C
Javascript variables and datatypes
Workshop 1: Good practices in JavaScript
Marat-Slides
Five things for you - Yahoo developer offers
golang_getting_started.pptx
HTML5 & The Open Web - at Nackademin
Leaving Flatland: getting started with WebGL
INTERVIEW QUESTIONS_Verilog_PART-2.pdf
Learning WebGLで学ぶWebGL入門
Programa de objetos 3 d wire
Ad

More from krishnac481 (20)

PDF
How does excitation pass from cell 1 to cell 2- By acetylcholine By pa.pdf
PDF
How I can change the output size in insert import tkinter as tk imp.pdf
PDF
How does PowerBI relate to digital transformation- (Minimum 200 words).pdf
PDF
How does Any Rand view the American idea of -making money-- A- Nega.pdf
PDF
How has social media impacted consumer behavior pre and post covid19-.pdf
PDF
How is Peru being impacted by Climate Change- Compare and contrast the.pdf
PDF
How is each group classified- Parasitic worms Fungi Protozoa What is t.pdf
PDF
How do I print the lists vertically- sizes -- -Small-- -Medium-- -Larg.pdf
PDF
How is long-run economic growth measured- What is the basis of long-ru.pdf
PDF
How do I calculate the following for relative error by hand- I'm aware.pdf
PDF
How is global human resource management different from domestic HRM- Q.pdf
PDF
How has Europe benefited from its location and its major physical feat.pdf
PDF
How each of this happens and their clinical correlates-a- Inversion du.pdf
PDF
How is the beginning of intramembranous ossification different from en.pdf
PDF
How do the architecture and design of these systems reflect the functi.pdf
PDF
How does the fluid mosaic model represents the structure of the plasma.pdf
PDF
How do you differentiate a crypto exchange from a traditional stock ex.pdf
PDF
How does the heterozygous condition for sickle cell disease confer an.pdf
PDF
How does Celebrity Cruises collect data about the customer experience-.pdf
PDF
How do retail stores display their merchandise taking into account sub.pdf
How does excitation pass from cell 1 to cell 2- By acetylcholine By pa.pdf
How I can change the output size in insert import tkinter as tk imp.pdf
How does PowerBI relate to digital transformation- (Minimum 200 words).pdf
How does Any Rand view the American idea of -making money-- A- Nega.pdf
How has social media impacted consumer behavior pre and post covid19-.pdf
How is Peru being impacted by Climate Change- Compare and contrast the.pdf
How is each group classified- Parasitic worms Fungi Protozoa What is t.pdf
How do I print the lists vertically- sizes -- -Small-- -Medium-- -Larg.pdf
How is long-run economic growth measured- What is the basis of long-ru.pdf
How do I calculate the following for relative error by hand- I'm aware.pdf
How is global human resource management different from domestic HRM- Q.pdf
How has Europe benefited from its location and its major physical feat.pdf
How each of this happens and their clinical correlates-a- Inversion du.pdf
How is the beginning of intramembranous ossification different from en.pdf
How do the architecture and design of these systems reflect the functi.pdf
How does the fluid mosaic model represents the structure of the plasma.pdf
How do you differentiate a crypto exchange from a traditional stock ex.pdf
How does the heterozygous condition for sickle cell disease confer an.pdf
How does Celebrity Cruises collect data about the customer experience-.pdf
How do retail stores display their merchandise taking into account sub.pdf
Ad

Recently uploaded (20)

PDF
Basic Mud Logging Guide for educational purpose
PPTX
Cell Types and Its function , kingdom of life
PDF
Computing-Curriculum for Schools in Ghana
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Lesson notes of climatology university.
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Classroom Observation Tools for Teachers
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Institutional Correction lecture only . . .
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
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Microbial disease of the cardiovascular and lymphatic systems
Basic Mud Logging Guide for educational purpose
Cell Types and Its function , kingdom of life
Computing-Curriculum for Schools in Ghana
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Lesson notes of climatology university.
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Classroom Observation Tools for Teachers
STATICS OF THE RIGID BODIES Hibbelers.pdf
O7-L3 Supply Chain Operations - ICLT Program
Institutional Correction lecture only . . .
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
GDM (1) (1).pptx small presentation for students
TR - Agricultural Crops Production NC III.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
RMMM.pdf make it easy to upload and study
Microbial disease of the cardiovascular and lymphatic systems

How do I modify this code in order to create a rotating pyramid instea.pdf

  • 1. How do I modify this code in order to create a rotating pyramid instead of a rotating cube? HTML : <!DOCTYPE html> <html> <script id="vertex-shader" type="x-shader/x-vertex"> #version 300 es in vec4 aPosition; in vec4 aColor; out vec4 vColor; uniform vec3 uTheta; void main() { // Compute the sines and cosines of theta for each of // the three axes in one computation. vec3 angles = radians(uTheta); vec3 c = cos(angles); vec3 s = sin(angles); // Remeber: thse matrices are column-major mat4 rx = mat4(1.0, 0.0, 0.0, 0.0, 0.0, c.x, s.x, 0.0, 0.0, -s.x, c.x, 0.0, 0.0, 0.0, 0.0, 1.0); mat4 ry = mat4(c.y, 0.0, -s.y, 0.0,
  • 2. 0.0, 1.0, 0.0, 0.0, s.y, 0.0, c.y, 0.0, 0.0, 0.0, 0.0, 1.0); mat4 rz = mat4(c.z, s.z, 0.0, 0.0, -s.z, c.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0); vColor = aColor; gl_Position = rz * ry * rx * aPosition; gl_Position.z = -gl_Position.z; } </script> <script id="fragment-shader" type="x-shader/x-fragment"> #version 300 es precision mediump float; in vec4 vColor; out vec4 fColor; void main() { fColor = vColor; }
  • 3. </script> <script type="text/javascript" src="initShaders.js"></script> <script type="text/javascript" src="MV.js"></script> <script type="text/javascript" src="RLSlProj4.js"></script> <script type="text/javascript" src="webgl-utils.js"></script> <body> <canvas id="gl-canvas" width="512"" height="512"> Oops ... your browser doesn't support the HTML5 canvas element </canvas> <br/> <button id= "xButton">Rotate X</button> <button id= "yButton">Rotate Y</button> <button id= "zButton">Rotate Z</button> </body> </html> Javascript : "use strict"; var canvas; var gl; var numPositions = 36; var positions = []; var colors = []; var xAxis = 0;
  • 4. var yAxis = 1; var zAxis = 2; var axis = 0; var theta = [0, 0, 0]; var thetaLoc; window.onload = function init() { canvas = document.getElementById("gl-canvas"); gl = canvas.getContext('webgl2'); if (!gl) alert("WebGL 2.0 isn't available"); colorCube(); 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 // var program = initShaders(gl, "vertex-shader", "fragment-shader"); gl.useProgram(program); var cBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer); gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW); var colorLoc = gl.getAttribLocation( program, "aColor" );
  • 5. gl.vertexAttribPointer( colorLoc, 4, gl.FLOAT, false, 0, 0 ); gl.enableVertexAttribArray( colorLoc ); var vBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer); gl.bufferData(gl.ARRAY_BUFFER, flatten(positions), 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, "uTheta"); //event listeners for buttons document.getElementById( "xButton" ).onclick = function () { axis = xAxis; }; document.getElementById( "yButton" ).onclick = function () { axis = yAxis; }; document.getElementById( "zButton" ).onclick = function () { axis = zAxis; }; render(); } function colorCube()
  • 6. { 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); } function quad(a, b, c, d) { 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 vertexColors = [ vec4(0.0, 0.0, 0.0, 1.0), // black vec4(1.0, 0.0, 0.0, 1.0), // red
  • 7. 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, 1.0, 1.0, 1.0), // cyan vec4(1.0, 1.0, 1.0, 1.0) // white ]; // We need to parition the quad into two triangles in order for // WebGL to be able to render it. In this case, we create two // triangles from the quad indices //vertex color assigned by the index of the vertex var indices = [a, b, c, a, c, d]; for ( var i = 0; i < indices.length; ++i ) { positions.push( vertices[indices[i]] ); //colors.push( vertexColors[indices[i]] ); // for solid colored faces use colors.push(vertexColors[a]); } } function render() { gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); theta[axis] += 2.0;
  • 8. gl.uniform3fv(thetaLoc, theta); gl.drawArrays(gl.TRIANGLES, 0, numPositions); requestAnimationFrame(render); }