Skip to content

Commit 439fdc1

Browse files
authored
Merge pull request #45 from t0gre/math-lib-tweaks
Math lib tweaks
2 parents 37cf6fd + d7d6d83 commit 439fdc1

13 files changed

Lines changed: 514 additions & 410 deletions

demo-01-ts-webgl/index.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import { Camera } from './lib/camera'
66
import { initBasicRenderProgram, drawScene, createShadowMap, initShadowRenderProgram } from './lib/BasicRenderProgram'
77
import { loadObj } from './lib/loaders/ObjLoader'
88
import { InputState } from './lib/input'
9-
import { m4fromPositionAndEuler, m4lookAt, m4vectorMultiply, m4yRotation } from './lib/mat4'
9+
import { m4fromPositionAndEuler, m4lookAt } from './lib/mat4'
1010
import { initGlState } from './lib/gl'
1111
import { getWorldRayFromClipSpaceAndCamera, rayIntersectsScene, sortBySceneDepth } from './lib/raycast'
1212
import { getPointerClickInClipSpace } from './lib/events'
13-
import { Vec3, Vec4, calculateOrbitPosition } from './lib/vec'
13+
import { Color, POS_ORIGIN, ROT_NONE, Vec3, calculateOrbitPosition } from './lib/vec'
1414

1515

1616
type NodeName = "yellow tree" | "orange tree" | "green tree" | "floor";
1717

