简体   繁体   English

OpenGL:glRotatef机制以及如何围绕正方形旋转而不移动

[英]OpenGl: glRotatef mechanism & how to rotate square around itself without moving

I have problem understanding the mechanism of glrotatef or how does it work, after doing some research I found out that it consists it takes 4 variables, which are the angle that I use to rotate, 1 or 0 for x axis, 1 or 0 for y axis, and 1 or 0 for z axis. 我在了解glrotatef的机理或其工作原理时遇到问题,经过一些研究,我发现它包含4个变量,分别是我旋转的角度,x轴为1或0,x轴为1或0。 y轴,z轴为1或0。

answering of the following questions will help me understanding the glrotatef 回答以下问题将有助于我理解glrotatef

the questions are 问题是

  1. if we're talking about a square that we want to rotate it, the angle that we send to the function glrotatef, will it work on vertex points of the square, or on the lines to rotate them ? 如果我们正在讨论要旋转的正方形,那么我们发送给函数glrotatef的角度将在正方形的顶点或在旋转它们的线上起作用吗?
  2. what is the difference between writing the function glRotatef one time, and writing it four times when using it on rotating a square, does every one glRotatef function work on one side of the square ? 一次编写函数glRotatef和在旋转一个正方形时使用它写入四次有什么区别,每一个glRotatef函数都可以在正方形的一侧工作吗?
  3. how can I use glrotatef & gltranslatef to rotate a square around itself without moving from it's place. 如何使用glrotatef和gltranslatef围绕自身旋转正方形而不移动它的位置。
  4. can anyone explain in the simplest way how does glrotatef work, and give some examples without relating to any part of codes because I'm a newbie and I didn't understand most of the codes written on stackoverflow questions about glrotatef. 谁能以最简单的方式解释glrotatef的工作原理,并给出一些示例而不涉及代码的任何部分,因为我是新手,而且我不了解有关glrotatef的stackoverflow问题上编写的大多数代码。

Before reading the answers of question, I suggest you to read how translate/rotate/scale is done with model matrix 在阅读问题的答案之前,我建议您阅读如何使用模型矩阵进行平移/旋转/缩放

When you need to transform your object, you simply perform operation for each vertex that your object has. 当需要变换对象时,只需对对象具有的每个顶点执行操作。

P = [x,y,z,1] -- your point in 3D P = [x,y,z,1]-您在3D中的点
M = [..] -- your 4x4 rotation matrix M = [..]-您的4x4 旋转矩阵

M * P -- your rotation operation M * P-您的旋转操作

In the light of references I provide, here are the very simplified answers : 根据我提供的参考,这是非常简单的答案:

  1. When you do rotate, your each vertex coordinate position will be recalculated and Opengl will generate your square again from the new 4 vertex. 旋转时,将重新计算每个顶点坐标位置,Opengl将从新的4个顶点再次生成您的正方形。
  2. There is no difference between rotating 1 times 60 degree and 2 times 30 degree you can simply control it by doing the math (check the rotation matrix link): 旋转1度60度和旋转2度30度之间没有区别,您可以通过数学运算简单地控制它(检查旋转矩阵链接):

M1 = your 60 degree rotation matrix M1 =您的60度旋转矩阵

M2 = your 30 degree rotation matrix M2 =您的30度旋转矩阵

M1 = M2 * M2 => M1 * P = M2 * M2 * P M1 = M2 * M2 => M1 * P = M2 * M2 * P

  1. Here there is my answer : Rotating around a point different from origin 这是我的答案: 绕与原点不同的点旋转
  2. Rotatef() function is just creates a 4x4 rotation matrix according to your parameters you provided and it updates your ModelView matrix by multiplying with your rotation generated. Rotatef()函数仅根据您提供的参数创建一个4x4旋转矩阵,并通过乘以生成的旋转来更新ModelView矩阵。

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

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