Skip to content

Commit ec8c9d4

Browse files
authored
feat: gl.createVertexArray() (#19)
1 parent 6b7b1b6 commit ec8c9d4

File tree

11 files changed

+387
-170
lines changed

11 files changed

+387
-170
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import * as byegl from 'byegl';
2+
import type { ExampleContext } from '../../types.ts';
3+
4+
export default function ({ canvas }: ExampleContext) {
5+
const gl = canvas.getContext('webgl2');
6+
7+
if (!gl) {
8+
throw new Error('WebGL not supported');
9+
}
10+
11+
const vertexShaderSource = `\
12+
#version 300 es
13+
in vec2 a_position;
14+
in vec3 a_color;
15+
16+
out vec3 v_color;
17+
18+
void main() {
19+
gl_Position = vec4(a_position * 0.5, 0.0, 1.0);
20+
v_color = a_color;
21+
}
22+
`;
23+
24+
const fragmentShaderSource = `\
25+
#version 300 es
26+
precision mediump float;
27+
28+
in vec3 v_color;
29+
out vec4 fragColor;
30+
31+
void main() {
32+
fragColor = vec4(v_color, 1.0);
33+
}
34+
`;
35+
36+
const vertexShader = gl.createShader(gl.VERTEX_SHADER) as WebGLShader;
37+
gl.shaderSource(vertexShader, vertexShaderSource);
38+
gl.compileShader(vertexShader);
39+
40+
console.log('Vertex Info: ', gl.getShaderInfoLog(vertexShader));
41+
42+
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER) as WebGLShader;
43+
gl.shaderSource(fragmentShader, fragmentShaderSource);
44+
gl.compileShader(fragmentShader);
45+
46+
console.log('Fragment Info: ', gl.getShaderInfoLog(fragmentShader));
47+
48+
const program = gl.createProgram();
49+
gl.attachShader(program, vertexShader);
50+
gl.attachShader(program, fragmentShader);
51+
gl.linkProgram(program);
52+
53+
console.log('Program Info: ', gl.getProgramInfoLog(program));
54+
55+
if (byegl.isIntercepted(gl)) {
56+
console.log(byegl.getWGSLSource(gl, program));
57+
}
58+
59+
const positionLocation = gl.getAttribLocation(program, 'a_position');
60+
const colorLocation = gl.getAttribLocation(program, 'a_color');
61+
62+
// Create and bind Vertex Array Object
63+
const vao = gl.createVertexArray();
64+
gl.bindVertexArray(vao);
65+
66+
// Position buffer
67+
const positionBuffer = gl.createBuffer();
68+
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
69+
gl.bufferData(
70+
gl.ARRAY_BUFFER,
71+
new Float32Array([
72+
// bottom-left
73+
-1, -1,
74+
// bottom-right
75+
1, -1,
76+
// top-left
77+
-1, 1,
78+
// top-right
79+
1, 1,
80+
]),
81+
gl.STATIC_DRAW,
82+
);
83+
gl.enableVertexAttribArray(positionLocation);
84+
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
85+
86+
// Color buffer
87+
const colorBuffer = gl.createBuffer();
88+
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
89+
gl.bufferData(
90+
gl.ARRAY_BUFFER,
91+
new Uint8Array([
92+
// bottom-left
93+
255, 0, 0,
94+
// bottom-right
95+
255, 0, 255,
96+
// top-left
97+
0, 255, 0,
98+
// top-right
99+
0, 255, 255,
100+
]),
101+
gl.STATIC_DRAW,
102+
);
103+
gl.enableVertexAttribArray(colorLocation);
104+
gl.vertexAttribPointer(colorLocation, 3, gl.UNSIGNED_BYTE, true, 0, 0);
105+
106+
// Index buffer
107+
const indexBuffer = gl.createBuffer();
108+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
109+
gl.bufferData(
110+
gl.ELEMENT_ARRAY_BUFFER,
111+
new Uint16Array([
112+
// first triangle
113+
0, 1, 2,
114+
// second triangle
115+
2, 1, 3,
116+
]),
117+
gl.STATIC_DRAW,
118+
);
119+
120+
// Unbind VAO
121+
gl.bindVertexArray(null);
122+
123+
gl.useProgram(program);
124+
gl.bindVertexArray(vao);
125+
gl.clearColor(0, 0, 0, 1);
126+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
127+
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
128+
gl.bindVertexArray(null);
129+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "Compat: Vertex Array"
3+
}

apps/docs/src/examples/three/cube/meta.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

apps/docs/src/examples/three/cube/index.ts renamed to apps/docs/src/examples/three/webgl_geometry_cube/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@ import type { ExampleContext } from '../../types.ts';
44
export default function ({ canvas }: ExampleContext) {
55
const scene = new THREE.Scene();
66
const camera = new THREE.PerspectiveCamera(
7-
75,
7+
70,
88
canvas.width / canvas.height,
99
0.1,
10-
1000,
10+
100,
1111
);
12+
camera.position.z = 2;
1213

13-
const renderer = new THREE.WebGLRenderer({ canvas });
14-
renderer.setSize(canvas.width, canvas.height);
14+
const texture = new THREE.TextureLoader().load(
15+
'https://raw.githubusercontent.com/mrdoob/three.js/refs/heads/master/examples/textures/crate.gif',
16+
);
17+
texture.colorSpace = THREE.SRGBColorSpace;
18+
19+
const geometry = new THREE.BoxGeometry();
20+
// TODO: Fix texture bug in order to show the crate texture
21+
const material = new THREE.MeshBasicMaterial({ color: 0xff0055 });
1522

16-
const geometry = new THREE.BoxGeometry(1, 1, 1);
17-
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
1823
const cube = new THREE.Mesh(geometry, material);
1924
scene.add(cube);
2025

21-
camera.position.z = 5;
26+
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
27+
renderer.setPixelRatio(window.devicePixelRatio);
28+
renderer.setSize(canvas.width, canvas.height);
2229

2330
function animate() {
2431
cube.rotation.x += 0.01;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "Three.js: geometry / cube",
3+
"draft": true
4+
}

packages/byegl/src/attribute.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { ByeGLBuffer, VertexBufferSegment } from './buffer.ts';
2+
import { $internal } from './types.ts';
3+
4+
/**
5+
* WebGL state related to attributes. There is a global AttributeState, but
6+
* also one per each Vertex Array Object.
7+
*/
8+
export interface AttributeState {
9+
/**
10+
* Set using gl.enableVertexAttribArray and gl.disableVertexAttribArray.
11+
*/
12+
enabledVertexAttribArrays: Set<number>;
13+
14+
vertexBufferSegments: VertexBufferSegment[];
15+
16+
/**
17+
* Set using gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ???).
18+
*/
19+
boundElementArrayBuffer: ByeGLBuffer | null;
20+
}
21+
22+
export class ByeGLVertexArrayObject {
23+
readonly [$internal]: AttributeState;
24+
25+
constructor() {
26+
this[$internal] = {
27+
boundElementArrayBuffer: null,
28+
enabledVertexAttribArrays: new Set(),
29+
vertexBufferSegments: [],
30+
};
31+
}
32+
}

0 commit comments

Comments
 (0)