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-2006 Purdue Research Foundation |
---|
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 | |
---|
17 | #include "Texture2D.h" |
---|
18 | #include "Trace.h" |
---|
19 | #include <stdio.h> |
---|
20 | #include <assert.h> |
---|
21 | #include <math.h> |
---|
22 | |
---|
23 | #include "config.h" |
---|
24 | |
---|
25 | Texture2D::Texture2D(){} |
---|
26 | |
---|
27 | Texture2D::Texture2D(int width, int height, GLuint type=GL_FLOAT, |
---|
28 | GLuint interp=GL_LINEAR, int n=4, float* data = 0) |
---|
29 | { |
---|
30 | assert(type == GL_UNSIGNED_BYTE || |
---|
31 | type == GL_FLOAT || |
---|
32 | type ==GL_UNSIGNED_INT); |
---|
33 | assert(interp == GL_LINEAR || interp == GL_NEAREST); |
---|
34 | |
---|
35 | this->width = width; |
---|
36 | this->height = height; |
---|
37 | this->type = type; |
---|
38 | this->interp_type = interp; |
---|
39 | this->n_components = n; |
---|
40 | |
---|
41 | this->id = 0; |
---|
42 | |
---|
43 | if(data != 0) |
---|
44 | initialize(data); |
---|
45 | } |
---|
46 | |
---|
47 | GLuint |
---|
48 | Texture2D::initialize(float *data) |
---|
49 | { |
---|
50 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
---|
51 | |
---|
52 | glGenTextures(1, &id); |
---|
53 | glBindTexture(GL_TEXTURE_2D, id); |
---|
54 | assert(id != (GLuint)-1); |
---|
55 | |
---|
56 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
---|
57 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
---|
58 | |
---|
59 | if(interp_type == GL_LINEAR){ |
---|
60 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
61 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
---|
62 | } else { |
---|
63 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
64 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
65 | } |
---|
66 | |
---|
67 | //to do: add handling to more formats |
---|
68 | if (type==GL_FLOAT) { |
---|
69 | switch(n_components){ |
---|
70 | #ifdef NV40 |
---|
71 | case 1: |
---|
72 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16F_ARB, width, height, |
---|
73 | 0, GL_LUMINANCE, GL_FLOAT, data); |
---|
74 | break; |
---|
75 | case 2: |
---|
76 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA16F_ARB, width, |
---|
77 | height, 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data); |
---|
78 | break; |
---|
79 | case 3: |
---|
80 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F_ARB, width, height, 0, |
---|
81 | GL_RGB, GL_FLOAT, data); |
---|
82 | break; |
---|
83 | case 4: |
---|
84 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, |
---|
85 | GL_RGBA, GL_FLOAT, data); |
---|
86 | break; |
---|
87 | #else |
---|
88 | case 1: |
---|
89 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, |
---|
90 | GL_LUMINANCE, GL_FLOAT, data); |
---|
91 | break; |
---|
92 | case 2: |
---|
93 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, width, height, |
---|
94 | 0, GL_LUMINANCE_ALPHA, GL_FLOAT, data); |
---|
95 | break; |
---|
96 | case 3: |
---|
97 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, |
---|
98 | GL_FLOAT, data); |
---|
99 | break; |
---|
100 | case 4: |
---|
101 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, |
---|
102 | GL_FLOAT, data); |
---|
103 | break; |
---|
104 | #endif |
---|
105 | default: |
---|
106 | break; |
---|
107 | } |
---|
108 | } else { |
---|
109 | int comp[5] = { -1, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA }; |
---|
110 | glTexImage2D(GL_TEXTURE_2D, 0, comp[n_components], width, height, 0, |
---|
111 | GL_RGBA, type, data); |
---|
112 | } |
---|
113 | assert(glGetError()==0); |
---|
114 | return id; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | void |
---|
119 | Texture2D::activate() |
---|
120 | { |
---|
121 | glBindTexture(GL_TEXTURE_2D, id); |
---|
122 | glEnable(GL_TEXTURE_2D); |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | void |
---|
127 | Texture2D::deactivate() |
---|
128 | { |
---|
129 | glDisable(GL_TEXTURE_2D); |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | Texture2D::~Texture2D() |
---|
134 | { |
---|
135 | glDeleteTextures(1, &id); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | void |
---|
140 | Texture2D::check_max_size(){ |
---|
141 | GLint max = 0; |
---|
142 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max); |
---|
143 | |
---|
144 | TRACE("max texture size: %d\n", max); |
---|
145 | } |
---|
146 | |
---|
147 | void |
---|
148 | Texture2D::check_max_unit(){ |
---|
149 | int max; |
---|
150 | glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max); |
---|
151 | |
---|
152 | TRACE("max texture units: %d.\n", max); |
---|
153 | } |
---|
154 | |
---|