@@ -2,14 +2,26 @@ import {
22 channelFragmentShader ,
33 channelVertexShader ,
44} from "./shader/channelShader" ;
5+ import { headFragmentShader , headVertexShader } from "./shader/headShader" ;
56import { createShaderProgram } from "./shader/shaderHelper" ;
67import { get } from "svelte/store" ;
7- import { LINE_COLORS_WEBGL } from "./const" ;
8- import { amplitudeAdjustment , offsetAdjustment } from "./stores" ;
8+ import {
9+ LINE_COLORS_WEBGL ,
10+ LINE_THICKNESS_DELTA ,
11+ LINE_THICKNESS_DUPLICATES ,
12+ } from "./const" ;
13+ import {
14+ amplitudeAdjustment ,
15+ offsetAdjustment ,
16+ channelActivated ,
17+ thicknessAdjustment ,
18+ } from "./stores" ;
919export class OscilloscopeWebGl {
1020 private channelProgram : WebGLProgram ;
11- private webgl : WebGL2RenderingContext ;
21+ private headProgram : WebGLProgram ;
1222 private channelVertexBuffer : WebGLBuffer ;
23+ private headVertexBuffer : WebGLBuffer ;
24+ private webgl : WebGL2RenderingContext ;
1325
1426 constructor ( webgl : WebGL2RenderingContext ) {
1527 this . webgl = webgl ;
@@ -31,7 +43,13 @@ export class OscilloscopeWebGl {
3143 channelVertexShader ,
3244 channelFragmentShader
3345 ) ;
46+ this . headProgram = createShaderProgram (
47+ this . webgl ,
48+ headVertexShader ,
49+ headFragmentShader
50+ ) ;
3451 this . channelVertexBuffer = this . webgl . createBuffer ( ) as WebGLBuffer ;
52+ this . headVertexBuffer = this . webgl . createBuffer ( ) as WebGLBuffer ;
3553 }
3654
3755 clear ( ) {
@@ -64,6 +82,7 @@ export class OscilloscopeWebGl {
6482 ) ;
6583 this . webgl . enableVertexAttribArray ( sampleAttribute ) ;
6684
85+ // Colour
6786 let colors = new Float32Array ( LINE_COLORS_WEBGL [ i ] ) ;
6887 let colorUniform = this . webgl . getUniformLocation (
6988 this . channelProgram ,
@@ -72,13 +91,15 @@ export class OscilloscopeWebGl {
7291
7392 this . webgl . uniform4fv ( colorUniform , colors ) ;
7493
94+ // Offset
7595 let offsetUniform = this . webgl . getUniformLocation (
7696 this . channelProgram ,
7797 "u_offset"
7898 ) ;
7999 let channelOffset = get ( offsetAdjustment ) [ i ] ;
80100 this . webgl . uniform1f ( offsetUniform , channelOffset ) ;
81101
102+ // Amplitude
82103 let amplitudeUniform = this . webgl . getUniformLocation (
83104 this . channelProgram ,
84105 "u_amplitude"
@@ -87,6 +108,110 @@ export class OscilloscopeWebGl {
87108 this . webgl . uniform1f ( amplitudeUniform , channelAmplitude ) ;
88109
89110 this . webgl . drawArrays ( this . webgl . LINE_STRIP , 0 , samples . length ) ;
111+
112+ // draw three lines to make thicker line -> Thickness
113+ if ( get ( thicknessAdjustment ) [ i ] ) {
114+ // First line
115+ let thicknessUniform = this . webgl . getUniformLocation (
116+ this . channelProgram ,
117+ "u_thick_line_id"
118+ ) ;
119+ this . webgl . uniform1i ( thicknessUniform , 1 ) ;
120+ this . webgl . drawArrays ( this . webgl . LINE_STRIP , 0 , samples . length ) ;
121+
122+ // Second line
123+ thicknessUniform = this . webgl . getUniformLocation (
124+ this . channelProgram ,
125+ "u_thick_line_id"
126+ ) ;
127+ this . webgl . uniform1i ( thicknessUniform , 2 ) ;
128+ this . webgl . drawArrays ( this . webgl . LINE_STRIP , 0 , samples . length ) ;
129+
130+ // Third line -> "original" line
131+ thicknessUniform = this . webgl . getUniformLocation (
132+ this . channelProgram ,
133+ "u_thick_line_id"
134+ ) ;
135+ this . webgl . uniform1i ( thicknessUniform , 0 ) ;
136+ this . webgl . drawArrays ( this . webgl . LINE_STRIP , 0 , samples . length ) ;
137+ }
138+ }
139+ }
140+
141+ drawHeads ( xArr : number [ ] , channelSamples : number [ ] [ ] ) {
142+ this . webgl . useProgram ( this . headProgram ) ;
143+
144+ let headVertices = new Float32Array ( [
145+ 1.0 , 1.0 , - 1.0 , 1.0 , 1.0 , - 1.0 , - 1.0 , - 1.0 ,
146+ ] ) ;
147+ this . webgl . bindBuffer ( this . webgl . ARRAY_BUFFER , this . headVertexBuffer ) ;
148+ this . webgl . bufferData (
149+ this . webgl . ARRAY_BUFFER ,
150+ headVertices ,
151+ this . webgl . STATIC_DRAW
152+ ) ;
153+ let vertexAttribute = this . webgl . getAttribLocation (
154+ this . headProgram ,
155+ "aVertex"
156+ ) ;
157+
158+ this . webgl . vertexAttribPointer (
159+ vertexAttribute ,
160+ 2 ,
161+ this . webgl . FLOAT ,
162+ false ,
163+ 0 ,
164+ 0
165+ ) ;
166+ this . webgl . enableVertexAttribArray ( vertexAttribute ) ;
167+
168+ for ( let i = 0 ; i < channelSamples . length ; i ++ ) {
169+ let activated = get ( channelActivated ) [ i ] ;
170+
171+ if ( ! activated ) continue ;
172+
173+ let xCurr = xArr [ i ] ;
174+ let xCurrUniform = this . webgl . getUniformLocation (
175+ this . headProgram ,
176+ "u_xCurr"
177+ ) ;
178+ this . webgl . uniform1f ( xCurrUniform , xCurr ) ;
179+
180+ let sampleCurr = channelSamples [ i ] [ xCurr ] ;
181+
182+ let sampleUniform = this . webgl . getUniformLocation (
183+ this . headProgram ,
184+ "u_sample"
185+ ) ;
186+ this . webgl . uniform1f ( sampleUniform , sampleCurr ) ;
187+
188+ let color = new Float32Array ( LINE_COLORS_WEBGL [ i ] ) ;
189+ let colorUniform = this . webgl . getUniformLocation (
190+ this . headProgram ,
191+ "u_colour"
192+ ) ;
193+
194+ this . webgl . uniform4fv ( colorUniform , color ) ;
195+
196+ let offsetUniform = this . webgl . getUniformLocation (
197+ this . headProgram ,
198+ "u_offset"
199+ ) ;
200+ let channelOffset = get ( offsetAdjustment ) [ i ] ;
201+ this . webgl . uniform1f ( offsetUniform , channelOffset ) ;
202+
203+ let amplitudeUniform = this . webgl . getUniformLocation (
204+ this . headProgram ,
205+ "u_amplitude"
206+ ) ;
207+ let channelAmplitude = get ( amplitudeAdjustment ) [ i ] ;
208+ this . webgl . uniform1f ( amplitudeUniform , channelAmplitude ) ;
209+
210+ this . webgl . drawArrays (
211+ this . webgl . TRIANGLE_STRIP ,
212+ 0 ,
213+ headVertices . length / 2
214+ ) ;
90215 }
91216 }
92217}
0 commit comments