computer-graphics-study/index.ts

95 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-09-02 18:16:43 +09:00
/// <reference path="node_modules/webgl-strict-types/index.d.ts" />
2021-09-02 21:09:50 +09:00
import {createProgramFromSource, ProgramError, ShaderError} from "./src/gl_util";
/// <reference path="./src/glsl.d.ts" />
2021-09-05 15:46:06 +09:00
import vert_src from "./src/vertex";
import frag_src from "./src/fragment";
2021-09-02 18:16:43 +09:00
2021-09-05 15:46:06 +09:00
function findCanvas(){
2021-09-02 18:16:43 +09:00
const canvas = document.querySelector("canvas");
if(canvas === null){
console.error("couldn't find canvas");
2021-09-05 15:46:06 +09:00
throw new Error("canvas is null");
2021-09-02 18:16:43 +09:00
}
2021-09-05 15:46:06 +09:00
return canvas;
}
function getGLContext(canvas : HTMLCanvasElement){
const gl = canvas.getContext("webgl2") as any as WebGL2RenderingContextStrict|null;
2021-09-02 18:16:43 +09:00
if(gl === null){
console.error("webgl2 is not supported!");
2021-09-05 15:46:06 +09:00
throw new Error("webgl2 not supported");
2021-09-02 21:09:50 +09:00
}
2021-09-05 15:46:06 +09:00
return gl;
}
class Renderer{
gl : WebGL2RenderingContextStrict;
constructor(gl: WebGL2RenderingContextStrict){
this.gl = gl;
2021-09-28 15:27:00 +09:00
}
init(){
const gl = this.gl;
let program: WebGLProgram;
2021-09-05 15:46:06 +09:00
try{
2021-09-28 15:27:00 +09:00
program = createProgramFromSource(gl,vert_src,frag_src);
2021-09-05 15:46:06 +09:00
gl.useProgram(program);
2021-09-02 21:09:50 +09:00
}
2021-09-05 15:46:06 +09:00
catch(e){
if(e instanceof ShaderError){
console.log(e.message,"\n",e.info);
}
else if(e instanceof ProgramError){
console.log(e.message,"\n",e.info);
}
else throw e;
2021-09-02 21:09:50 +09:00
}
2021-09-05 15:46:06 +09:00
const position = [
-0.5,-0.5,
2021-09-05 16:23:37 +09:00
0.5,-0.5,
0.5,0.5,
-0.5,0.5
];
const index = [
0,1,2,
2,3,0
2021-09-05 15:46:06 +09:00
];
2021-09-05 16:23:37 +09:00
const positionBuffer = gl.createBuffer();
2021-09-05 15:46:06 +09:00
gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer);
2021-09-05 16:23:37 +09:00
gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(position),gl.STATIC_DRAW);
const indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(index),gl.STATIC_DRAW);
2021-09-28 15:27:00 +09:00
let location = gl.getUniformLocation(program, "u_color"); //u_color 변수 위치를 참조
gl.uniform4f(location, 0.8, 0.3, 0.8, 1.0); //해당 위치에 0.2, 0.3, 0.8, 1.0 데이터를 전달
2021-09-05 16:23:37 +09:00
}
draw(){
const gl = this.gl;
gl.viewport(0,0,gl.canvas.width,gl.canvas.height);
2021-09-05 15:46:06 +09:00
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0,2,gl.FLOAT,false,0,0);
2021-09-05 16:23:37 +09:00
gl.drawElements(gl.TRIANGLES,6,gl.UNSIGNED_SHORT,0);
2021-09-05 15:46:06 +09:00
}
}
function main(){
const canvas = findCanvas();
const gl = getGLContext(canvas);
const renderer = new Renderer(gl);
2021-09-05 16:23:37 +09:00
renderer.init()
2021-09-05 15:46:06 +09:00
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();
2021-09-02 18:16:43 +09:00
}
main();