usata-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Usata-commits] Changes to usata2/src/texture.cpp


From: David Lau
Subject: [Usata-commits] Changes to usata2/src/texture.cpp
Date: Wed, 19 Jan 2005 20:15:12 -0500

Index: usata2/src/texture.cpp
diff -u usata2/src/texture.cpp:1.8 usata2/src/texture.cpp:1.9
--- usata2/src/texture.cpp:1.8  Wed Jan 19 06:28:47 2005
+++ usata2/src/texture.cpp      Thu Jan 20 01:15:09 2005
@@ -1,7 +1,7 @@
 // -*- mode: C++; tab-width: 4; indent-tabs-mode: t; -*- vim:ts=4:sw=4
 //
-// Copyright (C) 2004  David Lau (skunix)
-//                     Chong Kai Xiong (descender)
+// Copyright (C) 2004-2005  David Lau (skunix)
+//                          Chong Kai Xiong (descender)
 //
 // This file is part of The Plains of Usata.
 //
@@ -10,58 +10,103 @@
 // included in the software distribution, or visit
 // http://www.fsf.org/licenses/gpl.html.
 //
-// $Id: texture.cpp,v 1.8 2005/01/19 06:28:47 skunix Exp $
+// $Id: texture.cpp,v 1.9 2005/01/20 01:15:09 skunix Exp $
 
 #include <boost/scoped_array.hpp>
-
-#include <GL/glew.h>
-#include "texture.hpp"
-#include "texture-meta.hpp"
 #include <boost/tuple/tuple.hpp>
 #include <boost/format.hpp>
 #include <boost/bind.hpp>
+
 #include <Magick++.h>
