Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /* ## COMPILED C++ ##
  2. // Author: Jackson Luff
  3. // Name: SegmentedGrid.cpp
  4. // Type: CPP File
  5. // Description:
  6. // * This is where the map is
  7. // * created pre-process. This
  8. // * is more efficent then textures.
  9. */
  10.  
  11. void SegmentedGrid::GenPerlin(unsigned int a_dims, float a_dimScale)
  12. {
  13.     // Populate verts with row + cols input
  14.     m_vertices = new VertexData[a_dims * a_dims];
  15.  
  16.     // Set up scale and octave count
  17.     float scale = (1.0f / a_dims) * 6.0f;
  18.     int octaves = 9;
  19.  
  20.     // Iterate through verticies and apply perlin noise
  21.     // (Could be optimized)
  22.     for (unsigned int r = 0; r < a_dims; ++r)
  23.     {
  24.         for (unsigned int c = 0; c < a_dims; ++c)
  25.         {
  26.            float amplitude = 1.f;
  27.            float persistence = 0.2f;
  28.            
  29.            m_vertices[r * a_dims + c].position = glm::vec4((float)(c * a_dimScale), 0, (float)(r * a_dimScale), 1.0f);
  30.            m_vertices[r * a_dims + c].uv = glm::vec2((float)c / a_dims, (float)r / a_dims);
  31.            
  32.            // Iterate through octaves per vert
  33.            // octaves are used to 'tone' the noise
  34.            for (int o = 0; o < octaves; ++o)
  35.            {
  36.               // generate simplex and perlin with scale and frequency
  37.               float freq = powf(4, (float)o);
  38.               float simplex_sample = glm::simplex(glm::vec2((float)r, (float)c) * scale * freq/10.0f) * 0.5f + 0.5f;
  39.               float perlin_sample = glm::perlin(glm::vec2((float)r, (float)c) * scale * freq) * 0.5f + 0.5f;
  40.            
  41.                // Apply simplex and perlin together, to create a blend between the two
  42.                m_vertices[r * a_dims + c].position.y += (perlin_sample+simplex_sample) * amplitude;
  43.                amplitude *= persistence;
  44.            }
  45.            
  46.             // Generate Perlin noise with a height scale based on a ratio (to keep things clean)
  47.             float mapGeneralScale = 5.0f;
  48.             float mapHeightScale = ((a_dims * 150.0f) / 512.0f) * (a_dimScale/3.0f);
  49.             m_vertices[r * a_dims + c].position.y *= mapHeightScale*mapGeneralScale;
  50.             m_vertices[r * a_dims + c].position.x *= mapGeneralScale;
  51.             m_vertices[r * a_dims + c].position.z *= mapGeneralScale;
  52.         }
  53.     }
  54.  
  55. /* ## FRAGMENT SHADER ##
  56. // Author: Jackson Luff
  57. // Name: Terrain.frag
  58. // Type: Fragment Shader
  59. // Description:
  60. // * Here I work with splotching
  61. // * the terrain textures everywhere.
  62. // * If i didn't the texture work
  63. // * would actually look quite bland.
  64. */
  65.  
  66. #version 430
  67.  
  68. in vec3 vPosition;
  69. in vec3 vNormal;
  70. in vec3 vTangent;
  71. in vec3 vBiNormal;
  72. in vec2 vCoords;
  73.  
  74. uniform mat4 World = mat4(0);
  75. uniform float elapsedTime = -1.0;
  76.  
  77. float DiffuseLightPower = 5.0;
  78. vec3 DiffuseLightColour = vec3(1);
  79. float SpecularLightPower = 5.0;
  80. vec3 SpecularLightColour = vec3(1, 0.75, 0.65);
  81. float VCoordScale = 50.0;
  82.  
  83. uniform sampler2D grassDiffuse;
  84. uniform sampler2D grassNormal;
  85. uniform sampler2D stoneDiffuse;
  86. uniform sampler2D stoneNormal;
  87.  
  88. uniform sampler2D bumpyPerlinTexture;
  89. uniform sampler2D smoothPerlinTexture;
  90.  
  91. out vec4 pixelColour;
  92.  
  93. vec3 DiffuseSplotch(in sampler2D t, in vec2 coords, in float baseCoordMulti,
  94.   in float lCoordMulti, in float lAlter)
  95. {
  96.     // Setup textures with varied tile samples
  97.     vec3 data  = texture(t, coords*baseCoordMulti).rgb;
  98.     vec3 firstOverlay   = texture(t, coords*lCoordMulti).rgb;
  99.     vec3 secondOverlay  = texture(t, coords*20.0).rgb;
  100.    
  101.     // Check for light splotching of green
  102.     if(firstOverlay.g > 0.2)
  103.             data += firstOverlay + lAlter;
  104.  
  105.     // Check for heavy splotching of blue
  106.     if(secondOverlay.g > 0.6)
  107.             data += secondOverlay * vec3(0.7);
  108.    
  109.     data = mix(data, data*vec3(0.8), 1);
  110.    
  111.     return data;
  112. }
  113.  
  114. vec3 NormalSplotch(in sampler2D t, in vec2 coords, in float baseCoordMulti,
  115.  in float lSplotch, in float lCoordMulti,
  116.  in float hSplotch, in float hCoordMulti)
  117. {
  118.     // Setup textures with varied tile samples
  119.     vec3 data  = texture(t, coords*baseCoordMulti).rgb * 2 - 1;
  120.     vec3 firstOverlay   = texture(t, coords*lCoordMulti).rgb * 2 - 1;
  121.     vec3 secondOverlay  = texture(t, coords*hCoordMulti).rgb * 2 - 1;
  122.    
  123.     // Check for light splotching of green
  124.     if(firstOverlay.g > lSplotch)
  125.             data = normalize(data + firstOverlay);
  126.        
  127.     // Check for heavy splotching of green
  128.     if(secondOverlay.b > hSplotch)
  129.             data = normalize(data + secondOverlay);
  130.     return data;
  131. }
  132.  
  133. void main()
  134. {
  135.     // Set up light vector
  136.     vec3 lightPosition = vec3(3000 + sin(elapsedTime), 5000, 3000);
  137.     vec3 lightVector = normalize(lightPosition - vPosition);
  138.    
  139.     // Smooth step to allowed gradual use in mix()
  140.     float dotNorm = dot(normalize(vNormal), vec3(0.0,1.0,0.0));
  141.     dotNorm = smoothstep(0.8, 1.0, dotNorm * 0.5 + 0.5);
  142.    
  143.     // Create a Tangent Bi-Normal for accurate normal reads
  144.     mat3 TBN = mat3(
  145.         normalize( vTangent ),
  146.         normalize( vBiNormal ),
  147.         normalize( vNormal ));
  148.    
  149.     // Here we set up the normal information
  150.     // (The grass normal was too intense, so I mashed it up)
  151.     vec3 grassNormalSamp = NormalSplotch(grassNormal, vCoords, VCoordScale, 0.2, 3, 0.6, 10);
  152.     vec3 stoneNormalSamp = texture(stoneNormal, vCoords*VCoordScale).rgb * 2 -1;
  153.     vec3 normalSampData = mix(stoneNormalSamp, grassNormalSamp, dotNorm);
  154.    
  155.     // Set up the diffuse of the texture mapping
  156.     vec3 grassDiffuseSamp = DiffuseSplotch(grassDiffuse, vCoords, VCoordScale, 3, -0.2);
  157.     vec3 stoneDiffuseSamp = DiffuseSplotch(stoneDiffuse, vCoords, VCoordScale, 2, +0.0) * 0.85;
  158.     vec3 diffuseSampData = mix(stoneDiffuseSamp, grassDiffuseSamp, dotNorm);
  159.  
  160.     // Produce lighting based on normal information
  161.     float dNLight = max( 0, dot( TBN * normalSampData, lightVector ));
  162.     vec4 diffLight = vec4(vec3(pow(max( 0, dot( vNormal, lightVector )), 2.0)), 1.0);
  163.    
  164.     // i'm using both lighting sets to get a smooth and effective amount of light
  165.     pixelColour = vec4(diffuseSampData * dNLight, 1.0) * clamp(diffLight, 0.2, 1.0);
  166. }