-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube.js
70 lines (55 loc) · 2.01 KB
/
cube.js
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
const canvas = document.getElementById("webglCanvas");
const gl = canvas.getContext("webgl");
if (!gl) {
alert("WebGL is not available.");
}
// Vertex and Fragment shaders
const vertexShaderSource = `
attribute vec4 a_position;
uniform mat4 u_model;
void main() {
gl_Position = u_model * a_position;
}
`;
const fragmentShaderSource = `
precision mediump float;
void main() {
gl_FragColor = vec4(1, 0, 0, 1); // Cube color: Red
}
`;
function createShader(type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
return shader;
}
const vertexShader = createShader(gl.VERTEX_SHADER, vertexShaderSource);
const fragmentShader = createShader(gl.FRAGMENT_SHADER, fragmentShaderSource);
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
gl.useProgram(program);
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Cube vertices
const vertices = [
// Front face
-0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5,
// ... other 5 faces ...
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
const positionAttributeLocation = gl.getAttribLocation(program, "a_position");
gl.enableVertexAttribArray(positionAttributeLocation);
gl.vertexAttribPointer(positionAttributeLocation, 3, gl.FLOAT, false, 0, 0);
function drawCube(x, y, z, size) {
// Create a simple model matrix to place the cube at (x, y, z) and scale it
const modelMatrix = [size, 0, 0, x, 0, size, 0, y, 0, 0, size, z, 0, 0, 0, 1];
const modelMatrixLocation = gl.getUniformLocation(program, "u_model");
gl.uniformMatrix4fv(modelMatrixLocation, false, modelMatrix);
gl.drawArrays(gl.TRIANGLE_FAN, 0, 4);
// ... repeat for other 5 faces ...
}
gl.clearColor(0.9, 0.9, 0.9, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
drawCube(0.2, 0.2, 0.0, 0.5); // Place cube at (0.2, 0.2, 0.0) with a size of 0.5