From 64a2a6d71023ab382c996ccdb8e403660fa19916 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Wed, 6 Oct 2010 12:11:46 +0000 Subject: replaces skydomes with skyboxes --- src/render/Makefile.am | 10 ++-- src/render/draw.cc | 48 +++++----------- src/render/dust.h | 2 +- src/render/render.cc | 20 ++----- src/render/sky.cc | 147 +++++++++++++++++++++++++++++++++++++++++++++++++ src/render/sky.h | 35 ++++++++++++ 6 files changed, 208 insertions(+), 54 deletions(-) create mode 100644 src/render/sky.cc create mode 100644 src/render/sky.h (limited to 'src/render') diff --git a/src/render/Makefile.am b/src/render/Makefile.am index 9fd8f50..4636250 100644 --- a/src/render/Makefile.am +++ b/src/render/Makefile.am @@ -10,8 +10,8 @@ endif librender_la_LDFLAGS = -avoid-version -no-undefined @GL_LIBS@ librender_la_LIBADD = $(top_builddir)/src/math/libmath.la librender_la_SOURCES = camera.cc draw.cc dust.cc gl.cc image.cc jpgfile.cc \ - particles.cc pngfile.cc render.cc renderext.cc screenshot.cc state.cc text.cc \ - textures.cc tgafile.cc -noinst_HEADERS = camera.h draw.h dust.h gl.h image.h jpgfile.h particles.h \ - pngfile.h render.h renderext.h screenshot.h state.h text.h \ - textures.h tgafile.h + particles.cc pngfile.cc render.cc renderext.cc screenshot.cc sky.cc \ + state.cc text.cc textures.cc tgafile.cc +noinst_HEADERS = camera.h draw.h dust.h gl.h image.h jpgfile.h \ + particles.h pngfile.h render.h renderext.h screenshot.h sky.h \ + state.h text.h textures.h tgafile.h diff --git a/src/render/draw.cc b/src/render/draw.cc index f5978f7..068c4dc 100644 --- a/src/render/draw.cc +++ b/src/render/draw.cc @@ -18,6 +18,7 @@ #include "render/draw.h" #include "render/dust.h" #include "render/gl.h" +#include "render/sky.h" #include "math/functions.h" namespace render @@ -136,6 +137,7 @@ void pass_prepare(float seconds) /* ----- Skybox ---------------------------------------------------- */ +/* void draw_sphere_inside(math::Color const & color, float radius) { gl::scale(radius, radius, radius); @@ -152,6 +154,7 @@ void draw_sphere_inside(math::Color const & color, float radius) } } +*/ void draw_pass_sky() { @@ -164,27 +167,7 @@ void draw_pass_sky() if (!core::localplayer()->zone()->sky().size()) return; - if (!core::localplayer()->zone()->sky_texture()) { - - std::string texture_name("textures/env/"); - texture_name.append(core::localplayer()->zone()->sky()); - core::localplayer()->zone()->set_sky_texture(Textures::load(texture_name)); - - if (!core::localplayer()->zone()->sky_texture()) { - core::localplayer()->zone()->set_sky(""); - return; - } - } - - Textures::bind(core::localplayer()->zone()->sky_texture()); - gl::enable(GL_TEXTURE_2D); - gl::push(); - - gl::translate(Camera::eye()); - draw_sphere_inside(math::Color(), 1016.0f); - - gl::pop(); - gl::disable(GL_TEXTURE_2D); + Sky::draw(core::localplayer()->zone()->sky()); } @@ -702,12 +685,16 @@ void draw_model_fragments(model::Model *model, if (material->flags() & model::Material::Environment) { if (!(material->flags() & model::Material::Texture)) { + /* // use sky as envmap if the material defines no texture + // FIXME broken since skybox if (core::localplayer()->zone()->sky_texture()) { Textures::bind(core::localplayer()->zone()->sky_texture()); gl::enable(GL_TEXTURE_2D); use_texture = true; - } else if (use_texture) { + } else + */ + if (use_texture) { gl::disable(GL_TEXTURE_2D); use_texture = false; } @@ -1239,25 +1226,18 @@ void draw(float seconds) glPolygonMode(GL_FRONT, GL_FILL); } + gl::disable(GL_DEPTH_TEST); + gl::depthmask(GL_FALSE); // disable depth buffer writing + + draw_pass_sky(); // draw the skybox + // set vertex array pointers glInterleavedArrays(GL_T2F_N3F_V3F, 0, core::game()->vertexarray()->ptr()); - /* - // this doesnt work - glTexCoordPointer(2, GL_FLOAT, 6 * sizeof(float), core::game()->vertexarray()->ptr()); - glNormalPointer(GL_FLOAT, 5 * sizeof(float), &core::game()->vertexarray()->ptr()[2]); - glVertexPointer(3, GL_FLOAT, 5 * sizeof(float), &core::game()->vertexarray()->ptr()[5]); - */ - // enable vertex arrays glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); - - gl::disable(GL_DEPTH_TEST); - gl::depthmask(GL_FALSE); // disable depth buffer writing - - draw_pass_sky(); // draw the skysphere gl::depthmask(GL_TRUE); // enable writing to the depth buffer gl::enable(GL_DEPTH_TEST); diff --git a/src/render/dust.h b/src/render/dust.h index 4a3b7bd..4653550 100644 --- a/src/render/dust.h +++ b/src/render/dust.h @@ -1,5 +1,5 @@ /* - render/text.h + render/dust.h This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ diff --git a/src/render/render.cc b/src/render/render.cc index 5a6c6bc..acc9c86 100644 --- a/src/render/render.cc +++ b/src/render/render.cc @@ -21,6 +21,7 @@ #include "render/dust.h" #include "render/render.h" #include "render/screenshot.h" +#include "render/sky.h" #include "render/textures.h" #include "sys/sys.h" @@ -150,14 +151,8 @@ void init(int width, int height) // unload game assets (zone change) void unload() { - // clear zone sky textures - for (core::Zone::Registry::iterator it = core::Zone::registry().begin(); it != core::Zone::registry().end(); it++) { - core::Zone *zone = (*it).second; - if (zone->sky_texture()) { - render::Textures::unload(zone->sky_texture()); - zone->set_sky_texture(0); - } - } + // unload skybox + Sky::unload(); for (core::Entity::Registry::iterator it = core::Entity::registry().begin(); it != core::Entity::registry().end(); it++) { core:: Entity *entity = (*it).second; @@ -179,12 +174,9 @@ void unload() // clear all assets void clear() { - // clear zone sky textures - for (core::Zone::Registry::iterator it = core::Zone::registry().begin(); it != core::Zone::registry().end(); it++) { - core::Zone *zone = (*it).second; - zone->set_sky_texture(0); - } - + // unload skybox + Sky::unload(); + // clear entity models, and globe textures, this will force a reload for (core::Entity::Registry::iterator it = core::Entity::registry().begin(); it != core::Entity::registry().end(); it++) { core::Entity *entity = (*it).second; diff --git a/src/render/sky.cc b/src/render/sky.cc new file mode 100644 index 0000000..e40c4be --- /dev/null +++ b/src/render/sky.cc @@ -0,0 +1,147 @@ +/* + render/sky.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "render/camera.h" +#include "render/sky.h" +#include "render/textures.h" + +namespace render +{ + +size_t Sky::sky_texture_up = 0; +size_t Sky::sky_texture_down = 0; +size_t Sky::sky_texture_left = 0; +size_t Sky::sky_texture_right = 0; +size_t Sky::sky_texture_front = 0; +size_t Sky::sky_texture_back = 0; + +std::string Sky::sky_name; + +void Sky::draw(const std::string & name) +{ + if (sky_name.compare(name) != 0) { + unload(); + } + + if (!sky_name.size()) { + + if (name.size()) { + const std::string basename("textures/sky/" + name); + + sky_texture_up = Textures::load(basename + "_up"); + sky_texture_down = Textures::load(basename + "_down"); + sky_texture_left = Textures::load(basename + "_left"); + sky_texture_right = Textures::load(basename + "_right"); + sky_texture_front = Textures::load(basename + "_front"); + sky_texture_back = Textures::load(basename + "_back"); + } + sky_name.assign(name); + } + + /* + * NOTE + * for quake3 skyboxes: the _left texture is actually the right side of the skybox + */ + gl::push(); + gl::translate(Camera::eye()); + gl::enable(GL_TEXTURE_2D); + gl::color(1.0f, 1.0f, 1.0f, 1.0f); + + // front + Textures::bind(sky_texture_front); + gl::begin(gl::Quads); + gl::texcoord(0, 0); gl::vertex(1, 1, 1); + gl::texcoord(1, 0); gl::vertex(1, -1, 1); + gl::texcoord(1, 1); gl::vertex(1, -1, -1); + gl::texcoord(0, 1); gl::vertex(1, 1, -1); + gl::end(); + + // right + Textures::bind(sky_texture_right); + gl::begin(gl::Quads); + gl::texcoord(0, 0); gl::vertex(1, -1, 1); + gl::texcoord(1, 0); gl::vertex(-1, -1, 1); + gl::texcoord(1, 1); gl::vertex(-1, -1, -1); + gl::texcoord(0, 1); gl::vertex(1, -1, -1); + gl::end(); + + // back + Textures::bind(sky_texture_back); + gl::begin(gl::Quads); + gl::texcoord(0, 0); gl::vertex(-1, -1, 1); + gl::texcoord(1, 0); gl::vertex(-1, 1, 1); + gl::texcoord(1, 1); gl::vertex(-1, 1, -1); + gl::texcoord(0, 1); gl::vertex(-1, -1, -1); + gl::end(); + + // left + Textures::bind(sky_texture_left); + gl::begin(gl::Quads); + gl::texcoord(0, 0); gl::vertex(-1, 1, 1); + gl::texcoord(1, 0); gl::vertex(1, 1, 1); + gl::texcoord(1, 1); gl::vertex(1, 1, -1); + gl::texcoord(0, 1); gl::vertex(-1, 1, -1); + gl::end(); + + // up + Textures::bind(sky_texture_up); + gl::begin(gl::Quads); + gl::texcoord(0, 0); gl::vertex(-1, 1, 1); + gl::texcoord(1, 0); gl::vertex(-1, -1, 1); + gl::texcoord(1, 1); gl::vertex(1, -1, 1); + gl::texcoord(0, 1); gl::vertex(1, 1, 1); + gl::end(); + + // down + Textures::bind(sky_texture_down); + gl::begin(gl::Quads); + gl::texcoord(0, 0); gl::vertex(1, 1, -1); + gl::texcoord(1, 0); gl::vertex(1, -1, -1); + gl::texcoord(1, 1); gl::vertex(-1, -1, -1); + gl::texcoord(0, 1); gl::vertex(-1, 1, -1); + gl::end(); + + gl::pop(); + + gl::disable(GL_TEXTURE_2D); +} + +void Sky::unload() +{ + if (sky_texture_up) { + Textures::unload(sky_texture_up); + sky_texture_up = 0; + } + + if (sky_texture_down) { + Textures::unload(sky_texture_down); + sky_texture_down = 0; + } + + if (sky_texture_left) { + Textures::unload(sky_texture_left); + sky_texture_left = 0; + } + + if (sky_texture_right) { + Textures::unload(sky_texture_right); + sky_texture_right = 0; + } + + if (sky_texture_front) { + Textures::unload(sky_texture_front); + sky_texture_front = 0; + } + + if (sky_texture_back) { + Textures::unload(sky_texture_back); + sky_texture_back = 0; + } + + sky_name.clear(); +} + +} // namespace render diff --git a/src/render/sky.h b/src/render/sky.h new file mode 100644 index 0000000..d922d75 --- /dev/null +++ b/src/render/sky.h @@ -0,0 +1,35 @@ +/* + render/sky.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_RENDER_SKY_H__ +#define __INCLUDED_RENDER_SKY_H__ + +#include + +namespace render +{ + +class Sky { +public: + + static void unload(); + + static void draw(const std::string & name); + +private: + + static size_t sky_texture_up; + static size_t sky_texture_down; + static size_t sky_texture_left; + static size_t sky_texture_right; + static size_t sky_texture_front; + static size_t sky_texture_back; + + static std::string sky_name; +}; + +} // namespace render +#endif // __INCLUDED_RENDER_SKY_H__ \ No newline at end of file -- cgit v1.2.3