Results 1 to 3 of 3

Thread: Open Box Problem

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    9

    Open Box Problem

    I feel kinda stupid but I can't seem to figure out why this isn't working. I just wanted to create an open box (cube with no top). Working off of the tutorial at http://learningwebgl.com/blog/?p=370 I edited the code to remove the top face of the cube like such. all I did was comment out the sections pertaining to the top face and then adjusted the size of the arrays. But when I run nothing draws. I'm at a loss and its probably something stupid but any help is appreciated.

    Code :
        cubeVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
        vertices = [
          // Front face
          -1.0, -1.0,  1.0,
           1.0, -1.0,  1.0,
           1.0,  1.0,  1.0,
          -1.0,  1.0,  1.0,
     
          // Back face
          -1.0, -1.0, -1.0,
          -1.0,  1.0, -1.0,
           1.0,  1.0, -1.0,
           1.0, -1.0, -1.0,
     
          // Top face
          //-1.0,  1.0, -1.0,
          //-1.0,  1.0,  1.0,
           //1.0,  1.0,  1.0,
           //1.0,  1.0, -1.0,
     
          // Bottom face
          -1.0, -1.0, -1.0,
           1.0, -1.0, -1.0,
           1.0, -1.0,  1.0,
          -1.0, -1.0,  1.0,
     
          // Right face
           1.0, -1.0, -1.0,
           1.0,  1.0, -1.0,
           1.0,  1.0,  1.0,
           1.0, -1.0,  1.0,
     
          // Left face
          -1.0, -1.0, -1.0,
          -1.0, -1.0,  1.0,
          -1.0,  1.0,  1.0,
          -1.0,  1.0, -1.0,
        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
        cubeVertexPositionBuffer.itemSize = 3;
        cubeVertexPositionBuffer.numItems = 20;
     
        cubeVertexColorBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexColorBuffer);
        var colors = [
          [1.0, 0.0, 0.0, 1.0],     // Front face
          [1.0, 1.0, 0.0, 1.0],     // Back face
          //[0.0, 1.0, 0.0, 1.0],     // Top face
          [1.0, 0.5, 0.5, 1.0],     // Bottom face
          [1.0, 0.0, 1.0, 1.0],     // Right face
          [0.0, 0.0, 1.0, 1.0],     // Left face
        ];
        var unpackedColors = []
        for (var i in colors) {
          var color = colors[i];
          for (var j=0; j < 4; j++) {
            unpackedColors = unpackedColors.concat(color);
          }
        }
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColors), gl.STATIC_DRAW);
        cubeVertexColorBuffer.itemSize = 4;
        cubeVertexColorBuffer.numItems = 20;
     
        cubeVertexIndexBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
        var cubeVertexIndices = [
          0, 1, 2,      0, 2, 3,    // Front face
          4, 5, 6,      4, 6, 7,    // Back face
          //8, 9, 10,     8, 10, 11,  // Top face
          12, 13, 14,   12, 14, 15, // Bottom face
          16, 17, 18,   16, 18, 19, // Right face
          20, 21, 22,   20, 22, 23  // Left face
        ]
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(cubeVertexIndices), gl.STATIC_DRAW);
        cubeVertexIndexBuffer.itemSize = 1;
        cubeVertexIndexBuffer.numItems = 30;

  2. #2
    Senior Member
    Join Date
    May 2010
    Posts
    207

    Re: Open Box Problem

    You need to adjust the numbers in the cubeVertexIndices array. When you removed those four vertices, you caused all of the vertices after that to have index values that are four smaller.

    Incidentally - when you do get it working, you probably won't see the interior surfaces of the cube. Either turn off back-face culling or make more faces inside the box to represent the interior.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    9

    Re: Open Box Problem

    Yep, definite brain fart moment. Thanks.

Similar Threads

  1. Chrome + Spinning Box Offline
    By HaWaN in forum OS & Browser specific questions
    Replies: 6
    Last Post: 03-19-2012, 05:59 PM
  2. problem with the back of a box (building)
    By crischprolch in forum OpenGL ES general technical discussions
    Replies: 9
    Last Post: 03-26-2008, 12:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •