- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图弄清楚如何根据鼠标事件绘制三 Angular 形而不是点。代码如下:
var gl;
var points = [];
var tpoints = [];
var index = 0;
var cIndex = 0;
var dTri = false;
var dPoint = true;
var count = 0;
var counter = 0;
function init() {
var canvas = document.getElementById("gl-canvas");
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) {
alert("WebGL isn't available");
}
var colors = [
vec4(0.3921, 0.5843, 0.9294, 1.0), //Default
vec4(0.0, 0.0, 0.0, 1.0), // black
vec4(1.0, 0.0, 0.0, 1.0), // red
vec4(1.0, 1.0, 0.0, 1.0), // yellow
vec4(0.0, 1.0, 0.0, 1.0), // green
vec4(0.0, 0.0, 1.0, 1.0), // blue
vec4(1.0, 0.0, 1.0, 1.0), // magenta
vec4(0.0, 1.0, 1.0, 1.0) // cyan
];
gl.viewport(0, 0, canvas.width, canvas.height);
var m = document.getElementById("mymenu");
m.addEventListener("click", function() {
cIndex = m.selectedIndex;
});
var a = colors[cIndex][0];
var b = colors[cIndex][1];
var c = colors[cIndex][2];
var d = colors[cIndex][3];
gl.clearColor(a, b, c, d);
// Load shaders and initialize attribute buffers
var program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
var vbuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferData(gl.ARRAY_BUFFER, 8 * 200, gl.STATIC_DRAW);
var vPosition = gl.getAttribLocation(program, "vPosition");
gl.vertexAttribPointer(vPosition, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vPosition);
var cBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferData(gl.ARRAY_BUFFER, 16 * 200, gl.STATIC_DRAW);
var vColor = gl.getAttribLocation(program, "vColor");
gl.vertexAttribPointer(vColor, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vColor);
canvas.addEventListener("mousedown", function(event) {
counter++;
var i = index;
x = 2 * (event.clientX - 12) / canvas.width - 1;
y = 2 * (canvas.height - (event.clientY - 165)) / canvas.height - 1;
var pts = [x, y];
console.log(pts);
if (dPoint || (dTri && counter % 3 != 0)) {
points.push(pts);
} else if (dTri) {
index = index - 3;
tpoints.push(pts);
tpoints.push(points.pop());
tpoints.push(points.pop());
count++;
}
index++;
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 8 * i, flatten(pts));
t = vec4(colors[cIndex]);
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 16 * i, flatten(t));
});
render();
};
function drawTriangles() {
dTri = true;
dPoint = false;
counter = 0;
}
function drawPoints() {
dTri = false;
dPoint = true;
}
function render() {
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.POINTS, 0, index);
gl.drawArrays(gl.TRIANGLES, 0, count);
window.requestAnimationFrame(render);
}
function clearC() {
var oldcanv = document.getElementById('gl-canvas');
document.body.removeChild(oldcanv);
var canv = document.createElement('canvas');
canv.id = 'gl-canvas';
canv.width = 500;
canv.height = 500;
document.body.appendChild(canv);
index = 0;
count = 0;
init();
}
<!DOCTYPE html>
<html>
<head>
<title>CG - Part2</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script id="vertex-shader" type="x-shader/x-vertex">
attribute vec4 vPosition; attribute vec4 vColor; varying vec4 fColor; void main() { gl_Position = vPosition; gl_PointSize = 10.0; fColor = vColor; }
</script>
<script id="fragment-shader" type="x-shader/x-fragment">
precision mediump float; varying vec4 fColor; void main() { gl_FragColor = fColor; }
</script>
<script src="https://www.cs.unm.edu/~angel/WebGL/7E/Common/initShaders.js"></script>
<script src="https://www.cs.unm.edu/~angel/WebGL/7E/Common/MV.js"></script>
<script src="https://www.cs.unm.edu/~angel/WebGL/7E/Common/webgl-utils.js"></script>
<script>
window.onload = init;
</script>
</head>
<body>
<div style="margin-top: 10px; margin-bottom: 10px">
<input type="button" onClick="clearC()" value="Clear">
<input type="button" onClick="drawTriangles()" value="Draw Triangles">
<input type="button" onClick="drawPoints()" value="Draw Points">
<select id="mymenu" size="8">
<option selected value="0">Default</option>
<option value="1">Black</option>
<option value="2">Red</option>
<option value="3">Yellow</option>
<option value="4">Green</option>
<option value="5">Blue</option>
<option value="6">Magenta</option>
<option value="7">Cyan</option>
</select>
</div>
<canvas id="gl-canvas" width="500" height="500">
Oops ... your browser doesn't support the HTML5 canvas element
</canvas>
</body>
</html>
正如你所看到的, Canvas 使用点数组而不是三 Angular 形点数组来绘制三 Angular 形。谁能看出我哪里出了问题吗?
更新:
var gl, canvas, program, colors, menu;
var points = [];
var tpoints = [];
var cpoints = [];
var colorsP = [];
var tcolorsP = [];
var ccolorsP = [];
var pointIndex = 0;
var triangleIndex = 0;
var circleIndex = 0;
var colorIndex = 0;
var counter = 0;
var numTris = 100;
var degPerTri = (2 * Math.PI) / numTris;
var dTri = false;
var dPoint = true;
var dCircle = false;
var vbuffer;
var cBuffer;
function begin() {
canvas = document.getElementById("gl-canvas");
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) { alert("WebGL isn't available"); }
gl.viewport(0, 0, canvas.width, canvas.height);
program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
colors = [
vec4(0.3921, 0.5843, 0.9294, 1.0), //Default
vec4(0.0, 0.0, 0.0, 1.0), // black
vec4(1.0, 0.0, 0.0, 1.0), // red
vec4(1.0, 1.0, 0.0, 1.0), // yellow
vec4(0.0, 1.0, 0.0, 1.0), // green
vec4(0.0, 0.0, 1.0, 1.0), // blue
vec4(1.0, 0.0, 1.0, 1.0), // magenta
vec4(0.0, 1.0, 1.0, 1.0) // cyan
];
r = colors[colorIndex][0];
g = colors[colorIndex][1];
b = colors[colorIndex][2];
a = colors[colorIndex][3];
gl.clearColor(r, g, b, a);
menu = document.getElementById("mymenu");
}
function init() {
begin();
vbuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferData(gl.ARRAY_BUFFER, 8 * 200, gl.STATIC_DRAW);
var vPosition = gl.getAttribLocation(program, "vPosition");
gl.vertexAttribPointer(vPosition, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vPosition);
cBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferData(gl.ARRAY_BUFFER, 16 * 200, gl.STATIC_DRAW);
var vColor = gl.getAttribLocation(program, "vColor");
gl.vertexAttribPointer(vColor, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vColor);
menu.addEventListener("click", function () {
colorIndex = menu.selectedIndex;
});
canvas.addEventListener("mousedown", function (event) {
var mousePos = getMousePos(canvas, event);
x = 2 * (mousePos.x - 5) / canvas.width - 1;
y = 2 * (canvas.height - (mousePos.y - 5)) / canvas.height - 1;
var pts = [x, y];
counter++;
console.log('Punkt : ', pts);
if (dPoint || (dTri && counter % 3 != 0) || (dCircle && counter % 2 != 0)) {
points.push(pts);
colorsP.push(vec4(colors[colorIndex]));
} else if (dTri) {
pointIndex = pointIndex - 3;
tpoints.push(pts);
tcolorsP.push(vec4(colors[colorIndex]));
tpoints.push(points.pop());
tcolorsP.push(colorsP.pop());
tpoints.push(points.pop());
tcolorsP.push(colorsP.pop());
triangleIndex++;
} else if (dCircle) {
pointIndex = pointIndex - 2;
cpoints.push(pts);
ccolorsP.push(vec4(colors[colorIndex]));
cpoints.push(points.pop());
ccolorsP.push(colorsP.pop());
var xs = cpoints[0][0] - cpoints[1][0];
var ys = cpoints[0][1] - cpoints[1][1];
console.log('Diff x:',xs, ' Diff y:',ys);
var radius = Math.sqrt(Math.pow(xs, 2) + Math.pow(ys, 2));
console.log(radius);
cpoints = [
cpoints[0][0], cpoints[0][1],
cpoints[1][0], cpoints[1][1]
];
console.log('Punkt nr: 0', 'X Koordinat:', cpoints[0], 'Y Koordinat:', cpoints[1]);
console.log('Punkt nr: 1', 'X Koordinat:', cpoints[2], 'Y Koordinat:', cpoints[3]);
for (var i = 0; i < numTris; i++) {
var index = 2 * 2 + i * 2;
var angle = degPerTri * (i+1);
cpoints[index] = (Math.cos(angle) / 4);
cpoints[index + 1] = (Math.sin(angle) / 4);
console.log('Punkt nr:', index, 'X Koordinat:', cpoints[index], 'Y Koordinat:', cpoints[index+1]);
}
circleIndex++;
}
pointIndex++;
});
render();
};
function drawCircles() {
dTri = false;
dPoint = false;
dCircle = true;
counter = 0;
}
function drawTriangles() {
dTri = true;
dPoint = false;
dCircle = false;
counter = 0;
}
function drawPoints() {
dTri = false;
dPoint = true;
dCircle = false;
}
function render() {
gl.clear(gl.COLOR_BUFFER_BIT);
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(points));
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(colorsP));
gl.drawArrays(gl.POINTS, 0, pointIndex);
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(tpoints));
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(tcolorsP));
gl.drawArrays(gl.TRIANGLES, 0, triangleIndex * 3);
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(cpoints));
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(ccolorsP));
gl.drawArrays(gl.TRIANGLE_FAN, 0, 52 * (circleIndex * 2));
window.requestAnimationFrame(render);
}
function clearC() {
var oldcanv = document.getElementById('gl-canvas');
document.body.removeChild(oldcanv);
var canv = document.createElement('canvas');
canv.id = 'gl-canvas';
canv.width = 500;
canv.height = 500;
document.body.appendChild(canv);
pointIndex = 0;
triangleIndex = 0;
circleIndex = 0;
counter = 0;
points = [];
colorsP = [];
tpoints = [];
tcolorsP = [];
cpoints = [];
ccolorsP = [];
init();
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
最佳答案
抱歉,我不是 JavaScript 大师,但这里是有效的代码:
var gl;
var points = [];
var tpoints = [];
var colorsP = [];
var tcolorsP = [];
var index = 0;
var cIndex = 0;
var dTri = false;
var dPoint = true;
var vbuffer;
var cBuffer;
var count = 0;
var counter = 0;
function init() {
var canvas = document.getElementById("gl-canvas");
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) {
alert("WebGL isn't available");
}
colors = [
vec4(0.3921, 0.5843, 0.9294, 1.0), //Default
vec4(0.0, 0.0, 0.0, 1.0), // black
vec4(1.0, 0.0, 0.0, 1.0), // red
vec4(1.0, 1.0, 0.0, 1.0), // yellow
vec4(0.0, 1.0, 0.0, 1.0), // green
vec4(0.0, 0.0, 1.0, 1.0), // blue
vec4(1.0, 0.0, 1.0, 1.0), // magenta
vec4(0.0, 1.0, 1.0, 1.0) // cyan
];
gl.viewport(0, 0, canvas.width, canvas.height);
var m = document.getElementById("mymenu");
m.addEventListener("click", function() {
cIndex = m.selectedIndex;
});
var a = colors[cIndex][0];
var b = colors[cIndex][1];
var c = colors[cIndex][2];
var d = colors[cIndex][3];
gl.clearColor(a, b, c, d);
// Load shaders and initialize attribute buffers
var program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
vbuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferData(gl.ARRAY_BUFFER, 8 * 200, gl.STATIC_DRAW);
var vPosition = gl.getAttribLocation(program, "vPosition");
gl.vertexAttribPointer(vPosition, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vPosition);
cBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferData(gl.ARRAY_BUFFER, 16 * 200, gl.STATIC_DRAW);
var vColor = gl.getAttribLocation(program, "vColor");
gl.vertexAttribPointer(vColor, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vColor);
canvas.addEventListener("mousedown", function(event) {
counter++;
var i = index;
x = 2 * (event.clientX - 12) / canvas.width - 1;
y = 2 * (canvas.height - (event.clientY - 165)) / canvas.height - 1;
var pts = [x, y];
console.log(pts);
if (dPoint || (dTri && counter % 3 != 0)) {
points.push(pts);
colorsP.push(vec4(colors[cIndex]));
} else if (dTri) {
index = index - 3;
tpoints.push(pts);
tcolorsP.push(vec4(colors[cIndex]));
tpoints.push(points.pop());
tcolorsP.push(colorsP.pop());
tpoints.push(points.pop());
tcolorsP.push(colorsP.pop());
count++;
}
index++;
});
render();
};
function drawTriangles() {
dTri = true;
dPoint = false;
counter = 0;
}
function drawPoints() {
dTri = false;
dPoint = true;
}
function render() {
gl.clear(gl.COLOR_BUFFER_BIT);
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(points));
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(colorsP));
gl.drawArrays(gl.POINTS, 0, index);
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(tpoints));
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(tcolorsP));
gl.drawArrays(gl.TRIANGLES, 0, count*3);
window.requestAnimationFrame(render);
}
function clearC() {
var oldcanv = document.getElementById('gl-canvas');
document.body.removeChild(oldcanv);
var canv = document.createElement('canvas');
canv.id = 'gl-canvas';
canv.width = 500;
canv.height = 500;
document.body.appendChild(canv);
index = 0;
count = 0;
init();
}
出了什么问题以及我做了什么:
gl.drawArrays(gl.TRIANGLES, 0, count); -> gl.drawArrays(gl.TRIANGLES, 0, count*3);
drawArrays 需要点数,因此需要 3 * 三 Angular 形数量。
我假设您想要将点与三 Angular 形分开,因此您不能混合三 Angular 形顶点和点。每次鼠标单击后,您都会发送有关 1 个点及其颜色的数据。并且永远不会更改以前的数据,因此 gl.drawArrays(gl.POINTS, 0, index);
工作正常。但随后您想要绘制三 Angular 形,因此您需要提供新数据。您将三 Angular 形的顶点存储在 tpoints 中,但从未将其发送到 GPU 内存中。因此,您需要在 2 个 VAO 中创建 4 个缓冲区(2 个位置、2 个颜色),或者(这更简单,我在上面展示过)为每个帧复制数据两次 - 一次为点提供数据,一次为三 Angular 形提供数据。
希望对您有帮助,欢迎提出更多问题。
var gl, canvas, program, colors, menu;
var points = [];
var tpoints = [];
var cpoints = [];
var colorsP = [];
var tcolorsP = [];
var ccolorsP = [];
var pointIndex = 0;
var triangleIndex = 0;
var circleIndex = 0;
var colorIndex = 0;
var counter = 0;
var numTris = 20;
var degPerTri = (2 * Math.PI) / numTris;
var dTri = false;
var dPoint = true;
var dCircle = false;
var vbuffer;
var cBuffer;
function begin() {
canvas = document.getElementById("gl-canvas");
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) { alert("WebGL isn't available"); }
gl.viewport(0, 0, canvas.width, canvas.height);
program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
colors = [
vec4(0.3921, 0.5843, 0.9294, 1.0), //Default
vec4(0.0, 0.0, 0.0, 1.0), // black
vec4(1.0, 0.0, 0.0, 1.0), // red
vec4(1.0, 1.0, 0.0, 1.0), // yellow
vec4(0.0, 1.0, 0.0, 1.0), // green
vec4(0.0, 0.0, 1.0, 1.0), // blue
vec4(1.0, 0.0, 1.0, 1.0), // magenta
vec4(0.0, 1.0, 1.0, 1.0) // cyan
];
r = colors[colorIndex][0];
g = colors[colorIndex][1];
b = colors[colorIndex][2];
a = colors[colorIndex][3];
gl.clearColor(r, g, b, a);
menu = document.getElementById("mymenu");
}
function init() {
begin();
vbuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferData(gl.ARRAY_BUFFER, 8 * 200, gl.STATIC_DRAW);
var vPosition = gl.getAttribLocation(program, "vPosition");
gl.vertexAttribPointer(vPosition, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vPosition);
cBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferData(gl.ARRAY_BUFFER, 16 * 200, gl.STATIC_DRAW);
var vColor = gl.getAttribLocation(program, "vColor");
gl.vertexAttribPointer(vColor, 4, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vColor);
menu.addEventListener("click", function () {
colorIndex = menu.selectedIndex;
});
canvas.addEventListener("mousedown", function (event) {
var mousePos = getMousePos(canvas, event);
x = 2 * (mousePos.x - 5) / canvas.width - 1;
y = 2 * (canvas.height - (mousePos.y - 5)) / canvas.height - 1;
var pts = [x, y];
counter++;
console.log('Punkt : ', pts);
if (dPoint || (dTri && counter % 3 != 0) || (dCircle && counter % 2 != 0)) {
points.push(pts);
colorsP.push(vec4(colors[colorIndex]));
} else if (dTri) {
pointIndex = pointIndex - 3;
tpoints.push(pts);
tcolorsP.push(vec4(colors[colorIndex]));
tpoints.push(points.pop());
tcolorsP.push(colorsP.pop());
tpoints.push(points.pop());
tcolorsP.push(colorsP.pop());
triangleIndex++;
} else if (dCircle) {
pointIndex = pointIndex - 2;
var p2 = pts;
var p1 = points.pop();
var c2 = vec4(colors[colorIndex]);
var c1 = colorsP.pop();
var xs = p1[0] - p2[0];
var ys = p1[1] - p2[1];
console.log('Diff x:',xs, ' Diff y:',ys);
var radius = Math.sqrt(Math.pow(xs, 2) + Math.pow(ys, 2));
console.log(radius);
console.log('Punkt nr: 0', 'X Koordinat:', p1, 'Y Koordinat:', cpoints[1]);
console.log('Punkt nr: 1', 'X Koordinat:', cpoints[2], 'Y Koordinat:', cpoints[3]);
for (var i = 0; i < numTris; i++) {
var angle1 = degPerTri * (i);
var angle2 = degPerTri * (i+1);
var pt2 = vec2(Math.cos(angle1)*radius, Math.sin(angle1)*radius);
var pt3 = vec2(Math.cos(angle2)*radius, Math.sin(angle2)*radius);
var p2 = vec2(p1[0]+pt2[0], p1[1]+pt2[1]);
var p3 = vec2(p1[0]+pt3[0], p1[1]+pt3[1]);
cpoints.push(p1);
cpoints.push(p2);
cpoints.push(p3);
ccolorsP.push(c1);
ccolorsP.push(c2);
ccolorsP.push(c2);
}
console.log(cpoints);
circleIndex++;
}
pointIndex++;
});
render();
};
function drawCircles() {
dTri = false;
dPoint = false;
dCircle = true;
counter = 0;
}
function drawTriangles() {
dTri = true;
dPoint = false;
dCircle = false;
counter = 0;
}
function drawPoints() {
dTri = false;
dPoint = true;
dCircle = false;
}
function render() {
gl.clear(gl.COLOR_BUFFER_BIT);
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(points));
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(colorsP));
gl.drawArrays(gl.POINTS, 0, pointIndex);
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(tpoints));
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(tcolorsP));
gl.drawArrays(gl.TRIANGLES, 0, triangleIndex * 3);
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(cpoints));
gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, flatten(ccolorsP));
gl.drawArrays(gl.TRIANGLES, 0, 3*circleIndex*numTris);
window.requestAnimationFrame(render);
}
function clearC() {
var oldcanv = document.getElementById('gl-canvas');
document.body.removeChild(oldcanv);
var canv = document.createElement('canvas');
canv.id = 'gl-canvas';
canv.width = 500;
canv.height = 500;
document.body.appendChild(canv);
pointIndex = 0;
triangleIndex = 0;
circleIndex = 0;
counter = 0;
points = [];
colorsP = [];
tpoints = [];
tcolorsP = [];
cpoints = [];
ccolorsP = [];
init();
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
我改变了什么:
var numTris = 20;
使用gl.bufferData
,您可以在GPU上分配一些内存并复制一些数据(或不复制)。使用gl.bufferSubData
,您只需复制数据而不分配任何内存。如果您的数据大于分配的内存,您将遇到大麻烦。
gl.drawArrays(gl.TRIANGLES, 0, 3*circleIndex*numTris);
使用gl.TRIANGLE_FAN
,您将无法绘制多个圆圈,因为扇形中的三 Angular 形是相连的。我还删除了魔法常数。
for (var i = 0; i < numTris; i++) {
var angle1 = degPerTri * (i);
var angle2 = degPerTri * (i+1);
var pt2 = vec2(Math.cos(angle1)*radius, Math.sin(angle1)*radius);
var pt3 = vec2(Math.cos(angle2)*radius, Math.sin(angle2)*radius);
var p2 = vec2(p1[0]+pt2[0], p1[1]+pt2[1]);
var p3 = vec2(p1[0]+pt3[0], p1[1]+pt3[1]);
cpoints.push(p1);
cpoints.push(p2);
cpoints.push(p3);
ccolorsP.push(c1);
ccolorsP.push(c2);
ccolorsP.push(c2);
}
你在这里做了一些疯狂的事情,将一些值分配给cpoints
而不是改变它,一团糟。在这里,我为圆圈中的每个三 Angular 形推送 3 个位置和 3 种颜色。抱歉,额外的作业,但是 javascript 字符串连接而不是加法让我发疯;) p1
也是圆的中心
关于javascript - 在同一 Canvas 上绘制 gl.POINTS 和 gl.TRIANGLES,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33086376/
我有以下三 Angular 形 由以下 css 创建: .triangle { width: 0; height: 0; border-left: 5px solid tran
在(不久前)阅读索引索引的三角形列表后,由于需要较少的绘制调用,因此我决定在三角形列表上构建我的引擎。今天,我偶然发现了0xffffffff,在DX中该值被认为是 strip 切割索引,因此您可以在一
该类包含: 三个 double 数据字段,分别命名为 side1、side2 和 side3,具有默认值1.0 表示三角形的三边 无参数构造函数,用于创建具有指定 side1、side2 和 side
我一直在尝试用 C 语言解决 HackerRank 网站中的“小三角形,大三角形”问题,这是一种排序数组问题,我使用了选择排序算法。我的代码似乎可以工作,但它仅适用于某些测试用例。这是任务: You
我需要打印一个三角形及其倒三角形(站在其尖端)。我设法只打印出三角形。我知道我可以轻松使用 for 循环,但我想知道如何使用递归,就我而言,我不知道如何打印三角形和倒三角形。谢谢。 Example d
如果使用函数 triangular_mesh 可以传递一组三角形,以索引列表、顶点位置和一组数据的形式给出,以将颜色分配给顶点。然后通过顶点颜色之间的插值对三角形着色。 如何在不进行任何插值的情况下按
我正在实现 Wikipedia 中介绍的 Bowyer-Watson 算法.在我的实现中,一切都如我所料,直到伪代码的最后一部分: for each triangle in triangulation
题目地址 https://leetcode.com/problems/triangle/description/ 题目描述: Given a triangle, find the minimum
我想知道是否有一些教程或指南来理解和实现 3D 环境中的三角形-三角形相交测试。 (我不需要知道交叉点发生的确切位置,但只需要知道交叉点已经发生) 我打算按照理论 pdf 来实现它,但我很困在 计算三
我制作了一个程序来制作帕斯卡三角形并输入数字(行)> 5,存在对齐问题,即 ncr > 10。请帮帮我。我已经包含了程序输出的图像。 Output Image #include int factori
由于很难解释,我将向您展示我假装的图像。 问题不在于“三 Angular 形”,而在于要在三 Angular 形生成的“空间”中显示的图像背景。 最佳答案 这是我的方法,如何处理这个任务。我将伪元素
题目地址:https://leetcode.com/problems/pascals-triangle/open in new window Total Accepted: 83023 Total
如何使用 Triangle.NET 对具有如下所示孔的多边形进行三角剖分? 我需要由三角形覆盖的绿色区域。我想我需要在触摸点上分割外部轮廓的右侧部分,但之后我不知道是否需要将两个轮廓合并为一个(在黑色
我在碰撞检测代码中使用的代码是这样的: (注意:Vector3f 是 LWJGL 库的一部分。) (注2:Tri是由LWJGL的Vector3fs.v1、v2、v3三个组成的类。) public Ve
有一个三角网格生成器(Shewchuk,CMU), "Triangle" ,由于其重量轻而受到人们的推荐。它是用 C 编写的。但是我无法在我的 VS2012 C++ 项目中使用它。我正在使用 x64
我应该编写的程序应该按以下方式打印三角形: 如果行数为2: * *** 如果行数为3: * * * ***** 但是,我所做的以下代码打印了最后一行的
(我在这里搜索并通读了钻石和虚拟继承问题,但找不到答案。我的想法是这种情况有点不寻常,我愿意考虑我的要求有点关闭。另一方面,我认为这应该以一种“不错”的方式实现。) 情况及要求: 我有一个 C++ 类
我构建 HTML/CSS/JS 菜单并希望将箭头向右对齐以指向该元素是子菜单。 我的问题是在 Firefox 中三 Angular 形(“▶”符号)显示在下一行而不是当前行... Chromium 在
我正在创建一个 OpenGL 程序,它只是创建一个三角形并将其绘制在屏幕上。我创建了一个名为 Drawable 的类,其中包含绘制函数以及使用的 VAO 和 VBO 对象。我还创建了一个继承自 Dra
我正在尝试使用嵌套循环打印下面的图片(我应该使用 for 和 while 循环): ** **** ****** ******** 以 5 个空格开头,每行减少 2 个空格。从 2
我是一名优秀的程序员,十分优秀!