[英]webgl losing reference to an object in javascript
Im trying to use an objective approach with WebGL. 我试图在WebGL中使用一种客观的方法。 I have three variables that I want to dyanmically rename.
我有三个要动态重命名的变量。 I want this because I will have many variations of the same variable with other names so I want to make the one buffer that creates this.
我想要这个,是因为我将使用相同的变量来使用其他名称进行很多变体,因此我想创建一个创建此变量的缓冲区。 This buffer will be dynamically give a name based on the objects name.
该缓冲区将根据对象名称动态命名。
Here is how I create the variables: 这是我创建变量的方法:
var VertexPositionBuffer = [];
var VertexColorBuffer = [];
var VertexIndexBuffer = [];
function setBufferName(Planets)
{
for(var i = 0; i < Planets.length; i++)
{
VertexPositionBuffer[i] = Planets[i].name+"VertexPositionBuffer";
VertexColorBuffer[i] = Planets[i].name+"VertexColorBuffer";
VertexIndexBuffer[i] = Planets[i].name+"VertexIndexBuffer";
}
}
Planets is an array. 行星是一个数组。 Currently the names are being set to "Sun" and "Mercury".
当前,名称被设置为“ Sun”和“ Mercury”。
In initBuffers(Planets)
I create the different gl buffers and loose reference to them. 在
initBuffers(Planets)
我创建了不同的gl缓冲区并松散引用了它们。
function initBuffers(Planets)
{
for(var i = 0; i < Planets.length; i++)
{
VertexPositionBuffer[i] = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, VertexPositionBuffer[i]);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(Planets.vertices), gl.STATIC_DRAW);
VertexPositionBuffer[i].itemSize = 3;
VertexPositionBuffer[i].numItems = 24;
VertexColorBuffer[i] = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, VertexColorBuffer[i]);
var unpackedColors = [];
for (var j in Planets.colorArray)
{
var color = Planets.colorArray[j];
for (var j=0; j < 4; j++)
{
unpackedColors = unpackedColors.concat(color);
}
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColors), gl.STATIC_DRAW);
VertexColorBuffer[i].itemSize = 4;
VertexColorBuffer[i].numItems = 24;
VertexIndexBuffer[i] = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, VertexIndexBuffer[i]);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(Planets.cubeVertexIndices), gl.STATIC_DRAW);
VertexIndexBuffer[i].itemSize = 1;
VertexIndexBuffer[i].numItems = 36;
}
console.log('finished init buffers');
}
Now the problem begins because I need to reference these buffers when I want to draw them. 现在问题开始了,因为要绘制它们时需要引用这些缓冲区。 this Function shows how I draw them:
此功能显示了我如何绘制它们:
function drawScene(Planets)
{
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);
mat4.identity(mvMatrix);
for(var i = 0; i < Planets.length; i++)
{
console.log("Planet: "+Planets[i].name+'\n'+
"Planet positionX: "+Planets[i].positionX+'\n'+
"Planet positionY: "+Planets[i].positionY+'\n'+
"Planet positionZ: "+Planets[i].positionZ+'\n'+
"VertexPositionBuffer: "+VertexPositionBuffer[i]+'\n'+
"VertexColorBuffer: "+VertexColorBuffer[i]+'\n'+
"VertexIndexBuffer: "+VertexIndexBuffer[i]+'\n'+
"VertexPositionBuffer itemSize: "+VertexPositionBuffer[i].itemSize+'\n'+
"VertexColorBuffer itemSize: "+VertexColorBuffer[i].itemSize+'\n'+
"VertexIndexBuffer numItems: "+VertexIndexBuffer[i].numItems
);
mat4.translate(mvMatrix, [Planets[i].positionX, Planets[i].positionY, Planets[i].positionZ]);
mvPushMatrix();
gl.bindBuffer(gl.ARRAY_BUFFER, VertexPositionBuffer[i]);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, VertexPositionBuffer[i].itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, VertexColorBuffer[i]);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, VertexColorBuffer[i].itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, VertexIndexBuffer[i]);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, VertexIndexBuffer[i].numItems, gl.UNSIGNED_SHORT, 0);
mvPopMatrix();
}
}
On the console.log()
it can be seen that I have [object WebGLBuffer]
and no name anymore to reference any of the buffers I created in setBufferName(Planets)
. 在
console.log()
,可以看到我有[object WebGLBuffer]
并且没有名称可以引用我在setBufferName(Planets)
创建的任何缓冲区。 Any help yould be greatly appreciated!
任何帮助您将不胜感激!
I assume you are calling setBufferName(Planets)
before you call initBuffers(Planets)
. 我假设您在调用
initBuffers(Planets)
之前先调用setBufferName(Planets)
initBuffers(Planets)
。
So setBufferName(Planets)
will set VertexPositionBuffer[i]
to a name, 因此
setBufferName(Planets)
会将VertexPositionBuffer[i]
设置为名称,
VertexPositionBuffer[i] = Planets[i].name+"VertexPositionBuffer";
but then initBuffers(Planets)
will set VertexPositionBuffer[i]
to a WebGLBuffer, 但随后
initBuffers(Planets)
会将VertexPositionBuffer[i]
设置为WebGLBuffer,
VertexPositionBuffer[i] = gl.createBuffer();
So you will lose your name. 因此,您将失去名字。
I suggest you call setBufferName(Planets)
after initBuffers(Planets)
and change it so that you add a name property to the VertexPositionBuffer[i]
. 我建议您在
initBuffers(Planets)
setBufferName(Planets)
之后调用setBufferName(Planets)
并进行更改,以便将名称属性添加到VertexPositionBuffer[i]
。 Like so: 像这样:
function setBufferName(Planets)
{
for(var i = 0; i < Planets.length; i++)
{
VertexPositionBuffer[i].name = Planets[i].name+"VertexPositionBuffer";
VertexColorBuffer[i].name = Planets[i].name+"VertexColorBuffer";
VertexIndexBuffer[i].name = Planets[i].name+"VertexIndexBuffer";
}
}
Of course all this holds true for VertexColorBuffer[i]
and VertexIndexBuffer[i]
as well. 当然,所有这些对于
VertexColorBuffer[i]
和VertexIndexBuffer[i]
也适用。
If you had something else in mind for where you wanted the name to be let me know, but I think this fix is what you want. 如果您想在其他地方输入姓名,但我认为此解决方法就是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.