这是我想要的结果:
但是我得到了这个:
当我使用这个公式时:
更确切地说,是这段 GLSL 顶点着色器代码:
编辑2:问题不在于
![旋转的正方形](https://istack.dev59.com/wochI.gif)
![公式](https://istack.dev59.com/Wc711.webp)
#version 330
layout (location = 0) in vec2 pos;
layout (location = 1) in vec3 clr;
uniform float sinr, cosr;
float x, y;
out vec3 color;
void main() {
color = clr;
x = pos.x * cosr - pos.y * sinr;
y = pos.x * sinr + pos.y * cosr;
gl_Position = vec4(x, y, 0.0, 1.0); }
我已经没有更多的想法了。我已经算出旋转的代码,但显然它并不能正确旋转。有人能告诉我为什么以及如何解决吗?
编辑:设置/更新程序:
void tgle_rot(int i, float rad) {
float *sin = &global.objs[i].sinr, *cos = &global.objs[i].cosr;
*sin = *sin * cosf(rad) + *cos * sinf(rad);
*cos = *cos * cosf(rad) - *sin * sinf(rad); }
void callback(void) {
tgle_rot(0, 0.01); }
static void run_draw(void) {
/* ... */
for(int i = 0; i < global.num_objs; ++i) {
/* ... */
glUniform1f(global.shader.sinr, global.objs[i].sinr);
glUniform1f(global.shader.cosr, global.objs[i].cosr);
/* ... */ }
/* ... */
glfwSwapBuffers(global.window); }
编辑2:问题不在于
tgle_rot
!我已将其更改为void tgle_rot(int i, float rad) {
float *sin = &global.objs[i].sinr, *cos = &global.objs[i].cosr;
static float rotation = 0;
rotation += 0.01;
*sin = sinf(rotation);
*cos = cosf(rotation); }
程序行为没有改变!
编辑3:以下是单行旋转:
解决方案:问题在于变换的顺序。我先进行缩放,再进行旋转。交换缩放和旋转的顺序解决了问题。我之前没有添加缩放部分,因为我认为它不重要。结果证明我错了。现在我正在弄清楚原因。感谢您的时间,我将根据您的建议修改我的 tgle_rot
函数。
事实上,问题出在着色器上。
void main() {
color = clr;
x = x * cosr - y * sinr;
y = x * sinr + y * cosr; // using modified x!!!
gl_Position = vec4(x, y, 0.0, 1.0);
sinr
和cosr
? - M Oehm