index.vert
#version 300 es
in vec3 aVertexPosition;
in vec3 aVertexNormal;
in vec4 aVertexColor;
uniform mat4 uModelMatrix;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
out vec4 vColor;
out vec3 vNormal;
out vec3 vPosition;
void main() {
vPosition = (uModelMatrix * vec4(aVertexPosition, 1.0)).xyz;
vNormal = aVertexNormal;
vColor = aVertexColor;
gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aVertexPosition, 1.0);
}
index.frag
#version 300 es
precision highp float;
in vec4 vColor;
in vec3 vNormal;
in vec3 vPosition;
uniform mat4 uInvModelMatrix;
uniform vec3 uLightPosition;
uniform vec3 uEyeDirection;
uniform vec4 uAmbientColor;
out vec4 fragColor;
void main() {
vec3 lightDirection = uLightPosition - vPosition;
vec3 invLight = normalize(uInvModelMatrix * vec4(lightDirection, 0.0)).xyz;
vec3 invEye = normalize(uInvModelMatrix * vec4(uEyeDirection, 0.0)).xyz;
vec3 halfLE = normalize(invLight + invEye);
float diffuse = clamp(dot(vNormal, invLight), 0.0, 1.0) + 0.2;
float specular = pow(clamp(dot(vNormal, halfLE), 0.0, 1.0), 50.0);
vec4 light = vColor * vec4(vec3(diffuse), 1.0) + vec4(vec3(specular), 1.0) + uAmbientColor;
fragColor = light;
}
render.ts
import { Space } from "@/lib/canvas/index"
import { Program } from "@/lib/webgl/program"
import { Scene } from "@/lib/webgl/scene"
import { Transforms } from "@/lib/webgl/transforms"
import { Matrix4 } from "@/lib/math/matrix"
import { torus } from "@/lib/shape/torus"
import { Clock } from "@/lib/event/clock"
import { Light } from "@/lib/webgl/light"
import { Quaternion } from "@/lib/math/quaternion"
import vertexSource from "./index.vert?raw"
import fragmentSource from "./index.frag?raw"
import { Camera } from "@/lib/webgl/camera"
export const onload = () => {
const space = new Space("gl-canvas")
const canvas = space.canvas
const gl = space.gl
if (!canvas || !gl) return
let scene: Scene
let camera: Camera
let transforms: Transforms
let clock: Clock
let light: Light
let count = 0
const onResize = () => {
space.fitScreen()
render()
}
const configure = () => {
space.fitScreenSquare()
gl.enable(gl.DEPTH_TEST)
gl.depthFunc(gl.LEQUAL)
gl.enable(gl.CULL_FACE)
gl.clearColor(0.0, 0.0, 0.0, 1.0)
gl.clearDepth(1.0)
const program = new Program(gl, vertexSource, fragmentSource)
scene = new Scene(gl, program)
clock = new Clock()
camera = new Camera()
camera.fov = 45
camera.near = 0.1
camera.far = 100
transforms = new Transforms(gl, program, camera, canvas)
light = new Light(gl, program)
light.position = [15.0, 10.0, 15.0]
light.ambientColor = [0.1, 0.1, 0.1, 1.0]
space.onResize = onResize
}
const regiisterGeometry = () => {
const torusGeometry = torus(1.5, 0.5, 64, 64)
scene.add(torusGeometry)
}
const render = () => {
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height)
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
count++
const rad = ((count % 180) * Math.PI) / 90
const rad2 = ((count % 720) * Math.PI) / 360
const quaternion = Quaternion.rotationX(rad2)
camera.positionVector = quaternion.toRotatedVector3(0.0, 0.0, 10.0)
camera.upVector = quaternion.toRotatedVector3(0.0, 1.0, 0.0)
camera.update()
const model = Matrix4.identity().rotateY(rad)
transforms.push(model)
light.model = model
light.eye = camera.position
scene.traverseDraw((obj) => {
obj.bind()
transforms.pop()
transforms.setMatrixUniforms()
light.reflect()
gl.drawElements(gl.TRIANGLES, obj.indices.length, gl.UNSIGNED_SHORT, 0)
obj.cleanup()
})
}
const init = () => {
configure()
regiisterGeometry()
clock.on("tick", render)
}
init()
}