Skip to content

Commit dfbcd36

Browse files
Adding proof of concept page for OpenGL support
1 parent d4e6090 commit dfbcd36

File tree

7 files changed

+66
-10
lines changed

7 files changed

+66
-10
lines changed

src/src/app/app.routes.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {LoginComponent} from './view/login/login.component';
88
import {TwitchBotIndexComponent} from './view/twitch/twitch-bot-index/twitch-bot-index.component';
99
import {TwitchBotConfigComponent} from './view/twitch/twitch-bot-config/twitch-bot-config.component';
1010
import {ImdbSearchComponent} from './view/imdb-search/imdb-search.component';
11-
import {BackgroundWebglComponent} from "./view/background-webgl/background-webgl.component";
1211
import {BackgroundWebglExampleComponent} from "./view/background-webgl-example/background-webgl-example.component";
1312

1413
export const routes: Routes = [

src/src/app/common/components/standard-banner/standard-banner.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22
import { LogoComponent } from '../logo/logo.component';
33
import { environment } from '../../../../environments/environment';
4-
import { Router } from '@angular/router';
54
import { MatButton } from '@angular/material/button';
65

76
@Component({
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
<app-background-webgl></app-background-webgl>
1+
@if(enum === OpenGlBackgrounds.BOX) {
2+
<app-background-webgl></app-background-webgl>
3+
} @else {
4+
5+
}
26

37
<app-standard-banner />
8+
<div class="floating-button">
9+
<button mat-stroked-button color="primary" [matMenuTriggerFor]="menu">Menu</button>
10+
<mat-menu #menu="matMenu">
11+
<button mat-menu-item (click)="this.enum = OpenGlBackgrounds.BOX">Box</button>
12+
<button mat-menu-item (click)="this.enum = OpenGlBackgrounds.TEXTURE">Texture</button>
13+
</mat-menu>
14+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.floating-button {
2+
position: absolute;
3+
bottom: 0px;
4+
right: 50px;
5+
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { Component } from '@angular/core';
22
import {BackgroundWebglComponent} from "../background-webgl/background-webgl.component";
33
import {StandardBannerComponent} from "../../common/components/standard-banner/standard-banner.component";
4+
import {MatButton} from "@angular/material/button";
5+
import {MatMenu, MatMenuItem, MatMenuTrigger} from "@angular/material/menu";
6+
import {OpenGlBackgrounds} from "./backgrounds";
47

58
@Component({
69
selector: 'app-background-webgl-example',
710
standalone: true,
811
imports: [
912
BackgroundWebglComponent,
10-
StandardBannerComponent
13+
StandardBannerComponent,
14+
MatButton,
15+
MatMenu,
16+
MatMenuItem,
17+
MatMenuTrigger
1118
],
1219
templateUrl: './background-webgl-example.component.html',
1320
styleUrl: './background-webgl-example.component.scss'
1421
})
1522
export class BackgroundWebglExampleComponent {
16-
23+
public enum: OpenGlBackgrounds = OpenGlBackgrounds.BOX;
24+
protected readonly OpenGlBackgrounds = OpenGlBackgrounds;
1725
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum OpenGlBackgrounds {
2+
BOX,
3+
TEXTURE
4+
}

src/src/app/view/background-webgl/background-webgl.component.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ void main() {
3636
}`;
3737
animate: () => void = () => {};
3838
dispose = false;
39+
private disposableBuffers: WebGLBuffer[] = [];
40+
private disposableArrayObjects: WebGLVertexArrayObject[] = [];
41+
private disposablePrograms: WebGLProgram[] = [];
42+
private disposableShaders: WebGLShader[] = [];
43+
private gl: WebGL2RenderingContext | null = null;
3944

4045
ngOnInit(): void {
4146
window.addEventListener('resize', this.resize.bind(this), false);
@@ -45,11 +50,27 @@ void main() {
4550
ngOnDestroy(): void {
4651
this.dispose = true;
4752
window.removeEventListener('resize', this.resize.bind(this));
53+
54+
// Release the OpenGL resources we allocated
55+
if (null !== this.gl) {
56+
for (let i = this.disposableBuffers.length - 1; i >= 0; i--) {
57+
this.gl.deleteBuffer(this.disposableBuffers[i]);
58+
}
59+
for (let i = this.disposableArrayObjects.length - 1; i >= 0; i--) {
60+
this.gl.deleteVertexArray(this.disposableArrayObjects[i]);
61+
}
62+
for (let i = this.disposablePrograms.length - 1; i >= 0; i--) {
63+
this.gl.deleteProgram(this.disposablePrograms[i]);
64+
}
65+
for (let i = this.disposableShaders.length - 1; i >= 0; i--) {
66+
this.gl.deleteShader(this.disposableShaders[i]);
67+
}
68+
}
4869
}
4970

5071
ngAfterViewInit(): void {
51-
const stuff = this.init();
52-
if (!stuff) {
72+
const init = this.init();
73+
if (!init) {
5374
return;
5475
}
5576

@@ -58,7 +79,7 @@ void main() {
5879
return;
5980
}
6081

61-
this.render(stuff[0], stuff[1]);
82+
this.render(init[0], init[1]);
6283
requestAnimationFrame(this.animate);
6384
};
6485

@@ -86,6 +107,7 @@ void main() {
86107
gl.compileShader(shader);
87108
const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
88109
if (success) {
110+
this.disposableShaders.push(shader);
89111
return shader;
90112
}
91113

@@ -105,6 +127,7 @@ void main() {
105127
gl.linkProgram(program);
106128
const success = gl.getProgramParameter(program, gl.LINK_STATUS);
107129
if (success) {
130+
this.disposablePrograms.push(program);
108131
return program;
109132
}
110133

@@ -140,6 +163,10 @@ void main() {
140163
return null;
141164
}
142165

166+
// Save for disposing of resources later
167+
this.disposableArrayObjects.push(VAO)
168+
this.disposableBuffers.push(...[VBO, EBO]);
169+
143170
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
144171
gl.bindVertexArray(VAO);
145172

@@ -179,18 +206,21 @@ void main() {
179206
return;
180207
}
181208

209+
// Save the reference for when we dispose
210+
this.gl = gl;
211+
182212
// Tell WebGL how to convert from clip space to pixels
183213
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
184214

185215
// Clear the canvas
186-
gl.clearColor(0.2, 0.3, 0.3, 1.0);
216+
gl.clearColor(0.10, 0.09, 0.09, 1.0);
187217
gl.clear(gl.COLOR_BUFFER_BIT);
188218

189219
// draw our first triangle
190220
gl.useProgram(program);
191221

192222
const uniformLocation = gl.getUniformLocation(program, "ourColor");
193-
gl.uniform4fv(uniformLocation, new Float32Array([1, 0.5, 0.2, 1]))
223+
gl.uniform4fv(uniformLocation, new Float32Array([0.2, 0.18, 0.18, 1]))
194224

195225
gl.bindVertexArray(VAO);
196226
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0);

0 commit comments

Comments
 (0)