Javascript Can';t将webgl对象解析为我的对象
我试图在javascript中创建一个包含3dObject的对象,以及与之相关的所有内容:顶点位置、法线、纹理坐标等+处理该对象所需的方法:加载对象、绘制等 问题是,当我试图解析WebGL上下文对象时,在我的例子中,gl是声明为全局的,为了使用诸如createBuffer()、bindBuffer()、javascript等特定方法,javascript给了我以下错误:“Uncaught TypeError:无法调用undefined的方法‘createBuffer’” 这是目标代码:Javascript Can';t将webgl对象解析为我的对象,javascript,oop,webgl,Javascript,Oop,Webgl,我试图在javascript中创建一个包含3dObject的对象,以及与之相关的所有内容:顶点位置、法线、纹理坐标等+处理该对象所需的方法:加载对象、绘制等 问题是,当我试图解析WebGL上下文对象时,在我的例子中,gl是声明为全局的,为了使用诸如createBuffer()、bindBuffer()、javascript等特定方法,javascript给了我以下错误:“Uncaught TypeError:无法调用undefined的方法‘createBuffer’” 这是目标代码: func
function object3D(gl){
this.vertexPositionBuffer = gl.createBuffer();
this.textureCoordBuffer = gl.createBuffer();
this.normalDirectionBuffer = gl.createBuffer();
this.indexBuffer = gl.createBuffer();
this.loadVertices = function (vertices, itemSize, numItems){
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
this.vertexPositionBuffer.numItems = numItems;
this.vertexPositionBuffer.itemSize = itemSize;
}
this.loadNormals = function (normals, itemSize, numItems){
gl.bindBuffer(gl.ARRAY_BUFFER, this.normalDirectionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
this.normalDirectionBuffer.numItems = numItems;
this.normalDirectionBuffer.itemSize = itemSize;
}
this.loadIndex = function (indexArray, itemSize, numItems){
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array, gl.STATIC_DRAW);
this.indexBuffer.numItems = numItems;
this.indexBuffer.itemSize = itemSize;
}
this.loadTexture = function (textureCoord, itemSize, numItems){
gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoord), gl.STATIC_DRAW);
this.textureCoordBuffer.numItems = numItems;
this.textureCoordBuffer.itemSize = itemSize;
}
}
以下是我尝试创建此对象实例的方式:
var cube1 = new object3D(gl);
WebGL上下文分配:
var gl;
function initGL(canvas){
try{
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
}catch(e){
}
if(!gl){
alert("Could not initialise WebGL");
}
}
在任何函数外部和我的对象定义控制台内部。log(gl)表示未定义,但在其他地方它工作正常,尽管它声明为全局
<>我不得不提到我有一个C++的坚实背景,但是我对JavaScript和处理对象很陌生。代码>作为
object3D
中的第一条语句显示。另外,您能否向我们展示一条语句,在该语句中,您将gl
指定为WebGL webcontext对象。另外,签出three.js可能会为您节省大量时间。“在任何函数外部和我的对象定义控制台内部。log(gl)表示未定义,但在其他地方,它工作正常,尽管它声明为全局。”这告诉我,gl
不是全局的。哪里是您的var gl代码>语句是否已找到?它真的是在全局范围内还是在函数内?我的gl声明它就在任何函数外的标记之后。你能在这里跳过你的全部代码吗?你似乎按错误的顺序做事,但如果没有代码就无法确定这是不是真的:)@SariMihai我正在处理一个非常类似的程序,并且有相同的问题,你解决了你的问题吗,Sari?还有谁能给我看一个面向对象的webgl程序的例子吗?任何帮助都将不胜感激,