Advertisement
Guest User

Untitled

a guest
Feb 7th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1.   void load() {
  2.     int totalOut = mHeader.gridX * mHeader.gridY * mHeader.gridZ;
  3.     int numCubes = 0;
  4.    
  5.     mBlocks = new List<M4Block>(totalOut);
  6.    
  7.     Uint8List cubeMask = new Uint8List(totalOut);
  8.     int curOut = 0;
  9.     while(curOut < totalOut) {
  10.       int curByte = mReader.getUInt8();
  11.       if((curByte & 0x80) != 0) {
  12.         int bitMask = curByte & 0x7F;
  13.         int i = 0;
  14.         while(curOut < totalOut && i < 7) {
  15.           if((bitMask & (1 << i)) != 0) {
  16.             cubeMask[curOut] = 1;
  17.             ++numCubes;
  18.           }
  19.          
  20.           ++i;
  21.           ++curOut;
  22.         }
  23.       } else {
  24.         int value = ((curByte & 0x40) >> 6);
  25.         int repeat = curByte & 0x3F;
  26.         int i = 0;
  27.         while(curOut < totalOut && i < repeat) {
  28.           cubeMask[curOut] = value;
  29.           if(value != 0) {
  30.             ++numCubes;
  31.           }
  32.          
  33.           ++i;
  34.           ++curOut;
  35.         }
  36.       }
  37.     }
  38.    
  39.     int cur = 0;
  40.     var rnd = new Random();
  41.    
  42.     mVertexData = new VertexData(28, numCubes * 24);
  43.     mIndices = new Uint32List(numCubes * 36);
  44.     mIndicesWireframe = new Uint32List(numCubes * 48);
  45.     for(int x = 0; x < mHeader.gridX; ++x) {
  46.       for(int y = 0; y < mHeader.gridY; ++y) {
  47.         for(int z = 0; z < mHeader.gridZ; ++z) {
  48.           if(cubeMask[cur] == 0) {
  49.             mBlocks[cur] = null;
  50.             ++cur;
  51.             continue;
  52.           }
  53.          
  54.           M4Block block;
  55.          
  56.           if(mParent.version > 1) {
  57.             var colorIndex = mReader.getUInt8();
  58.             block = new M4Block(mColorTable[colorIndex], colorIndex);
  59.           } else {
  60.             block = new M4Block(0xFFFFFFFF, 0);
  61.           }
  62.          
  63.           mBlocks[cur] = block;
  64.           int r = rnd.nextInt(255);
  65.           int g = rnd.nextInt(255);
  66.           int b = rnd.nextInt(255);
  67.           int a = 0xFF;
  68.           int color = (r & 0xFF) | ((g & 0xFF) << 8) | ((b & 0xFF) << 16) | ((a & 0xFF) << 24);
  69.          
  70.           num cx = mHeader.bboxMin.x + x * mHeader.blockSize;
  71.           num cz = mHeader.bboxMin.y + y * mHeader.blockSize;
  72.           num cy = mHeader.bboxMin.z + z * mHeader.blockSize;
  73.          
  74.           Vector3 bottom = new Vector3(cx, cy, cz);
  75.           Vector3 top = new Vector3(cx + mHeader.blockSize, cy + mHeader.blockSize, cz + mHeader.blockSize);
  76.           this.writeBlock(bottom, top, color);
  77.           ++cur;
  78.         }
  79.       }
  80.     }
  81.    
  82.     mCubeCount = numCubes;
  83.    
  84.     mVertexBuffer.setDataTyped(mVertexData.data);
  85.     mIndexBuffer.setIndices(mIndices);
  86.     mIndexBufferWF.setIndices(mIndicesWireframe);
  87.    
  88.     VertexElement colorElem = new VertexElement(VertexSemantic.Color, 0, 4, type: DataType.Byte);
  89.     colorElem.setNormalized();
  90.    
  91.     mGeometry.getDrawState().BlendEnabled = true;
  92.    
  93.     mGeometry..setProgram(ProgramCollection.instance.get("M4"))
  94.              ..setIndexBuffer(mIndexBuffer)
  95.              ..setVertexBuffer(mVertexBuffer)
  96.              ..setStride(28)
  97.              ..setTriangleCount(numCubes * 12)
  98.              ..setVertexCount(numCubes * 24)
  99.              ..addNewElement(VertexSemantic.Position, 0, 3)
  100.              ..addNewElement(VertexSemantic.Normal, 0, 3)
  101.              ..addElement(colorElem)
  102.              ..finalize();
  103.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement