More fixes
[supertux.git] / src / video / gl / gl_lightmap.cpp
index 120aae6..f8a6735 100644 (file)
 #include <physfs.h>
 #include <sstream>
 
-#include "obstack/obstackpp.hpp"
 #include "supertux/gameconfig.hpp"
 #include "supertux/globals.hpp"
-#include "video/lightmap.hpp"
+#include "util/obstackpp.hpp"
 #include "video/drawing_context.hpp"
 #include "video/drawing_request.hpp"
 #include "video/font.hpp"
 #include "video/gl/gl_surface_data.hpp"
+#include "video/gl/gl_renderer.hpp"
 #include "video/gl/gl_texture.hpp"
 #include "video/glutil.hpp"
+#include "video/lightmap.hpp"
 #include "video/renderer.hpp"
 #include "video/surface.hpp"
 #include "video/texture_manager.hpp"
 
-namespace {
-
-inline void intern_draw(float left, float top, float right, float bottom,
-                        float uv_left, float uv_top,
-                        float uv_right, float uv_bottom,
-                        float angle, float alpha,
-                        const Color& color,
-                        const Blend& blend,
-                        DrawingEffect effect)
-{
-  if(effect & HORIZONTAL_FLIP)
-    std::swap(uv_left, uv_right);
-  if(effect & VERTICAL_FLIP) 
-    std::swap(uv_top, uv_bottom);
-
-  // unrotated blit
-  glBlendFunc(blend.sfactor, blend.dfactor);
-  glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
-  if (angle == 0.0f) {
-    float vertices[] = {
-      left, top,
-      right, top,
-      right, bottom,
-      left, bottom,
-    };
-    glVertexPointer(2, GL_FLOAT, 0, vertices);
-
-    float uvs[] = {
-      uv_left, uv_top,
-      uv_right, uv_top,
-      uv_right, uv_bottom,
-      uv_left, uv_bottom,
-    };
-    glTexCoordPointer(2, GL_FLOAT, 0, uvs);
-
-    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-  } else {
-    // rotated blit
-    float center_x = (left + right) / 2;
-    float center_y = (top + bottom) / 2;
-
-    float sa = sinf(angle/180.0f*M_PI);
-    float ca = cosf(angle/180.0f*M_PI);
-
-    left  -= center_x;
-    right -= center_x;
-
-    top    -= center_y;
-    bottom -= center_y;
-
-    float vertices[] = {
-      left*ca - top*sa + center_x, left*sa + top*ca + center_y,
-      right*ca - top*sa + center_x, right*sa + top*ca + center_y,
-      right*ca - bottom*sa + center_x, right*sa + bottom*ca + center_y,
-      left*ca - bottom*sa + center_x, left*sa + bottom*ca + center_y
-    };
-    glVertexPointer(2, GL_FLOAT, 0, vertices);
-
-    float uvs[] = {
-      uv_left, uv_top,
-      uv_right, uv_top,
-      uv_right, uv_bottom,
-      uv_left, uv_bottom,
-    };
-    glTexCoordPointer(2, GL_FLOAT, 0, uvs);
-
-    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-  }
-
-  // FIXME: find a better way to restore the blend mode
-  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
-  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-}
-
-static inline int next_po2(int val)
-{
-  int result = 1;
-  while(result < val)
-    result *= 2;
-
-  return result;
-}
-
-} // namespace
-
 GLLightmap::GLLightmap() :
   screen(),
   lightmap(),
