refact : move renderer in app
This commit is contained in:
parent
0630970a84
commit
6ac30925d0
22
index.tsx
22
index.tsx
@ -5,6 +5,7 @@ import ReactDOM from 'react-dom';
|
|||||||
|
|
||||||
import { MenuBar } from "./src/menu";
|
import { MenuBar } from "./src/menu";
|
||||||
import { TriangleRenderer } from './src/triangle_renderer';
|
import { TriangleRenderer } from './src/triangle_renderer';
|
||||||
|
import {CanvasApp} from "./src/app"
|
||||||
|
|
||||||
function findCanvas(){
|
function findCanvas(){
|
||||||
const canvas = document.querySelector("canvas");
|
const canvas = document.querySelector("canvas");
|
||||||
@ -43,26 +44,23 @@ function main(){
|
|||||||
setupButton();
|
setupButton();
|
||||||
const canvas = findCanvas();
|
const canvas = findCanvas();
|
||||||
const gl = getGLContext(canvas);
|
const gl = getGLContext(canvas);
|
||||||
const renderer = new Renderer2D(gl);
|
const app = new CanvasApp(gl);
|
||||||
const trenderer = new TriangleRenderer(gl);
|
if(!app.intialize()){
|
||||||
renderer.prepare();
|
console.log("app initialize failed!!!");
|
||||||
trenderer.prepare(gl);
|
return;
|
||||||
|
}
|
||||||
ReactDOM.render(<MenuBar u={renderer.uniforms}
|
ReactDOM.render(<MenuBar u={app.renderer.uniforms}
|
||||||
onUniformChange={(u)=>{renderer.settingUniform(u); renderer.draw();
|
onUniformChange={(u)=>{app.renderer.setUniform(u);
|
||||||
trenderer.draw(gl);
|
|
||||||
}} />
|
}} />
|
||||||
,document.getElementById("drawer"));
|
,document.getElementById("drawer"));
|
||||||
window.addEventListener("resize",(e)=>{
|
window.addEventListener("resize",(e)=>{
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
canvas.width = document.body.clientWidth;
|
canvas.width = document.body.clientWidth;
|
||||||
canvas.height = document.body.clientHeight;
|
canvas.height = document.body.clientHeight;
|
||||||
renderer.draw();
|
|
||||||
trenderer.draw(gl);
|
|
||||||
});
|
});
|
||||||
canvas.width = document.body.clientWidth;
|
canvas.width = document.body.clientWidth;
|
||||||
canvas.height = document.body.clientHeight;
|
canvas.height = document.body.clientHeight;
|
||||||
renderer.draw();
|
app.startRun();
|
||||||
trenderer.draw(gl);
|
app.startDraw();
|
||||||
}
|
}
|
||||||
main();
|
main();
|
19
src/app.ts
19
src/app.ts
@ -1,33 +1,42 @@
|
|||||||
import { Renderer2D } from "./renderer";
|
import { Renderer2D } from "./renderer";
|
||||||
|
import { TriangleRenderer } from "./triangle_renderer";
|
||||||
|
|
||||||
export class CanvasApp{
|
export class CanvasApp{
|
||||||
readonly gl: WebGL2RenderingContext;
|
readonly gl: WebGL2RenderingContext;
|
||||||
renderer: Renderer2D;
|
renderer: Renderer2D;
|
||||||
|
trenderer : TriangleRenderer;
|
||||||
constructor(gl: WebGL2RenderingContext){
|
constructor(gl: WebGL2RenderingContext){
|
||||||
this.gl = gl;
|
this.gl = gl;
|
||||||
this.renderer = new Renderer2D(gl);
|
this.renderer = new Renderer2D(gl);
|
||||||
|
this.trenderer = new TriangleRenderer(gl);
|
||||||
}
|
}
|
||||||
intialize():boolean{
|
intialize():boolean{
|
||||||
|
this.renderer.prepare();
|
||||||
|
this.trenderer.prepare(this.gl);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
run(){
|
startRun(){
|
||||||
const current = new Date();
|
const current = new Date();
|
||||||
this.loop();
|
this.loop();
|
||||||
const delta = (new Date().getMilliseconds()) - current.getMilliseconds();
|
const delta = (new Date().getMilliseconds()) - current.getMilliseconds();
|
||||||
const delay = (delta < 16) ? (16 - delta) : 1;
|
const delay = (delta < 16) ? (16 - delta) : 1;
|
||||||
setTimeout((()=>{
|
setTimeout((()=>{
|
||||||
this.run();
|
this.startRun();
|
||||||
}).bind(this),delay);
|
}).bind(this),delay);
|
||||||
this.drawScene();
|
|
||||||
}
|
}
|
||||||
loop(){
|
loop(){
|
||||||
|
|
||||||
|
}
|
||||||
|
startDraw(){
|
||||||
|
this.drawScene();
|
||||||
}
|
}
|
||||||
drawScene(){
|
drawScene(){
|
||||||
|
this.gl.viewport(0,0,this.gl.canvas.width,this.gl.canvas.height);
|
||||||
|
|
||||||
this.gl.clearColor(0,0,0,0);
|
this.gl.clearColor(0,0,0,0);
|
||||||
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
|
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
|
||||||
this.renderer.draw();
|
this.renderer.draw(this.gl);
|
||||||
|
this.trenderer.draw(this.gl);
|
||||||
requestAnimationFrame(this.drawScene.bind(this));
|
requestAnimationFrame(this.drawScene.bind(this));
|
||||||
}
|
}
|
||||||
};
|
};
|
@ -56,25 +56,20 @@ export class Renderer2D implements RenderProgram{
|
|||||||
|
|
||||||
this.indexBuffer = G.createIndexBuffer(gl,index);
|
this.indexBuffer = G.createIndexBuffer(gl,index);
|
||||||
this.indexBuffer.bind(gl);
|
this.indexBuffer.bind(gl);
|
||||||
this.settingUniform(this.uniforms);
|
this.setUniform(this.uniforms);
|
||||||
}
|
}
|
||||||
useProgram(){
|
useProgram(){
|
||||||
this.program.use(this.gl);
|
this.program.use(this.gl);
|
||||||
}
|
}
|
||||||
settingUniform(u:UniformSet){
|
setUniform(u:UniformSet){
|
||||||
const gl = this.gl;
|
|
||||||
this.uniforms = u;
|
this.uniforms = u;
|
||||||
|
}
|
||||||
|
draw(gl:WebGL2RenderingContext){
|
||||||
this.useProgram();
|
this.useProgram();
|
||||||
|
|
||||||
const location = this.program.getUniformLocation(gl,"u_color");//u_color 변수 위치를 참조
|
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 데이터를 전달
|
gl.uniform4f(location, this.uniforms.redcolor, 0.3, 0.8, 1.0); //해당 위치에 0.2, 0.3, 0.8, 1.0 데이터를 전달
|
||||||
}
|
|
||||||
draw(){
|
|
||||||
const gl = this.gl;
|
|
||||||
gl.clearColor(0,0,0,0);
|
|
||||||
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
||||||
|
|
||||||
gl.viewport(0,0,gl.canvas.width,gl.canvas.height);
|
|
||||||
this.useProgram();
|
|
||||||
gl.bindVertexArray(this.vao);
|
gl.bindVertexArray(this.vao);
|
||||||
this.indexBuffer.bind(gl);
|
this.indexBuffer.bind(gl);
|
||||||
gl.drawElements(gl.TRIANGLES,this.indexBuffer.count,gl.UNSIGNED_SHORT,0);
|
gl.drawElements(gl.TRIANGLES,this.indexBuffer.count,gl.UNSIGNED_SHORT,0);
|
||||||
|
Loading…
Reference in New Issue
Block a user