-
Notifications
You must be signed in to change notification settings - Fork 8
/
Pas2JS_WebGL.pas
160 lines (128 loc) · 3.84 KB
/
Pas2JS_WebGL.pas
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
program Pas2JS_WebGL;
uses
Mat4, MemoryBuffer, GLUtils, GLTypes, SysUtils,
BrowserConsole, Web, WebGL, WebGL2, JS, Math;
type
GLVertex2 = record
pos: TVec2;
color: TRGBAb;
end;
const
kSIZEOF_VERTEX = 12; // vec2 + RGBAb
function GetVertexData: TJSUInt8Array;
var
buffer: TMemoryBuffer;
verts: TJSArray;
v: GLVertex2;
i: integer;
begin
// there's really no reason to build the array
// as vectors first then pack into bytes but
// we're doing it anyways because this how most
// will be familar with vertex data from standard
// OpenGL
verts := TJSArray.new;
v.pos := V2(0, 0);
v.color := RGBAb(255, 0, 0, 255);
verts.push(v);
v.pos := V2(0, 100);
v.color := RGBAb(0, 255, 0, 255);
verts.push(v);
v.pos := V2(100, 100);
v.color := RGBAb(0, 0, 255, 255);
verts.push(v);
// pack the array of verticies into a byte buffer
buffer := TMemoryBuffer.Create(kSIZEOF_VERTEX * verts.length);
for i := 0 to verts.length - 1 do
begin
v := GLVertex2(verts[i]);
buffer.AddFloats(2, {v.pos}ToFloats(v.pos));
buffer.AddBytes(4, v.color);
end;
result := buffer.GetBytes;
end;
var
nextTime: single = 0;
deltaTime: single = 0;
var
gl: TJSWebGLRenderingContext;
shader: TShader;
projTransform: TMat4;
viewTransform: TMat4;
modelTransform: TMat4;
var
rotateAngle: double = 0;
procedure UpdateCanvas(time: TDOMHighResTimeStamp);
var
now: single;
begin
now := time * 0.001;
deltaTime := now - nextTime;
nextTime := now;
modelTransform := TMat4.Identity;
modelTransform := modelTransform.Multiply(TMat4.Translate(100, 100, 0));
modelTransform := modelTransform.Multiply(TMat4.RotateZ(DegToRad(rotateAngle)));
rotateAngle := rotateAngle + (20 * deltaTime);
shader.SetUniformMat4('modelTransform', modelTransform);
//writeln(deltaTime);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);
window.requestAnimationFrame(@UpdateCanvas);
end;
var
canvas: TJSHTMLCanvasElement;
i: integer;
stride: integer;
offset: integer;
vertexShaderSource: string;
fragmentShaderSource: string;
buffer: TJSWebGLBuffer;
begin
// make webgl context
canvas := TJSHTMLCanvasElement(document.createElement('canvas'));
canvas.width := 300;
canvas.height := 300;
document.body.appendChild(canvas);
gl := TJSWebGLRenderingContext(canvas.getContext('webgl'));
if gl = nil then
begin
writeln('failed to load webgl!');
exit;
end;
// create shaders from source in html
vertexShaderSource := document.getElementById('vertex.glsl').textContent;
fragmentShaderSource := document.getElementById('fragment.glsl').textContent;
shader := TShader.Create(gl, vertexShaderSource, fragmentShaderSource);
shader.Compile;
shader.BindAttribLocation(0, 'in_position');
shader.BindAttribLocation(1, 'in_color');
shader.Link;
shader.Use;
// prepare context
gl.clearColor(0.9, 0.9, 0.9, 1);
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clear(gl.COLOR_BUFFER_BIT);
// setup transform matricies
projTransform := TMat4.Ortho(0, gl.canvas.width, gl.canvas.height, 0, -1, 1);
viewTransform := TMat4.Identity;
modelTransform := TMat4.Identity;
shader.SetUniformMat4('projTransform', projTransform);
shader.SetUniformMat4('viewTransform', viewTransform);
shader.SetUniformMat4('modelTransform', modelTransform);
// create buffer
buffer := gl.createBuffer;
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, GetVertexData, gl.STATIC_DRAW);
offset := 0;
stride := kSIZEOF_VERTEX;
// position
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, stride, offset);
offset += GLSizeof(gl.FLOAT) * 2;
// color (normalized = true since we're using unsigned byte)
gl.enableVertexAttribArray(1);
gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, stride, offset);
offset += GLSizeof(gl.UNSIGNED_BYTE) * 4;
// fire off the timer to draw
window.requestAnimationFrame(@UpdateCanvas);
end.