18-
const meshColorMap: Record<NodeName, Vec3> = {
19-
"yellow tree": [0.7, 0.7, 0.01 ],
20-
"orange tree": [0.6, 0.3, 0.001],
21-
"green tree": [0.1, 0.6, 0.1],
22-
"floor": [0.2, 0.2, 0.4]
18+
const meshColorMap: Record<NodeName, Color> = {
19+
"yellow tree": { r: 0.7, g: 0.7, b: 0.01 },
20+
"orange tree": { r: 0.6, g: 0.3, b: 0.001 },
21+
"green tree": { r: 0.1, g: 0.6, b: 0.1 },
22+
"floor": { r: 0.2, g: 0.2, b: 0.4}
2323
};
2424

2525
type Orbit = {
@@ -33,7 +33,7 @@ const orbit: Orbit = {
3333
azimuth: Math.PI * -0.2, // horizontal angle, in radians
3434
elevation: 3 * Math.PI / 4, // vertical angle, in radians
3535
radius: 15,
36-
target: [-3, 2, -2],
36+
target: {x: -3, y: 2, z: -2},
3737
sensitivity: 0.01,
3838
}
3939

@@ -78,35 +78,35 @@ if (!basicRenderProgram) {
7878

7979
const vertices = await loadObj('/rainbowtree.obj');
8080

81-
const yellowTree = initSceneNode(m4fromPositionAndEuler( [0,0,0], [0, Math.PI /2, 0]),
81+
const yellowTree = initSceneNode(m4fromPositionAndEuler( POS_ORIGIN, { x: 0, y: Math.PI /2, z: 0}),
8282
{
8383
vertices,
8484
material: {
8585
color: meshColorMap["yellow tree"],
86-
specularColor: [0.2,0.2,0.2],
86+
specularColor: { r: 0.2, g: 0.2, b:0.2 },
8787
shininess: 0.9
8888
}},
8989
"yellow tree")
9090

9191

9292
const orangeTree = initSceneNode(
93-
m4fromPositionAndEuler( [5,0,0], [0, Math.PI /2, 0]),
93+
m4fromPositionAndEuler( { x: 5, y: 0, z: 0 }, { x: 0, y: Math.PI /2, z: 0 }),
9494
{
9595
vertices,
9696
material: {
9797
color: meshColorMap["orange tree"],
98-
specularColor: [0.2,0.2,0.2],
98+
specularColor: { r: 0.2, g: 0.2, b: 0.2},
9999
shininess: 0.9
100100
}},
101101
"orange tree")
102102

103103
const greenTree = initSceneNode(
104-
m4fromPositionAndEuler( [5,0,0], [0, Math.PI /2, 0]),
104+
m4fromPositionAndEuler( { x: 5, y: 0, z: 0}, { x: 0, y: Math.PI /2, z: 0 }),
105105
{
106106
vertices,
107107
material: {
108108
color: meshColorMap["green tree"],
109-
specularColor: [0.2,0.2,0.2],
109+
specularColor: { r: 0.2, g: 0.2, b: 0.2},
110110
shininess: 0.5
111111
}},
112112
"green tree")
@@ -145,12 +145,12 @@ const floorVertices: Vertices = {
145145
}
146146

147147

148-
const floorNode = initSceneNode(m4fromPositionAndEuler( [0,0.1,0], [0, 0, 0]),
148+
const floorNode = initSceneNode(m4fromPositionAndEuler( { x: 0, y: 0.1, z: 0}, ROT_NONE),
149149
{
150150
vertices: floorVertices,
151151
material: {
152152
color: meshColorMap["floor"],
153-
specularColor: [0.2,0.2,0.2],
153+
specularColor: { r: 0.2, g: 0.2, b: 0.2},
154154
shininess: 0.9
155155
}},
156156
"floor")
@@ -161,17 +161,17 @@ const scene = [yellowTree, floorNode]
161161

162162
// create lights
163163
const ambientLight: AmbientLight = {
164-
color: [0.2, 0.2, 0.2]
164+
color: { r: 0.2, g: 0.2, b: 0.2 }
165165
};
166166

167167
const directionalLight: DirectionalLight = {
168-
rotation : [ 0.0, -0.8 , -0.5],
169-
color : [0.6, 0.6, 0.6],
168+
rotation : { x: 0, y: -0.8 , z: -0.5},
169+
color : { r: 0.6, g: 0.6, b: 0.6 },
170170
};
171171

172172
const pointLight: PointLight = {
173-
position: [ 0, 5.0, 5],
174-
color: [ 0.7, 0.7, 0.7],
173+
position: { x: 0, y: 5.0, z: 5 },
174+
color: {r: 0.7, g: 0.7, b: 0.7 },
175175
constant: 1.0,
176176
linear: 0.009,
177177
quadratic: 0.032
@@ -184,7 +184,7 @@ const cameraPosition = calculateOrbitPosition(
184184
orbit.radius
185185
);
186186

187-
const up: Vec3 = [0, 1, 0]
187+
const up: Vec3 = {x: 0, y: 1, z: 0 }
188188
const camera: Camera = {
189189
fieldOfViewRadians: degToRad(60),
190190
aspect: canvas.clientWidth / canvas.clientHeight,
@@ -197,7 +197,7 @@ const camera: Camera = {
197197

198198

199199
const input: InputState = {
200-
pointerPosition: [0,0]
200+
pointerPosition: {x: 0, y: 0 }
201201
}
202202

203203

@@ -250,12 +250,12 @@ canvas.addEventListener('pointerdown', () => {
250250

251251
///////////////////////////
252252

253-
let lastTime = 0;
253+
// let lastTime = 0;
254254
function animate(time: DOMHighResTimeStamp) {
255255
time *= 0.001 // convert from millis to seconds
256-
const dt = time - lastTime;
257-
lastTime = time;
258-
updateLight(pointLight, dt)
256+
// const dt = time - lastTime;
257+
// lastTime = time;
258+
// updateLight(pointLight, dt) // this both break under the shadow mapping
259259
// updateDirectionalLight(directionalLight, time)
260260
resizeCanvasToDisplaySize(canvas);
261261
camera.aspect = canvas.clientWidth / canvas.clientHeight;
@@ -302,24 +302,24 @@ function resizeCanvasToDisplaySize(canvas: HTMLCanvasElement): void {
302302
}
303303

304304
}
305+
306+
305307
// function updateDirectionalLight(light: DirectionalLight, time: number) {
306308

307309
// const oldRotation = light.rotation
308310

309311

310-
// light.rotation = [oldRotation[0], Math.sin(time), oldRotation[2]]
312+
// light.rotation = { x: oldRotation.x, y: Math.sin(time), z: oldRotation.z }
311313

312314
// }
313315

314-
function updateLight(pointLight: PointLight, dt: number) {
315-
const rotator = m4yRotation(Math.PI / (dt * 10000));
316-
const oldTransform: Vec4 = [
317-
pointLight.position[0],
318-
pointLight.position[1],
319-
pointLight.position[2],
320-
0.0
321-
];
316+
// function updateLight(pointLight: PointLight, dt: number) {
317+
// const rotator = m4yRotation(Math.PI / (dt * 10000));
318+
// const oldTransform: Vec4 = {
319+
// ...pointLight.position,
320+
// w: 0.0
321+
// };
322322

323-
const newTransform = m4vectorMultiply(oldTransform, rotator);
324-
pointLight.position = [newTransform[0],newTransform[1],newTransform[2]]
325-
}
323+
// const newTransform = m4PositionMultiply(oldTransform, rotator);
324+
// pointLight.position = { x: newTransform.x, y: newTransform.y, z: newTransform.z }
325+
// }

demo-01-ts-webgl/lib/BasicRenderProgram.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AttributeBinding, createProgramFromRaw } from "./shaderUtils"
22
import { AmbientLight, DirectionalLight, PointLight } from "./light";
3-
import { m4fromPositionAndEuler, m4inverse, m4multiply, m4orthographic, m4perspective, m4PositionMultiply, m4xRotation, Mat4 } from "./mat4";
3+
import { m4fromPositionAndEuler, m4inverse, m4multiply, m4orthographic, m4perspective, m4PositionMultiply, m4ToArray, m4xRotation, Mat4 } from "./mat4";
44
import { Camera } from "./camera";
55
import { InputState } from "./input";
66

@@ -14,7 +14,7 @@ import shadowFragmentSource from "./shaders/depth-only.frag?raw"
1414
import { GlState } from "./gl";
1515
import { Mesh } from "./mesh";
1616
import { SceneNode } from "./scene";
17-
import { Vec3 } from "./vec";
17+
import { colorToArray, eulerToArray, Vec3, vec3ToArray } from "./vec";
1818

1919
function guaranteeUniformLocation(
2020
gl: WebGL2RenderingContext,
@@ -103,31 +103,31 @@ export function updateUniforms(
103103
camera.aspect,
104104
camera.near,
105105
camera.far)
106-
gl.uniformMatrix4fv(renderProgram.worldMatrixUniformLocation, false, shapeWorld);
107-
gl.uniformMatrix4fv(renderProgram.viewUniformLocation, false, viewMatrix);
108-
gl.uniformMatrix4fv(renderProgram.projectionUniformLocation, false, projectionMatrix);
106+
gl.uniformMatrix4fv(renderProgram.worldMatrixUniformLocation, false, m4ToArray(shapeWorld));
107+
gl.uniformMatrix4fv(renderProgram.viewUniformLocation, false, m4ToArray(viewMatrix));
108+
gl.uniformMatrix4fv(renderProgram.projectionUniformLocation, false, m4ToArray(projectionMatrix));
109109

110110
// update material uniforms
111-
gl.uniform3fv(renderProgram.materialUniform.colorLocation, mesh.material.color);
112-
gl.uniform3fv(renderProgram.materialUniform.specularColorLocation, mesh.material.specularColor);
111+
gl.uniform3fv(renderProgram.materialUniform.colorLocation, colorToArray(mesh.material.color));
112+
gl.uniform3fv(renderProgram.materialUniform.specularColorLocation, colorToArray(mesh.material.specularColor));
113113
gl.uniform1f(renderProgram.materialUniform.shininessLocation, mesh.material.shininess);
114114

115115

116116
// update light uniforms
117117
// set ambient light
118-
gl.uniform3fv(renderProgram.ambientLightUniform.colorLocation,ambientLight.color);
118+
gl.uniform3fv(renderProgram.ambientLightUniform.colorLocation, colorToArray(ambientLight.color));
119119
// set directional light
120-
gl.uniform3fv(renderProgram.directionalLightUniform.colorLocation,directionalLight.color);
121-
gl.uniform3fv(renderProgram.directionalLightUniform.rotationLocation,directionalLight.rotation);
120+
gl.uniform3fv(renderProgram.directionalLightUniform.colorLocation,colorToArray(directionalLight.color));
121+
gl.uniform3fv(renderProgram.directionalLightUniform.rotationLocation, eulerToArray(directionalLight.rotation));
122122

123123
// shadows
124124
// shadow map must be bound
125-
gl.uniformMatrix4fv(renderProgram.shadowUniform.lightViewLocation, false, lightViewProjectionMatrix);
125+
gl.uniformMatrix4fv(renderProgram.shadowUniform.lightViewLocation, false, m4ToArray(lightViewProjectionMatrix));
126126

127127

128128
// set point light
129-
gl.uniform3fv(renderProgram.pointLightUniform.colorLocation,pointLight.color);
130-
gl.uniform3fv(renderProgram.pointLightUniform.positionLocation,pointLight.position);
129+
gl.uniform3fv(renderProgram.pointLightUniform.colorLocation, colorToArray(pointLight.color));
130+
gl.uniform3fv(renderProgram.pointLightUniform.positionLocation, vec3ToArray(pointLight.position));
131131
gl.uniform1f(renderProgram.pointLightUniform.constantLocation,pointLight.constant);
132132
gl.uniform1f(renderProgram.pointLightUniform.linearLocation,pointLight.linear);
133133
gl.uniform1f(renderProgram.pointLightUniform.quadraticLocation,pointLight.quadratic);
@@ -439,13 +439,15 @@ export function drawScene(
439439

440440

441441
// Compute light's view-projection matrix (for directional light)
442-
const [x,y,z] = directionalLight.rotation
442+
const x = directionalLight.rotation.x
443+
const y = directionalLight.rotation.y
444+
const z = directionalLight.rotation.z
443445

444446
const xMatrix = m4xRotation(x)
445447
const yMatrix = m4xRotation(y)
446448
const zMatrix = m4xRotation(z)
447449

448-
const imaginaryCameraPosition: Vec3 = [10,10,10]
450+
const imaginaryCameraPosition: Vec3 = { x: 10,y: 10, z: 10}
449451
let effectiveCameraPosition = m4PositionMultiply(imaginaryCameraPosition,xMatrix)
450452
effectiveCameraPosition = m4PositionMultiply(effectiveCameraPosition,yMatrix)
451453
effectiveCameraPosition = m4PositionMultiply(effectiveCameraPosition,zMatrix)
@@ -583,8 +585,8 @@ export function drawShadowScene(
583585
const drawInitializedMesh = (mesh: Mesh) => {
584586
const vao = glState.vaos.get(mesh._id!)!;
585587
gl.bindVertexArray(vao);
586-
gl.uniformMatrix4fv(shadowProgram.u_model, false, node._worldTransform);
587-
gl.uniformMatrix4fv(shadowProgram.u_lightViewProj, false, lightViewProj);
588+
gl.uniformMatrix4fv(shadowProgram.u_model, false, m4ToArray(node._worldTransform));
589+
gl.uniformMatrix4fv(shadowProgram.u_lightViewProj, false, m4ToArray(lightViewProj));
588590
if (mesh.vertices.indices) {
589591
gl.drawElements(gl.TRIANGLES, mesh.vertices.indices.length, gl.UNSIGNED_SHORT, 0);
590592
} else {
@@ -608,4 +610,5 @@ export function drawShadowScene(
608610
node.children.forEach(child => drawShadowScene(glState, [child], renderProgram, shadowProgram, lightViewProj));
609611
}
610612
});
611-
}
613+
}
614+

demo-01-ts-webgl/lib/events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ export function getPointerClickInClipSpace(
1717
x = x / canvas.width * 2 -1;
1818
y = y / canvas.height * -2 + 1;
1919

20-
return [x,y]
20+
return {x,y}
2121
}

demo-01-ts-webgl/lib/light.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { Vec3 } from "./vec";
1+
import { Color, Vec3, Euler } from "./vec";
22

33
export type PointLight = {
4-
color: Vec3;
4+
color: Color;
55
position: Vec3;
66
constant: number;
77
linear: number;
88
quadratic: number;
99
}
1010

1111
export type DirectionalLight = {
12-
color: Vec3;
13-
rotation: Vec3;
12+
color: Color;
13+
rotation: Euler;
1414
};
1515

1616
export type AmbientLight = {
17-
color: Vec3;
17+
color: Color;
1818
}

0 commit comments

Comments
 (0)