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 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'scene.cpp') 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); -- cgit v1.2.3