glteximage.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:9k
- /*
- * ===========================================================================
- * PRODUCTION $Log: glteximage.cpp,v $
- * PRODUCTION Revision 1000.1 2004/06/01 20:51:02 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.5
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: glteximage.cpp,v 1000.1 2004/06/01 20:51:02 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Mike DiCuccio
- *
- * File Description:
- *
- */
- #include <ncbi_pch.hpp>
- #include <gui/opengl/glteximage.hpp>
- #include <gui/opengl/glexception.hpp>
- #include <util/image/image_io.hpp>
- #include <gui/opengl.h>
- BEGIN_NCBI_SCOPE
- //
- // static flag: can we use texture objects?
- // 0 = no 1 = yes 2 = haven't tested yet
- CTexImage::ETexSupport CTexImage::sm_TexObjFlag = CTexImage::eNotChecked;
- // default ctor
- CTexImage::CTexImage()
- : m_TexId(0),
- m_WrapS(GL_CLAMP),
- m_WrapT(GL_CLAMP),
- m_FilterMin(GL_LINEAR),
- m_FilterMag(GL_LINEAR),
- m_TexEnv(GL_MODULATE)
- {
- }
- //
- // ctor: create an empty image
- CTexImage::CTexImage(size_t w, size_t h, size_t depth)
- : m_TexId(0),
- m_WrapS(GL_CLAMP),
- m_WrapT(GL_CLAMP),
- m_FilterMin(GL_LINEAR),
- m_FilterMag(GL_LINEAR),
- m_TexEnv(GL_MODULATE)
- {
- Init(w, h, depth);
- }
- //
- // ctor: create an image around an existing image
- CTexImage::CTexImage(CImage* image)
- : m_TexId(0),
- m_WrapS(GL_CLAMP),
- m_WrapT(GL_CLAMP),
- m_FilterMin(GL_LINEAR),
- m_FilterMag(GL_LINEAR),
- m_TexEnv(GL_MODULATE)
- {
- Swallow(image);
- }
- //
- // get an image from a file
- CTexImage::CTexImage(const string& filename)
- : m_TexId(0),
- m_WrapS(GL_CLAMP),
- m_WrapT(GL_CLAMP),
- m_FilterMin(GL_LINEAR),
- m_FilterMag(GL_LINEAR),
- m_TexEnv(GL_MODULATE)
- {
- m_Image.Reset(CImageIO::ReadImage(filename));
- if ( !m_Image ) {
- string msg("CTexImage(): cannot read image from file ");
- msg += filename;
- NCBI_THROW(COpenGLException, eTextureError, msg);
- }
- m_FileName = filename;
- }
- //
- // initialize an empty image
- //
- void CTexImage::Init(size_t w, size_t h, size_t d)
- {
- Clear();
- // initialize our image
- m_Image.Reset(new CImage(w, h, d));
- }
- //
- // x_InitTexObj()
- // test whether the system supports texture objects
- //
- void CTexImage::x_InitTexObj(void)
- {
- // test whether the system supports texture objects
- // the requirements are:
- // - extensions advertize GL_EXT_texture_object
- // -- or --
- // - version >= 1.1
- switch (sm_TexObjFlag) {
- case eNotChecked:
- {{
- char *extens = (char *)glGetString (GL_EXTENSIONS);
- char *version = (char *)glGetString (GL_VERSION);
- if (extens && version) {
- if (strstr (extens, "GL_EXT_texture_object") ||
- strncmp (version, "1.1", 3) == 0) {
- sm_TexObjFlag = eTexObj;
- _TRACE("texture support = texture object");
- } else {
- sm_TexObjFlag = eTexDisplayList;
- _TRACE("texture support = display list");
- }
- } else {
- _TRACE("texture support = not determinable");
- }
- }}
- break;
- default:
- break;
- }
- }
- //
- // clear the current image
- //
- void CTexImage::Clear(void)
- {
- Unload();
- m_Image.Reset();
- m_FileName.erase();
- }
- //
- // "swallow" an image
- //
- void CTexImage::Swallow(CImage *image)
- {
- // clear the current image
- Clear();
- // assign the wrapped image
- m_Image.Reset(image);
- // load the texture
- Load();
- }
- //
- // bind the image as a texture
- //
- void CTexImage::MakeCurrent(void)
- {
- if (sm_TexObjFlag == eNotChecked) {
- x_InitTexObj();
- if (sm_TexObjFlag == eNotChecked) {
- LOG_POST(Error << "CTexImage::MakeCurrent(): "
- "Failed to determine texture support");
- return;
- }
- }
- if ( !m_TexId ) {
- Load();
- }
- // Texture environment is not saved in the texture object, so we set it
- // here
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, m_TexEnv);
- switch (sm_TexObjFlag) {
- case eTexObj:
- glBindTexture(GL_TEXTURE_2D, m_TexId);
- break;
- case eTexDisplayList:
- glCallList(m_TexId);
- break;
- }
- }
- //
- // create a representation of the texture for rendering
- // this is either a display list or a texture object
- //
- void CTexImage::Load(void)
- {
- _TRACE("CTexImage::Load()");
- // unload the current texture
- Unload();
- // safety first!
- if ( !m_Image ) {
- _TRACE(" no image to load!");
- return;
- }
- // make sure we know what to do with textures
- x_InitTexObj();
- switch (sm_TexObjFlag) {
- case eNotChecked:
- NCBI_THROW(COpenGLException, eTextureError,
- "Texturing not supported");
- case eTexObj:
- glGenTextures(1, &m_TexId);
- glBindTexture(GL_TEXTURE_2D, m_TexId);
- _TRACE("created texture object = " << m_TexId);
- break;
- case eTexDisplayList:
- {{
- GLint val;
- glGetIntegerv(GL_MAX_TEXTURE_SIZE, &val);
- if ( m_Image->GetWidth() > (size_t)val) {
- NCBI_THROW(COpenGLException, eTextureError,
- "Texture is too large");
- }
- }}
- // all tests passed, allocate a display list
- m_TexId = glGenLists(1);
- glNewList(m_TexId, GL_COMPILE_AND_EXECUTE);
- _TRACE("created texture display list = " << m_TexId);
- break;
- }
- // set texture params
- glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_WrapS);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, m_WrapT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, m_FilterMag);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, m_FilterMin);
- // call glTexImage2D
- switch (m_Image->GetDepth()) {
- case 3:
- gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB,
- m_Image->GetWidth(), m_Image->GetHeight(),
- GL_RGB, GL_UNSIGNED_BYTE,
- m_Image->GetData());
- break;
- case 4:
- gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA,
- m_Image->GetWidth(), m_Image->GetHeight(),
- GL_RGBA, GL_UNSIGNED_BYTE,
- m_Image->GetData());
- break;
- default:
- LOG_POST(Error << "CTexImage::Load(): unhandled image depth");
- break;
- }
- // don't forget to end our display list
- if (sm_TexObjFlag == 0) {
- glEndList();
- }
- }
- //
- // unload the image as a texture
- void CTexImage::Unload (void)
- {
- // check to see if we can use texture objects
- x_InitTexObj();
- switch (sm_TexObjFlag) {
- case eNotChecked:
- break;
- case eTexObj:
- if (glIsTexture (m_TexId)) {
- glDeleteTextures (1, &m_TexId);
- }
- break;
- case eTexDisplayList:
- if (glIsList (m_TexId)) {
- glDeleteLists (m_TexId, 1);
- }
- break;
- }
- m_TexId = 0;
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: glteximage.cpp,v $
- * Revision 1000.1 2004/06/01 20:51:02 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.5
- *
- * Revision 1.5 2004/05/21 22:27:45 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.4 2003/06/16 16:56:10 dicuccio
- * Fix compiler error for MSVC
- *
- * Revision 1.3 2003/06/04 17:01:03 rsmith
- * GLint not plain int more compatible.
- *
- * Revision 1.2 2003/06/04 00:32:44 ucko
- * Change GL headers to #include <gui/opengl.h> for portability.
- *
- * Revision 1.1 2003/06/03 17:42:06 dicuccio
- * Added texture support. Added classes to handle OpenGL camera setup and
- * viewport specification.
- *
- * ===========================================================================
- */