我正在尝试使3d对象绕其x轴简单旋转。该程序设置一个片段和顶点着色器,以用于WebGl程序中的对象。该程序包含onLoad函数,一旦将其加载到Web浏览器中,该函数就会激活函数中的所有内容。首先,该功能设置要用于保存3D对象或多个3D对象的画布。然后创建要转换的3D块对象,默认情况下将其设置为原点。我正在创建一个新列表,并用旧列表中的所有向量填充它,同时还将转换应用于每个向量。我相信我的问题在于尝试将翻译应用于每个向量的for循环。
const vertex_shader = `
attribute vec4 vPosition;
attribute vec4 vColor;
uniform mat4 transform;
varying vec4 fColor;
void main()
{
gl_Position = transform * vPosition;
fColor = vColor;
gl_PointSize = 10.0;
}`;
const fragment_shader = `
precision mediump float;
varying vec4 fColor;
void main()
{
gl_FragColor = fColor;
}`;
let params = {
transform: mat4(),
transformLoc: undefined,
maxVertices: 7000,
colors: undefined
};
function params_setup(gl, program) {
params.transformLoc = gl.getUniformLocation(program, "transform");
gl.uniformMatrix4fv(params.transformLoc, false, flatten(params.transform));
// params.bands = 5;
// params.sides = 10;
}
window.onload = function init()
{
const lg = noLogging("Firebrick");
lg.insertAtEnd = false;
let canvas = document.getElementById( "gl-canvas" );
let gl = WebGLUtils.setupWebGL( canvas );
if ( !gl ) { alert( "WebGL isn't available" ); }
gl.enable(gl.DEPTH_TEST);
// gl.enable(gl.CULL_FACE);
gl.cullFace(gl.BACK);
//
// Configure WebGL
//
gl.viewport( 0, 0, canvas.width, canvas.height );
X11.clearColor(gl, X11.AliceBlue);
// Load shaders and initialize attribute buffers
let program = initShaders( gl, vertex_shader, fragment_shader);
gl.useProgram( program );
params_setup(gl, program);
//creates the block object that will be transformed
let blk = new cs4722_objects.Block();
blk.height = .75;
blk.width = .5;
blk.depth = .25;
let vertices = blk.points;
let colors = blk.color_scheme;
let trans = rotateX(45);
// modify vertices using the transformation here
let newList = [];
//I believe my problem resides in this for loop here
for(var i = 0; i < vertices.length; i++)
{
newList.push(mult(trans, vertices[i]));
}
//to here
let bufferId = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, bufferId );
gl.bufferData( gl.ARRAY_BUFFER, 16*params.maxVertices, gl.STATIC_DRAW );
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(vertices));
// Associate out shader variables with our data buffer
let vPosition = gl.getAttribLocation( program, "vPosition" );
gl.vertexAttribPointer( vPosition, 4, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray( vPosition );
let cBufferId = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, cBufferId );
gl.bufferData( gl.ARRAY_BUFFER, 16*params.maxVertices, gl.STATIC_DRAW );
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(colors));
let vColor = gl.getAttribLocation( program, "vColor" );
// four color components per color
gl.vertexAttribPointer( vColor, 4, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray( vColor );
function render() {
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.drawArrays( gl.TRIANGLES, 0, vertices.length);
};
render();
};
答案 0 :(得分:0)
使用它:
<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {
margin: auto;
border: 1px solid black;
width: 200px;
height: 100px;
background-color: coral;
color: white;
}
</style>
</head>
<body>
<p>Click the "Try it" button to rotate the DIV element:</p>
<button onclick="myFunction()">Try it</button>
<div id="myDIV">
<h1>myDIV</h1>
</div>
<script>
function myFunction() {
// Code for Safari
document.getElementById("myDIV").style.WebkitTransform = "rotate(20deg)";
// Code for IE9
document.getElementById("myDIV").style.msTransform = "rotate(20deg)";
// Standard syntax
document.getElementById("myDIV").style.transform = "rotate(20deg)";
}
</script>
<p><b>Note:</b> Internet Explorer 9 supports an alternative, the msTransform property. Newer versions of IE and Edge support the transform property (do not need the ms prefix).</p>
<p><b>Note:</b> Safari supports an alternative, the WebkitTransform property.</p>
</body>
</html>
答案 1 :(得分:0)
您没有使用newList
。您的mult
函数会返回经过修改的新顶点,因此将其放置在新数组中是正确的,但是您要将未经修改的vertices
数组传递给gl.bufferSubData
。
要么:
vertices
替换对newList
的其他引用vertices
一样就地修改vertices = vertices.map(vert => mult(trans, vert));
(我假设您的mult
函数来自以下模板:http://ksuweb.kennesaw.edu/~bsetzer/4722fa18/nanoc/output/assignments/4/)