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