64 lines
No EOL
1.9 KiB
TypeScript
64 lines
No EOL
1.9 KiB
TypeScript
import { Drawable, RenderState } from "./drawable";
|
|
import {createProgramFromSource, ProgramError, ShaderError} from "./gl_util";
|
|
import { createIndexBuffer, createVertexArray, createVertexBuffer, GLProgram, IndexBuffer, VertexArray, VertexBuffer } from "./glWrapper";
|
|
|
|
const vertex_shader_code = `#version 300 es
|
|
layout(location=0) in vec4 pos;
|
|
|
|
void main() {
|
|
gl_Position = pos;
|
|
}
|
|
`;
|
|
|
|
const frag_shader_code = `#version 300 es
|
|
precision highp float;
|
|
layout(location=0) out vec4 outColor;
|
|
void main() {
|
|
outColor = vec4(1.0,0.5,1.0,1.0);
|
|
}
|
|
`;
|
|
|
|
export class TriangleDrawer implements Drawable{
|
|
readonly program : GLProgram;
|
|
vao : VertexArray;
|
|
indexBuffer : IndexBuffer;
|
|
constructor(gl: WebGL2RenderingContext){try{
|
|
this.program = new GLProgram(createProgramFromSource(gl,vertex_shader_code,frag_shader_code));
|
|
//gl.useProgram(this.program);
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
prepare(gl : WebGL2RenderingContext){
|
|
const vertex = createVertexBuffer(gl,[
|
|
0.5,0.5,
|
|
0.0,0.25,
|
|
0.0,0.75]);
|
|
this.indexBuffer = createIndexBuffer(gl,[0,1,2]);
|
|
const posLoc = this.program.getAttribLocation(gl,"pos");
|
|
this.vao = createVertexArray(gl);
|
|
this.vao.bind(gl);
|
|
this.vao.addBuffer(gl,vertex,posLoc,{
|
|
count: 2,
|
|
type: gl.FLOAT,
|
|
normalized: false,
|
|
stride: 0,
|
|
offset: 0
|
|
});
|
|
this.indexBuffer.bind(gl);
|
|
this.vao.unbind(gl);
|
|
}
|
|
draw(gl: WebGL2RenderingContext, state :RenderState): void {
|
|
this.program.use(gl);
|
|
this.vao.bind(gl);
|
|
|
|
gl.drawElements(gl.TRIANGLES,this.indexBuffer.count,gl.UNSIGNED_SHORT,0);
|
|
}
|
|
} |