Vao Abstraction

This commit is contained in:
monoid 2021-10-03 22:11:47 +09:00
parent 6ac30925d0
commit badd6e7f5f
3 changed files with 57 additions and 18 deletions

View File

@ -82,4 +82,33 @@ export class GLProgram{
unuse(gl:WebGL2RenderingContext):void{
gl.useProgram(null);
}
}
export class VertexArray{
readonly vao:WebGLVertexArrayObject;
constructor(vao:WebGLVertexArrayObject){
this.vao = vao;
}
bind(gl:WebGL2RenderingContext){
gl.bindVertexArray(this.vao);
}
unbind(gl:WebGL2RenderingContext){
gl.bindVertexArray(null);
}
addBuffer(gl:WebGL2RenderingContext,va:VertexBuffer,loc:number,layout:{
/**count of one element */
count: GLint,
type: GLenum,
normalized: GLboolean,
stride: GLsizei,
offset: GLintptr
}){
this.bind(gl);
va.bind(gl);
gl.enableVertexAttribArray(loc);
gl.vertexAttribPointer(loc,layout.count,layout.type,layout.normalized,layout.stride,layout.offset);
}
}
export function createVertexArray(gl:WebGL2RenderingContext){
return new VertexArray(gl.createVertexArray());
}

View File

@ -13,7 +13,7 @@ export class Renderer2D implements RenderProgram{
uniforms : UniformSet;
program: G.GLProgram;
vao: WebGLVertexArrayObject;
vao: G.VertexArray;
positionBuffer: G.VertexBuffer;
indexBuffer: G.IndexBuffer;
constructor(gl: WebGL2RenderingContext){
@ -45,17 +45,22 @@ export class Renderer2D implements RenderProgram{
0,1,2,
2,3,0
];
this.vao = gl.createVertexArray();
const posLoc = this.program.getAttribLocation(gl,"pos");
gl.bindVertexArray(this.vao);
this.vao = G.createVertexArray(gl);
const posLoc = this.program.getAttribLocation(gl,"pos");
this.vao.bind(gl);
this.positionBuffer = G.createVertexBuffer(gl,position);
this.positionBuffer.bind(gl);
gl.enableVertexAttribArray(posLoc);
gl.vertexAttribPointer(posLoc,2,gl.FLOAT,false,0,0);
this.vao.addBuffer(gl,this.positionBuffer,posLoc,{
count:2,
type:gl.FLOAT,
normalized:false,
stride:0,
offset:0
});
this.indexBuffer = G.createIndexBuffer(gl,index);
this.indexBuffer.bind(gl);
this.vao.unbind(gl);
this.setUniform(this.uniforms);
}
useProgram(){
@ -69,9 +74,9 @@ export class Renderer2D implements RenderProgram{
const location = this.program.getUniformLocation(gl,"u_color");//u_color 변수 위치를 참조
gl.uniform4f(location, this.uniforms.redcolor, 0.3, 0.8, 1.0); //해당 위치에 0.2, 0.3, 0.8, 1.0 데이터를 전달
this.vao.bind(gl);
gl.bindVertexArray(this.vao);
this.indexBuffer.bind(gl);
gl.drawElements(gl.TRIANGLES,this.indexBuffer.count,gl.UNSIGNED_SHORT,0);
this.vao.unbind(gl);
}
}

View File

@ -1,6 +1,6 @@
import { RenderProgram } from "./program";
import {createProgramFromSource, ProgramError, ShaderError} from "./gl_util";
import { createIndexBuffer, createVertexBuffer, GLProgram, IndexBuffer, VertexBuffer } from "./glWrapper";
import { createIndexBuffer, createVertexArray, createVertexBuffer, GLProgram, IndexBuffer, VertexArray, VertexBuffer } from "./glWrapper";
const vertex_shader_code = `#version 300 es
layout(location=0) in vec4 pos;
@ -20,7 +20,7 @@ void main() {
export class TriangleRenderer implements RenderProgram{
readonly program : GLProgram;
vao : WebGLVertexArrayObject;
vao : VertexArray;
indexBuffer : IndexBuffer;
constructor(gl: WebGL2RenderingContext){try{
this.program = new GLProgram(createProgramFromSource(gl,vertex_shader_code,frag_shader_code));
@ -43,17 +43,22 @@ export class TriangleRenderer implements RenderProgram{
0.0,0.75]);
this.indexBuffer = createIndexBuffer(gl,[0,1,2]);
const posLoc = this.program.getAttribLocation(gl,"pos");
this.vao = gl.createVertexArray();
gl.bindVertexArray(this.vao);
gl.enableVertexAttribArray(posLoc);
vertex.bind(gl);
gl.vertexAttribPointer(posLoc,2,gl.FLOAT,false,0,0);
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): void {
this.program.use(gl);
gl.bindVertexArray(this.vao);
this.indexBuffer.bind(gl);
this.vao.bind(gl);
gl.drawElements(gl.TRIANGLES,this.indexBuffer.count,gl.UNSIGNED_SHORT,0);
}
}