src/vamos/geometry/Gl_Texture_Image.cc

Go to the documentation of this file.
00001 //  Gl_Texture_Image.h - an interface to OpenGL texture objects.
00002 //
00003 //      Vamos Automotive Simulator
00004 //  Copyright (C) 2003 Sam Varner
00005 //
00006 //  This program is free software; you can redistribute it and/or modify
00007 //  it under the terms of the GNU General Public License as published by
00008 //  the Free Software Foundation; either version 2 of the License, or
00009 //  (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 #include <vamos/geometry/Gl_Texture_Image.h>
00021 
00022 #include <iostream>
00023 #include <cassert>
00024 
00025 // Windows puts OpenGL 1.2 and 1.3 stuff in glext.h.
00026 #if HAVE_GL_GLEXT_H
00027 # include <GL/glext.h>
00028 #endif
00029 
00030 Vamos_Geometry::
00031 Gl_Texture_Image::Gl_Texture_Image (std::string file_name,
00032                                                                         bool smooth, bool mip_map,
00033                                                                         double width, double height) :
00034   Texture_Image (file_name, width, height),
00035   m_texture_name ()
00036 {
00037 /*glGenTextures (1, &m_texture_name);
00038   glBindTexture (GL_TEXTURE_2D, m_texture_name);
00039   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00040   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00041 
00042   GLint format = 0;
00043   switch (channels ())
00044         {
00045         case 1:
00046           format = GL_LUMINANCE;
00047           break;
00048         case 3:
00049           format = GL_RGB;
00050           break;
00051         case 4:
00052           format = GL_RGBA;
00053           break;
00054         default:
00055           assert (false);
00056         }
00057 
00058   if (mip_map)
00059         {
00060           if (smooth)
00061                 {
00062                   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00063                   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
00064                                                    GL_LINEAR_MIPMAP_LINEAR);
00065                 }
00066           else
00067                 {
00068                   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00069                   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
00070                                                    GL_NEAREST_MIPMAP_NEAREST);
00071                 }
00072           gluBuild2DMipmaps (GL_TEXTURE_2D, format, 
00073                                                  width_pixels (), height_pixels (),
00074                                                  format, GL_UNSIGNED_BYTE, 
00075                                                  data ());
00076         }
00077   else
00078         {
00079           if (smooth)
00080                 {
00081                   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00082                   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00083                 }
00084           else
00085                 {
00086                   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00087                   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00088                 }
00089 
00090           glTexImage2D (GL_TEXTURE_2D, 0, format, 
00091                                         width_pixels (), height_pixels (),
00092                                         0, format, GL_UNSIGNED_BYTE, 
00093                                         data ());
00094         }*/
00095         m_texture_name.Load((string)file_name, true, m_width_pixels, m_height_pixels );
00096 //    cout << m_width_pixels << endl;
00097 }
00098 
00099 Vamos_Geometry::
00100 Gl_Texture_Image::Gl_Texture_Image (const Gl_Texture_Image& image) :
00101   Texture_Image (image),
00102   m_texture_name (image.m_texture_name)
00103 {
00104 }
00105 
00106 const Vamos_Geometry::Gl_Texture_Image& Vamos_Geometry::
00107 Gl_Texture_Image::operator = (const Gl_Texture_Image& image)
00108 {
00109   if (&image != this)
00110         {
00111           Texture_Image::operator = (image);
00112           m_texture_name = image.m_texture_name;
00113         }
00114   return *this;
00115 }
00116 
00117 void Vamos_Geometry::
00118 Gl_Texture_Image::activate ()
00119 {
00120   //assert (glIsTexture (m_texture_name) == GL_TRUE);
00121   //glBindTexture (GL_TEXTURE_2D, m_texture_name);
00122         m_texture_name.Activate();
00123 }
00124 
00125 // Set texture the texture wrap parameter to GL_CLAMP_TO_EDGE.
00126 // GL_REPEAT is the default.
00127 void Vamos_Geometry::
00128 Gl_Texture_Image::clamp_to_edge ()
00129 {
00130   activate ();
00131   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00132   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00133 }
00134 
00135 // Draw the image to the stencil buffer.
00136 void Vamos_Geometry::
00137 Gl_Texture_Image::draw_stencil ()
00138 {
00139 //  glDrawPixels (width_pixels (), height_pixels (), GL_STENCIL_INDEX,
00140 //                              GL_UNSIGNED_BYTE, data ());
00141 }
00142 
00143 using namespace Vamos_Geometry;
00144 
00145 //* Class Facade
00146 Facade::Facade (std::string image_name, bool draw_back) 
00147   : Gl_Texture_Image (image_name, true, true, 1.0, 1.0),
00148     m_draw_back (draw_back),
00149     m_x_offset (0.0),
00150     m_y_offset (0.0),
00151     m_z_offset (0.0)
00152 {
00153 }
00154 
00155 void
00156 Facade::set_radius (double radius)
00157 {
00158   set_width (2.0 * radius * aspect_ratio ());
00159         //set_width (2.0 * radius * aspect_ratio());
00160   set_height (2.0 * radius);
00161   m_x_offset = -width () / 2.0;
00162   m_y_offset = -height () / 2.0;
00163 }
00164 
00165 void
00166 Facade::draw ()
00167 {
00168   activate ();
00169   glColor3d (1.0, 1.0, 1.0);
00170   glEnable (GL_CULL_FACE);
00171   glBegin (GL_QUADS);
00172   glNormal3f (0.0, 0.0, 1.0);
00173   glTexCoord2d (0.0, 1.0);
00174   glVertex3d (m_x_offset, m_y_offset, m_z_offset);
00175   glTexCoord2d (1.0, 1.0);
00176   glVertex3d (m_x_offset + width (), m_y_offset, m_z_offset);
00177   glTexCoord2d (1.0, 0.0);
00178   glVertex3d (m_x_offset + width (), m_y_offset + height (), m_z_offset);
00179   glTexCoord2d (0.0, 0.0);
00180   glVertex3d (m_x_offset, m_y_offset + height (), m_z_offset);
00181 
00182   if (m_draw_back)
00183     {
00184       glNormal3f (0.0, 0.0, -1.0);
00185       glVertex3d (m_x_offset, m_y_offset, m_z_offset);
00186       glVertex3d (m_x_offset, m_y_offset + height (), m_z_offset);
00187       glVertex3d (m_x_offset + width (), m_y_offset + height (), m_z_offset);
00188       glVertex3d (m_x_offset + width (), m_y_offset, m_z_offset);
00189     }
00190   glEnd ();
00191   glDisable (GL_CULL_FACE);
00192 }

Generated on Thu Oct 19 04:05:55 2006 by  doxygen 1.4.6