Exemple #1
0
        public void LoadViewMatrix(JCamera camera)
        {
            Matrix4 viewMatrix = JMathUtils.createViewMatrix(camera);

            base.loadMatrix(location_viewMatrix, viewMatrix);
            base.LoadVector(location_cameraPosition, camera.Position);
        }
        private Vector3 CalculateNormal(int x, int z, float[,] noiseMap)
        {
            if (x == 0)
            {
                x = 1;
            }
            if (z == 0)
            {
                z = 1;
            }
            if (z == (VERTEX_COUNT - 1))
            {
                z = (VERTEX_COUNT - 2);
            }
            if (x == (VERTEX_COUNT - 1))
            {
                x = (VERTEX_COUNT - 2);
            }

            float heightL = JMathUtils.HeightCurve2(noiseMap[x - 1, z]) * MAX_HEIGHT;
            float heightR = JMathUtils.HeightCurve2(noiseMap[x + 1, z]) * MAX_HEIGHT;
            float heightD = JMathUtils.HeightCurve2(noiseMap[x, z - 1]) * MAX_HEIGHT;
            float heightU = JMathUtils.HeightCurve2(noiseMap[x, z + 1]) * MAX_HEIGHT;

            Vector3 normal = new Vector3(heightL - heightR, 2f, heightD - heightU);

            normal.Normalize();
            return(normal);
        }
        public float GetHeightOfTerrain(float worldX, float worldZ)
        {
            float terrainX       = worldX - this.X;
            float terrainZ       = worldZ - this.Z;
            float gridSquareSize = SIZE / ((float)heights.GetLength(0) - 1);
            int   gridX          = (int)Math.Floor(terrainX / gridSquareSize);
            int   gridZ          = (int)Math.Floor(terrainZ / gridSquareSize);

            if (gridX >= heights.GetLength(0) - 1 || gridZ >= heights.GetLength(1) - 1 || gridX < 0 || gridZ < 0)
            {
                return(0);
            }

            float xCoord = (terrainX % gridSquareSize) / gridSquareSize;
            float zCoord = (terrainZ % gridSquareSize) / gridSquareSize;
            float answer;

            if (xCoord <= (1 - zCoord))
            {
                answer = JMathUtils.BarryCentric(new Vector3(0, heights[gridX, gridZ], 0), new Vector3(1,
                                                                                                       heights[gridX + 1, gridZ], 0), new Vector3(0,
                                                                                                                                                  heights[gridX, gridZ + 1], 1), new Vector2(xCoord, zCoord));
            }
            else
            {
                answer = JMathUtils.BarryCentric(new Vector3(1, heights[gridX + 1, gridZ], 0), new Vector3(1,
                                                                                                           heights[gridX + 1, gridZ + 1], 1), new Vector3(0,
                                                                                                                                                          heights[gridX, gridZ + 1], 1), new Vector2(xCoord, zCoord));
            }
            return(answer);
        }
