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

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

Some minor refactoring, also add some more fine grained config.h defines
(e.g. replace NV40 define with feature defines). Add tests for some required
OpenGL extensions (should always check for extensions or base version before
calling entry points from the extension). Also, clamp diffuse and specular
values on input and warn when they are out of range.

  • Property svn:eol-style set to native
File size: 4.2 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    static GLuint floatFormats[] = { -1, GL_LUMINANCE32F_ARB, GL_LUMINANCE_ALPHA32F_ARB, GL_RGB32F_ARB, GL_RGBA32F_ARB };
91    static GLuint halfFloatFormats[] = { -1, GL_LUMINANCE16F_ARB, GL_LUMINANCE_ALPHA16F_ARB, GL_RGB16F_ARB, GL_RGBA16F_ARB };
92    static GLuint basicFormats[] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
93
94    glBindTexture(GL_TEXTURE_3D, _id);
95
96    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
97
98    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, _wrapS);
99    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, _wrapT);
100    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, _wrapR);
101
102    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, _interpType);
103    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, _interpType);
104
105    //to do: add handling to more formats
106    GLuint *targetFormats;
107#ifdef HAVE_FLOAT_TEXTURES
108    if (_type == GL_FLOAT) {
109# ifdef USE_HALF_FLOAT
110        targetFormats = halfFloatFormats;
111# else
112        targetFormats = floatFormats;
113# endif
114    } else {
115#endif
116        targetFormats = basicFormats;
117#ifdef HAVE_FLOAT_TEXTURES
118    }
119#endif
120
121    glTexImage3D(GL_TEXTURE_3D, 0, targetFormats[_numComponents],
122                 _width, _height, _depth, 0,
123                 basicFormats[_numComponents], _type, data);
124
125    assert(glGetError()==0);
126}
127
128void Texture3D::activate()
129{
130    glEnable(GL_TEXTURE_3D);
131    glBindTexture(GL_TEXTURE_3D, _id);
132}
133
134void Texture3D::deactivate()
135{
136    glDisable(GL_TEXTURE_3D);           
137}
138
139void Texture3D::setWrapS(GLuint wrapMode)
140{
141    _wrapS = wrapMode;
142}
143
144void Texture3D::setWrapT(GLuint wrapMode)
145{
146    _wrapT = wrapMode;
147}
148
149void Texture3D::setWrapR(GLuint wrapMode)
150{
151    _wrapR = wrapMode;
152}
153
154void Texture3D::check_max_size()
155{
156    GLint max = 0;
157    glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_EXT, &max);
158
159    TRACE("max 3d texture size: %d\n", max);
160}
161
162void Texture3D::check_max_unit()
163{
164    int max;
165    glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
166
167    TRACE("max texture units: %d.\n", max);
168}
Note: See TracBrowser for help on using the repository browser.