GLTexture.m 1.41 KB
Newer Older
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
1 2
#import "GLTexture.h"
#import "RCTLog.h"
3
#import "RCTUtils.h"
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
4 5 6

@implementation GLTexture
{
7 8
  GLuint _handle; // The identifier of the gl texture
  GLImageData* dataCurrentlyUploaded; // The last set data (cache)
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
9 10 11 12 13 14 15 16 17 18 19 20 21
}

- (instancetype)init
{
  self = [super init];
  if (self) {
    [self makeTexture];
  }
  return self;
}

- (void)dealloc
{
22
  glDeleteTextures(1, &_handle);
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
23 24 25 26 27
  dataCurrentlyUploaded = nil;
}

- (void) makeTexture
{
28 29
  glGenTextures(1, &_handle);
  glBindTexture(GL_TEXTURE_2D, _handle);
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
30 31 32 33 34 35 36 37 38
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}

- (int)bind: (int)unit
{
  glActiveTexture(GL_TEXTURE0 + unit);
39
  glBindTexture(GL_TEXTURE_2D, _handle);
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
40 41 42
  return unit;
}

43 44 45 46 47 48 49 50 51 52 53 54
- (void)bind
{
  glBindTexture(GL_TEXTURE_2D, _handle);
}

- (void)setShapeWithWidth:(float)width withHeight:(float)height
{
  [self bind];
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
}

- (void)setPixels: (GLImageData *)data
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
55
{
56 57 58
  GLImageData *d = data==nil ? [GLImageData empty] : data;
  if (d != dataCurrentlyUploaded) {
    dataCurrentlyUploaded = d;
59
    [self bind];
60
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, d.width, d.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, d.data);
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
61 62 63 64
  }
}

@end