From 0986cf8f33df8926dd8da67f98ed18f696141313 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 11 Apr 2011 22:06:54 +0200 Subject: Draw a selection marker on the terrain. --- scene.cpp | 25 +++++++++++++------------ scene.h | 2 +- shaders/terrain_fragment.glsl | 18 ++++++++++++++---- shaders/terrain_vertex.glsl | 5 ++--- textures/cross.png | Bin 0 -> 2144 bytes 5 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 textures/cross.png diff --git a/scene.cpp b/scene.cpp index 180c90d..f994627 100644 --- a/scene.cpp +++ b/scene.cpp @@ -35,12 +35,15 @@ Scene::Scene() { glUniform1i(tex1loc, 1); GLint tex2loc = glGetUniformLocation(terrain_program.get_program(), "tex[2]"); glUniform1i(tex2loc, 2); + GLint markloc = glGetUniformLocation(terrain_program.get_program(), "marktex"); + glUniform1i(markloc, 3); glUseProgram(0); /* load textures */ 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"); + marker_texture = load_texture("textures/cross.png"); /* load heightmap */ SDL_Surface *hm = IMG_Load("heightmap.png"); @@ -250,7 +253,6 @@ void Scene::render() { CEGUI::System::getSingleton().injectTimePulse(0.001*(float)steps); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glLoadIdentity(); video::persp(); unsigned char *keystate = SDL_GetKeyState(NULL); @@ -316,6 +318,13 @@ void Scene::render() { unsigned int chunks_rendered = 0; if(terrain) { terrain_program.use(); + GLint show_sel = glGetUniformLocation(terrain_program.get_program(), "show_sel"); + glUniform1i(show_sel, show_selection ? 1 : 0); + if(show_selection) { + GLint selpos = glGetUniformLocation(terrain_program.get_program(), "selpos"); + glUniform3f(selpos, selected.x, selected.y, selected.z); + } + glEnable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE0); @@ -327,6 +336,9 @@ void Scene::render() { glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, soil_texture); + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_2D, marker_texture); + std::queue q; q.push(qt->root); while(!q.empty()) { @@ -386,17 +398,6 @@ void Scene::render() { selected = Vector3(px, py, pz); show_selection = true; } - if(show_selection) { - glColor3f(1, 0, 0); - glBegin(GL_TRIANGLE_FAN); - glVertex3f(selected.x, selected.y, selected.z); - glVertex3f(selected.x+.5, selected.y+1, selected.z-.5); - glVertex3f(selected.x+.5, selected.y+1, selected.z+.5); - glVertex3f(selected.x-.5, selected.y+1, selected.z+.5); - glVertex3f(selected.x-.5, selected.y+1, selected.z-.5); - glVertex3f(selected.x+.5, selected.y+1, selected.z-.5); - glEnd(); - } video::ortho(); float height = font->LineHeight(); diff --git a/scene.h b/scene.h index d494994..95ae158 100644 --- a/scene.h +++ b/scene.h @@ -34,7 +34,7 @@ class Scene { GLShaderProgram terrain_program; - GLuint grass_texture, rock_texture, soil_texture; + GLuint grass_texture, rock_texture, soil_texture, marker_texture; Scene(); ~Scene(); diff --git a/shaders/terrain_fragment.glsl b/shaders/terrain_fragment.glsl index 2d02dc8..0925c6b 100644 --- a/shaders/terrain_fragment.glsl +++ b/shaders/terrain_fragment.glsl @@ -1,22 +1,32 @@ #version 120 -varying vec3 normal, light_pos; -varying float y; +varying vec3 normal, light_pos, pos; uniform sampler2D tex[3]; +uniform sampler2D marktex; +uniform vec3 selpos; +uniform bool show_sel; void main() { vec3 n = normalize(normal); float diffuse = max(dot(n, light_pos), 0.5); - float f1 = clamp((y-15.0) / 3.0, 0.0, 1.0); - float f2 = clamp((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); 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); gl_FragColor = mix(mix(t2, t0, f2), t1, f1) * diffuse; + + /* selection marker */ + if(show_sel && + pos.x >= selpos.x-1 && pos.x <= selpos.x+1 && + pos.z >= selpos.z-1 && pos.z <= selpos.z+1) { + vec2 st = vec2((pos.x + 1 - selpos.x) / 2, (pos.z + 1 - selpos.z) / 2); + gl_FragColor += texture2D(marktex, st); + } } /* vim: set syn=glsl: */ diff --git a/shaders/terrain_vertex.glsl b/shaders/terrain_vertex.glsl index e53c432..5fcd17b 100644 --- a/shaders/terrain_vertex.glsl +++ b/shaders/terrain_vertex.glsl @@ -1,12 +1,11 @@ #version 120 -varying vec3 normal, light_pos; -varying float y; +varying vec3 normal, light_pos, pos; void main() { normal = gl_Normal; light_pos = vec3(0, 1, 0); - y = gl_Vertex.y; + pos = gl_Vertex.xyz; gl_TexCoord[0] = gl_MultiTexCoord0; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; diff --git a/textures/cross.png b/textures/cross.png new file mode 100644 index 0000000..2a50c6c Binary files /dev/null and b/textures/cross.png differ -- cgit v1.2.3