Changes between Initial Version and Version 1 of OpenGL_Texturing


Ignore:
Timestamp:
Feb 23, 2008, 4:18:58 AM (16 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OpenGL_Texturing

    v1 v1  
     1[KEEP IN SYNC WITH OGL_TEX.H!]
     2
     3== Introduction ==
     4
     5This module simplifies use of textures in OpenGL. It provides an easy-to-use load/upload/bind/free API that completely replaces direct access to OpenGL's texturing calls.
     6
     7It basically wraps tex.cpp's texture info in a resource object (see h_mgr.h) that maintains associated GL state and provides for reference counting, caching, hotloading and safe access.
     8
     9
     10== Texture Parameters ==
     11
     12OpenGL textures are conditioned on several parameters including filter and wrap mode. These are typically set once when the texture is created, but must survive reloads (1). To that end, all state (2) is set via ogl_tex_set_* (instead of direct glTexParameter calls) and re-applied after a reload.
     13
     14 1. the purpose of hotloading is to permit artists to see their changes in-game without having to restart the map. reloads where the texture looks different due to changed state are useless.
     15 1. currently only filter and wrap mode. no other glTexParameter settings are used ATM; if that changes, add to !OglTexState.
     16
     17
     18== Uploading to OpenGL ==
     19
     20.. deserves some clarification. This entails calling glTexImage2D (or its variants for mipmaps/compressed textures) and transfers texture parameters and data from system memory to OpenGL (and thereby usually video memory).
     21
     22In so doing, choices are made as to the texture's internal representation (how it is stored in vmem) - in particular, the bit depth. This can trade performance (more/less data to copy) for quality (fidelity to original).
     23
     24We provide a mechanism that applies defaults to all uploads; this allows a global "quality" setting that can boost performance on older graphics cards without requiring anything else to be changed. Textures with specific quality needs can override this via ogl_tex_set_* or ogl_tex_upload parameters.
     25
     26
     27== Caching and Texture Instances ==
     28
     29Caching is both an advantage and drawback. When opening the same texture twice without previously freeing it, a reference to the first instance is returned. Therefore, be advised that concurrent use of the same texture but with differing parameters (e.g. upload quality) followed by a reload of the first instance will result in using the wrong parameters. For background and rationale why this is acceptable, see struct !OglTex.
     30
     31
     32== Example Usage ==
     33
     34Note: to keep the examples simple, we leave out error handling by ignoring all return values. Each function will still raise a warning (assert) if it fails and passing e.g. invalid Handles will only cause the next function to fail, but real apps should check and report errors.
     35
     36 * Basic usage: load texture from file.
     37{{{
     38 Handle hTexture = ogl_tex_load("filename.dds");
     39 (void)ogl_tex_upload(hTexture);
     40 
     41 [when rendering:]
     42 (void)ogl_tex_bind(hTexture);
     43 [.. do something with OpenGL that uses the currently bound texture]
     44 
     45 [at exit:]
     46 // (done automatically, but this avoids it showing up as a leak)
     47 (void)ogl_tex_free(hTexture);
     48}}}
     49
     50 * Advanced usage: wrap existing texture data, override filter, specify internal_format and use multitexturing.
     51{{{
     52 Tex t;
     53 const uint flags = 0;  // image is plain RGB, default orientation
     54 void* data = [pre-existing image]
     55 (void)tex_wrap(w, h, 24, flags, data, &t);
     56 Handle hCompositeAlphaMap = ogl_tex_wrap(&t, "(alpha map composite)");
     57 (void)ogl_tex_set_filter(hCompositeAlphaMap, GL_LINEAR);
     58 (void)ogl_tex_upload(hCompositeAlphaMap, 0, 0, GL_INTENSITY);
     59 // (your responsibility! tex_wrap attaches a reference but it is
     60 // removed by ogl_tex_upload.)
     61 free(data);
     62 
     63 [when rendering:]
     64 (void)ogl_tex_bind(hCompositeAlphaMap, 1);
     65 [.. do something with OpenGL that uses the currently bound texture]
     66 
     67 [at exit:]
     68 // (done automatically, but this avoids it showing up as a leak)
     69 (void)ogl_tex_free(hCompositeAlphaMap);
     70}}}
     71
     72
     73== Reference ==
     74
     75{{{
     76 //
     77 // quality mechanism
     78 //
     79 
     80 enum OglTexQualityFlags
     81 {
     82        // emphatically require full quality for this texture.
     83        // (q_flags are invalid if this is set together with any other bit)
     84        // rationale: the value 0 is used to indicate "use default flags" in
     85        // ogl_tex_upload and ogl_tex_set_defaults, so this is the only
     86        // way we can say "disregard default and do not reduce anything".
     87        OGL_TEX_FULL_QUALITY = 0x20,
     88 
     89        // store the texture at half the normal bit depth
     90        // (4 bits per pixel component, as opposed to 8).
     91        // this increases performance on older graphics cards due to
     92        // decreased size in vmem. it has no effect on
     93        // compressed textures because they have a fixed internal format.
     94        OGL_TEX_HALF_BPP = 0x10,
     95 
     96        // store the texture at half its original resolution.
     97        // this increases performance on older graphics cards due to
     98        // decreased size in vmem.
     99        // this is useful for also reducing quality of compressed textures,
     100        // which are not affected by OGL_TEX_HALF_BPP.
     101        // currently only implemented for images that contain mipmaps
     102        // (otherwise, we'd have to resample, which is slow).
     103        // note: scaling down to 1/4, 1/8, .. is easily possible without
     104        // extra work, so we leave some bits free for that.
     105        OGL_TEX_HALF_RES = 0x01
     106 };
     107 
     108 // change default settings - these affect performance vs. quality.
     109 // may be overridden for individual textures via parameter to
     110 // ogl_tex_upload or ogl_tex_set_filter, respectively.
     111 //
     112 // pass 0 to keep the current setting; defaults and legal values are:
     113 // - q_flags: OGL_TEX_FULL_QUALITY; combination of OglTexQualityFlags
     114 // - filter: GL_LINEAR; any valid OpenGL minification filter
     115 extern void ogl_tex_set_defaults(uint q_flags, GLint filter);
     116 
     117 
     118 //
     119 // open/close
     120 //
     121 
     122 // load and return a handle to the texture given in <fn>.
     123 // for a list of supported formats, see tex.h's tex_load.
     124 extern Handle ogl_tex_load(const char* fn, uint flags = 0);
     125 
     126 // make the given Tex object ready for use as an OpenGL texture
     127 // and return a handle to it. this will be as if its contents
     128 // had been loaded by ogl_tex_load.
     129 //
     130 // we need only add bookkeeping information and "wrap" it in
     131 // a resource object (accessed via Handle), hence the name.
     132 //
     133 // <fn> isn't strictly needed but should describe the texture so that
     134 // h_filename will return a meaningful comment for debug purposes.
     135 extern Handle ogl_tex_wrap(Tex* t, const char* fn = 0, uint flags = 0);
     136 
     137 // free all resources associated with the texture and make further
     138 // use of it impossible. (subject to refcount)
     139 extern int ogl_tex_free(Handle& ht);
     140 
     141 
     142 //
     143 // set texture parameters
     144 //
     145 
     146 // these must be called before uploading; this simplifies
     147 // things and avoids calling glTexParameter twice.
     148 
     149 // override default filter (as set above) for this texture.
     150 // must be called before uploading (raises a warning if called afterwards).
     151 // filter is as defined by OpenGL; it is applied for both minification and
     152 // magnification (for rationale and details, see OglTexState)
     153 extern int ogl_tex_set_filter(Handle ht, GLint filter);
     154 
     155 // override default wrap mode (GL_REPEAT) for this texture.
     156 // must be called before uploading (raises a warning if called afterwards).
     157 // wrap is as defined by OpenGL and applies to both S and T coordinates
     158 // (rationale: see OglTexState).
     159 extern int ogl_tex_set_wrap(Handle ht, GLint wrap);
     160 
     161 
     162 //
     163 // upload
     164 //
     165 
     166 // bind the texture to the specified unit [number] in preparation for
     167 // using it in rendering. assumes multitexturing is available.
     168 // not necessary before calling ogl_tex_upload!
     169 // side effects:
     170 // - enables (or disables, if <ht> == 0) texturing on the given unit.
     171 extern int ogl_tex_bind(Handle ht, GLenum unit = 0);
     172 
     173 // upload the texture to OpenGL.
     174 // if not 0, parameters override the following:
     175 //   fmt_ovr     : OpenGL format (e.g. GL_RGB) decided from bpp / Tex flags;
     176 //   q_flags_ovr : global default "quality vs. performance" flags;
     177 //   int_fmt_ovr : internal format (e.g. GL_RGB8) decided from fmt / q_flags.
     178 //
     179 // side effects:
     180 // - enables texturing on TMU 0 and binds the texture to it;
     181 // - frees the texel data! see ogl_tex_get_data.
     182 extern int ogl_tex_upload(const Handle ht, GLenum fmt_ovr = 0, uint q_flags_ovr = 0, GLint int_fmt_ovr = 0);
     183 
     184 
     185 //
     186 // return information about the texture
     187 //
     188 
     189 // retrieve texture dimensions and bits per pixel.
     190 // all params are optional and filled if non-NULL.
     191 extern int ogl_tex_get_size(Handle ht, uint* w, uint* h, uint* bpp);
     192 
     193 // retrieve Tex.flags and the corresponding OpenGL format.
     194 // the latter is determined during ogl_tex_upload and is 0 before that.
     195 // all params are optional and filled if non-NULL.
     196 extern int ogl_tex_get_format(Handle ht, uint* flags, GLenum* fmt);
     197 
     198 // retrieve pointer to texel data.
     199 //
     200 // note: this memory is freed after a successful ogl_tex_upload for
     201 // this texture. after that, the pointer we retrieve is NULL but
     202 // the function doesn't fail (negative return value) by design.
     203 // if you still need to get at the data, add a reference before
     204 // uploading it or read directly from OpenGL (discouraged).
     205 extern int ogl_tex_get_data(Handle ht, void** p);
     206 
     207 
     208 //
     209 // misc
     210 //
     211 
     212 // apply the specified transforms (as in tex_transform) to the image.
     213 // must be called before uploading (raises a warning if called afterwards).
     214 extern int ogl_tex_transform(Handle ht, uint flags);
     215}}}