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

Last change on this file since 3492 was 3492, checked in by ldelgass, 6 years ago

Fix camera reset for nanovis. Includes refactoring of vector/matrix classes
in nanovis to consolidate into vrmath library. Also add preliminary canonical
view control to clients for testing.

  • Property svn:eol-style set to native
File size: 3.7 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-2012  HUBzero Foundation, LLC
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#include <stdio.h>
17#include <assert.h>
18#include <math.h>
19
20#include "config.h"
21
22#include "Texture3D.h"
23#include "Trace.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    if (data != NULL)
55        initialize(data);
56}
57
58Texture3D::~Texture3D()
59{
60    glDeleteTextures(1, &_id);
61}
62
63GLuint Texture3D::initialize(void *data)
64{
65    if (_glResourceAllocated)
66        glDeleteTextures(1, &_id);
67
68    glGenTextures(1, &_id);
69
70    update(data);
71
72    _glResourceAllocated = true;
73    return _id;
74}
75
76void Texture3D::update(void *data)
77{
78    static GLuint floatFormats[] = { -1, GL_LUMINANCE32F_ARB, GL_LUMINANCE_ALPHA32F_ARB, GL_RGB32F_ARB, GL_RGBA32F_ARB };
79    static GLuint halfFloatFormats[] = { -1, GL_LUMINANCE16F_ARB, GL_LUMINANCE_ALPHA16F_ARB, GL_RGB16F_ARB, GL_RGBA16F_ARB };
80    static GLuint basicFormats[] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
81
82    glBindTexture(GL_TEXTURE_3D, _id);
83
84    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
85
86    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, _wrapS);
87    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, _wrapT);
88    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, _wrapR);
89
90    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, _interpType);
91    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, _interpType);
92
93    //to do: add handling to more formats
94    GLuint *targetFormats;
95#ifdef HAVE_FLOAT_TEXTURES
96    if (_type == GL_FLOAT) {
97# ifdef USE_HALF_FLOAT
98        targetFormats = halfFloatFormats;
99# else
100        targetFormats = floatFormats;
101# endif
102    } else {
103#endif
104        targetFormats = basicFormats;
105#ifdef HAVE_FLOAT_TEXTURES
106    }
107#endif
108
109    glTexImage3D(GL_TEXTURE_3D, 0, targetFormats[_numComponents],
110                 _width, _height, _depth, 0, 
111                 basicFormats[_numComponents], _type, data);
112
113    assert(glGetError()==0);
114}
115
116void Texture3D::activate()
117{
118    glEnable(GL_TEXTURE_3D);
119    glBindTexture(GL_TEXTURE_3D, _id);
120}
121
122void Texture3D::deactivate()
123{
124    glDisable(GL_TEXTURE_3D);           
125}
126
127void Texture3D::setWrapS(GLuint wrapMode)
128{
129    _wrapS = wrapMode;
130}
131
132void Texture3D::setWrapT(GLuint wrapMode)
133{
134    _wrapT = wrapMode;
135}
136
137void Texture3D::setWrapR(GLuint wrapMode)
138{
139    _wrapR = wrapMode;
140}
141
142void Texture3D::checkMaxSize()
143{
144    GLint max = 0;
145    glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_EXT, &max);
146
147    TRACE("max 3d texture size: %d", max);
148}
149
150void Texture3D::checkMaxUnit()
151{
152    int max;
153    glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
154
155    TRACE("max texture units: %d", max);
156}
Note: See TracBrowser for help on using the repository browser.