-#include "log.hpp"
-namespace usata
-{
 
+#include <GL/glew.h>
 
-namespace
-{
+#include "texture.hpp"
+#include "texture-meta.hpp"
+#include "log.hpp"
 
-struct PixmapType
+namespace usata
 {
-               
-};
 
 class Pixmap
 {
+       public:
+       enum ITYPE { RGB=3, RGBA=4, LUM=1, LUMA=2};
+
+       private:
        unsigned Width;
        unsigned Height;
-       unsigned Depth;
-       std::string Format;
+
+       ITYPE Type;
        boost::scoped_array<unsigned char> pixels;
 
        public:
-               Pixmap(unsigned, unsigned, unsigned char, std::string &type);
-               unsigned width() const { return Width; }
-               unsigned height() const { return Height; }
-               const std::string&
-               format() const {return Format;}
-               unsigned char*get_pixels(){ return pixels.get();}
-               
+               Pixmap(const std::string& fn);
+               unsigned width() const
+               {
+                       return Width;
+               }
+               unsigned height() const
+               {
+                       return Height;
+               }
+               ITYPE type()
+               {
+                       return Type;
+               }       
+               unsigned char* get_pixels()
+               {
+                       return pixels.get();
+               }
+               GLint get_glformat();
 };
 
-}
-Pixmap::Pixmap(unsigned w, unsigned h, unsigned char d, std::string &type)
-:      Width(w),
-       Height(h),
-       Format(type),
-       pixels( new unsigned char[w*h*(d/8)])
+GLint Pixmap::get_glformat()
 {
-
+       switch (Type)
+       {
+               case RGB:
+                       return GL_RGB;
+               case RGBA:
+                       return GL_RGBA;
+               case LUM:
+                       return GL_LUMINANCE;
+               case LUMA:
+                       return GL_LUMINANCE_ALPHA;
+       }
+       throw exceptions::FatalError("A bug in usata::Pixmap! (from 
get_glformat)");
 }
 
+Pixmap::Pixmap(const std::string& fn)
+{
+       std::auto_ptr<Magick::Image> magick(new Magick::Image(fn));
+       std::string mt;
+       Magick::ImageType image_type = magick->type();
+       switch (image_type)
+       {
+               case Magick::TrueColorType:
+               case Magick::PaletteType:
+                       Type = RGB; mt = "RGB";
+                       break;
+               case Magick::TrueColorMatteType:
+               case Magick::PaletteMatteType:
+                       Type=RGBA; mt = "RGBA";
+                       break;
+               case Magick::GrayscaleType:
+                       Type=LUM; mt = "R";
+                       break;
+               case Magick::GrayscaleMatteType:
+                       Type=LUMA;mt="RA";
+                       break;
+               default:
+                       throw std::runtime_error("Unsupported image pixel 
layout");
+       }
+       Width = magick->columns();
+       Height = magick->rows();
+       pixels.reset(new unsigned char[Width*Height*Type]);
+       magick->write(0,0,Width,Height, mt, Magick::CharPixel,pixels.get());
+       return;
+}
 
 TextureMeta::TextureMeta(const boost::filesystem::path& p)
 : filepath(p)
@@ -69,140 +114,114 @@
 
 }
 
-TextureMapManager::Image_sp 
+TextureMap_sp
 TextureMapManager::get(const std::string& name)
 {
        return get_resource(name);
 }
 
-void 
+void
 TextureMapManager::meta_add(const TextureMeta* meta)
 {
        add_meta(meta->alias, *meta);
+       return;
 }
 
-void 
+void
 TextureMapManager::load_path(const boost::filesystem::path& p)
 {
        using boost::filesystem::path;
+
        log::BufferedStream logstream;
+
        path mf = p / "textures" / "meta";
-       logstream << (boost::format("Loading Image metadata from 
'%1%'")%mf.native_file_string()).str() << log::commit;
-       
+
+               boost::format msg("Loading texture metadata from '%1%'");
+               msg % mf.native_file_string();
+
+       logstream << msg.str() << log::commit;
 
        texture_meta_parse(mf,
                boost::bind(&TextureMapManager::meta_add,this,_1)
                );
-
+       return;
 }
 
 void
-Texture::allocate_gltex(void)
+TextureMap::allocate_gltex(void)
 {
        glGenTextures(1,&tex_id);
+       return;
 }
-Texture::Texture(const TextureMeta& tm)
+void
+TextureMap::finish_load_1d(GLint internal_gl_format)
 {
-       const TextureMeta* meta = &tm;
-       Magick::Image XI(meta->filepath.native_file_string());
-       log::BufferedStream logstream(log::Level::DEBUG);
-       Magick::ImageType image_type = XI.type();
-       unsigned Depth;
-       GLint  format = 0;      
-       std::string Type;       
-       switch (image_type)
-       {
-               case Magick::TrueColorType:
-               case Magick::PaletteType:
-               {
-                       format = GL_RGB;
-                       Type="RGB";
-                       Depth = 24;
-                       break;  
-               }
-
-               case Magick::TrueColorMatteType:
-               case Magick::PaletteMatteType:
-               {
-                       format = GL_RGBA;
-                       Type="RGBA";
-                       Depth = 32;
-                       break;
-               }
-
-               case Magick::GrayscaleType:
-               {
-               }
-       
-               default:
-               {
-                       throw std::runtime_error("Unsupported image pixel 
layout");
-               }
-
-       }
-
-       Pixmap pb(XI.columns(),XI.rows(),Depth,Type);
-       XI.write(0,0,pb.width(), pb.height(), 
pb.format(),Magick::CharPixel,pb.get_pixels());
-       glBindTexture(GL_TEXTURE_2D, tex_id);
-    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);    
-    
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
-       gluBuild2DMipmaps(GL_TEXTURE_2D, format, pb.width(), pb.height(), 
format, GL_UNSIGNED_BYTE,pb.get_pixels());
-
+       gluBuild1DMipmaps(GL_TEXTURE_1D, internal_gl_format, m_pixmap->width(),
+                                         m_pixmap->get_glformat(), 
GL_UNSIGNED_BYTE,
+                                         m_pixmap->get_pixels());
        return;
+}
+void
+TextureMap::finish_load_2d(GLint internal_gl_format)
+{
+       gluBuild2DMipmaps(GL_TEXTURE_2D, internal_gl_format, m_pixmap->width(),
+                                               m_pixmap->height(), 
m_pixmap->get_glformat(),
+                                               GL_UNSIGNED_BYTE,
+                                               m_pixmap->get_pixels());
 
+       return;
 }
 
-Texture::Texture (const TextureMeta* meta)
+void
+TextureMap::finish_load()
 {
-       Magick::Image XI(meta->filepath.native_file_string());
        log::BufferedStream logstream(log::Level::DEBUG);
-       Magick::ImageType image_type = XI.type();
-       unsigned Depth;
-       GLint  format = 0;      
-       std::string Type;       
-       switch (image_type)
-       {
-               case Magick::TrueColorType:
-               case Magick::PaletteType:
-               {
-                       format = GL_RGB;
-                       Type="RGB";
-                       Depth = 24;
-                       break;  
-               }
+       if (! m_pixmap.get() )
+               return;
 
-               case Magick::TrueColorMatteType:
-               case Magick::PaletteMatteType:
-               {
-                       format = GL_RGBA;
-                       Type="RGBA";
-                       Depth = 32;
-                       break;
-               }
+       allocate_gltex();
 
-               case Magick::GrayscaleType:
-               {
-               }
+       GLint  internal_gl_format = m_pixmap->type();
+
+       if (m_pixmap->height() == 1)
+                       m_gl_texture_type = GL_TEXTURE_1D;
+
+       glBindTexture(m_gl_texture_type, tex_id);
+
+    glTexParameteri(m_gl_texture_type, 
+                                       GL_TEXTURE_MAG_FILTER,
+                                       GL_LINEAR);
+
+    glTexParameteri(m_gl_texture_type,
+                                       GL_TEXTURE_MIN_FILTER,
+                                       GL_LINEAR_MIPMAP_NEAREST);
        
-               default:
-               {
-                       throw std::runtime_error("Unsupported image pixel 
layout");
-               }
+       if (m_gl_texture_type == GL_TEXTURE_2D)
+               finish_load_2d(internal_gl_format);
+       else 
+               finish_load_1d(internal_gl_format);
 
-       }
+       // free that bastard.
+       m_pixmap.reset(0);
+       return;
 
-       Pixmap pb(XI.columns(),XI.rows(),Depth,Type);
-       XI.write(0,0,pb.width(), pb.height(), 
pb.format(),Magick::CharPixel,pb.get_pixels());
-       glBindTexture(GL_TEXTURE_2D, tex_id);
-    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);    
-    
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
-       gluBuild2DMipmaps(GL_TEXTURE_2D, format, pb.width(), pb.height(), 
format, GL_UNSIGNED_BYTE,pb.get_pixels());
+}
 
+TextureMap::TextureMap(const TextureMeta& tm)
+:      m_gl_texture_type(GL_TEXTURE_2D)
+{
+       const TextureMeta* meta = &tm;
+       m_pixmap.reset(
+               new Pixmap(meta->filepath.native_file_string())
+               );
+       
        return;
+
 }
 
-Texture::~Texture()
+TextureMap::~TextureMap()
 {
+       return;
 }
 
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]