/
texture.c
442 lines (368 loc) · 12.7 KB
/
texture.c
1
2
3
4
5
6
7
8
9
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include "texture.h"
VALUE rb_cTexture;
// Helpers
static VALUE texture_allocate(VALUE klass);
static void texture_mark(Texture* texture);
static void texture_free(Texture* texture);
static void ensure_cache_exists(VALUE self, Texture* texture);
static void ensure_fbo_exists(Texture* texture);
void Init_Ashton_Texture(VALUE module)
{
rb_cTexture = rb_define_class_under(module, "Texture", rb_cObject);
rb_define_alloc_func(rb_cTexture, texture_allocate);
rb_define_protected_method(rb_cTexture, "initialize_", Ashton_Texture_init, 3);
rb_define_protected_method(rb_cTexture, "render_", Ashton_Texture_render, 0);
rb_define_method(rb_cTexture, "cache", Ashton_Texture_get_cache, 0);
rb_define_method(rb_cTexture, "width", Ashton_Texture_get_width, 0);
rb_define_method(rb_cTexture, "height", Ashton_Texture_get_height, 0);
rb_define_method(rb_cTexture, "fbo_id", Ashton_Texture_get_fbo_id, 0);
rb_define_method(rb_cTexture, "id", Ashton_Texture_get_id, 0);
rb_define_method(rb_cTexture, "[]", Ashton_Texture_get_pixel, 2);
rb_define_method(rb_cTexture, "rgba", Ashton_Texture_get_rgba_array, 2);
rb_define_method(rb_cTexture, "red", Ashton_Texture_get_red, 2);
rb_define_method(rb_cTexture, "green", Ashton_Texture_get_green, 2);
rb_define_method(rb_cTexture, "blue", Ashton_Texture_get_blue, 2);
rb_define_method(rb_cTexture, "alpha", Ashton_Texture_get_alpha, 2);
rb_define_method(rb_cTexture, "transparent?", Ashton_Texture_is_transparent, 2);
rb_define_method(rb_cTexture, "refresh_cache", Ashton_Texture_refresh_cache, 0);
rb_define_method(rb_cTexture, "to_blob", Ashton_Texture_to_blob, 0);
rb_define_method(rb_cTexture, "draw", Ashton_Texture_draw, -1);
}
//
VALUE Ashton_Texture_get_width(VALUE self)
{
TEXTURE();
return UINT2NUM(texture->width);
}
//
VALUE Ashton_Texture_get_height(VALUE self)
{
TEXTURE();
return UINT2NUM(texture->height);
}
//
static void ensure_fbo_exists(Texture* texture)
{
if(!texture->fbo_id)
{
glGenFramebuffersEXT(1, &texture->fbo_id);
// Bind our already existing texture to the FBO.
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texture->fbo_id);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, texture->id, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
}
VALUE Ashton_Texture_get_fbo_id(VALUE self)
{
TEXTURE();
ensure_fbo_exists(texture);
return UINT2NUM(texture->fbo_id);
}
VALUE Ashton_Texture_get_id(VALUE self)
{
TEXTURE();
return UINT2NUM(texture->id);
}
//
VALUE Ashton_Texture_init(VALUE self, VALUE width, VALUE height, VALUE blob)
{
TEXTURE();
if(!GL_EXT_framebuffer_object)
{
rb_raise(rb_eRuntimeError, "Ashton::Texture requires GL_EXT_framebuffer_object, which is not supported by OpenGL");
}
texture->width = NUM2UINT(width);
texture->height = NUM2UINT(height);
texture->rb_cache = Qnil;
// Create the texture.
glGenTextures(1, &texture->id);
glBindTexture(GL_TEXTURE_2D, texture->id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// MAG_FILTER set on each draw
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Create the texture, either undefined or based on RGBA blob data string.
char* data;
if(NIL_P(blob))
{
data = NULL; // Create an empty texture, that might be filled with junk.
}
else
{
data = StringValuePtr(blob); // Create from blob data.
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texture->width,
texture->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
// Ensure the texture was created.
GLint created_width;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &created_width);
if(created_width == 0) {
rb_raise(rb_eArgError, "Unable to create a texture of size %dx%d",
texture->width, texture->height);
}
// Be as lazy as possible in creating the actual FBO :)
return Qnil;
}
VALUE Ashton_Texture_render(VALUE self)
{
TEXTURE();
ensure_fbo_exists(texture);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texture->fbo_id);
return Qnil;
}
//
static VALUE texture_allocate(VALUE klass)
{
Texture* texture = ALLOC(Texture);
memset(texture, 0, sizeof(Texture));
return Data_Wrap_Struct(klass, texture_mark, texture_free, texture);
}
//
static void texture_mark(Texture* texture)
{
if(!NIL_P(texture->rb_cache)) rb_gc_mark(texture->rb_cache);
}
//
static void texture_free(Texture* texture)
{
// FBO might never have been created.
if(texture->fbo_id) glDeleteFramebuffersEXT(1, &texture->fbo_id);
glDeleteTextures(1, &texture->id);
xfree(texture);
}
//
void ensure_cache_exists(VALUE self, Texture* texture)
{
if(NIL_P(texture->rb_cache))
{
texture->rb_cache = rb_funcall(rb_cPixelCache, rb_intern("new"), 1, self);
}
}
// Returns the cache. Creates it if necessary.
VALUE Ashton_Texture_get_cache(VALUE self)
{
TEXTURE();
ensure_cache_exists(self, texture);
return texture->rb_cache;
}
//
VALUE Ashton_Texture_get_pixel(VALUE self, VALUE x, VALUE y)
{
TEXTURE();
ensure_cache_exists(self, texture);
return Ashton_PixelCache_get_pixel(texture->rb_cache, x, y);
}
VALUE Ashton_Texture_get_rgba_array(VALUE self, VALUE x, VALUE y)
{
TEXTURE();
ensure_cache_exists(self, texture);
return Ashton_PixelCache_get_rgba_array(texture->rb_cache, x, y);
}
//
VALUE Ashton_Texture_get_red(VALUE self, VALUE x, VALUE y)
{
TEXTURE();
ensure_cache_exists(self, texture);
return Ashton_PixelCache_get_red(texture->rb_cache, x, y);
}
//
VALUE Ashton_Texture_get_green(VALUE self, VALUE x, VALUE y)
{
TEXTURE();
ensure_cache_exists(self, texture);
return Ashton_PixelCache_get_green(texture->rb_cache, x, y);
}
//
VALUE Ashton_Texture_get_blue(VALUE self, VALUE x, VALUE y)
{
TEXTURE();
ensure_cache_exists(self, texture);
return Ashton_PixelCache_get_blue(texture->rb_cache, x, y);
}
//
VALUE Ashton_Texture_get_alpha(VALUE self, VALUE x, VALUE y)
{
TEXTURE();
ensure_cache_exists(self, texture);
return Ashton_PixelCache_get_alpha(texture->rb_cache, x, y);
}
//
VALUE Ashton_Texture_is_transparent(VALUE self, VALUE x, VALUE y)
{
TEXTURE();
ensure_cache_exists(self, texture);
return Ashton_PixelCache_is_transparent(texture->rb_cache, x, y);
}
VALUE Ashton_Texture_refresh_cache(VALUE self)
{
TEXTURE();
ensure_cache_exists(self, texture);
return Ashton_PixelCache_refresh(texture->rb_cache);
}
VALUE Ashton_Texture_to_blob(VALUE self)
{
TEXTURE();
ensure_cache_exists(self, texture);
return Ashton_PixelCache_to_blob(texture->rb_cache);
}
// ----------------------------------------
static VALUE draw_block(VALUE yield_value, VALUE parameters, int argc, VALUE argv[])
{
VALUE self = rb_ary_entry(parameters, 0);
float x = (float)NUM2DBL(rb_ary_entry(parameters, 1));
float y = (float)NUM2DBL(rb_ary_entry(parameters, 2));
VALUE blend_mode = rb_ary_entry(parameters, 3);
VALUE color = rb_ary_entry(parameters, 4);
VALUE shader = rb_ary_entry(parameters, 5);
VALUE multitexture = rb_ary_entry(parameters, 6);
VALUE pixelated = rb_ary_entry(parameters, 7);
TEXTURE(); // Uses 'self' value, so can't be at start of function.
if(!NIL_P(shader))
{
VALUE options = rb_hash_new();
rb_hash_aset(options, SYMBOL("required"), Qfalse);
VALUE location = rb_funcall(shader, rb_intern("send"), 3, SYMBOL("uniform_location"), SYMBOL("texture_enabled"), options);
if(FIX2INT(location) != -1)
{
rb_funcall(shader, rb_intern("send"), 3, SYMBOL("set_uniform"), location, Qtrue);
}
rb_funcall(shader, rb_intern("color="), 1, color);
}
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture->id);
// Pixelate or smooth, depending on preference.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
RTEST(pixelated) ? GL_NEAREST : GL_LINEAR);
// Set blending mode.
// Don't need an 'else' clause, since we checked before.
ID blend_id = SYM2ID(blend_mode);
if(blend_id == rb_intern(DRAW_MODE_ALPHA_BLEND))
{
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
else if(blend_id == rb_intern(DRAW_MODE_ADD))
{
glBlendFunc(GL_ONE, GL_ONE);
}
else if(blend_id == rb_intern(DRAW_MODE_MULTIPLY))
{
glBlendFunc(GL_DST_COLOR, GL_ZERO);
}
else if(blend_id == rb_intern(DRAW_MODE_REPLACE))
{
glBlendFunc(GL_ONE, GL_ZERO);
}
// Enable multitexturing.
if(!NIL_P(multitexture))
{
uint texture_id = NUM2UINT(rb_funcall(multitexture, rb_intern("id"), 0));
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture_id);
}
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0);
glMultiTexCoord2d(GL_TEXTURE1, 0.0, 0.0);
glVertex2d(x, y + texture->height); // BL
glTexCoord2d(0.0, 1.0);
glMultiTexCoord2d(GL_TEXTURE1, 0.0, 1.0);
glVertex2d(x, y); // TL
glTexCoord2d(1.0, 1.0);
glMultiTexCoord2d(GL_TEXTURE1, 1.0, 1.0);
glVertex2d(x + texture->width, y); // TR
glTexCoord2d(1.0, 0.0);
glMultiTexCoord2d(GL_TEXTURE1, 1.0, 0.0);
glVertex2d(x + texture->width, y + texture->height); // BR
glEnd();
// Disable multitexturing.
if(!NIL_P(multitexture))
{
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
}
return Qnil;
}
// ----------------------------------------
VALUE Ashton_Texture_draw(int argc, VALUE argv[], VALUE self)
{
// TEXTURE(); // This isn't actually needed in this function.
VALUE x, y, z, options;
VALUE shader, blend_mode, color, multitexture, pixelated;
if(rb_scan_args(argc, argv, "31", &x, &y, &z, &options) == 4)
{
// Get :shader
shader = rb_hash_aref(options, SYMBOL("shader"));
if(!NIL_P(shader) && !rb_obj_is_kind_of(shader, rb_cShader))
{
rb_raise(rb_eTypeError, "Expected :shader option of type Ashton::Shader");
}
// Get draw :mode
blend_mode = rb_hash_aref(options, SYMBOL("mode"));
if(NIL_P(blend_mode))
{
blend_mode = SYMBOL(DRAW_MODE_ALPHA_BLEND);
}
else
{
Check_Type(blend_mode, T_SYMBOL);
ID blend_id = SYM2ID(blend_mode);
if(!(blend_id == rb_intern(DRAW_MODE_ALPHA_BLEND) ||
blend_id == rb_intern(DRAW_MODE_ADD) ||
blend_id == rb_intern(DRAW_MODE_MULTIPLY) ||
blend_id == rb_intern(DRAW_MODE_REPLACE)))
{
rb_raise(rb_eArgError, "Unsupported draw :mode, :%s", rb_id2name(blend_id));
}
}
// Get :color
color = rb_hash_aref(options, SYMBOL("color"));
if(NIL_P(color))
{
color = UINT2NUM(0xffffffff);
}
else if(!rb_obj_is_kind_of(color, rb_cColor))
{
rb_raise(rb_eTypeError, "Expecting :color to be a Gosu::Color");
}
// Get :multitexture
multitexture = rb_hash_aref(options, SYMBOL("multitexture"));
if(!NIL_P(multitexture) && !rb_obj_is_kind_of(multitexture, rb_cTexture))
{
rb_raise(rb_eTypeError, "Expected :multitexture option of type Ashton::Texture");
}
// Get :pixelated
pixelated = rb_hash_aref(options, SYMBOL("pixelated"));
if(NIL_P(pixelated))
{
pixelated = rb_funcall(rb_cTexture, rb_intern("pixelated?"), 0);
}
}
else
{
shader = multitexture = Qnil;
blend_mode = SYMBOL(DRAW_MODE_ALPHA_BLEND);
color = UINT2NUM(0xffffffff);
pixelated = rb_funcall(rb_cTexture, rb_intern("pixelated?"), 0);
}
// Enable the shader, if provided.
if(!NIL_P(shader)) rb_funcall(shader, rb_intern("enable"), 1, z);
// Create a GL block to do the actual drawing.
VALUE window = rb_gv_get("$window");
VALUE block_argv[1];
block_argv[0] = z;
VALUE parameters = rb_ary_new();
rb_ary_push(parameters, self);
rb_ary_push(parameters, x);
rb_ary_push(parameters, y);
rb_ary_push(parameters, blend_mode);
rb_ary_push(parameters, color);
rb_ary_push(parameters, shader);
rb_ary_push(parameters, multitexture);
rb_ary_push(parameters, pixelated);
rb_block_call(window, rb_intern("gl"), 1, block_argv,
RUBY_METHOD_FUNC(draw_block), parameters);
// Disable the shader, if provided.
if(!NIL_P(shader)) rb_funcall(shader, rb_intern("disable"), 1, z);
return Qnil;
}