From 0a85366521f28b38fe83e69f792de0a576f33e42 Mon Sep 17 00:00:00 2001
From: Ansgar Philippsen <ansgar.philippsen@gmail.com>
Date: Tue, 31 Aug 2010 16:04:28 -0400
Subject: [PATCH] added texture to gfx; fixed gl includes

---
 modules/gfx/pymod/wrap_gfx.cc      |  7 +--
 modules/gfx/src/CMakeLists.txt     |  4 +-
 modules/gfx/src/bitmap_io.cc       |  2 +-
 modules/gfx/src/gfx_test_object.cc | 11 ++---
 modules/gfx/src/gfx_test_object.hh |  2 +-
 modules/gfx/src/gl_helper.hh       |  1 -
 modules/gfx/src/map_slab.hh        |  2 +-
 modules/gfx/src/scene.hh           |  2 +-
 modules/gfx/src/shader.hh          |  2 +-
 modules/gfx/src/texture.cc         | 41 +++++++++++++++++
 modules/gfx/src/texture.hh         | 74 ++++++++++++++++++++++++++++++
 11 files changed, 130 insertions(+), 18 deletions(-)
 create mode 100644 modules/gfx/src/texture.cc
 create mode 100644 modules/gfx/src/texture.hh

diff --git a/modules/gfx/pymod/wrap_gfx.cc b/modules/gfx/pymod/wrap_gfx.cc
index 1c8ca0065..cd308a6a7 100644
--- a/modules/gfx/pymod/wrap_gfx.cc
+++ b/modules/gfx/pymod/wrap_gfx.cc
@@ -19,13 +19,14 @@
 #include <boost/python.hpp>
 using namespace boost::python;
 
+#include <ost/gfx/module_config.hh>
+#if OST_SHADER_SUPPORT_ENABLED
+#include <ost/gfx/shader.hh>
+#endif
 #include <ost/info/info.hh>
 #include <ost/gfx/prim_list.hh>
 #include <ost/gfx/gradient.hh>
 #include <ost/gfx/gfx_test_object.hh>
-#if OST_SHADER_SUPPORT_ENABLED
-#include <ost/gfx/shader.hh>
-#endif
 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
 
 extern void export_Scene();
diff --git a/modules/gfx/src/CMakeLists.txt b/modules/gfx/src/CMakeLists.txt
index 26fb2b7b8..ee2a67f4e 100644
--- a/modules/gfx/src/CMakeLists.txt
+++ b/modules/gfx/src/CMakeLists.txt
@@ -22,6 +22,7 @@ prim_list.hh
 scene.hh
 selection.hh
 surface.hh
+texture.hh
 vertex_array.hh
 vertex_array_helper.hh
 scene_observer.hh
@@ -90,9 +91,7 @@ bitmap_io.cc
 color.cc
 primitives.cc
 entity.cc
-
 symmetry_node.cc
-
 gfx_node.cc
 gfx_object.cc
 gfx_prim.cc
@@ -107,6 +106,7 @@ vertex_array.cc
 vertex_array_helper.cc
 material.cc
 povray.cc
+texture.cc
 color_ops/color_op.cc
 color_ops/by_element_color_op.cc
 color_ops/by_chain_color_op.cc
diff --git a/modules/gfx/src/bitmap_io.cc b/modules/gfx/src/bitmap_io.cc
index 7c8fdf71b..3f1645137 100644
--- a/modules/gfx/src/bitmap_io.cc
+++ b/modules/gfx/src/bitmap_io.cc
@@ -148,7 +148,7 @@ Bitmap import_png(const String& filename)
   channels=png_get_channels(png_ptr,info_ptr);
 
   if(channels<1 || channels>4) {
-    LOGN_ERROR("error importing bitmap: " << filename << " has " << channels << " channels, excpected 1-4");
+    LOGN_ERROR("error importing bitmap: " << filename << " has " << channels << " channels, expected 1-4");
     return bm;
   }
   
