redraw on resize
This commit is contained in:
parent
bcef7a452f
commit
d0252f94c9
@ -8,6 +8,6 @@
|
|||||||
<script src="./index.ts" defer></script>
|
<script src="./index.ts" defer></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<canvas id="canvas"></canvas>
|
<canvas id="canvas" width="500" height="500"></canvas>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
51
index.ts
51
index.ts
@ -2,33 +2,48 @@
|
|||||||
import {createProgramFromSource, ProgramError, ShaderError} from "./src/gl_util";
|
import {createProgramFromSource, ProgramError, ShaderError} from "./src/gl_util";
|
||||||
|
|
||||||
/// <reference path="./src/glsl.d.ts" />
|
/// <reference path="./src/glsl.d.ts" />
|
||||||
import vert_src from "./src/vertex.vert";
|
import vert_src from "./src/vertex";
|
||||||
import frag_src from "./src/fragment.frag";
|
import frag_src from "./src/fragment";
|
||||||
|
|
||||||
function main(){
|
function findCanvas(){
|
||||||
const canvas = document.querySelector("canvas");
|
const canvas = document.querySelector("canvas");
|
||||||
if(canvas === null){
|
if(canvas === null){
|
||||||
console.error("couldn't find canvas");
|
console.error("couldn't find canvas");
|
||||||
return;
|
throw new Error("canvas is null");
|
||||||
}
|
}
|
||||||
let gl = canvas.getContext("webgl2") as any as WebGL2RenderingContextStrict|null;
|
return canvas;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getGLContext(canvas : HTMLCanvasElement){
|
||||||
|
const gl = canvas.getContext("webgl2") as any as WebGL2RenderingContextStrict|null;
|
||||||
if(gl === null){
|
if(gl === null){
|
||||||
console.error("webgl2 is not supported!");
|
console.error("webgl2 is not supported!");
|
||||||
return;
|
throw new Error("webgl2 not supported");
|
||||||
}
|
}
|
||||||
|
return gl;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Renderer{
|
||||||
|
gl : WebGL2RenderingContextStrict;
|
||||||
|
constructor(gl: WebGL2RenderingContextStrict){
|
||||||
|
this.gl = gl;
|
||||||
try{
|
try{
|
||||||
const program = createProgramFromSource(gl,vert_src,frag_src);
|
const program = createProgramFromSource(gl,vert_src,frag_src);
|
||||||
gl.useProgram(program);
|
gl.useProgram(program);
|
||||||
}
|
}
|
||||||
catch(e){
|
catch(e){
|
||||||
if(e instanceof ShaderError){
|
if(e instanceof ShaderError){
|
||||||
console.error(e.message,e.getShaderInfoLog(gl));
|
console.log(e.message,"\n",e.info);
|
||||||
}
|
}
|
||||||
else if(e instanceof ProgramError){
|
else if(e instanceof ProgramError){
|
||||||
console.error(e.message,e.getProgramInfoLog(gl));
|
console.log(e.message,"\n",e.info);
|
||||||
}
|
}
|
||||||
throw e;
|
else throw e;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
draw(){
|
||||||
|
const gl = this.gl;
|
||||||
|
gl.viewport(0,0,gl.canvas.width,gl.canvas.height);
|
||||||
const position = [
|
const position = [
|
||||||
-0.5,-0.5,
|
-0.5,-0.5,
|
||||||
0.0,0.5,
|
0.0,0.5,
|
||||||
@ -44,5 +59,23 @@ function main(){
|
|||||||
const floatPosition = new Float32Array(position);
|
const floatPosition = new Float32Array(position);
|
||||||
gl.bufferData(gl.ARRAY_BUFFER,floatPosition,gl.STATIC_DRAW);
|
gl.bufferData(gl.ARRAY_BUFFER,floatPosition,gl.STATIC_DRAW);
|
||||||
gl.drawArrays(gl.TRIANGLES,0,3);
|
gl.drawArrays(gl.TRIANGLES,0,3);
|
||||||
|
gl.deleteBuffer(positionBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function main(){
|
||||||
|
const canvas = findCanvas();
|
||||||
|
const gl = getGLContext(canvas);
|
||||||
|
const renderer = new Renderer(gl);
|
||||||
|
|
||||||
|
window.addEventListener("resize",(e)=>{
|
||||||
|
e.preventDefault();
|
||||||
|
canvas.width = document.body.clientWidth;
|
||||||
|
canvas.height = document.body.clientHeight;
|
||||||
|
renderer.draw();
|
||||||
|
});
|
||||||
|
canvas.width = document.body.clientWidth;
|
||||||
|
canvas.height = document.body.clientHeight;
|
||||||
|
renderer.draw();
|
||||||
}
|
}
|
||||||
main();
|
main();
|
@ -3,23 +3,17 @@ import GL2 = WebGL2RenderingContextStrict;
|
|||||||
|
|
||||||
export class ShaderError extends Error{
|
export class ShaderError extends Error{
|
||||||
constructor(
|
constructor(
|
||||||
readonly shader:WebGLShader
|
readonly info:string
|
||||||
,msg?:string){
|
){
|
||||||
super(msg);
|
super("Could not compile shader!");
|
||||||
}
|
|
||||||
getShaderInfoLog(gl:GL2){
|
|
||||||
return gl.getShaderInfoLog(this.shader);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ProgramError extends Error{
|
export class ProgramError extends Error{
|
||||||
constructor(
|
constructor(
|
||||||
readonly program:WebGLProgram
|
readonly info:string
|
||||||
,msg?:string){
|
){
|
||||||
super(msg);
|
super("Could not link program");
|
||||||
}
|
|
||||||
getProgramInfoLog(gl:GL2){
|
|
||||||
return gl.getProgramInfoLog(this.program);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +26,8 @@ export function compileShader(gl:GL2, source:string, type: GL.ShaderType): WebGL
|
|||||||
gl.compileShader(shader);
|
gl.compileShader(shader);
|
||||||
const status = gl.getShaderParameter(shader,gl.COMPILE_STATUS);
|
const status = gl.getShaderParameter(shader,gl.COMPILE_STATUS);
|
||||||
if(!status){
|
if(!status){
|
||||||
throw new ShaderError(shader,"Could not compile shader!");
|
const info = gl.getShaderInfoLog(shader);
|
||||||
|
throw new ShaderError(info);
|
||||||
}
|
}
|
||||||
return shader;
|
return shader;
|
||||||
}
|
}
|
||||||
@ -46,7 +41,8 @@ export function createProgram(gl:GL2, vertex:WebGLShader,frag:WebGLShader){
|
|||||||
|
|
||||||
const status = gl.getProgramParameter(program,gl.LINK_STATUS);
|
const status = gl.getProgramParameter(program,gl.LINK_STATUS);
|
||||||
if(!status){
|
if(!status){
|
||||||
throw new ProgramError(program,"Coudl not link program!");
|
const info = gl.getProgramInfoLog(program);
|
||||||
|
throw new ProgramError(info);
|
||||||
}
|
}
|
||||||
return program;
|
return program;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user