7006df5f
David Mayerich
reformat of direc...
|
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
///This class stores an OpenGL texture map and is used by rts_glShaderProgram.
class glTexture
{
private:
void get_type() //guesses the texture type based on the size
{
if(size[1] == 0)
texture_type = GL_TEXTURE_1D;
else if(size[2] == 0)
texture_type = GL_TEXTURE_2D;
else
texture_type = GL_TEXTURE_3D;
}
void set_wrapping() //set the texture wrapping based on the dimensions
{
CHECK_OPENGL_ERROR
switch(texture_type)
{
case GL_TEXTURE_3D:
glTexParameteri(texture_type, GL_TEXTURE_WRAP_R_EXT, GL_REPEAT);
case GL_TEXTURE_2D:
glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
case GL_TEXTURE_1D:
glTexParameteri(texture_type, GL_TEXTURE_WRAP_S, GL_REPEAT);
break;
case GL_TEXTURE_RECTANGLE_ARB:
glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
break;
default:
break;
}
CHECK_OPENGL_ERROR
}
//void set_bits(GLvoid* bits);
public:
vector<GLsizei, 3> size; //vector representing the size of the texture
GLuint name; //texture name assigned by OpenGL
GLenum texture_type; //1D, 2D, 3D
GLint internal_format; //number of components (ex. 4 for RGBA)
GLenum pixel_format; //type of data (RGBA, LUMINANCE)
GLenum data_type; //data type of the bits (float, int, etc.)
//constructor
glTexture()
{
name = 0;
}
glTexture(GLvoid *bits,
GLenum type = GL_TEXTURE_2D,
GLsizei width = 256,
GLsizei height = 256,
GLsizei depth = 0,
GLint internalformat = 1,
GLenum format = GL_LUMINANCE,
GLenum datatype = GL_UNSIGNED_BYTE,
GLint interpolation = GL_LINEAR)
{
init(bits, type, width, height, depth, internalformat, format, datatype, interpolation);
}
void begin()
{
glEnable(texture_type);
CHECK_OPENGL_ERROR
glBindTexture(texture_type, name);
CHECK_OPENGL_ERROR
}
void end()
{
glDisable(texture_type);
CHECK_OPENGL_ERROR
}
///Creates an OpenGL texture map. This function requires basic information about the texture map as well as a pointer to the bit data describing the texture.
void init(GLvoid *bits,
GLenum type = GL_TEXTURE_2D,
GLsizei width = 256,
GLsizei height = 256,
GLsizei depth = 0,
GLint internalformat = 1,
GLenum format = GL_LUMINANCE,
GLenum datatype = GL_UNSIGNED_BYTE,
GLint interpolation = GL_LINEAR)
{
CHECK_OPENGL_ERROR
if(name != 0)
glDeleteTextures(1, &name);
CHECK_OPENGL_ERROR
if(datatype == GL_FLOAT)
{
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); //I honestly don't know what this does but it fixes problems
}
else if(datatype == GL_UNSIGNED_BYTE)
{
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//glPixelStorei(GL_PACK_ALIGNMENT, 1);
}
else if(datatype == GL_UNSIGNED_SHORT)
{
//glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
//glPixelStorei(GL_PACK_ALIGNMENT, 2);
}
CHECK_OPENGL_ERROR
glGenTextures(1, &name); //get the texture name from OpenGL
//cout<<"OpenGL Name: "<<name<<endl;
CHECK_OPENGL_ERROR
size = vector<GLsizei, 3>(width, height, depth); //assign the texture size
//get_type(); //guess the type based on the size
texture_type = type; //set the type of texture
glEnable(texture_type); //enable the texture map
CHECK_OPENGL_ERROR
glBindTexture(texture_type, name); //bind the texture for editing
CHECK_OPENGL_ERROR
set_wrapping(); //set the texture wrapping parameters
CHECK_OPENGL_ERROR
glTexParameteri(texture_type, GL_TEXTURE_MAG_FILTER, interpolation); //set filtering
CHECK_OPENGL_ERROR
glTexParameteri(texture_type, GL_TEXTURE_MIN_FILTER, interpolation);
CHECK_OPENGL_ERROR
internal_format = internalformat; //set the number of components per pixel
pixel_format = format; //set the pixel format
data_type = datatype; //set the data type
SetBits(bits); //send the bits to the OpenGL driver
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //replace the specified vertex color
CHECK_OPENGL_ERROR
glDisable(texture_type);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
void Clean()
{
if(name != 0)
{
glDeleteTextures(1, &name);
CHECK_OPENGL_ERROR
name = 0;
}
}
void SetBits(GLvoid *bits)
{
glEnable(texture_type); //enable the texture map
CHECK_OPENGL_ERROR
glBindTexture(texture_type, name);
CHECK_OPENGL_ERROR
switch(texture_type)
{
case GL_TEXTURE_3D:
glTexImage3D(texture_type, 0, internal_format, size[0], size[1], size[2], 0, pixel_format, data_type, bits);
CHECK_OPENGL_ERROR
break;
case GL_TEXTURE_2D:
case GL_TEXTURE_RECTANGLE_ARB:
glTexImage2D(texture_type, 0, internal_format, size[0], size[1], 0, pixel_format, data_type, bits);
CHECK_OPENGL_ERROR
break;
case GL_TEXTURE_1D:
glTexImage1D(texture_type, 0, internal_format, size[0], 0, pixel_format, data_type, bits);
CHECK_OPENGL_ERROR
break;
default:
//glTexImage2D(texture_type, 0, internal_format, size.x, size.y, 0, pixel_format, data_type, bits);
break;
}
CHECK_OPENGL_ERROR
}
void ResetBits(GLvoid *bits)
{
glEnable(texture_type); //enable the texture map
CHECK_OPENGL_ERROR
glBindTexture(texture_type, name);
CHECK_OPENGL_ERROR
switch(texture_type)
{
case GL_TEXTURE_3D:
//glTexImage3D(texture_type, 0, internal_format, size.x, size.y, size.z, 0, pixel_format, data_type, bits);
break;
case GL_TEXTURE_2D:
case GL_TEXTURE_RECTANGLE_ARB:
glTexSubImage2D(texture_type, 0, 0, 0, size[0], size[1], pixel_format, data_type, bits);
CHECK_OPENGL_ERROR
break;
case GL_TEXTURE_1D:
//glTexImage1D(texture_type, 0, internal_format, size.x, 0, pixel_format, data_type, bits);
break;
default:
//glTexImage2D(texture_type, 0, internal_format, size.x, size.y, 0, pixel_format, data_type, bits);
break;
}
glDisable(texture_type);
CHECK_OPENGL_ERROR
}
void* GetBits(GLenum format, GLenum type)
{
//returns the texture data
int components;
switch(format)
{
case GL_RED:
case GL_GREEN:
case GL_BLUE:
case GL_ALPHA:
case GL_LUMINANCE:
components = 1;
break;
case GL_LUMINANCE_ALPHA:
components = 2;
break;
case GL_RGB:
case GL_BGR:
components = 3;
break;
case GL_RGBA:
case GL_BGRA:
components = 4;
break;
}
int type_size;
switch(type)
{
case GL_UNSIGNED_BYTE:
case GL_BYTE:
type_size = sizeof(char);
break;
case GL_UNSIGNED_SHORT:
case GL_SHORT:
type_size = sizeof(short);
break;
case GL_UNSIGNED_INT:
case GL_INT:
type_size = sizeof(int);
break;
case GL_FLOAT:
type_size = sizeof(float);
break;
}
//allocate memory for the texture
void* result = malloc(components*type_size * size[0] * size[1]);
begin();
glGetTexImage(texture_type, 0, format, type, result);
CHECK_OPENGL_ERROR
end();
return result;
}
};
}
#define RTS_UNKNOWN 0
#endif
|