Commits

Kota UENISHI  committed 2079d56

add lesson 4 (by half done)

  • Participants
  • Parent commits 2d8765a

Comments (0)

Files changed (1)

File js/mywebgl.js

 
 var triangleVertexPositionBuffer;
 var triangleVertexColorBuffer;
+var pyramidVertexPositionBuffer;
+var pyramidVertexColorBuffer;
 var squareVertexPositionBuffer;
 var squareVertexColorBuffer;
 
 function initBuffers() {
-    triangleVertexPositionBuffer = gl.createBuffer();
+/*    triangleVertexPositionBuffer = gl.createBuffer();
     gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
     var vertices = [
         0.0,  1.0,  0.0,
     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
     triangleVertexColorBuffer.itemSize = 4;
     triangleVertexColorBuffer.numItems = 3;
+*/
+    pyramidVertexPositionBuffer = gl.createBuffer();
+    gl.bindBuffer(gl.ARRAY_BUFFER, pyramidVertexPositionBuffer);
+        var vertices = [
+            // Front face
+             0.0,  1.0,  0.0,
+            -1.0, -1.0,  1.0,
+             1.0, -1.0,  1.0,
+
+            // Right face
+             0.0,  1.0,  0.0,
+             1.0, -1.0,  1.0,
+             1.0, -1.0, -1.0,
+
+            // Back face
+             0.0,  1.0,  0.0,
+             1.0, -1.0, -1.0,
+            -1.0, -1.0, -1.0,
+
+            // Left face
+             0.0,  1.0,  0.0,
+            -1.0, -1.0, -1.0,
+            -1.0, -1.0,  1.0
+        ];
+    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
+    pyramidVertexPositionBuffer.itemSize = 3;
+    pyramidVertexPositionBuffer.numItems = 12;
+    
+    pyramidVertexColorBuffer = gl.createBuffer();
+    gl.bindBuffer(gl.ARRAY_BUFFER, pyramidVertexColorBuffer);
+        var colors = [
+            // Front face
+            1.0, 0.0, 0.0, 1.0,
+            0.0, 1.0, 0.0, 1.0,
+            0.0, 0.0, 1.0, 1.0,
+
+            // Right face
+            1.0, 0.0, 0.0, 1.0,
+            0.0, 0.0, 1.0, 1.0,
+            0.0, 1.0, 0.0, 1.0,
+
+            // Back face
+            1.0, 0.0, 0.0, 1.0,
+            0.0, 1.0, 0.0, 1.0,
+            0.0, 0.0, 1.0, 1.0,
+
+            // Left face
+            1.0, 0.0, 0.0, 1.0,
+            0.0, 0.0, 1.0, 1.0,
+            0.0, 1.0, 0.0, 1.0
+        ];
+    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
+    pyramidVertexColorBuffer.itemSize = 4;
+    pyramidVertexColorBuffer.numItems = 12;
     
     squareVertexPositionBuffer = gl.createBuffer();
     gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
     vertices = [
         1.0,  1.0,  0.0,
        -1.0,  1.0,  0.0,
-        1.0, -1.0,  0.0,
+        1.0, -1.0,  0.0 ,
        -1.0, -1.0,  0.0
     ];
     gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
     squareVertexColorBuffer.numItems = 4;
 }
 
-var rTri = 0;
-var rSquare = 0;
+var rPyramid = 0;
+var rCube = 0;
+//var rTri = 0;
+//var rSquare = 0;
 
 function drawScene() {
     gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
     mat4.translate(mvMatrix, [-1.5, 0.0, -7.0]);
 
     mvPushMatrix();
-    mat4.rotate(mvMatrix, degToRad(rTri), [0, 1, 0]);
+    mat4.rotate(mvMatrix, degToRad(rPyramid), [.0, 1, 0]);
 
-    gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
-    gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
+//    gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
+    gl.bindBuffer(gl.ARRAY_BUFFER, pyramidVertexPositionBuffer);
+    gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,
+//			   triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
+			   pyramidVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
     
-    gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer);
-    gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, triangleVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
+//    gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer);
+    gl.bindBuffer(gl.ARRAY_BUFFER, pyramidVertexColorBuffer);
+    gl.vertexAttribPointer(shaderProgram.vertexColorAttribute,
+//			   triangleVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
+			   pyramidVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
     
     setMatrixUniforms();
-    gl.drawArrays(gl.TRIANGLES, 0, triangleVertexPositionBuffer.numItems);
+//    gl.drawArrays(gl.TRIANGLES, 0, triangleVertexPositionBuffer.numItems);
+    gl.drawArrays(gl.TRIANGLES, 0, pyramidVertexPositionBuffer.numItems);
     mvPopMatrix();
     
-    mat4.translate(mvMatrix, [3.0, 1.0, 0.0]);
+    mat4.translate(mvMatrix, [3.0, 0.0, 0.0]);
 
     mvPushMatrix();
-    mat4.rotate(mvMatrix, degToRad(rSquare), [1, 0, 0]);
+    mat4.rotate(mvMatrix, degToRad(rCube), [0, -1, 0]);
     gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
     gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
     
         if (lastTime != 0) {
             var elapsed = timeNow - lastTime;
 
-            rTri += (90 * elapsed) / 1000.0;
-            rSquare += (75 * elapsed) / 1000.0;
+            rPyramid += (90 * elapsed) / 1000.0;
+            rCube -= (75 * elapsed) / 1000.0;
+//            rTri += (90 * elapsed) / 1000.0;
+//            rSquare += (75 * elapsed) / 1000.0;
         }
         lastTime = timeNow;
     }