[2798] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
[1028] | 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 | * |
---|
[3502] | 10 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
[1028] | 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 | |
---|
[3492] | 22 | #include "Texture3D.h" |
---|
| 23 | #include "Trace.h" |
---|
| 24 | |
---|
[3611] | 25 | using namespace nv; |
---|
| 26 | |
---|
[2831] | 27 | Texture3D::Texture3D() : |
---|
[2857] | 28 | _width(0), |
---|
| 29 | _height(0), |
---|
| 30 | _depth(0), |
---|
| 31 | _numComponents(3), |
---|
| 32 | _glResourceAllocated(false), |
---|
| 33 | _id(0), |
---|
| 34 | _type(GL_FLOAT), |
---|
| 35 | _interpType(GL_LINEAR), |
---|
| 36 | _wrapS(GL_CLAMP_TO_EDGE), |
---|
| 37 | _wrapT(GL_CLAMP_TO_EDGE), |
---|
| 38 | _wrapR(GL_CLAMP_TO_EDGE) |
---|
[2831] | 39 | {} |
---|
[1028] | 40 | |
---|
[2831] | 41 | Texture3D::Texture3D(int width, int height, int depth, |
---|
| 42 | GLuint type, GLuint interp, |
---|
| 43 | int numComponents, void *data) : |
---|
[2857] | 44 | _width(width), |
---|
| 45 | _height(height), |
---|
| 46 | _depth(depth), |
---|
| 47 | _numComponents(numComponents), |
---|
| 48 | _glResourceAllocated(false), |
---|
| 49 | _id(0), |
---|
| 50 | _type(type), |
---|
| 51 | _interpType(interp), |
---|
| 52 | _wrapS(GL_CLAMP_TO_EDGE), |
---|
| 53 | _wrapT(GL_CLAMP_TO_EDGE), |
---|
| 54 | _wrapR(GL_CLAMP_TO_EDGE) |
---|
[1028] | 55 | { |
---|
[2831] | 56 | if (data != NULL) |
---|
| 57 | initialize(data); |
---|
[1028] | 58 | } |
---|
| 59 | |
---|
[2831] | 60 | Texture3D::~Texture3D() |
---|
[1028] | 61 | { |
---|
[2857] | 62 | glDeleteTextures(1, &_id); |
---|
[2831] | 63 | } |
---|
[1028] | 64 | |
---|
[2831] | 65 | GLuint Texture3D::initialize(void *data) |
---|
| 66 | { |
---|
[2857] | 67 | if (_glResourceAllocated) |
---|
| 68 | glDeleteTextures(1, &_id); |
---|
[1028] | 69 | |
---|
[2857] | 70 | glGenTextures(1, &_id); |
---|
[1028] | 71 | |
---|
[2831] | 72 | update(data); |
---|
[2857] | 73 | |
---|
| 74 | _glResourceAllocated = true; |
---|
| 75 | return _id; |
---|
[1028] | 76 | } |
---|
| 77 | |
---|
[2831] | 78 | void Texture3D::update(void *data) |
---|
[1028] | 79 | { |
---|
[3561] | 80 | #ifdef USE_HALF_FLOAT |
---|
| 81 | static GLuint halfFloatFormats[] = { |
---|
| 82 | (GLuint)-1, GL_LUMINANCE16F_ARB, GL_LUMINANCE_ALPHA16F_ARB, |
---|
| 83 | GL_RGB16F_ARB, GL_RGBA16F_ARB |
---|
[3559] | 84 | }; |
---|
[3561] | 85 | #elif defined(HAVE_FLOAT_TEXTURES) |
---|
| 86 | static GLuint floatFormats[] = { |
---|
| 87 | (GLuint)-1, GL_LUMINANCE32F_ARB, GL_LUMINANCE_ALPHA32F_ARB, |
---|
| 88 | GL_RGB32F_ARB, GL_RGBA32F_ARB |
---|
| 89 | }; |
---|
| 90 | #endif |
---|
| 91 | static GLuint basicFormats[] = { |
---|
| 92 | (GLuint)-1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA |
---|
| 93 | }; |
---|
| 94 | |
---|
[2857] | 95 | glBindTexture(GL_TEXTURE_3D, _id); |
---|
[1028] | 96 | |
---|
| 97 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
---|
| 98 | |
---|
[2857] | 99 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, _wrapS); |
---|
| 100 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, _wrapT); |
---|
| 101 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, _wrapR); |
---|
[1028] | 102 | |
---|
[2857] | 103 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, _interpType); |
---|
| 104 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, _interpType); |
---|
[1028] | 105 | |
---|
| 106 | //to do: add handling to more formats |
---|
[2877] | 107 | GLuint *targetFormats; |
---|
| 108 | #ifdef HAVE_FLOAT_TEXTURES |
---|
[2857] | 109 | if (_type == GL_FLOAT) { |
---|
[2877] | 110 | # ifdef USE_HALF_FLOAT |
---|
| 111 | targetFormats = halfFloatFormats; |
---|
| 112 | # else |
---|
| 113 | targetFormats = floatFormats; |
---|
| 114 | # endif |
---|
[2831] | 115 | } else { |
---|
[1028] | 116 | #endif |
---|
[2877] | 117 | targetFormats = basicFormats; |
---|
| 118 | #ifdef HAVE_FLOAT_TEXTURES |
---|
[1028] | 119 | } |
---|
[2831] | 120 | #endif |
---|
[1028] | 121 | |
---|
[2877] | 122 | glTexImage3D(GL_TEXTURE_3D, 0, targetFormats[_numComponents], |
---|
| 123 | _width, _height, _depth, 0, |
---|
| 124 | basicFormats[_numComponents], _type, data); |
---|
| 125 | |
---|
[2831] | 126 | assert(glGetError()==0); |
---|
[1028] | 127 | } |
---|
| 128 | |
---|
| 129 | void Texture3D::activate() |
---|
| 130 | { |
---|
| 131 | glEnable(GL_TEXTURE_3D); |
---|
[2857] | 132 | glBindTexture(GL_TEXTURE_3D, _id); |
---|
[1028] | 133 | } |
---|
| 134 | |
---|
| 135 | void Texture3D::deactivate() |
---|
| 136 | { |
---|
| 137 | glDisable(GL_TEXTURE_3D); |
---|
| 138 | } |
---|
| 139 | |
---|
[2857] | 140 | void Texture3D::setWrapS(GLuint wrapMode) |
---|
| 141 | { |
---|
| 142 | _wrapS = wrapMode; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void Texture3D::setWrapT(GLuint wrapMode) |
---|
| 146 | { |
---|
| 147 | _wrapT = wrapMode; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | void Texture3D::setWrapR(GLuint wrapMode) |
---|
| 151 | { |
---|
| 152 | _wrapR = wrapMode; |
---|
| 153 | } |
---|
| 154 | |
---|
[2900] | 155 | void Texture3D::checkMaxSize() |
---|
[1028] | 156 | { |
---|
| 157 | GLint max = 0; |
---|
| 158 | glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_EXT, &max); |
---|
[2831] | 159 | |
---|
[3452] | 160 | TRACE("max 3d texture size: %d", max); |
---|
[1028] | 161 | } |
---|
| 162 | |
---|
[2900] | 163 | void Texture3D::checkMaxUnit() |
---|
[2831] | 164 | { |
---|
[1028] | 165 | int max; |
---|
| 166 | glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max); |
---|
| 167 | |
---|
[3452] | 168 | TRACE("max texture units: %d", max); |
---|
[1028] | 169 | } |
---|