Example #1
0
        public void setFragmentModelWithShader(ShaderProgram shader, ChunkFragmentModel model)
        {
            //if (fragmentPerShader.ContainsKey(shader))
            fragmentPerShader.Remove(shader);

            fragmentPerShader.Add(shader, model);
            shaders.Add(shader);
        }
Example #2
0
        public ChunkModel generateModel(World w, ChunkModel previousChunkModel)
        {
            var            possibleDirections = (EnumFacing[])Enum.GetValues(typeof(EnumFacing));
            List <RawQuad> quads;

            var l_x = 16;
            var l_y = 256;
            var l_z = 16;

            var MODEL_RAW = new Dictionary <ShaderProgram, List <RawQuad> >();

            //generate the model / fill MODEL_RAW
            for (int z = 0; z < l_z; z++)
            {
                for (int y = 0; y < l_y; y++)
                {
                    for (int x = 0; x < l_x; x++)
                    {
                        var pos = new BlockPos(x, y, z);

                        var block = getBlock(w, pos);

                        if (block == EnumBlock.AIR)
                        {
                            continue;
                        }

                        var blockModel = ModelManager.getModelForBlock(block, getMetadata(w, pos));

                        if (!MODEL_RAW.TryGetValue(blockModel.shader, out quads))
                        {
                            MODEL_RAW.Add(blockModel.shader, quads = new List <RawQuad>());
                        }

                        for (int i = 0; i < possibleDirections.Length; i++)
                        {
                            var dir     = possibleDirections[i];
                            var block_o = getBlock(w, pos.offset(dir));

                            if (block_o == EnumBlock.AIR || (block_o == EnumBlock.GLASS && block != EnumBlock.GLASS))
                            {
                                /*if (isBlockAbove(pos)) //TODO: Lighting
                                 * {
                                 * }*/

                                var quad = ((RawBlockModel)blockModel.rawModel)?.getQuadForSide(dir)?.offset(pos);

                                if (quad != null)
                                {
                                    quads.Add(quad);
                                }
                            }
                        }
                    }
                }
            }

            var previousShaders = previousChunkModel.getShadersPresent();

            var finish = new ThreadLock(() =>
            {
                var newShaders = MODEL_RAW.Keys.ToArray();

                for (int i = 0; i < previousShaders.Count; i++)
                {
                    var oldShader = previousShaders[i];

                    if (!newShaders.Contains(oldShader))
                    {
                        previousChunkModel.getFragmentModelWithShader(oldShader).overrideData(new List <RawQuad>());
                    }
                }

                foreach (var value in MODEL_RAW)
                {
                    var newShader = value.Key;
                    var newData   = value.Value;

                    if (!previousShaders.Contains(newShader))
                    {
                        var newFragment = new ChunkFragmentModel(newShader, newData);
                        previousChunkModel.setFragmentModelWithShader(newShader, newFragment);
                    }
                    else
                    {
                        previousChunkModel.getFragmentModelWithShader(newShader).overrideData(newData);
                    }
                }
            });

            Game.MAIN_THREAD_QUEUE.Add(finish);
            finish.WaitFor();

            return(previousChunkModel);
        }