Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //<body onload="EngineStart()">
- var canvas,
- scene,
- objectA, // "GameObject A"
- objectB; // "GameObject B"
- //=========================================================================
- // (*) RENDER LOOP
- //=========================================================================
- function Render(){
- for(var i = 0; i < totalCanvases; i++){
- var canvas = Canvas[i],
- gl = canvas.gl;
- gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
- // Scene is a "Node" that contains children that are geometry which contain meshes.
- scene.Draw(gl);
- }
- //=========================================================================
- // (1) Start Engine
- //=========================================================================
- function EngineStart(){
- // Wrapper, holds width,height,gl contexts,etc..
- canvas = new Canvas('id');
- // Nww Mesh creates a wrapper that holds Shader Programs, typed arrays, etc..
- var meshA = new Mesh(canvas,meshVertices,colorVertices);
- var meshB = new Mesh(canvas,meshVertices,colorVertices);
- //Object3D is the wrapper used to translate, rotate, scale the mesh. This would be the GameObject in Unity3D.
- objectA = new Object3D(canvas,meshA);
- objectB = new Object3D(canvas,meshB);
- }
- //=========================================================================
- // (2) Load3DObjects: Called when images,sounds,etc are fully loaded
- //=========================================================================
- function Load3DObjects(){
- for(var i = 0; i < totalCanvases; i++){
- var canvas = Canvases[i],
- gl = canvas.gl,
- aspect = gl.drawingBufferWidth/gl.drawingBufferHeight;
- ForgeSDK.Math.Matrix4.Projection = ForgeSDK.Math.Matrix4.Perspective(fov,aspect,near,far);
- gl.viewport(0,0,canvas.width,canvas.height);
- gl.clearColor(_r,_g,_b,_a);
- gl.enable(gl.CULL_FACE);
- gl.enable(gl.DEPTH_TEST);
- LoadShaders();
- }
- }
- //=========================================================================
- // (2) Load Shaders: If Materials present on mesh, runs LoadShaderProgram()
- // adds the program to an array on mesh, and waits for a Mesh.ProgramLoaded callback.
- //=========================================================================
- function LoadShaders(){
- for(var i = 0; i < totalMeshes; i++){
- var mesh = Meshes[i],
- gl = mesh.gl;
- // Each Mesh has a Material Array that holds Vertex and fragment shader urls
- mesh.matsToLoad = mesh.materials.length;
- var that = mesh;
- for(mat in mesh.materials){ // Load Shader Programs
- var prog = LoadShaderProgram(gl,material.vertexShader, material.fragmentShader,
- function(prog){that.ProgramLoaded(gl,prog);});
- // The Mesh Wrapper (new Mesh) comes with a ProgramLoaded function I use as a callback
- mesh.materialsToLoad = mesh.materials.length;
- mesh.programs.push(prog);
- }
- }
- }
- //=========================================================================
- // (3) LoadShaderProgram(): Compiles, waits for callback, links, and finally fires:
- // mesh.ProgramLoaded() callback;
- //=========================================================================
- function LoadShaderProgram(){
- var program = gl.createProgram();
- function vshaderLoaded(str){ // Callback, ready to Link
- program.vshaderSource = str;
- if(program.fshaderSource){
- LinkShaderProgram(gl,program); // Link
- callback(program); // Mesh.PromLoaded
- }
- }
- function fshaderLoaded(str){ // Callback, ready to Link
- program.fshaderSource = str;
- if(program.vshaderSource){
- LinkShaderProgram(gl,program); // Link
- callback(program); // Mesh.PromLoaded
- }
- } // First Load Shader Files via XMLHttpRequest. Wait for Back to LinkShader Program
- LoadShaderFile(vs,vshaderLoaded,true);
- LoadShaderFile(fs,fshaderLoaded,true);
- return program;
- }
- //=========================================================================
- // (4) Mesh.ProgramLoaded(): Add Uniforms and Attributes to Program
- // and when all of them are loaded, call Create GLBuffers
- //=========================================================================
- Mesh.ProgramLoaded: function (){
- program.projectionUniform = gl.getUniformLocation(program,'u_matrix');
- program.vertexPosAttribute = gl.getAttribLocation(program,'aVertPos');
- program.colorPosAttribute = gl.getAttribLocation(program,"aColor");
- gl.enableVertexAttribArray(program.vertexPosAttribute);
- gl.enableVertexAttribArray(program.colorPosAttribute);
- if(--this.materialsToLoad == 0){
- // All Materials Loaded, Create Buffer
- CreateGLBuffers();
- }
- }
- //=========================================================================
- // (5) Create Vertex, Color Buffers and Create "Nodes"
- //=========================================================================
- function CreateGLBuffers(){
- var sceneChildren = [];
- for(var i = 0; i < totalObjects3D; i++){
- var object = objects3D[i],
- _mesh = object.mesh,
- gl = object.gl;
- _mesh.vertexPosBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, _mesh.vertexPosBuffer);
- gl.bufferData(gl.ARRAY_BUFFER, _mesh.meshVertices, gl.DYNAMIC_DRAW);
- _mesh.colorPosBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER,_mesh.colorPosBuffer);
- gl.bufferData(gl.ARRAY_BUFFER,_mesh.colorVertices, gl.DYNAMIC_DRAW);
- var geometry = new Node([new Goemetry(_mesh)]);
- geometry.name = "Geometry Node";
- geometry.ref = object;
- sceneChildren[sceneChildren.length] = new Node([geometry]);
- }
- scene = new Node(sceneChildren);
- RUN_GAME_LOOP();
- }
- //=========================================================================
- // CREATE A NEW NODE FUNCTION
- //=========================================================================
- New Node Object Function {
- // Properties
- x: 0,
- y: 0,
- z: 0,
- rx: 0,
- ry: 0,
- rz: 0,
- sx: 1,
- sy: 1,
- sz: 1,
- localPosition: Matrix4.Translation(this.x,this.y,this.z),
- localRotationX: Matrix4.RotationX(this.rx),
- localRotationY: Matrix4.RotationY(this.ry),
- localRotationZ: Matrix4.RotationZ(this.rz),
- localScale: Matrix4.Scale(this.sx,this.sy,this.sz),
- children: child ? child : [];
- }
- //=========================================================================
- // CREATE A NEW GEOMETRY NODE FUNCTION
- //=========================================================================
- New Geometry Object Function {
- // Properties
- name: 'geometry',
- mesh: _mesh
- }
- //=========================================================================
- // NODE DRAW AND CALCULATE MESH MATRICES FUNCTION
- //=========================================================================
- Node.Draw: function (gl,program){
- PushModelMatrix4(this.CalculateMatrices());
- for(ch in this.children){
- var child = this.children[ch];
- child.Draw();
- }
- PopModelMatrix4();
- },
- Node.CalculateMatrices: function(){
- // Calculate this Meshes: translate, rotations, and scale
- // Make new Local Positon, Rotations Scale Matrices
- // Multiply alll that to get new Model Matrix and RETURN IT
- return modelMatrix;
- }
- //=========================================================================
- // GEOMETRY NODE DRAW FUNCTION
- //=========================================================================
- Geometry.Draw: function (gl){ this.mesh.Draw(); } // Draw a Mesh
- //=========================================================================
- // MESH DRAW FUNCTION
- //=========================================================================
- Mesh.Draw: function (){
- var gl = this.gl;
- for(var p in this.programs){
- var program = this.programs[p];
- gl.useProgram(program);
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexPosBuffer);
- gl.vertexAttribPointer(program.vertexPosAttribute,3, gl.FLOAT, false, 0, 0);
- gl.bindBuffer(gl.ARRAY_BUFFER, this.colorPosBuffer);
- gl.vertexAttribPointer(program.colorPosAttribute,3,gl.UNSIGNED_BYTE,true,0,0);
- // Calculate Camera Matrix
- // Calcuate View Matrix
- // Calculate Projection
- gl.uniformMatrix4fv(program.projectionUniform, false, Matrix4.Model[ModelStackStop]);
- gl.drawArrays(gl.TRIANGLES,0,(this.meshVertices.length/3));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement