source: trunk/vizservers/nanovis/Texture3D.cpp @ 750

Last change on this file since 750 was 418, checked in by qiaow, 18 years ago

Minor fixes.

File size: 4.4 KB
Line 
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
23Texture3D::Texture3D(){ id=0; gl_resource_allocated = false; }
24
25Texture3D::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 = 1.;
39        this->aspect_ratio_height = (double)height/(double)width;
40        this->aspect_ratio_depth = (double)depth/(double)width;
41
42        //this->aspect_ratio_width = (double)width/(double)m;
43        //this->aspect_ratio_height = (double)height/(double)m;
44        //this->aspect_ratio_depth = (double)depth/(double)m;
45
46        this->type = type;
47        this->interp_type = interp;
48        this->n_components = components;
49
50        this->id = 0;
51        gl_resource_allocated = false;
52}
53
54GLuint Texture3D::initialize(float *data)
55{
56        //load texture with 16 bit half floating point precision if card is 6 series NV40
57        //half float with linear interpolation is only supported by 6 series and up cards
58        //If NV40 not defined, data is quantized to 8-bit from 32-bit.
59        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
60
61        glGenTextures(1, &id);
62        glBindTexture(GL_TEXTURE_3D, id);
63        assert(id!=-1);
64
65        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
66        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
67        glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
68
69        if(interp_type==GL_LINEAR){
70                glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
71                glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
72        }
73        else{
74                glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
75                glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
76        }
77
78        //to do: add handling to more formats
79        if(type==GL_FLOAT){
80          switch(n_components){
81            #ifdef NV40
82                case 1:
83                        glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE16F_ARB, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
84                        break;
85                case 2:
86                        glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA16F_ARB, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
87                        break;
88                case 3:
89                        glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F_ARB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
90                        break;
91                case 4:
92                        glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F_ARB, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
93                        break;
94            #else
95                case 1:
96                        glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, width, height, depth, 0, GL_LUMINANCE, GL_FLOAT, data);
97                        break;
98                case 2:
99                        glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE_ALPHA, width, height, depth, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
100                        break;
101                case 3:
102                        glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, width, height, depth, 0, GL_RGB, GL_FLOAT, data);
103                        break;
104                case 4:
105                        glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, width, height, depth, 0, GL_RGBA, GL_FLOAT, data);
106                        break;
107            #endif
108                default:
109                        break;
110          }
111        }
112
113
114        assert(glGetError()==0);
115       
116        gl_resource_allocated = true;
117        return id;
118}
119
120void Texture3D::activate()
121{
122        glEnable(GL_TEXTURE_3D);
123        glBindTexture(GL_TEXTURE_3D, id);
124}
125
126void Texture3D::deactivate()
127{
128        glDisable(GL_TEXTURE_3D);               
129}
130
131Texture3D::~Texture3D()
132{
133        glDeleteTextures(1, &id);
134}
135
136void Texture3D::check_max_size(){
137        GLint max = 0;
138        glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_EXT, &max);
139       
140        //printf("%d", glGetError());
141        fprintf(stderr, "max 3d texture size: %d\n", max);
142}
143
144void Texture3D::check_max_unit(){
145        int max;
146        glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max);
147
148        fprintf(stderr, "max texture units: %d.\n", max);
149}
Note: See TracBrowser for help on using the repository browser.