Skip to content

Commit 79fecb0

Browse files
committed
Rename Texture to Texture2D and BaseTexture to Texture
TextureType enum exported properly
1 parent 4e01b37 commit 79fecb0

13 files changed

+86
-43
lines changed

fbo.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import Texture from './texture';
1+
import Texture2D from './texture-2d';
22
import RenderBuffer from './renderbuffer';
33
import { GLContext } from './types';
4-
export declare type AttachmentTarget = Texture | RenderBuffer;
4+
export declare type AttachmentTarget = Texture2D | RenderBuffer;
55
export declare class Attachment {
66
level: number;
77
readonly target: AttachmentTarget;
@@ -25,7 +25,7 @@ declare class Fbo {
2525
detach(bindingPoint: GLenum): void;
2626
getAttachment(bindingPoint: GLenum): Attachment | null;
2727
getColor(index?: number): AttachmentTarget | null;
28-
getColorTexture(index?: number): Texture;
28+
getColorTexture(index?: number): Texture2D;
2929
getDepth(): AttachmentTarget | null;
3030
attachColor(format?: GLenum, type?: GLenum, internal?: GLenum): Attachment;
3131
attachDepth(depth?: boolean, stencil?: boolean, useTexture?: boolean): Attachment;

fbo.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Texture from './texture';
1+
import Texture2D from './texture-2d';
22
import RenderBuffer from './renderbuffer';
33
import { isWebgl2 } from './utils';
44
function isTexture(target) {
@@ -103,14 +103,14 @@ class Fbo {
103103
return att ? att.target : null;
104104
}
105105
attachColor(format, type, internal) {
106-
const t = new Texture(this.gl, format, type, internal);
106+
const t = new Texture2D(this.gl, format, type, internal);
107107
return this.attach(0x8ce0, t);
108108
}
109109
attachDepth(depth = true, stencil = false, useTexture = false) {
110110
let attachment;
111111
if (useTexture) {
112112
const cfg = dsTextureConfig(this.gl, stencil);
113-
attachment = new Texture(this.gl, cfg.format, cfg.type, cfg.internal);
113+
attachment = new Texture2D(this.gl, cfg.format, cfg.type, cfg.internal);
114114
}
115115
else {
116116
attachment = new RenderBuffer(this.gl, dsRenderbufferStorage(depth, stencil));

src/fbo.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import Texture from './texture'
1+
import Texture2D from './texture-2d'
22
import RenderBuffer from './renderbuffer'
33
import { GLContext } from './types';
44
import { isWebgl2 } from './utils';
55

6-
function isTexture(target: AttachmentTarget): target is Texture {
6+
function isTexture(target: AttachmentTarget): target is Texture2D {
77
return target.id instanceof WebGLTexture;
88
}
99

1010

1111

12-
function assertIsTexture(target: AttachmentTarget|null, msg:string): asserts target is Texture {
12+
function assertIsTexture(target: AttachmentTarget|null, msg:string): asserts target is Texture2D {
1313
if( target === null || !isTexture(target) ){
1414
throw new Error( msg );
1515
}
1616
}
1717

1818

19-
export type AttachmentTarget = Texture | RenderBuffer;
19+
export type AttachmentTarget = Texture2D | RenderBuffer;
2020

2121
export class Attachment {
2222

@@ -149,9 +149,9 @@ class Fbo {
149149
}
150150

151151

152-
getColorTexture(index: number = 0): Texture {
152+
getColorTexture(index: number = 0): Texture2D {
153153
const res = this.getColor( index );
154-
assertIsTexture( res, "Color attachment {index} is not a texture." );
154+
assertIsTexture( res, `Color attachment ${index} is not a texture.` );
155155
return res;
156156
}
157157

@@ -168,7 +168,7 @@ class Fbo {
168168
* Shortcut to attach texture to color attachment 0
169169
*/
170170
attachColor(format?: GLenum, type?: GLenum, internal?: GLenum) {
171-
const t = new Texture(this.gl, format, type, internal);
171+
const t = new Texture2D(this.gl, format, type, internal);
172172
return this.attach(0x8ce0, t);
173173
}
174174

@@ -183,7 +183,7 @@ class Fbo {
183183

184184
if (useTexture) {
185185
const cfg = dsTextureConfig(this.gl, stencil);
186-
attachment = new Texture(this.gl, cfg.format, cfg.type, cfg.internal);
186+
attachment = new Texture2D(this.gl, cfg.format, cfg.type, cfg.internal);
187187
} else {
188188
attachment = new RenderBuffer(this.gl, dsRenderbufferStorage(depth, stencil));
189189
}

src/texture.ts src/texture-2d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const GL_TEXTURE_2D = 0x0de1;
1212
* @param {GLenum} [type =GL_UNSIGNED_BYTE] the pixel data type, default to gl.UNSIGNED_BYTE
1313
* @param {GLenum} [internal=format] the pixel internal format, default to the same value than 'format' parameter (which must be in webgl 1)
1414
*/
15-
export default class Texture extends AbstractTexture {
15+
export default class Texture2D extends AbstractTexture {
1616

1717
readonly textureType : TextureType.TEXTURE_2D = TextureType.TEXTURE_2D;
1818

src/texture-base.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { GLContext } from './types';
22
import { getTextureFiltering } from './utils';
33
import TextureCube from './texture-cube';
4-
import Texture from './texture';
4+
import Texture2D from './texture-2d';
55

66
let _UID = 0;
77

88

9-
export const enum TextureType {
9+
export enum TextureType {
1010
NONE = 0 ,
1111
TEXTURE_2D = 0x0de1,
1212
TEXTURE_2D_ARRAY = 0x8C1A,
@@ -15,7 +15,7 @@ export const enum TextureType {
1515
}
1616

1717

18-
export type BaseTexture = TextureCube | Texture;
18+
export type Texture = TextureCube | Texture2D;
1919

2020

2121
/**

test/fbo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Fbo from '../fbo'
2-
import Texture2D from '../texture'
2+
import Texture2D from '../texture-2d'
33
import Program from '../program'
44
import Renderbuffer from '../renderbuffer'
55
var expect = require( 'expect.js' );

test/sampler.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
import Sampler from '../sampler'
4-
import Texture from '../texture'
4+
import Texture2D from '../texture-2d'
55
import Program from '../program'
66
var expect = require( 'expect.js' );
77

@@ -69,7 +69,7 @@ describe( "Sampler @WEBGL2", function(){
6969

7070

7171
it( "should render various filtering", function( ){
72-
var tex = new Texture( gl );
72+
var tex = new Texture2D( gl );
7373
tex.fromImage( mireRGB, false );
7474

7575
var sampler = new Sampler( gl )

test/texture.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
import Texture from '../texture'
3+
import Texture2D from '../texture-2d'
44
import Program from '../program'
55

66
var expect = require( 'expect.js' );
@@ -22,7 +22,7 @@ function loadImage( img, src ){
2222
var mireRGB, mireRGBA;
2323
var filltex, filltex16;
2424

25-
describe( "Texture", function(){
25+
describe( "Texture2d", function(){
2626

2727
before(function() {
2828
var vert = require( './glsl/filltex.vert')
@@ -49,33 +49,33 @@ describe( "Texture", function(){
4949

5050

5151
it( "should be exported in nanogl namespace", function(){
52-
expect( Texture ).to.be.ok( );
52+
expect( Texture2D ).to.be.ok( );
5353
});
5454

5555
it( "creation should leave clean gl state", function(){
56-
var tex = new Texture( gl );
56+
var tex = new Texture2D( gl );
5757
testContext.assertNoError();
5858
tex.dispose()
5959
});
6060

6161

6262
it( "dispose should leave clean gl state", function(){
63-
var tex = new Texture( gl );
63+
var tex = new Texture2D( gl );
6464
tex.dispose()
6565
testContext.assertNoError();
6666
});
6767

6868

6969
it( "should load rgb tex", function( ){
70-
var tex = new Texture( gl );
70+
var tex = new Texture2D( gl );
7171
tex.fromImage( mireRGB, false );
7272
tex.dispose();
7373
testContext.assertNoError();
7474
});
7575

7676

7777
it( "should load rgba tex", function( ){
78-
var tex = new Texture( gl );
78+
var tex = new Texture2D( gl );
7979
tex.fromImage( mireRGBA, true );
8080
tex.dispose();
8181
testContext.assertNoError();
@@ -84,7 +84,7 @@ describe( "Texture", function(){
8484

8585

8686
it( "should render rgb tex", function( ){
87-
var tex = new Texture( gl );
87+
var tex = new Texture2D( gl );
8888
tex.fromImage( mireRGB, false );
8989

9090
filltex.bind()
@@ -102,7 +102,7 @@ describe( "Texture", function(){
102102

103103

104104
it( "should render nearest filtering", function( ){
105-
var tex = new Texture( gl );
105+
var tex = new Texture2D( gl );
106106
tex.fromImage( mireRGB, false );
107107

108108
filltex.bind()
@@ -121,7 +121,7 @@ describe( "Texture", function(){
121121

122122

123123
it( "should render linear filtering", function( ){
124-
var tex = new Texture( gl );
124+
var tex = new Texture2D( gl );
125125
tex.fromImage( mireRGB, false );
126126

127127
filltex.bind()
@@ -140,7 +140,7 @@ describe( "Texture", function(){
140140

141141

142142
it( "should render nearest mip filtering", function( ){
143-
var tex = new Texture( gl );
143+
var tex = new Texture2D( gl );
144144
tex.fromImage( mireRGB, false );
145145

146146
filltex.bind()
@@ -160,7 +160,7 @@ describe( "Texture", function(){
160160

161161

162162
it( "should render nearest mip linear filtering", function( ){
163-
var tex = new Texture( gl );
163+
var tex = new Texture2D( gl );
164164
tex.fromImage( mireRGB, false );
165165

166166
filltex.bind()
@@ -180,7 +180,7 @@ describe( "Texture", function(){
180180

181181

182182
it( "should render linear mip linear filtering", function( ){
183-
var tex = new Texture( gl );
183+
var tex = new Texture2D( gl );
184184
tex.fromImage( mireRGB, false );
185185

186186
filltex.bind()
@@ -200,7 +200,7 @@ describe( "Texture", function(){
200200

201201

202202
it( "should render with program sampler helper", function( ){
203-
var tex = new Texture( gl );
203+
var tex = new Texture2D( gl );
204204
tex.fromImage( mireRGB, false );
205205

206206
filltex.bind()
@@ -216,7 +216,7 @@ describe( "Texture", function(){
216216

217217

218218
it( "@should accept Uint8Array RGB data", function( ){
219-
var tex = new Texture( gl, gl.RGB );
219+
var tex = new Texture2D( gl, gl.RGB );
220220
tex.bind();
221221
gl.pixelStorei( gl.UNPACK_ALIGNMENT, 1 );
222222

@@ -242,7 +242,7 @@ describe( "Texture", function(){
242242
});
243243

244244
it( "should accept Uint8Array RGBA data", function( ){
245-
var tex = new Texture( gl, gl.RGBA );
245+
var tex = new Texture2D( gl, gl.RGBA );
246246

247247
var data = new Uint8Array( [
248248
0x10, 0x10, 0x10, 0x60,

texture-2d.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import AbstractTexture, { TextureType } from './texture-base';
2+
import { GLContext } from './types';
3+
export default class Texture2D extends AbstractTexture {
4+
readonly textureType: TextureType.TEXTURE_2D;
5+
_target: GLenum;
6+
constructor(gl: GLContext, format?: GLenum, type?: GLenum, internal?: GLenum);
7+
fromImage(img: TexImageSource): void;
8+
fromData(width: number, height: number, data?: ArrayBufferView | null): void;
9+
}

texture-2d.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import AbstractTexture, { TextureType } from './texture-base';
2+
const GL_TEXTURE_2D = 0x0de1;
3+
export default class Texture2D extends AbstractTexture {
4+
constructor(gl, format, type, internal) {
5+
super(gl, format, type, internal);
6+
this.textureType = TextureType.TEXTURE_2D;
7+
this._target = GL_TEXTURE_2D;
8+
gl.bindTexture(GL_TEXTURE_2D, this.id);
9+
this.setFilter(true);
10+
}
11+
fromImage(img) {
12+
const gl = this.gl;
13+
this.width = img.width;
14+
this.height = img.height;
15+
gl.bindTexture(GL_TEXTURE_2D, this.id);
16+
gl.texImage2D(GL_TEXTURE_2D, 0, this.internal, this.format, this.type, img);
17+
}
18+
fromData(width, height, data = null) {
19+
const gl = this.gl;
20+
this.width = width;
21+
this.height = height;
22+
data = data || null;
23+
gl.bindTexture(GL_TEXTURE_2D, this.id);
24+
gl.texImage2D(GL_TEXTURE_2D, 0, this.internal, width, height, 0, this.format, this.type, data);
25+
}
26+
}

texture-base.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { GLContext } from './types';
22
import TextureCube from './texture-cube';
3-
import Texture from './texture';
4-
export declare const enum TextureType {
3+
import Texture2D from './texture-2d';
4+
export declare enum TextureType {
55
NONE = 0,
66
TEXTURE_2D = 3553,
77
TEXTURE_2D_ARRAY = 35866,
88
TEXTURE_CUBE = 34067,
99
TEXTURE_3D = 32879
1010
}
11-
export declare type BaseTexture = TextureCube | Texture;
11+
export declare type Texture = TextureCube | Texture2D;
1212
export default abstract class AbstractTexture {
1313
readonly textureType: TextureType;
1414
readonly gl: GLContext;

texture-base.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { getTextureFiltering } from './utils';
22
let _UID = 0;
3+
export var TextureType;
4+
(function (TextureType) {
5+
TextureType[TextureType["NONE"] = 0] = "NONE";
6+
TextureType[TextureType["TEXTURE_2D"] = 3553] = "TEXTURE_2D";
7+
TextureType[TextureType["TEXTURE_2D_ARRAY"] = 35866] = "TEXTURE_2D_ARRAY";
8+
TextureType[TextureType["TEXTURE_CUBE"] = 34067] = "TEXTURE_CUBE";
9+
TextureType[TextureType["TEXTURE_3D"] = 32879] = "TEXTURE_3D";
10+
})(TextureType || (TextureType = {}));
311
export default class AbstractTexture {
412
constructor(gl, format, type, internal) {
5-
this.textureType = 0;
13+
this.textureType = TextureType.NONE;
614
this.format = 0;
715
this.internal = 0;
816
this.type = 0;

texture-cube.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import AbstractTexture from './texture-base';
1+
import AbstractTexture, { TextureType } from './texture-base';
22
const GL_TEXTURE_CUBE = 0x8513;
33
export default class TextureCube extends AbstractTexture {
44
constructor(gl, format, type, internal) {
55
super(gl, format, type, internal);
6-
this.textureType = 34067;
6+
this.textureType = TextureType.TEXTURE_CUBE;
77
this._target = GL_TEXTURE_CUBE;
88
gl.bindTexture(GL_TEXTURE_CUBE, this.id);
99
this.setFilter(true);

0 commit comments

Comments
 (0)