简体   繁体   English

如何在webgl中的模型视图矩阵中设置旋转的枢轴

[英]How to set the pivot for rotation in model view matrix in webgl

I study this rotation code from webgl fundamentals, It rotates the object around the 0 0 origin.我从 webgl 基础研究了这个旋转代码,它围绕 0 0 原点旋转对象。 https://jsfiddle.net/eguneys/5s7n82pt/6/ . https://jsfiddle.net/eguneys/5s7n82pt/6/ I wrote exactly the same code myself and when I rotate, it rotates around the middle point (width / 2, height / 2) and it doesn't rotate correctly, it skews and just doesn't work.我自己编写了完全相同的代码,当我旋转时,它围绕中间点(宽度/ 2,高度/ 2)旋转,并且旋转不正确,它倾斜并且不起作用。

So my question would be why does that happen, and How do I rotate this example code around the middle point.所以我的问题是为什么会发生这种情况,以及如何围绕中间点旋转此示例代码。

in vec2 a_position;

// Used to pass in the resolution of the canvas
uniform vec2 u_resolution;

// A matrix to transform the positions by
uniform mat3 u_matrix;

// all shaders have a main function
void main() {
  // Multiply the position by the matrix.
  vec2 position = (u_matrix * vec3(a_position, 1)).xy;

  // convert the position from pixels to 0.0 to 1.0
  vec2 zeroToOne = position / u_resolution;

  // convert from 0->1 to 0->2
  vec2 zeroToTwo = zeroToOne * 2.0;

  // convert from 0->2 to -1->+1 (clipspace)
  vec2 clipSpace = zeroToTwo - 1.0;

  gl_Position = vec4(clipSpace, 0, 1);
}

The article you got that shader from this clearly this article你从这篇文章中清楚地得到了那个着色器的文章

You apparently didn't finish reading the article because that shader is from the top of the article and the entire point of the article is that that shader can be reduced to just您显然没有读完这篇文章,因为该着色器来自文章的顶部,而本文的全部要点是该着色器可以简化为

#version 300 es

in vec2 a_position;

uniform mat3 u_matrix;

void main() {
  gl_Position = vec4((u_matrix * vec3(a_position, 1)).xy, 0, 1);
}

It points out why that's better, one of the reasons being you can move the pivot point without having the change the shader.它指出了为什么更好,原因之一是您可以在不更改着色器的情况下移动枢轴点。 It gives an example where it changes the pivot of its example model它给出了一个示例,其中更改了示例模型的枢轴

It's starts with code like this where the model rotates around its local origin which is the top left corner of the model它以这样的代码开始,其中模型围绕其本地原点旋转,即模型的左上角

var translationMatrix = m3.translation(translation[0], translation[1]);
var rotationMatrix = m3.rotation(rotationInRadians);
var scaleMatrix = m3.scaling(scale[0], scale[1]);

// Multiply the matrices.
var matrix = m3.multiply(translationMatrix, rotationMatrix);
matrix = m3.multiply(matrix, scaleMatrix);

And moves the pivot to the center of the model, the model being 100 units wide and 150 units tall, like this.并将枢轴移动到模型的中心,模型的宽度为 100 个单位,高度为 150 个单位,如下所示。

var translationMatrix = m3.translation(translation[0], translation[1]);
var rotationMatrix = m3.rotation(rotationInRadians);
var scaleMatrix = m3.scaling(scale[0], scale[1]);

// make a matrix that will move the origin of the 'F' to its center.
var moveOriginMatrix = m3.translation(-50, -75);
...

// Multiply the matrices.
var matrix = m3.multiply(translationMatrix, rotationMatrix);
matrix = m3.multiply(matrix, scaleMatrix);
matrix = m3.multiply(matrix, moveOriginMatrix);

Further down it simplifies those functions so you can do this进一步向下它简化了这些功能,因此您可以执行此操作

// Multiply the matrices.
var matrix = m3.projection(gl.canvas.clientWidth, gl.canvas.clientHeight);
matrix = m3.translate(matrix, x, y);
matrix = m3.rotate(matrix, angle);
matrix = m3.scale(matrix, sx, sy);
matrix = m3.translate(matrix, centerOffsetX, centerOffsetY);

That series also follows up with both matrix stacks basically reproducing the canvas 2d matrix system and also scene graphs which most 3D engines use.该系列还跟进了两个矩阵堆栈,基本上再现了画布 2d 矩阵系统以及大多数 3D 引擎使用的场景图 Structured drawing systems like SVG and apps like Illustrator also use scene graphs. SVG 等结构化绘图系统和 Illustrator 等应用程序也使用场景图。 Both matrix stacks and scene graphs also make it easy to change a rotation pivot point.矩阵堆栈和场景图也可以轻松更改旋转轴心点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM