Beispiel #1
0
        public void CalcNormals(TerrainBlock[,] blocks)
        {
            bool not_above = J - 1 < 0;
            bool not_below = J + 1 >= blocks.GetLength(0);
            bool not_left = I - 1 < 0;
            bool not_right = I + 1 >= blocks.GetLength(1);

            Vector3 top_r = not_above ? Vector3.Zero : blocks[I, J - 1].br_helper ;
            Vector3 top_l = not_above ? Vector3.Zero : blocks[I, J - 1].bl_helper ;
            Vector3 right_t = not_right ? Vector3.Zero : blocks[I + 1, J].tl_helper ;
            Vector3 right_b = not_right ? Vector3.Zero : blocks[I + 1, J].bl_helper ;

            Vector3 bot_r = not_below ? Vector3.Zero : blocks[I, J + 1].tr_helper ;
            Vector3 bot_l = not_below ? Vector3.Zero : blocks[I, J + 1].tl_helper ;
            Vector3 left_t = not_left ? Vector3.Zero : blocks[I - 1, J].tr_helper ;
            Vector3 left_b = not_left ? Vector3.Zero : blocks[I - 1, J].br_helper ;

            Vector3 tl = not_above || not_left ? Vector3.Zero : blocks[I -1, J-1].br_helper ;
            Vector3 tr = not_above || not_right ? Vector3.Zero : blocks[I +1, J-1].bl_helper ;
            Vector3 bl = not_below || not_left ? Vector3.Zero : blocks[I -1, J+1].tr_helper ;
            Vector3 br = not_below || not_right ? Vector3.Zero : blocks[I + 1, J+1].tl_helper ;

            if (type == Diagonal.tlbr) {
                //top triangle
                Vector3 ttr = top.normal + top_r + tr + right_t ;
                Vector3 ttl = top.normal + bottom.normal + top_l + tl + left_t ;
                Vector3 tbr = top.normal + bottom.normal + bot_r + br + right_b ;

                top.CalcNormals(ttr, ttl, tbr);

                //bottom triangle
                Vector3 btl = ttl;
                Vector3 bbl = bottom.normal + bot_l + bl + left_b ;
                Vector3 bbr = tbr;

                bottom.CalcNormals(btl, bbl, bbr);
            }
            else {
                //bottom triangle
                Vector3 btr = top.normal + bottom.normal + top_r + tr + right_t ;
                Vector3 bbl = top.normal + bottom.normal + bot_l + bl + left_b ;
                Vector3 bbr = bottom.normal + bot_r + br + right_b ;

                bottom.CalcNormals(btr, bbl, bbr);

                //top triangle
                Vector3 ttr = btr;
                Vector3 ttl = top.normal + top_l + tl + left_t ;
                Vector3 tbl = bbl;

                top.CalcNormals(ttr,ttl, tbl);
            }
        }
Beispiel #2
0
        public void GenerateTerrainFromNoise()
        {
            int width = terrain_vertices.GetLength(0);
            int height = terrain_vertices.GetLength(1);

            //create terrain
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    foreach (var item in noise_gen) {
                        if (item.add)
                            terrain_vertices[i,j] += item.getNoise(i,j);
                        else
                            terrain_vertices[i,j] *= item.getNoise(i,j);
                    }

                    terrain_vertices[i,j] *= scale;

                    if (i > 0 && j > 0) {
                        //make and store info about terrain from vertices
                        terrain_blocks[i-1,j-1] = new TerrainBlock(5,
                            terrain_vertices[i-1, j-1], terrain_vertices[i, j-1],
                            terrain_vertices[i-1, j], terrain_vertices[i, j],
                            i-1, j-1, scale);
                    }
                }
            }

            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    if (i > 0 && j > 0) {
                        //compute normals for gouraud shading
                        terrain_blocks[i - 1, j - 1].CalcNormals(terrain_blocks);
                        //discover neighbours for all terrain triangles
                        terrain_blocks[i - 1, j - 1].DiscoverNeighbours(terrain_blocks);
                    }
                }
            }
        }
Beispiel #3
0
        public void DiscoverNeighbours(TerrainBlock[,] blocks)
        {
            bool not_above = J - 1 < 0;
            bool not_below = J + 1 >= blocks.GetLength(0);
            bool not_left = I - 1 < 0;
            bool not_right = I + 1 >= blocks.GetLength(1);

            if (type == Diagonal.tlbr)
            {
                top.neighbours[0] = not_above ? null : blocks[I, J - 1].bottom;
                top.neighbours[1] = bottom;
                top.neighbours[2] = not_right ? null : blocks[I + 1, J].Left();

                bottom.neighbours[0] = not_left ? null : blocks[I - 1, J].Right();
                bottom.neighbours[1] = not_below? null : blocks[I, J + 1].top;
                bottom.neighbours[2] = top;
            }
            else
            {
                top.neighbours[0] = not_above ? null : blocks[I, J - 1].bottom;
                top.neighbours[1] = not_left ? null : blocks[I - 1, J].Right();
                top.neighbours[2] = bottom;

                bottom.neighbours[0] = top;
                bottom.neighbours[1] = not_below ? null : blocks[I, J + 1].top ;
                bottom.neighbours[2] = not_right ? null : blocks[I + 1, J].Left();
            }
        }