From 303636788d5e6ae546f4581f772e71309e3bcffc Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 28 May 2011 13:58:39 +0200 Subject: Terrain changes, added water. --- scene.cpp | 57 ++++++++++++++++++++++++++++++++++++++++ scene.h | 3 ++- shaders/terrain_fragment.glsl | 17 +++++++----- shaders/water_fragment.glsl | 13 +++++++++ terrain_loader.cpp | 6 +++-- textures/zooboing-688-water.jpg | Bin 0 -> 271748 bytes 6 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 shaders/water_fragment.glsl create mode 100644 textures/zooboing-688-water.jpg diff --git a/scene.cpp b/scene.cpp index 352893e..c55c2dc 100644 --- a/scene.cpp +++ b/scene.cpp @@ -35,6 +35,11 @@ Scene::Scene() { terrain_program.attach(terrain_fragment); terrain_program.link(); + GLFragmentShader water_fragment("shaders/water_fragment.glsl"); + water_program.attach(terrain_vertex); + water_program.attach(water_fragment); + water_program.link(); + terrain_program.use(); GLint tex = glGetUniformLocation(terrain_program.get_program(), "tex"); GLint texv[] = {0, 1, 2}; @@ -47,6 +52,7 @@ Scene::Scene() { grass_texture = load_texture("textures/zooboing-366-grass.jpg"); rock_texture = load_texture("textures/zooboing-825-stone-modified.jpg"); soil_texture = load_texture("textures/zooboing-469-sand-modified.jpg"); + water_texture = load_texture("textures/zooboing-688-water.jpg"); marker_texture = load_texture("textures/cross.png"); /* init terrain */ @@ -418,8 +424,59 @@ void Scene::render() { glDisable(GL_TEXTURE_2D); // set active to texture0 to avoid breaking the texture font glActiveTexture(GL_TEXTURE0); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + water_program.use(); + chunk_pos = glGetUniformLocation(water_program.get_program(), "chunk_pos"); + player_pos = glGetUniformLocation(water_program.get_program(), "player_pos"); + glUniform3f(player_pos, pos.x, pos.y, pos.z); + + for(std::list::iterator it = terrain->chunks.begin(); it != terrain->chunks.end(); it++) { + Terrain::Chunk *chunk = *it; + glPushMatrix(); + glTranslatef(-pos.x + chunk->x, -pos.y, -pos.z + chunk->y); + glUniform2f(chunk_pos, chunk->x, chunk->y); + + float v[4*3]; + /*v[0] = v[2] = v[3] = v[9] = v[11] = v[17] = 0; + v[5] = v[6] = v[8] = v[12] = v[14] = v[15] = terrain->chunk_size; + v[1] = v[4] = v[7] = v[10] = v[13] = v[16] = 30;*/ + v[0] = v[2] = v[3] = v[11] = 0; + v[5] = v[6] = v[8] = v[9] = terrain->chunk_size; + v[1] = v[4] = v[7] = v[10] = 30; + + float n[4*3]; + /*n[0] = n[2] = n[3] = n[5] = n[6] = n[8] = n[9] = n[11] = n[12] = n[14] = n[15] = n[17]= 0; + n[1] = n[4] = n[7] = n[10] = n[13] = n[16] = 1;*/ + n[0] = n[2] = n[3] = n[5] = n[6] = n[8] = n[9] = n[11] = 0; + n[1] = n[4] = n[7] = n[10] = 1; + + float tc[4*2]; + /*tc[0] = tc[1] = tc[2] = tc[6] = tc[7] = tc[11] = 0; + tc[3] = tc[4] = tc[5] = tc[8] = tc[9] = tc[10] = 1;*/ + tc[0] = tc[1] = tc[2] = tc[7] = 0; + tc[3] = tc[4] = tc[5] = tc[6] = terrain->chunk_size / 2; + + glBindTexture(GL_TEXTURE_2D, water_texture); + glVertexPointer(3, GL_FLOAT, 0, v); + glNormalPointer(GL_FLOAT, 0, n); + glTexCoordPointer(2, GL_FLOAT, 0, tc); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glDrawArrays(GL_QUADS, 0, 4); + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + + glPopMatrix(); + } + glDisable(GL_BLEND); + glUseProgram(0); } + if(grid) { if(terrain) glColor3f(0, 0, 0); diff --git a/scene.h b/scene.h index 7d68583..9e3ddb7 100644 --- a/scene.h +++ b/scene.h @@ -39,8 +39,9 @@ class Scene { int sx, sy; GLShaderProgram terrain_program; + GLShaderProgram water_program; - GLuint grass_texture, rock_texture, soil_texture, marker_texture; + GLuint grass_texture, rock_texture, soil_texture, water_texture, marker_texture; Scene(); ~Scene(); diff --git a/shaders/terrain_fragment.glsl b/shaders/terrain_fragment.glsl index aa2de9b..7197883 100644 --- a/shaders/terrain_fragment.glsl +++ b/shaders/terrain_fragment.glsl @@ -11,14 +11,19 @@ void main() { vec3 n = normalize(normal); float diffuse = max(dot(n, light_pos), 0.5); - float f1 = clamp((pos.y-15.0) / 3.0, 0.0, 1.0); - float f2 = clamp((pos.y-2) / 3.0, 0.0, 1.0); + //float f1 = clamp((pos.y-15.0) / 3.0, 0.0, 1.0); + //float f2 = clamp((pos.y-2) / 3.0, 0.0, 1.0); + float f1 = clamp((pos.y-60.0) / 3.0, 0.0, 1.0); + float f2 = clamp((pos.y-30) / 3.0, 0.0, 1.0); - vec4 t0 = texture2D(tex[0], gl_TexCoord[0].st); - vec4 t1 = texture2D(tex[1], gl_TexCoord[0].st); - vec4 t2 = texture2D(tex[2], gl_TexCoord[0].st); + vec4 grass = texture2D(tex[0], gl_TexCoord[0].st); + vec4 rock = texture2D(tex[1], gl_TexCoord[0].st); + vec4 soil = texture2D(tex[2], gl_TexCoord[0].st); - gl_FragColor = mix(mix(t2, t0, f2), t1, f1) * diffuse; + gl_FragColor = mix(mix(soil, grass, f2), rock, f1) * diffuse; + + //gl_FragColor = mix(gl_FragColor, gl_FragColor*vec4(0, 0, 1, 0), clamp(31 - pos.y, 0.0, 1.0)); + //gl_FragColor += vec4(0, 0, 1, clamp(31 - pos.y, 0.0, 1.0)); /* selection marker */ if(show_sel && diff --git a/shaders/water_fragment.glsl b/shaders/water_fragment.glsl new file mode 100644 index 0000000..857a500 --- /dev/null +++ b/shaders/water_fragment.glsl @@ -0,0 +1,13 @@ +#version 120 + +varying vec3 pos; +uniform vec3 player_pos; +uniform sampler2D tex; + +void main() { + gl_FragColor = texture2D(tex, gl_TexCoord[0].st) - vec4(.2, .2, 0, 0); + gl_FragColor = mix(gl_FragColor, gl_Fog.color, pow(length(player_pos - pos)/100, 5)); + gl_FragColor.a = 0.7; +} + +/* vim: set syn=glsl: */ diff --git a/terrain_loader.cpp b/terrain_loader.cpp index d908aca..6374a7a 100644 --- a/terrain_loader.cpp +++ b/terrain_loader.cpp @@ -16,6 +16,7 @@ TerrainLoader::~TerrainLoader() { } float *TerrainLoader::generate_heights(int x, int y, int width, int height) { + const double scale_factor = 0.004; module::Perlin mod; mod.SetSeed(seed); @@ -26,13 +27,14 @@ float *TerrainLoader::generate_heights(int x, int y, int width, int height) { heightmap_builder.SetDestNoiseMap(heightmap); heightmap_builder.SetDestSize(width, height); - heightmap_builder.SetBounds((double)x / 100, (double)(x+width) / 100, (double)y / 100, (double)(y+height) / 100); + heightmap_builder.SetBounds((double)x * scale_factor, (double)(x+width) * scale_factor, + (double)y * scale_factor, (double)(y+height) * scale_factor); heightmap_builder.Build(); float *heights = new float[width*height]; for(int i = 0; i < width; i++) { for(int j = 0; j < height; j++) { - heights[i*height + j] = 10*(1+heightmap.GetValue(i, j)); + heights[i*height + j] = 50*(1+heightmap.GetValue(i, j)); } } diff --git a/textures/zooboing-688-water.jpg b/textures/zooboing-688-water.jpg new file mode 100644 index 0000000..e9fe890 Binary files /dev/null and b/textures/zooboing-688-water.jpg differ -- cgit v1.2.3