-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPas2JS_WebGL_OBJ.pas
158 lines (130 loc) · 3.92 KB
/
Pas2JS_WebGL_OBJ.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
program Pas2JS_WebGL_OBJ;
uses
Types, Mat4, MemoryBuffer, GLUtils, GLTypes, SysUtils,
BrowserConsole, Web, WebGL, WebGL2, JS, Math;
var
gl: TJSWebGLRenderingContext;
shader: TShader;
projTransform: TMat4;
viewTransform: TMat4;
modelTransform: TMat4;
var
dragonModel: TModel = nil;
rotateAngle: double = 0;
deltaTime: single = 0;
nextTime: single = 0;
procedure DrawCanvas;
begin
gl.clear(gl.COLOR_BUFFER_BIT + gl.DEPTH_BUFFER_BIT);
if dragonModel <> nil then
begin
modelTransform := TMat4.Identity;
modelTransform := modelTransform.Multiply(TMat4.RotateY(DegToRad(rotateAngle)));
shader.SetUniformMat4('modelTransform', modelTransform);
dragonModel.Draw;
end;
end;
procedure AnimateCanvas(time: TDOMHighResTimeStamp);
var
now: single;
list: TScalarArray;
begin
now := time * 0.001;
deltaTime := now - nextTime;
nextTime := now;
rotateAngle := rotateAngle + (20 * deltaTime);
DrawCanvas;
window.requestAnimationFrame(@AnimateCanvas);
end;
procedure StartAnimatingCanvas;
begin
window.requestAnimationFrame(@AnimateCanvas);
end;
type
TModelLoader = class
public
constructor Create (context: TJSWebGLRenderingContext; path: string);
private
gl: TJSWebGLRenderingContext;
request: TJSXMLHttpRequest;
procedure HandleLoaded;
end;
procedure TModelLoader.HandleLoaded;
var
data: TModelData;
begin
if (request.readyState = 4) and (request.status = 200) and (length(request.responseText) > 0) then
begin
data := LoadOBJFile(TJSString(request.responseText));
dragonModel := TModel.Create(gl, data);
StartAnimatingCanvas;
end;
end;
constructor TModelLoader.Create (context: TJSWebGLRenderingContext; path: string);
begin
gl := context;
request := TJSXMLHttpRequest.new;
request.open('GET', path);
request.overrideMimeType('application/text');
request.onreadystatechange := TJSOnReadyStateChangeHandler(@HandleLoaded);
request.send;
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 := 600;
canvas.height := 600;
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
// TODO: move these to .glsl files so error messages make more sense
// and give valid line numbers
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_texCoord');
shader.BindAttribLocation(2, 'in_normal');
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);
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.BLEND);
gl.Enable(gl.CULL_FACE);
gl.CullFace(gl.BACK);
// set projection transform
projTransform := TMat4.Perspective(60.0, canvas.width / canvas.height, 0.1, 2000);
shader.SetUniformMat4('projTransform', projTransform);
// set view transform
viewTransform := TMat4.Identity;
viewTransform := viewTransform.Multiply(TMat4.Translate(0, -3, -20));
shader.SetUniformMat4('viewTransform', viewTransform);
// NOTE: webgl glsl doesn't have the inverse function
// so we need to do this here
shader.SetUniformMat4('inverseViewTransform', viewTransform.Inverse);
// lighting
shader.SetUniformVec3('lightPosition', V3(0, 0, 25));
shader.SetUniformVec3('lightColor', V3(1, 1, 1));
// model material
shader.SetUniformFloat('shineDamper', 10);
shader.SetUniformFloat('reflectivity', 1);
// load obj file
TModelLoader.Create(gl, 'res/dragon.obj');
end.