Exemple #4
0
        /// <summary>
        /// Generate a float[mapWidth,mapHeight] containing values in the range [0,1] based on PerlinNoise algorithm.
        /// </summary>
        /// <param name="mapWidth"></param>
        /// <param name="mapHeight"></param>
        /// <param name="scale"></param>
        /// <param name="octaves">The number of octives. Must be greater than 0.</param>
        /// <param name="persistance">A value, in the range 0 to 1, commonly 0.5.</param>
        /// <param name="lacunarity">A value, greater than 1, commonly 2.</param>
        /// <returns>A float array of "randomly" distributed values.</returns>
        public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, float scale, int octaves, float persistance, float lacunarity)
        {
            float[,] noiseMap = new float[mapWidth, mapHeight];

            float maxNoiseHeight = float.MinValue;
            float minNoiseHeight = float.MaxValue;

            if (scale <= 0)
            {
                scale = 0.0001f;
            }

            for (int y = 0; y < mapHeight; y++)
            {
                for (int x = 0; x < mapWidth; x++)
                {
                    float amplitude   = 1;
                    float frequency   = 1;
                    float noiseHeight = 0;

                    for (int i = 0; i < octaves; i++)
                    {
                        float sampleX = (x / scale) * frequency;
                        float sampleY = (y / scale) * frequency;

                        float perlinValue = (float)JPerlinNoise.GetNoise((double)sampleX, (double)sampleY, (double)1.0f) * 2 - 1;
                        noiseHeight += perlinValue * amplitude;

                        // Amplitude decreases each octive.
                        amplitude *= persistance;

                        // Frequency increases each octive.
                        frequency *= lacunarity;
                    }

                    // Track the minimum and maximum values within noiseMap.
                    if (noiseHeight > maxNoiseHeight)
                    {
                        maxNoiseHeight = noiseHeight;
                    }
                    else if (noiseHeight < minNoiseHeight)
                    {
                        minNoiseHeight = noiseHeight;
                    }

                    noiseMap[x, y] = noiseHeight;
                }
            }

            // Map the noiseMap values to the range [0,1].
            for (int y = 0; y < mapHeight; y++)
            {
                for (int x = 0; x < mapWidth; x++)
                {
                    noiseMap[x, y] = JMathUtils.InvLerp(minNoiseHeight, maxNoiseHeight, noiseMap[x, y]);
                }
            }

            return(noiseMap);
        }
        private void prepareInstance(JEntity entity)
        {
            //Matrix4 transformationMatrix = JMathUtils.createTransformationMatrix(entity.Position, entity.RotX, entity.RotY, entity.RotZ, entity.Scale);
            //JMathUtils.testTransformationMatrix(entity.Position, entity.Orientation, entity.Scale);
            Matrix4 transformationMatrix = JMathUtils.testTransformationMatrix(entity.Position, entity.Orientation, entity.Scale);

            Shader.LoadTransformationMatrix(transformationMatrix);
        }
        private JRawModel generateTerrain(JLoader loader)
        {
            Console.WriteLine("Generating terrain...");
            float[,] test = JNoise.GenerateNoiseMap(100, 100, 20.0f, 4, 0.5f, 2.0f);

            int count = VERTEX_COUNT * VERTEX_COUNT;

            heights = new float[VERTEX_COUNT, VERTEX_COUNT];
            float[] vertices      = new float[count * 3];
            float[] normals       = new float[count * 3];
            float[] textureCoords = new float[count * 2];
            uint[]  indices       = new uint[6 * (VERTEX_COUNT - 1) * (VERTEX_COUNT - 1)];
            int     vertexPointer = 0;

            for (int i = 0; i < VERTEX_COUNT; i++)
            {
                for (int j = 0; j < VERTEX_COUNT; j++)
                {
                    vertices[vertexPointer * 3] = (float)j / ((float)VERTEX_COUNT - 1) * SIZE;
                    float height = JMathUtils.HeightCurve2(test[j, i]) * MAX_HEIGHT;
                    vertices[vertexPointer * 3 + 1] = height;
                    heights[j, i] = height;
                    vertices[vertexPointer * 3 + 2] = (float)i / ((float)VERTEX_COUNT - 1) * SIZE;
                    Vector3 normal = CalculateNormal(j, i, test);
                    normals[vertexPointer * 3]           = normal.X;
                    normals[vertexPointer * 3 + 1]       = normal.Y;
                    normals[vertexPointer * 3 + 2]       = normal.Z;
                    textureCoords[vertexPointer * 2]     = (float)j / ((float)VERTEX_COUNT - 1);
                    textureCoords[vertexPointer * 2 + 1] = (float)i / ((float)VERTEX_COUNT - 1);
                    vertexPointer++;
                }
            }
            int pointer = 0;

            for (uint gz = 0; gz < VERTEX_COUNT - 1; gz++)
            {
                for (uint gx = 0; gx < VERTEX_COUNT - 1; gx++)
                {
                    uint topLeft     = (uint)(gz * VERTEX_COUNT) + gx;
                    uint topRight    = topLeft + 1;
                    uint bottomLeft  = (uint)((gz + 1) * VERTEX_COUNT) + gx;
                    uint bottomRight = bottomLeft + 1;
                    indices[pointer++] = topLeft;
                    indices[pointer++] = bottomLeft;
                    indices[pointer++] = topRight;
                    indices[pointer++] = topRight;
                    indices[pointer++] = bottomLeft;
                    indices[pointer++] = bottomRight;
                }
            }

            Console.WriteLine("Terrain generation complete...");

            return(loader.LoadToVAO(vertices, textureCoords, normals, indices));
        }