diff --git a/modules/gfx/src/gfx_test_object.cc b/modules/gfx/src/gfx_test_object.cc
index 7baead968..83ae9b329 100644
--- a/modules/gfx/src/gfx_test_object.cc
+++ b/modules/gfx/src/gfx_test_object.cc
@@ -26,6 +26,7 @@
 #include <boost/filesystem/fstream.hpp>
 
 #include <ost/platform.hh>
+#include "texture.hh"
 #include "glext_include.hh"
 #include "gfx_test_object.hh"
 #include "scene.hh"
@@ -59,8 +60,8 @@ GfxTestObj::GfxTestObj():
   String ost_root=GetSharedDataPath();
   bf::path ost_root_dir(ost_root);
   bf::path tex_file(ost_root_dir / "textures/test_texture.png");
-  Bitmap bm = BitmapImport(tex_file.string(),".png");
-  if(!bm.data) {
+  Texture tex(BitmapImport(tex_file.string(),".png"));
+  if(!tex.IsValid()) {
     LOGN_ERROR("error loading " << tex_file.string());
   } else {
     LOGN_VERBOSE("importing tex with id " << tex_id);
@@ -70,11 +71,7 @@ GfxTestObj::GfxTestObj():
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-    if(bm.channels==3) {
-      glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bm.width,bm.height,0,GL_RGB,GL_UNSIGNED_BYTE,bm.data.get());
-    } else if(bm.channels==4) {
-      glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,bm.width,bm.height,0,GL_RGBA,GL_UNSIGNED_BYTE,bm.data.get());
-    }
+    glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,tex.width(),tex.height(),tex.border(),tex.format(),tex.type(),tex.data());
   }
 }
 
diff --git a/modules/gfx/src/gfx_test_object.hh b/modules/gfx/src/gfx_test_object.hh
index b233f2812..95eeeb2cc 100644
--- a/modules/gfx/src/gfx_test_object.hh
+++ b/modules/gfx/src/gfx_test_object.hh
@@ -26,7 +26,7 @@
 */
 
 #include "gfx_object.hh"
