Ignore:
Timestamp:
Mar 9, 2012 10:12:14 AM (8 years ago)
Author:
ldelgass
Message:

Refactor texture classes, misc. cleanups, cut down on header pollution -- still
need to fix header deps in Makefile.in

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/nanovis/Texture3D.cpp

    r2798 r2831  
    2323#include "config.h"
    2424
    25 Texture3D::Texture3D(){ id=0; gl_resource_allocated = false; }
     25Texture3D::Texture3D() :
     26    gl_resource_allocated(false),
     27    id(0)
     28{}
    2629
    27 Texture3D::Texture3D(int width, int height, int depth, GLuint type=GL_FLOAT, GLuint interp=GL_LINEAR, int components=4)
     30Texture3D::Texture3D(int width, int height, int depth,
     31                     GLuint type, GLuint interp,
     32                     int numComponents, void *data) :
     33    gl_resource_allocated(false),
     34    id(0)
    2835{
    29     assert(type == GL_UNSIGNED_BYTE || type == GL_FLOAT|| type ==GL_UNSIGNED_INT);
    30     assert(interp == GL_LINEAR || interp == GL_NEAREST);
    31        
    3236    this->width = width;
    3337    this->height = height;
     
    4852    this->type = type;
    4953    this->interp_type = interp;
    50     this->n_components = components;
     54    this->n_components = numComponents;
    5155
    52     this->id = 0;
    53     gl_resource_allocated = false;
     56    if (data != NULL)
     57        initialize(data);
    5458}
    5559
    56 void Texture3D::update(float* data)
     60Texture3D::~Texture3D()
    5761{
    58     //load texture with 16 bit half floating point precision if card is 6 series NV40
    59     //half float with linear interpolation is only supported by 6 series and up cards
    60     //If NV40 not defined, data is quantized to 8-bit from 32-bit.
    61     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    62 
    63     glBindTexture(GL_TEXTURE_3D, id);
    64     assert(id != (GLuint)-1);
    65 
    66     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    67     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    68     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    69 
    70     if(interp_type==GL_LINEAR){
    71         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    72         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    73     }
    74     else{
    75         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    76         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    77     }
    78 
    79     //to do: add handling to more formats
    80     if(type==GL_FLOAT){
    81         switch(n_components){
    82 #ifdef NV40
    83         case 1:
    84             glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE16F_ARB, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
    85             break;
    86         case 2:
    87             glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA16F_ARB, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
    88             break;
    89         case 3:
    90             glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F_ARB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
    91             break;
    92         case 4:
    93             glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F_ARB, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
    94             break;
    95 #else
    96         case 1:
    97             glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
    98             break;
    99         case 2:
    100             glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
    101             break;
    102         case 3:
    103             glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
    104             break;
    105         case 4:
    106             glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
    107             break;
    108 #endif
    109         default:
    110             break;
    111         }
    112     }
    113 
    114 
    115     assert(glGetError()==0);
    116        
    117     gl_resource_allocated = true;
    118 
     62    glDeleteTextures(1, &id);
    11963}
    12064
    121 GLuint Texture3D::initialize(float *data)
     65GLuint Texture3D::initialize(void *data)
    12266{
    123     if (id != 0) glDeleteTextures(1, &id);
     67    if (id != 0)
     68        glDeleteTextures(1, &id);
     69
     70    glGenTextures(1, &id);
     71
     72    update(data);
     73 
     74    return id;
     75}
     76
     77void Texture3D::update(void *data)
     78{
     79    assert(id > 0 && id != (GLuint)-1);
     80    glBindTexture(GL_TEXTURE_3D, id);
    12481
    12582    //load texture with 16 bit half floating point precision if card is 6 series NV40
     
    12885    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    12986
    130     glGenTextures(1, &id);
    131     glBindTexture(GL_TEXTURE_3D, id);
    132     assert(id != (GLuint)-1);
    133 
    13487    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    13588    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    13689    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    13790
    138     if(interp_type==GL_LINEAR){
    139         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    140         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    141     }
    142     else{
    143         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    144         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    145     }
     91    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, interp_type);
     92    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, interp_type);
    14693
    14794    //to do: add handling to more formats
    148     if(type==GL_FLOAT){
    149         switch(n_components){
    15095#ifdef NV40
    151         case 1:
    152             glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE16F_ARB, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
    153             break;
    154         case 2:
    155             glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA16F_ARB, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
    156             break;
    157         case 3:
    158             glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F_ARB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
    159             break;
    160         case 4:
    161             glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F_ARB, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
    162             break;
    163 #else
    164         case 1:
    165             glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
    166             break;
    167         case 2:
    168             glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
    169             break;
    170         case 3:
    171             glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
    172             break;
    173         case 4:
    174             glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
    175             break;
     96    if (type == GL_FLOAT) {
     97        GLuint targetFormat[5] = { -1, GL_LUMINANCE16F_ARB, GL_LUMINANCE_ALPHA16F_ARB, GL_RGB16F_ARB, GL_RGBA16F_ARB };
     98        GLuint format[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
     99        glTexImage3D(GL_TEXTURE_3D, 0, targetFormat[n_components],
     100                     width, height, depth, 0,
     101                     format[n_components], type, data);
     102    } else {
    176103#endif
    177         default:
    178             break;
    179         }
     104        GLuint format[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
     105        glTexImage3D(GL_TEXTURE_3D, 0, format[n_components],
     106                     width, height, depth, 0,
     107                     format[n_components], type, data);
     108#ifdef NV40
    180109    }
    181 
     110#endif
    182111
    183112    assert(glGetError()==0);
    184        
     113
    185114    gl_resource_allocated = true;
    186     return id;
    187115}
    188116
     
    198126}
    199127
    200 Texture3D::~Texture3D()
     128void Texture3D::check_max_size()
    201129{
    202     glDeleteTextures(1, &id);
    203 }
    204 
    205 void Texture3D::check_max_size(){
    206130    GLint max = 0;
    207131    glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_EXT, &max);
    208        
    209     //TRACE("%d", glGetError());
     132
    210133    TRACE("max 3d texture size: %d\n", max);
    211134}
    212135
    213 void Texture3D::check_max_unit(){
     136void Texture3D::check_max_unit()
     137{
    214138    int max;
    215139    glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
Note: See TracChangeset for help on using the changeset viewer.