我目前正在制作计算机图形课程的船模型,并且我遇到了严重的打击。调试器返回一条消息,说明我的handleLoadedTextures()调用的render()函数未定义。我已经在这里工作了几个小时,我确定这是一个愚蠢的错误,但我无法看到它。一切似乎都已到位,我的想法不合时宜。
调用看似未定义的render()的两个函数:
function handleLoadedTexture(texture) { //From texture-3cubes.js provided example
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
texture.isloaded = true;
render(); // Call render function when the image has been loaded (to insure the model is displayed)
gl.bindTexture(gl.TEXTURE_2D, null);
}
function handleLoadedTextureMap(texture) {
ct++;
if (ct == 6) {
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
var targets = [
gl.TEXTURE_CUBE_MAP_POSITIVE_X, gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
gl.TEXTURE_CUBE_MAP_POSITIVE_Y, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
gl.TEXTURE_CUBE_MAP_POSITIVE_Z, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
];
for (var j = 0; j < 6; j++) {
gl.texImage2D(targets[j], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img[j]);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
}
gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
texture.isloaded = true;
render(); // Call render function when the image has been loaded (to insure the model is displayed)
gl.bindTexture(gl.TEXTURE_2D, null);
}
}
&#13;
我的不同模型的声明,它工作得很好然后我开始制作新的应用不同的纹理,由于某种原因,程序再也找不到它们了。
var sphere, cylinderLight, cylinderDark, box, teapot, disk, torus, cone, fHull, wing, cockpit; // model identifiers
var envbox; // model identifiers
sphere = createModel(uvSphere(10.0, 25.0, 25.0));
box = createModel(cube(1000.0));
fHull = createModel(fHull(), hullTex);
cockpit = createModel(cockpit(), hullTex);
cylinderLight = createModel(uvCylinder(10.0, 20.0, 25.0, false, false), reactorTex);
cylinderDark = createModel(uvCylinder(10.0, 20.0, 25.0, false, true), sReactorTex);
wing = createModel(wing(), wingsTex);
stripes = createModel(wing(), stripesTex);
disk = createModel(ring(2.0, 10.0, 25.0), hullTex);
envbox = createModelbox(cube(1000.0));
teapot = createModel(teapotModel);
torus = createModel(uvTorus(15.0, 5.0, 25.0, 25.0));
cone = createModel(uvCone(10.0, 20.0, 25.0, true));
&#13;
形状(磁盘)的例子我不得不注释掉,因为它因为未定义而使我的船完全渲染,即使它在应用纹理之前有效。它只是功能的一部分:
function shipReactors() {
//------------------------------------------------------------------------------------------------
//REACTORS
//------------------------------------------------------------------------------------------------
//top left BIG
materialTerms(3, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(3.3, 4.6, -17.0));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.13, 0.13, 0.17));
cylinderLight.render(); //Main
//top left SMALL
materialTerms(4, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(3.5, 4.8, -20.0));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.07, 0.07, 0.21));
cylinderDark.render();
//Propulsion emitter
materialTerms(6, 1);
modelview = initialmodelview;
modelview = mult(modelview, translate(3.5, 4.8, -22.2));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.07, 0.07, 0.21));
//disk.render();
&#13;
最后,createmodel()函数,虽然我怀疑这是问题
function createModel(modelData, texture) {
var model = {};
model.coordsBuffer = gl.createBuffer();
model.normalBuffer = gl.createBuffer();
model.textureBuffer = gl.createBuffer();
model.indexBuffer = gl.createBuffer();
model.count = modelData.indices.length;
gl.bindBuffer(gl.ARRAY_BUFFER, model.coordsBuffer);
gl.bufferData(gl.ARRAY_BUFFER, modelData.vertexPositions, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, model.normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, modelData.vertexNormals, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, model.textureBuffer);
gl.bufferData(gl.ARRAY_BUFFER, modelData.vertexTextureCoords, gl.STATIC_DRAW);
console.log(modelData.vertexPositions.length);
console.log(modelData.indices.length);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, model.indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, modelData.indices, gl.STATIC_DRAW);
model.render = function() {
gl.bindBuffer(gl.ARRAY_BUFFER, this.coordsBuffer);
gl.vertexAttribPointer(CoordsLoc, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);
gl.vertexAttribPointer(NormalLoc, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, this.textureBuffer);
gl.vertexAttribPointer(TexCoordLoc, 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
gl.uniformMatrix4fv(ModelviewLoc, false, flatten(modelview)); //--- load flattened modelview matrix
gl.uniformMatrix3fv(NormalMatrixLoc, false, flatten(normalMatrix)); //--- load flattened normal matrix
if (texture.isloaded) {
gl.activeTexture(gl.TEXTURE0); //From texture-3cubes.js provided example
gl.bindTexture(gl.TEXTURE_2D, texture);
// Send texture to sampler
gl.uniform1i(TextureLoc, 0);
}
gl.drawElements(gl.TRIANGLE_STRIP, this.count, gl.UNSIGNED_SHORT, 0); //I use TRIANGLE_STRIP to allow more complex shapes to be implemented more easily
}
return model;
}
function createModelbox(modelData) { // For creating the environment box.
var model = {};
model.coordsBuffer = gl.createBuffer();
model.indexBuffer = gl.createBuffer();
model.count = modelData.indices.length;
gl.bindBuffer(gl.ARRAY_BUFFER, model.coordsBuffer);
gl.bufferData(gl.ARRAY_BUFFER, modelData.vertexPositions, gl.STATIC_DRAW);
console.log(modelData.vertexPositions.length);
console.log(modelData.indices.length);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, model.indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, modelData.indices, gl.STATIC_DRAW);
model.render = function() {
gl.bindBuffer(gl.ARRAY_BUFFER, this.coordsBuffer);
gl.vertexAttribPointer(aCoordsbox, 3, gl.FLOAT, false, 0, 0);
gl.uniformMatrix4fv(uModelviewbox, false, flatten(modelview));
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
gl.drawElements(gl.TRIANGLES, this.count, gl.UNSIGNED_SHORT, 0);
}
return model;
}
&#13;
提前谢谢你,我知道这是一个延伸,但我只需要一些新的眼睛。
编辑:这是专用的渲染功能
function render() {
gl.clearColor(0.8, 0.8, 0.8, 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
projection = perspective(70.0, 1.0, 1.0, 200.0);
//--- Get the rotation matrix obtained by the displacement of the mouse
//--- (note: the matrix obtained is already "flattened" by the function getViewMatrix)
flattenedmodelview = rotator.getViewMatrix();
modelview = unflatten(flattenedmodelview);
normalMatrix = extractNormalMatrix(modelview);
var initialmodelview = modelview;
if (texIDmap0.isloaded) { // if texture identifier has been assigned
var initialmodelview = modelview;
// Draw the environment (box)
gl.useProgram(progbox); // Select the shader program that is used for the environment box.
gl.uniformMatrix4fv(uProjectionbox, false, flatten(projection));
gl.enableVertexAttribArray(aCoordsbox);
gl.disableVertexAttribArray(aNormalbox); // normals are not used for the box
gl.disableVertexAttribArray(aTexCoordbox); // texture coordinates not used for the box
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texIDmap0);
// Send texture to sampler
gl.uniform1i(uEnvbox, 0);
modelview = initialmodelview;
envbox.render();
}
// Select shader program
gl.useProgram(prog);
gl.uniform1f(gl.getUniformLocation(prog, "shininess"), materialShininess);
gl.uniform4fv(gl.getUniformLocation(prog, "lightPosition"), flatten(lightPosition));
gl.uniformMatrix4fv(ProjectionLoc, false, flatten(projection)); // send projection matrix to the new shader program
gl.enableVertexAttribArray(CoordsLoc);
gl.enableVertexAttribArray(NormalLoc);
gl.disableVertexAttribArray(TexCoordLoc);
function shipMain() {
//------------------------------------------------------------------------------------------------
//HULL
//------------------------------------------------------------------------------------------------
materialTerms(0, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(0.0, 0.0, 0.0));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.7, 0.7, 0.7));
fHull.render();
//------------------------------------------------------------------------------------------------
//COCKPIT
//------------------------------------------------------------------------------------------------
materialTerms(2, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(0.0, 3.05, -4.5));
modelview = mult(modelview, rotate(-2.0, 1, 0, 0));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.4, 0.6, 0.6));
cockpit.render();
}
function shipWings() {
//------------------------------------------------------------------------------------------------
//WINGS
//------------------------------------------------------------------------------------------------
materialTerms(1, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(-2.7, 3.0, -18.0));
modelview = mult(modelview, rotate(-30.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(1, 1, 1));
wing.render(); //top right
//Red Stripes re-using wings models
materialTerms(5, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(-2.7, 3.4, -18.0));
modelview = mult(modelview, rotate(-30.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.9, 0.6, 0.4));
wing.render(); //top right
materialTerms(1, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(-2.7, 0.0, -18.0));
modelview = mult(modelview, rotate(30.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(1, 1, 1));
wing.render(); //bottom right
//Red Stripes re-using wings models
materialTerms(5, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(-2.7, -0.4, -18.0));
modelview = mult(modelview, rotate(30.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.9, 0.6, 0.4));
wing.render(); //bot right
materialTerms(1, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(2.7, 3.0, -18.0));
modelview = mult(modelview, rotate(-150.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(1, 1, 1));
wing.render(); //top left
//Red Stripes re-using wings models
materialTerms(5, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(2.7, 3.4, -18.0));
modelview = mult(modelview, rotate(-150.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.9, 0.6, 0.4));
wing.render(); //top left
materialTerms(1, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(2.7, 0.0, -18.0));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(1, 1, 1));
wing.render(); //bottom left
//Red Stripes re-using wings models
materialTerms(5, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(2.7, -0.4, -18.0));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.9, 0.6, 0.4));
wing.render(); //bottom left
}
function shipReactors() {
//------------------------------------------------------------------------------------------------
//REACTORS
//------------------------------------------------------------------------------------------------
//top left BIG
materialTerms(3, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(3.3, 4.6, -17.0));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.13, 0.13, 0.17));
cylinderLight.render(); //Main
//top left SMALL
materialTerms(4, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(3.5, 4.8, -20.0));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.07, 0.07, 0.21));
cylinderDark.render();
//Propulsion emitter
materialTerms(6, 1);
modelview = initialmodelview;
modelview = mult(modelview, translate(3.5, 4.8, -22.2));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.07, 0.07, 0.21));
//disk.render();
//bot left
materialTerms(3, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(3.3, -2, -17.0));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.13, 0.13, 0.17));
cylinderLight.render(); //Main
//bot left SMALL
materialTerms(4, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(3.5, -2.2, -20.0));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.07, 0.07, 0.21));
cylinderDark.render();
//Propulsion emitter
materialTerms(6, 1);
modelview = initialmodelview;
modelview = mult(modelview, translate(3.5, -2.2, -22.2));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.07, 0.07, 0.21));
//disk.render();
//bot right
materialTerms(3, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(-3.3, -2, -17.0));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.13, 0.13, 0.17));
cylinderLight.render(); //Main
//bot right SMALL
materialTerms(4, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(-3.5, -2.2, -20.0));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.07, 0.07, 0.21));
cylinderDark.render();
//Propulsion emitter
materialTerms(6, 1);
modelview = initialmodelview;
modelview = mult(modelview, translate(-3.5, -2.2, -22.2));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.07, 0.07, 0.21));
//disk.render();
//top right
materialTerms(3, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(-3.3, 4.6, -17.0));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.13, 0.13, 0.17));
cylinderLight.render(); //Main
//top right SMALL
materialTerms(4, 0);
modelview = initialmodelview;
modelview = mult(modelview, translate(-3.5, 4.8, -20.0));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.07, 0.07, 0.21));
cylinderDark.render();
//Propulsion emitter
materialTerms(6, 1);
modelview = initialmodelview;
modelview = mult(modelview, translate(-3.5, 4.8, -22.2));
modelview = mult(modelview, rotate(-210.0, 0, 0, 1));
normalMatrix = extractNormalMatrix(modelview); // always extract the normal matrix before scaling
modelview = mult(modelview, scale(0.07, 0.07, 0.21));
//disk.render();
}
shipMain(); //Originally created to fix an undefined function bug, but I kept it to make the code more flexible
shipWings();
shipReactors();
}
&#13;