-#include "gl_include.hh"
+#include "glext_include.hh"
 
 namespace ost { namespace gfx {
 
diff --git a/modules/gfx/src/gl_helper.hh b/modules/gfx/src/gl_helper.hh
index 7dc1155da..11f87c94d 100644
--- a/modules/gfx/src/gl_helper.hh
+++ b/modules/gfx/src/gl_helper.hh
@@ -36,7 +36,6 @@ Author: Juergen Haas
 #include <ost/geom/vec3.hh>
 
 #include "glext_include.hh"
-#include "gl_include.hh"
 
 #include <ost/log.hh>
 
diff --git a/modules/gfx/src/map_slab.hh b/modules/gfx/src/map_slab.hh
index 3af06bd5a..7cd85a111 100644
--- a/modules/gfx/src/map_slab.hh
+++ b/modules/gfx/src/map_slab.hh
@@ -29,8 +29,8 @@
 #include <ost/geom/geom.hh>
 
 #include <ost/img/map.hh>
-#include <ost/gfx/gl_include.hh>
 
+#include "glext_include.hh"
 #include "gfx_object.hh"
 
 namespace ost { namespace gfx {
diff --git a/modules/gfx/src/scene.hh b/modules/gfx/src/scene.hh
index a58a8652c..8f1b3b9c2 100644
--- a/modules/gfx/src/scene.hh
+++ b/modules/gfx/src/scene.hh
@@ -32,6 +32,7 @@
 #include <ost/gfx/module_config.hh>
 #include <ost/mol/transform.hh>
 
+#include "gl_include.hh"
 #include "color.hh"
 #include "gfx_object_fw.hh"
 #include "gfx_node_fw.hh"
@@ -40,7 +41,6 @@
 #include "glwin_base.hh"
 #include "scene_observer.hh"
 #include "gfx_prim.hh"
-#include "gl_include.hh"
 #include "povray_fw.hh"
 
 namespace ost { namespace gfx {
diff --git a/modules/gfx/src/shader.hh b/modules/gfx/src/shader.hh
index 8715e907c..f61aeeb97 100644
--- a/modules/gfx/src/shader.hh
+++ b/modules/gfx/src/shader.hh
@@ -29,7 +29,7 @@
 #include <map>
 #include <stack>
 #include <ost/gfx/module_config.hh>
-#include "gl_include.hh"
+#include "glext_include.hh"
 
 namespace ost { namespace gfx {
 
diff --git a/modules/gfx/src/texture.cc b/modules/gfx/src/texture.cc
new file mode 100644
index 000000000..56ea75553
--- /dev/null
+++ b/modules/gfx/src/texture.cc
@@ -0,0 +1,41 @@
+#include "texture.hh"
+
+namespace ost { namespace gfx {
+
+  Texture::Texture(const Bitmap& bm):
+    w_(bm.width), h_(bm.height),
+    d_()
+  {
+    if(!bm.data) return;
+    d_=boost::shared_array<Color>(new Color[w_*h_]);
+    static float f=1.0/255.0;
+    for(GLint v=0;v<h_;++v) {
+      for(GLint u=0;u<w_;++u) {
+        int p=v*w_+u;
+        Color& c = d_[p];
+        if(bm.channels==1) {
+          c[0]=f*static_cast<float>(bm.data[p]);
+          c[1]=c[0];
+          c[2]=c[0];
+          c[3]=1.0;
+        } else if(bm.channels==2) {
+          c[0]=f*static_cast<float>(bm.data[p*2+0]);
+          c[1]=c[0];
+          c[2]=c[0];
+          c[3]=f*static_cast<float>(bm.data[p*2+1]);
+        } else if(bm.channels==3) {
+          c[0]=f*static_cast<float>(bm.data[p*3+0]);
+          c[1]=f*static_cast<float>(bm.data[p*3+1]);
+          c[2]=f*static_cast<float>(bm.data[p*3+2]);
+          c[3]=1.0;
+        } else if(bm.channels==4) {
+          c[0]=f*static_cast<float>(bm.data[p*4+0]);
+          c[1]=f*static_cast<float>(bm.data[p*4+1]);
+          c[2]=f*static_cast<float>(bm.data[p*4+2]);
+          c[2]=f*static_cast<float>(bm.data[p*4+3]);
+        }
+      }
+    }
+  }
+
+}}
diff --git a/modules/gfx/src/texture.hh b/modules/gfx/src/texture.hh
new file mode 100644
index 000000000..471bdfd71
--- /dev/null
+++ b/modules/gfx/src/texture.hh
@@ -0,0 +1,74 @@
+//------------------------------------------------------------------------------
+// This file is part of the OpenStructure project <www.openstructure.org>
+//
+// Copyright (C) 2008-2010 by the OpenStructure authors
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License as published by the Free
+// Software Foundation; either version 3.0 of the License, or (at your option)
+// any later version.
+// This library is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+//------------------------------------------------------------------------------
+#ifndef OST_GFX_TEXTURE_HH
+#define OST_GFX_TEXTURE_HH
+
+/*
+  texture
+
+  Author: Ansgar Philippsen
+*/
+
+#include <boost/shared_array.hpp>
+
+#include "module_config.hh"
+#include "glext_include.hh"
+#include "color.hh"
+#include "bitmap_io.hh"
+
+namespace ost { namespace gfx {
+
+class Texture
+{
+public:
+  Texture():
+    w_(0),
+    h_(0),
+    d_()
+  {}
+
+  Texture(GLint w, GLint h):
+    w_(w),
+    h_(h),
+    d_(new Color[w*h])
+  {}
+
+  Texture(const Bitmap& b);
+
+  bool IsValid() const {return d_;}
+
+  Color& operator()(uint u, uint v) {return d_[v*w_+u];}
+  const Color& operator()(uint u, uint v) const {return d_[v*w_+u];}
+
+  float* data() {return d_[0];}
+  
+  GLint width() const {return w_;}
+  GLint height() const {return h_;}
+  GLint format() const {return GL_RGBA;}
+  GLint type() const {return GL_FLOAT;}
+  GLint border() const {return 0;}
+
+private:
+  GLint w_,h_;
+  boost::shared_array<Color> d_;
+};
+
+}} // ns
+
+#endif
-- 
GitLab