1 | /*
|
---|
2 | * ----------------------------------------------------------------------
|
---|
3 | * Texture3d.cpp: 3d texture class
|
---|
4 | *
|
---|
5 | * ======================================================================
|
---|
6 | * AUTHOR: Wei Qiao <qiaow@purdue.edu>
|
---|
7 | * Purdue Rendering and Perceptualization Lab (PURPL)
|
---|
8 | *
|
---|
9 | * Copyright (c) 2004-2006 Purdue Research Foundation
|
---|
10 | *
|
---|
11 | * See the file "license.terms" for information on usage and
|
---|
12 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
---|
13 | * ======================================================================
|
---|
14 | */
|
---|
15 |
|
---|
16 | #include "Texture3D.h"
|
---|
17 | #include <stdio.h>
|
---|
18 | #include <assert.h>
|
---|
19 | #include <math.h>
|
---|
20 |
|
---|
21 | #include "config.h"
|
---|
22 |
|
---|
23 | Texture3D::Texture3D(){ id=0; gl_resource_allocated = false; }
|
---|
24 |
|
---|
25 | Texture3D::Texture3D(int width, int height, int depth, GLuint type=GL_FLOAT, GLuint interp=GL_LINEAR, int components=4)
|
---|
26 | {
|
---|
27 | assert(type == GL_UNSIGNED_BYTE || type == GL_FLOAT|| type ==GL_UNSIGNED_INT);
|
---|
28 | assert(interp == GL_LINEAR || interp == GL_NEAREST);
|
---|
29 |
|
---|
30 | this->width = width;
|
---|
31 | this->height = height;
|
---|
32 | this->depth = depth;
|
---|
33 |
|
---|
34 | int m = (width > height) ? width : height;
|
---|
35 | m = (m > depth) ? m : depth;
|
---|
36 |
|
---|
37 | //int m = max(max(width, height), depth);
|
---|
38 | this->aspect_ratio_width = (double)width/(double)m;
|
---|
39 | this->aspect_ratio_height = (double)height/(double)m;
|
---|
40 | this->aspect_ratio_depth = (double)depth/(double)m;
|
---|
41 |
|
---|
42 | this->type = type;
|
---|
43 | this->interp_type = interp;
|
---|
44 | this->n_components = components;
|
---|
45 |
|
---|
46 | this->id = 0;
|
---|
47 | gl_resource_allocated = false;
|
---|
48 | }
|
---|
49 |
|
---|
50 | GLuint Texture3D::initialize(float *data)
|
---|
51 | {
|
---|
52 | //load texture with 16 bit half floating point precision if card is 6 series NV40
|
---|
53 | //half float with linear interpolation is only supported by 6 series and up cards
|
---|
54 | //If NV40 not defined, data is quantized to 8-bit from 32-bit.
|
---|
55 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
---|
56 |
|
---|
57 | glGenTextures(1, &id);
|
---|
58 | glBindTexture(GL_TEXTURE_3D, id);
|
---|
59 | assert(id!=-1);
|
---|
60 |
|
---|
61 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
62 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
63 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
---|
64 |
|
---|
65 | if(interp_type==GL_LINEAR){
|
---|
66 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
67 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
68 | }
|
---|
69 | else{
|
---|
70 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
71 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
72 | }
|
---|
73 |
|
---|
74 | //to do: add handling to more formats
|
---|
75 | if(type==GL_FLOAT){
|
---|
76 | switch(n_components){
|
---|
77 | #ifdef NV40
|
---|
78 | case 1:
|
---|
79 | glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE16F_ARB, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
|
---|
80 | break;
|
---|
81 | case 2:
|
---|
82 | glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA16F_ARB, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
|
---|
83 | break;
|
---|
84 | case 3:
|
---|
85 | glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F_ARB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
|
---|
86 | break;
|
---|
87 | case 4:
|
---|
88 | glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F_ARB, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
|
---|
89 | break;
|
---|
90 | #else
|
---|
91 | case 1:
|
---|
92 | glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
|
---|
93 | break;
|
---|
94 | case 2:
|
---|
95 | glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
|
---|
96 | break;
|
---|
97 | case 3:
|
---|
98 | glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
|
---|
99 | break;
|
---|
100 | case 4:
|
---|
101 | glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
|
---|
102 | break;
|
---|
103 | #endif
|
---|
104 | default:
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | assert(glGetError()==0);
|
---|
111 |
|
---|
112 | gl_resource_allocated = true;
|
---|
113 | return id;
|
---|
114 | }
|
---|
115 |
|
---|
116 | void Texture3D::activate()
|
---|
117 | {
|
---|
118 | glEnable(GL_TEXTURE_3D);
|
---|
119 | glBindTexture(GL_TEXTURE_3D, id);
|
---|
120 | }
|
---|
121 |
|
---|
122 | void Texture3D::deactivate()
|
---|
123 | {
|
---|
124 | glDisable(GL_TEXTURE_3D);
|
---|
125 | }
|
---|
126 |
|
---|
127 | Texture3D::~Texture3D()
|
---|
128 | {
|
---|
129 | glDeleteTextures(1, &id);
|
---|
130 | }
|
---|
131 |
|
---|
132 | void Texture3D::check_max_size(){
|
---|
133 | GLint max = 0;
|
---|
134 | glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_EXT, &max);
|
---|
135 |
|
---|
136 | //printf("%d", glGetError());
|
---|
137 | fprintf(stderr, "max 3d texture size: %d\n", max);
|
---|
138 | }
|
---|
139 |
|
---|
140 | void Texture3D::check_max_unit(){
|
---|
141 | int max;
|
---|
142 | glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
|
---|
143 |
|
---|
144 | fprintf(stderr, "max texture units: %d.\n", max);
|
---|
145 | }
|
---|