source: trunk/packages/vizservers/nanovis/Texture3D.cpp @ 2857

Last change on this file since 2857 was 2857, checked in by ldelgass, 8 years ago

Remove vr3d library - texture classes were redundant. Remove last vestiges of
opencv usage in code and remove from configure - replaced by imgLoaders library
functions.

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * ----------------------------------------------------------------------
4 * Texture3d.cpp: 3d texture class
5 *
6 * ======================================================================
7 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
8 *           Purdue Rendering and Perceptualization Lab (PURPL)
9 *
10 *  Copyright (c) 2004-2006  Purdue Research Foundation
11 *
12 *  See the file "license.terms" for information on usage and
13 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 * ======================================================================
15 */
16
17#include "Texture3D.h"
18#include "Trace.h"
19#include <stdio.h>
20#include <assert.h>
21#include <math.h>
22
23#include "config.h"
24
25Texture3D::Texture3D() :
26    _width(0),
27    _height(0),
28    _depth(0),
29    _numComponents(3),
30    _glResourceAllocated(false),
31    _id(0),
32    _type(GL_FLOAT),
33    _interpType(GL_LINEAR),
34    _wrapS(GL_CLAMP_TO_EDGE),
35    _wrapT(GL_CLAMP_TO_EDGE),
36    _wrapR(GL_CLAMP_TO_EDGE)
37{}
38
39Texture3D::Texture3D(int width, int height, int depth,
40                     GLuint type, GLuint interp,
41                     int numComponents, void *data) :
42    _width(width),
43    _height(height),
44    _depth(depth),
45    _numComponents(numComponents),
46    _glResourceAllocated(false),
47    _id(0),
48    _type(type),
49    _interpType(interp),
50    _wrapS(GL_CLAMP_TO_EDGE),
51    _wrapT(GL_CLAMP_TO_EDGE),
52    _wrapR(GL_CLAMP_TO_EDGE)
53{
54    //int m = (_width > _height) ? _width : _height;
55    //m = (m > _depth) ? m : _depth;
56
57    //int m = max(max(_width, _height), _depth);
58    _aspectRatioWidth = 1.;
59    _aspectRatioHeight = (double)_height/(double)_width;
60    _aspectRatioDepth = (double)_depth/(double)_width;
61
62    //_aspectRatioWidth = (double)_width/(double)m;
63    //_aspectRatioHeight = (double)_height/(double)m;
64    //_aspectRatioDepth = (double)_depth/(double)m;
65
66    if (data != NULL)
67        initialize(data);
68}
69
70Texture3D::~Texture3D()
71{
72    glDeleteTextures(1, &_id);
73}
74
75GLuint Texture3D::initialize(void *data)
76{
77    if (_glResourceAllocated)
78        glDeleteTextures(1, &_id);
79
80    glGenTextures(1, &_id);
81
82    update(data);
83
84    _glResourceAllocated = true;
85    return _id;
86}
87
88void Texture3D::update(void *data)
89{
90    glBindTexture(GL_TEXTURE_3D, _id);
91
92    //load texture with 16 bit half floating point precision if card is 6 series NV40
93    //half float with linear interpolation is only supported by 6 series and up cards
94    //If NV40 not defined, data is quantized to 8-bit from 32-bit.
95    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
96
97    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, _wrapS);
98    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, _wrapT);
99    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, _wrapR);
100
101    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, _interpType);
102    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, _interpType);
103
104    //to do: add handling to more formats
105#ifdef NV40
106    if (_type == GL_FLOAT) {
107        GLuint targetFormat[5] = { -1, GL_LUMINANCE16F_ARB, GL_LUMINANCE_ALPHA16F_ARB, GL_RGB16F_ARB, GL_RGBA16F_ARB };
108        GLuint format[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
109        glTexImage3D(GL_TEXTURE_3D, 0, targetFormat[_numComponents],
110                     _width, _height, _depth, 0,
111                     format[_numComponents], _type, data);
112    } else {
113#endif
114        GLuint format[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
115        glTexImage3D(GL_TEXTURE_3D, 0, format[_numComponents],
116                     _width, _height, _depth, 0,
117                     format[_numComponents], _type, data);
118#ifdef NV40
119    }
120#endif
121
122    assert(glGetError()==0);
123}
124
125void Texture3D::activate()
126{
127    glEnable(GL_TEXTURE_3D);
128    glBindTexture(GL_TEXTURE_3D, _id);
129}
130
131void Texture3D::deactivate()
132{
133    glDisable(GL_TEXTURE_3D);           
134}
135
136void Texture3D::setWrapS(GLuint wrapMode)
137{
138    _wrapS = wrapMode;
139}
140
141void Texture3D::setWrapT(GLuint wrapMode)
142{
143    _wrapT = wrapMode;
144}
145
146void Texture3D::setWrapR(GLuint wrapMode)
147{
148    _wrapR = wrapMode;
149}
150
151void Texture3D::check_max_size()
152{
153    GLint max = 0;
154    glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_EXT, &max);
155
156    TRACE("max 3d texture size: %d\n", max);
157}
158
159void Texture3D::check_max_unit()
160{
161    int max;
162    glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
163
164    TRACE("max texture units: %d.\n", max);
165}
Note: See TracBrowser for help on using the repository browser.