@@ -137,30 +51,28 @@ GLLightmap::GLLightmap() :
 {
   screen = SDL_GetVideoSurface();
 
-  lightmap_width = screen->w / LIGHTMAP_DIV;
-  lightmap_height = screen->h / LIGHTMAP_DIV;
+  lightmap_width = SCREEN_WIDTH / LIGHTMAP_DIV;
+  lightmap_height = SCREEN_HEIGHT / LIGHTMAP_DIV;
   unsigned int width = next_po2(lightmap_width);
   unsigned int height = next_po2(lightmap_height);
 
-  lightmap = new GLTexture(width, height);
+  lightmap.reset(new GLTexture(width, height));
 
   lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
   lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
-  texture_manager->register_texture(lightmap);
+  texture_manager->register_texture(lightmap.get());
 }
 
 GLLightmap::~GLLightmap()
 {
-  if(texture_manager){
-    texture_manager->remove_texture(lightmap);
-  }
-  delete lightmap;
 }
 
 void
 GLLightmap::start_draw(const Color &ambient_color)
 {
-  glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
+  
+  glGetFloatv(GL_VIEWPORT, old_viewport); //save viewport
+  glViewport(old_viewport[0], old_viewport[3] - lightmap_height + old_viewport[1], lightmap_width, lightmap_height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
 #ifdef GL_VERSION_ES_CM_1_0
@@ -180,9 +92,9 @@ GLLightmap::end_draw()
 {
   glDisable(GL_BLEND);
   glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
-  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
-
-  glViewport(0, 0, screen->w, screen->h);
+  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, old_viewport[0], old_viewport[3]  - lightmap_height + old_viewport[1], lightmap_width, lightmap_height);
+  
+  glViewport(old_viewport[0], old_viewport[1], old_viewport[2], old_viewport[3]);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
 #ifdef GL_VERSION_ES_CM_1_0
@@ -193,24 +105,24 @@ GLLightmap::end_draw()
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glEnable(GL_BLEND);
-  //glClear(GL_COLOR_BUFFER_BIT);
+
+  glClearColor(0, 0, 0, 1 );
+  glClear(GL_COLOR_BUFFER_BIT);
 }
 
 void
 GLLightmap::do_draw()
 {
-  const GLTexture* texture = lightmap;
-
   // multiple the lightmap with the framebuffer
   glBlendFunc(GL_DST_COLOR, GL_ZERO);
 
-  glBindTexture(GL_TEXTURE_2D, texture->get_handle());
+  glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
 
   float vertices[] = {
     0, 0,
-    SCREEN_WIDTH, 0,
-    SCREEN_WIDTH, SCREEN_HEIGHT,
-    0, SCREEN_HEIGHT
+    float(SCREEN_WIDTH), 0,
+    float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
+    0, float(SCREEN_HEIGHT)
   };
   glVertexPointer(2, GL_FLOAT, 0, vertices);
 
@@ -231,7 +143,7 @@ void
 GLLightmap::draw_surface(const DrawingRequest& request)
 {
   const Surface* surface = (const Surface*) request.request_data;
-  GLTexture *gltexture = dynamic_cast<GLTexture *>(surface->get_texture());
+  boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
   GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
 
   glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
@@ -254,8 +166,8 @@ GLLightmap::draw_surface_part(const DrawingRequest& request)
 {
   const SurfacePartRequest* surfacepartrequest
     = (SurfacePartRequest*) request.request_data;
-  const Surface *surface = surfacepartrequest->surface;
-  GLTexture *gltexture = dynamic_cast<GLTexture *>(surface->get_texture());
+  const Surfacesurface = surfacepartrequest->surface;
+  boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
   GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
 
   float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
@@ -295,9 +207,9 @@ GLLightmap::draw_gradient(const DrawingRequest& request)
 
   float vertices[] = {
     0, 0,
-    SCREEN_WIDTH, 0,
-    SCREEN_WIDTH, SCREEN_HEIGHT,
-    0, SCREEN_HEIGHT
+    float(SCREEN_WIDTH), 0,
+    float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
+    0, float(SCREEN_HEIGHT)
   };
   glVertexPointer(2, GL_FLOAT, 0, vertices);
 
@@ -359,8 +271,8 @@ GLLightmap::get_light(const DrawingRequest& request) const
   for( int i = 0; i<3; i++)
     pixels[i] = 0.0f; //set to black
 
-  float posX = request.pos.x * lightmap_width / SCREEN_WIDTH;
-  float posY = screen->h - request.pos.y * lightmap_height / SCREEN_HEIGHT;
+  float posX = request.pos.x * lightmap_width / SCREEN_WIDTH + old_viewport[0];
+  float posY = old_viewport[3] + old_viewport[1] - request.pos.y * lightmap_height / SCREEN_HEIGHT;
   glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
   *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);
 }