Changeset 2133
- Timestamp:
- Dec 7, 2012, 12:19:22 AM (10 years ago)
- Location:
- trunk/tutorial
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tutorial/04_texture.cpp
r2002 r2133 1 1 // 2 // Lol Engine - Noisetutorial2 // Lol Engine - Graphing tutorial 3 3 // 4 4 // Copyright: (c) 2012 Sam Hocevar <sam@hocevar.net> … … 19 19 using namespace lol; 20 20 21 static int const TEXTURE_WIDTH = 128; 22 21 23 extern char const *lolfx_04_texture; 22 24 … … 35 37 m_vertices << vec2( 1.0, 1.0); 36 38 37 m_heightmap = new uint8_t[4 * 512* 1];39 m_heightmap = new uint8_t[4 * TEXTURE_WIDTH * 1]; 38 40 } 39 41 … … 47 49 WorldEntity::TickGame(seconds); 48 50 49 /* Generate a new heightmap every 400 frames */ 50 if (m_frames % 400 == 0) 51 { 52 for (int i = 0, height = 64; i < 512; i++) 53 { 54 m_heightmap[4 * i] = height; 55 m_heightmap[4 * i + 1] = 255; /* unused */ 56 m_heightmap[4 * i + 2] = 255; /* unused */ 57 m_heightmap[4 * i + 3] = 255; /* unused */ 58 59 height += rand() % 17 - 8; 60 height += rand() % 17 - 8; 61 height = std::max(15, std::min(height, 240)); 62 } 63 } 51 /* Generate a new heightmap at the beginning */ 52 if (m_frames == 0) 53 memset(m_heightmap, 255, 4 * TEXTURE_WIDTH); 64 54 65 /* Slightly disturb the terrain */ 66 for (int i = 1; i < 511; i++) 67 { 68 int delta = (rand() & 1) ? 1 : -1; 55 /* Scroll left */ 56 for (int i = 0; i < TEXTURE_WIDTH - 1; i++) 57 m_heightmap[4 * i] = m_heightmap[4 * i + 4]; 69 58 70 if (rand() & 3) 71 continue; 72 73 uint8_t ¢er = m_heightmap[4 * i]; 74 uint8_t &side1 = m_heightmap[4 * (i - delta)]; 75 uint8_t &side2 = m_heightmap[4 * (i + delta)]; 76 77 if (center > side1) 78 { 79 center--; 80 side1++; 81 } 82 else if (center > side2) 83 { 84 center--; 85 side2++; 86 } 87 } 59 int height = m_heightmap[4 * (TEXTURE_WIDTH - 1)]; 60 height = height / 2 + 255 / 4 + rand() % 97 - 48; 61 height = std::max(15, std::min(height, 240)); 62 m_heightmap[4 * (TEXTURE_WIDTH - 1)] = height; 88 63 89 64 /* Update frame counter */ … … 98 73 if (!m_ready) 99 74 { 100 m_texture = new Texture(ivec2( 512, 1), PixelFormat::A8R8G8B8);75 m_texture = new Texture(ivec2(TEXTURE_WIDTH, 1), PixelFormat::A8R8G8B8); 101 76 102 77 m_shader = Shader::Create(lolfx_04_texture); -
trunk/tutorial/04_texture.lolfx
r2050 r2133 26 26 } 27 27 28 float point2segment(vec2 p1, vec2 p2, vec2 a) 29 { 30 float l2 = dot(p2 - p1, p2 - p1); 31 if (l2 == 0.0) 32 return distance(a, p1); 33 float t = dot(a - p1, p2 - p1) / l2; 34 if (t < 0.0) 35 return distance(a, p1); 36 else if (t > 1.0) 37 return distance(a, p2); 38 vec2 proj = p1 + t * (p2 - p1); 39 return distance(a, proj); 40 } 41 42 28 43 void main(void) 29 44 { 45 float width = 800.0; 46 float height = 600.0; 47 float line_width = 2.0; 48 30 49 vec2 t = pass_Position.xy; 31 vec4 c0 = texture2D(u_Texture, t); 32 float f = rand(pass_Position.xy, 12345.67); 50 vec2 tc1 = floor(t * 128.0) / 128.0; 51 vec2 tc2 = tc1 + vec2(1.0, 1.0) / 128.0; 52 vec2 tc0 = tc1 - vec2(1.0, 1.0) / 128.0; 53 vec2 tc3 = tc2 + vec2(1.0, 1.0) / 128.0; 33 54 34 if (t.y > c0.x) 35 { 36 /* Sky */ 37 float val = min(t.y * 2.0, 1.0); 38 if (f > 0.999) 39 gl_FragColor = vec4(1.0); 40 else 41 gl_FragColor = vec4(0.4, t.y, val, 1.0); 42 } 43 else if (t.y > c0.x - 0.03) 44 { 45 /* Grass */ 46 if (f > 0.99) 47 gl_FragColor = vec4(0.4, 0.7, 0.3, 1.0); 48 else if (f > 0.9) 49 gl_FragColor = vec4(0.3, 0.6, 0.2, 1.0); 50 else 51 gl_FragColor = vec4(0.2, 0.5, 0.1, 1.0); 52 } 53 else 54 { 55 /* Earth */ 56 if (f > 0.99) 57 gl_FragColor = vec4(0.7, 0.4, 0.3, 1.0); 58 else if (f > 0.9) 59 gl_FragColor = vec4(0.6, 0.3, 0.2, 1.0); 60 else 61 gl_FragColor = vec4(0.5, 0.2, 0.1, 1.0); 62 } 55 float c0 = texture2D(u_Texture, tc0).x; 56 float c1 = texture2D(u_Texture, tc1).x; 57 float c2 = texture2D(u_Texture, tc2).x; 58 float c3 = texture2D(u_Texture, tc3).x; 59 60 /* Artificially compress in Y */ 61 c0 *= 0.3; 62 c1 *= 0.3; 63 c2 *= 0.3; 64 c3 *= 0.3; 65 66 vec2 p0 = vec2(tc0.x * width, c0 * height); 67 vec2 p1 = vec2(tc1.x * width, c1 * height); 68 vec2 p2 = vec2(tc2.x * width, c2 * height); 69 vec2 p3 = vec2(tc3.x * width, c3 * height); 70 vec2 a = vec2(t.x * width, t.y * height); 71 72 float d0 = point2segment(p0, p1, a); 73 float d1 = point2segment(p1, p2, a); 74 float d2 = point2segment(p2, p3, a); 75 76 float d = clamp(line_width - min(min(d0, d1), d2), 0.0, 1.0); 77 78 gl_FragColor = vec4(t.y, d, d * 0.3, 1.0); 63 79 } 64 80
Note: See TracChangeset
for help on using the changeset viewer.