Lines Matching refs:gl
2 import { ExpoWebGLRenderingContext, GLView } from 'expo-gl';
22 function initializeContext(gl: ExpoWebGLRenderingContext, asset: Asset): RenderContext {
49 const vert = gl.createShader(gl.VERTEX_SHADER)!;
50 gl.shaderSource(vert, vertShader);
51 gl.compileShader(vert);
53 const frag = gl.createShader(gl.FRAGMENT_SHADER)!;
54 gl.shaderSource(frag, fragShader);
55 gl.compileShader(frag);
57 const program = gl.createProgram()!;
58 gl.attachShader(program, vert);
59 gl.attachShader(program, frag);
60 gl.linkProgram(program);
61 gl.useProgram(program);
63 const buffer = gl.createBuffer();
64 gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
65 gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
66 const positionAttrib = gl.getAttribLocation(program, 'a_position');
67 gl.enableVertexAttribArray(positionAttrib);
68 gl.vertexAttribPointer(positionAttrib, 2, gl.FLOAT, false, 0, 0);
70 const texture = gl.createTexture();
71 gl.activeTexture(gl.TEXTURE0);
72 gl.bindTexture(gl.TEXTURE_2D, texture);
73 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
74 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
75 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
76 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
77 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, asset as any);
79 const textureLocation = gl.getUniformLocation(program, 'u_texture');
80 const rotationLocation = gl.getUniformLocation(program, 'u_translate')!;
82 gl.clearColor(0, 0, 0, 0);
83 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
85 gl.uniform1i(textureLocation, 0);
91 onInit(gl: ExpoWebGLRenderingContext): RenderContext;
92 onRender(gl: ExpoWebGLRenderingContext, ctx: RenderContext): void;
99 const [gl, setGl] = useState<ExpoWebGLRenderingContext>();
104 if (!gl) {
121 })(gl.contextId);
123 const ctx = onInit(gl);
125 onRender(gl, ctx);
137 }, [gl, ...dependencies]);
138 return (gl: ExpoWebGLRenderingContext) => {
139 setGl(gl);
171 onInit: (gl: ExpoWebGLRenderingContext) => {
173 return initializeContext(gl, assets?.[0]!);
176 gl: ExpoWebGLRenderingContext,
180 gl.clearColor(0, 0, 0, 0);
181 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
182 gl.uniform2fv(rotationLocation, [
183 (translation.x.value * 2) / gl.drawingBufferWidth,
184 (translation.y.value * 2) / gl.drawingBufferHeight,
186 gl.drawArrays(gl.TRIANGLES, 0, verticesLength / 2);
187 gl.flush();
188 gl.flushEXP();
189 gl.endFrameEXP();