Exemple #7
0
 public void Render(List <JWaterTile> waterTiles, JCamera camera, JLight light)
 {
     PrepareRender(camera, light);
     foreach (JWaterTile tile in waterTiles)
     {
         Matrix4 modelMatrix = JMathUtils.createTransformationMatrix(new Vector3(tile.X, tile.Height, tile.Z), 0, 0, 0, JWaterTile.TILE_SIZE);
         WaterShader.LoadModelMatrix(modelMatrix);
         GL.DrawArrays(PrimitiveType.Triangles, 0, WaterQuad.vertexCount);
     }
     Unbind();
 }
        public JMasterRenderer(JTerrainTexturePack texturePack)
        {
            terrains = new List <JPerlinTerrain>();
            skyRed   = 0.5f;
            skyGreen = 0.5f;
            skyBlue  = 0.5f;

            GL.Enable(EnableCap.CullFace);
            GL.CullFace(CullFaceMode.Back);
            this.projectionMatrix = JMathUtils.createProjectionMatrix(JConfig.FOV, JConfig.NEAR_PLANE, JConfig.FAR_PLANE);
            this.Shader           = new JStaticShader();
            this.TerrainShader    = new JTerrainShader();
            this.Renderer         = new JEntityRenderer(Shader, projectionMatrix);
            this.TerrainRenderer  = new JTerrainRenderer(TerrainShader, projectionMatrix, texturePack);
            this.Entities         = new Dictionary <JTexturedModel, List <JEntity> >();
        }
        private void LoadModelMatrix(JPerlinTerrain terrain)
        {
            Matrix4 transformationMatrix = JMathUtils.createTransformationMatrix(new Vector3(terrain.X, 0, terrain.Z), 0, 0, 0, 1);

            TerrainShader.LoadTransformationMatrix(transformationMatrix);
        }
Exemple #10
0
        /// <summary>
        /// Use an offset to sample JPerlinNoise at a random location rather than a consistent location. Not working quite right yet.
        /// </summary>
        /// <param name="mapWidth"></param>
        /// <param name="mapHeight"></param>
        /// <param name="seed"></param>
        /// <param name="scale"></param>
        /// <param name="octaves">The number of octives. Must be greater than 0.</param>
        /// <param name="persistance">A value, in the range 0 to 1, commonly 0.5.</param>
        /// <param name="lacunarity">A value, greater than 1, commonly 2.</param>
        /// <returns></returns>
        public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, int seed, float scale, int octaves, float persistance, float lacunarity)
        {
            float[,] noiseMap = new float[mapWidth, mapHeight];

            float maxNoiseHeight = float.MinValue;
            float minNoiseHeight = float.MaxValue;

            Random rand = new Random(seed);

            Vector2[] octaveOffsets = new Vector2[octaves];

            for (int i = 0; i < octaves; i++)
            {
                float offsetX = rand.Next(-5, 5) / scale;
                float offsetY = rand.Next(-5, 5) / scale;
                octaveOffsets[i] = new Vector2(offsetX, offsetY);
            }

            if (scale <= 0)
            {
                scale = 0.0001f;
            }

            for (int y = 0; y < mapHeight; y++)
            {
                for (int x = 0; x < mapWidth; x++)
                {
                    float amplitude   = 1;
                    float frequency   = 1;
                    float noiseHeight = 0;

                    for (int i = 0; i < octaves; i++)
                    {
                        float sampleX = (x / scale) * frequency + octaveOffsets[i].X;
                        float sampleY = (y / scale) * frequency + octaveOffsets[i].Y;

                        float perlinValue = (float)JPerlinNoise.GetNoise((double)sampleX, (double)sampleY, (double)1.0f) * 2 - 1;
                        noiseHeight += perlinValue * amplitude;

                        // Amplitude decreases each octive.
                        amplitude *= persistance;

                        // Frequency increases each octive.
                        frequency *= lacunarity;
                    }

                    // Track the minimum and maximum values with noiseMap.
                    if (noiseHeight > maxNoiseHeight)
                    {
                        maxNoiseHeight = noiseHeight;
                    }
                    else if (noiseHeight < minNoiseHeight)
                    {
                        minNoiseHeight = noiseHeight;
                    }

                    noiseMap[x, y] = noiseHeight;
                }
            }

            for (int y = 0; y < mapHeight; y++)
            {
                for (int x = 0; x < mapWidth; x++)
                {
                    noiseMap[x, y] = JMathUtils.InvLerp(minNoiseHeight, maxNoiseHeight, noiseMap[x, y]);
                }
            }

            return(noiseMap);
        }
Exemple #11
0
        public void LoadViewMatrix(JCamera camera)
        {
            Matrix4 viewMatrix = JMathUtils.createViewMatrix(camera);

            base.loadMatrix(location_viewMatrix, viewMatrix);
        }