使用OpenGL如何实现物体绕体外任意轴旋转?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用OpenGL如何实现物体绕体外任意轴旋转?相关的知识,希望对你有一定的参考价值。

我用glRotatef(m_fAngleX,1.0f,0.0f,0.0f);然后让m_fAngleX循环加10,可以让立方体绕中心轴旋转;如果我想让立方体绕体外的任意轴旋转,我该怎么办呢?

可以这样
假设你要绕轴AB旋转
1、计算把轴AB一个点平衡原点(假如我这里是B点移动到原点)所需移动量为向量xyz,把立方体也移动向量xyz
2、把立方休按A点位置旋转(因为B点已经在原点了),即把A点坐标填入glRotate后面三个参数就是
3、把物体移动回向量xyz即可
参考技术A glQuaternion m_qHeading; //四元数
float m_HeadingDegrees; //转动角度
Vector3 eye, center; //摄像机参数
Vector3 m_Position; //轴位置
Matrix matrix; //矩阵

m_qHeading.CreateFromAxisAngle(0.0f, 1.0f, 0.0f, m_HeadingDegrees); //将转动角变成四元数
m_qHeading.CreateMatrix(matrix); //将四元数矩阵化

eye *= Matrix;
center *= Matrix;

gluLookAt(eye.x + m_Position.x,eye.y + m_Position.y,eye.z + m_Position.z, center.x + m_Position.x,center.y + m_Position.y,center.z + m_Position.z,0,1,0);

其中四元数可以直接百度“glQuaternion" 可以找到矩阵化的算法,向量和矩阵相乘你也可以找到一些资料的。本回答被提问者采纳

在 OpenGL 中绘制一个绕 Y 轴旋转的 Cog

【中文标题】在 OpenGL 中绘制一个绕 Y 轴旋转的 Cog【英文标题】:Drawing a Cog which rotates around the Y axis in OpenGL 【发布时间】:2019-09-13 06:23:38 【问题描述】:

如标题中所述:我想创建一个 3D 齿轮,它有 10 个围绕其中心旋转的齿(就像齿轮一样)。齿轮有方形齿,为了简单起见,齿之间有平坦的侧面 - 这个齿轮上没有曲线。

齿轮一侧应该是什么样子的可视化。请注意,角度并非 100% 完美。

根据上图,每个齿轮齿都必须是 8 边多边形,而齿之间的每条边都必须是 4 边多边形。然而,现在,牙齿绘制做了以下事情:

    尽管未应用初始旋转,但应面向摄像机的侧齿旋转到地板中。 虽然缩放和顶点创建是使用统一的数字完成的,但牙齿的宽度是高度的两倍(它在 x、y、z 方向上按s 缩放,并使用00.5 作为顶点坐标)。

完全可重现的示例:

import java.awt.Dimension;

import javax.swing.JFrame;

import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.awt.GLJPanel;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.FPSAnimator;

public class SpinCog3D implements GLEventListener 

    JFrame jf;
    GLJPanel gljpanel;
    Dimension dim = new Dimension(800, 600);
    FPSAnimator animator;

    float rotation;
    float speed;

    // set up the OpenGL Panel within a JFrame
    public SpinCog3D() 
        jf = new JFrame();
        gljpanel = new GLJPanel();
        gljpanel.addGLEventListener(this);
        gljpanel.requestFocusInWindow();
        jf.getContentPane().add(gljpanel);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        jf.setPreferredSize(dim);
        jf.pack();
        animator = new FPSAnimator(gljpanel, 20);
        rotation = 0.0f;
        speed = 0.1f;
        animator.start();
    

    public static void main(String[] args) 
        new SpinCog3D();
    

    public void init(GLAutoDrawable dr) 
        GL2 gl2 = dr.getGL().getGL2();
        GLU glu = new GLU();
        gl2.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
        gl2.glEnable(GL2.GL_DEPTH_TEST);

        gl2.glMatrixMode(GL2.GL_PROJECTION);
        gl2.glLoadIdentity();
        glu.gluPerspective(60.0, 1.0, 100.0, 800.0);

    

    public void display(GLAutoDrawable dr) 

        GL2 gl2 = dr.getGL().getGL2();
        GLU glu = new GLU();

        gl2.glMatrixMode(GL2.GL_MODELVIEW);
        gl2.glLoadIdentity();
        glu.gluLookAt(0.0, 200.0, 500.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

        gl2.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        // Draw 1 Cog Tooth + 1 Side
        drawTooth(gl2, 100.0, 1.0f, 0.0f, 0.0f, 0.0, 0.0, 1.0, 0.0, 0);
        drawSide(gl2, 100.0, 0.0f, 1.0f, 0.0f, 0.0, 0.0, 1.0, 0.0, 10);

        // Draw Floor
        sideRotatedColorScaledFloor(gl2, 300.0, 0.0f, 0.0f, 0.0f, 90.0, 1.0, 0.0, 0.0, 0.0);

        gl2.glFlush();
        rotation += speed;
        if (rotation > 360.9f)
            rotation = 0.0f;
    

    // draw a single side with a set color and orientation
    private void drawTooth(GL2 gl2, double s, float r, float g, float b, double a, double ax, double ay,
                                        double az, double zoffset) 
        gl2.glPushMatrix();
        gl2.glRotated(a, ax, ay, az);
        gl2.glColor3f(r, g, b);
        gl2.glTranslated(0.0, 0.0, zoffset);
        gl2.glScaled(s, s, s);

        tooth(gl2);
        gl2.glPopMatrix();
    

    private void drawSide(GL2 gl2, double s, float r, float g, float b, double a, double ax, double ay,
                           double az, double zoffset) 
        gl2.glPushMatrix();
        gl2.glRotated(a, ax, ay, az);
        gl2.glColor3f(r, g, b);
        gl2.glTranslated(0.0, 0.0, zoffset);
        gl2.glScaled(s, s, s);

        side(gl2);
        gl2.glPopMatrix();
    

    private void sideRotatedColorScaledFloor(GL2 gl2, double s, float r, float g, float b, double a, double ax, double ay,
                                        double az, double zoffset) 
        gl2.glPushMatrix();
        gl2.glRotated(a, ax, ay, az);
        gl2.glColor3f(r, g, b);
        gl2.glTranslated(0.0, 0.0, zoffset);
        gl2.glScaled(s, s, s);

        side(gl2);
        gl2.glPopMatrix();
    

    private void tooth(GL2 gl2) 
        gl2.glBegin(GL2.GL_POLYGON);
        gl2.glVertex3d(-0.5, -0.5, 0.0);
        gl2.glVertex3d(-0.5, 0.5, 0.0);
        gl2.glVertex3d(0.5, 0.5, 0.0);
        gl2.glVertex3d(0.5, -0.5, 0.0);
        gl2.glVertex3d(-0.5, -0.5, 0.5);
        gl2.glVertex3d(-0.5, 0.5, 0.5);
        gl2.glVertex3d(0.5, 0.5, 0.5);
        gl2.glVertex3d(0.5, -0.5, 0.5);
        gl2.glEnd();
    

    private void side(GL2 gl2) 
        gl2.glBegin(GL2.GL_POLYGON);
        gl2.glVertex3d(-0.5, -0.5, 0.0);
        gl2.glVertex3d(-0.5, 0.5, 0.0);
        gl2.glVertex3d(0.5, 0.5, 0.0);
        gl2.glVertex3d(0.5, -0.5, 0.0);
        gl2.glEnd();
    

是的,我知道 glBegin()glEnd() 已被永远弃用,但这不是重点。如何让我的牙齿正确绘制并使其与两侧正确对齐?

【问题讨论】:

您能否添加描述,或者更好的是,添加“奇怪事物”的图像?我认为这可能会使您的问题变得更好,这样我们就不必自己运行您的代码来查看问题可能是什么。 我在不使用矩阵的情况下添加了答案...您的错误很可能在哪里... 【参考方案1】:

牙齿画会做各种奇怪的事情......

不是一个好的问题描述。我敢打赌,您的网格有很多间隙和重叠,因为您使用的是硬编码形状,而没有对 Cog 的实际尺寸进行任何校正。就像牙齿必须有多大才能适合n 才能完全覆盖整个圆圈......

改用参数圆方程怎么样?

这将摆脱矩阵混乱,我认为会更简单。

所以我会简单地将 cog 分成三角形切片,每个切片都有几个边缘点。因此,将它们与QUADs 连接起来,然后将其放入for 循环中进行所有切片。

我不会在 JAVA 中编写代码,但这里使用旧的 GL api 并且没有花哨的 C++ 东西的小 C++ 示例,因此您应该能够轻松地移植它:

void glCog(float r0,float r1,float r2,float w,int n)    // shaft/inner/outer radiuses, width, tooths
    
    int i;
    float a,da,x,y,c,s;
    float p[6][3],q[6][3];  // slice points
    // set z for slice points
    a=-0.5*w; for (i=0;i<3;i++) p[i][2]=a; q[i][2]=a; 
    a=+0.5*w; for (i=3;i<6;i++) p[i][2]=a; q[i][2]=a; 
    // init first slice
    q[0][0]= r0; q[5][0]= r0;
    q[0][1]=0.0; q[5][1]=0.0;
    q[1][0]= r1; q[4][0]= r1;
    q[1][1]=0.0; q[4][1]=0.0;
    q[2][0]= r2; q[3][0]= r2;
    q[2][1]=0.0; q[3][1]=0.0;
    // divide circle to 2*n slices
    da=2.0*M_PI/float(4*n);
    glBegin(GL_QUADS);
    for (a=0.0,i=0;i<=n;i++)
        
        // points on circles at angle a
        c=cos(a); s=sin(a); a+=da;
        x=r0*c; y=r0*s; p[0][0]=x; p[5][0]=x;
                        p[0][1]=y; p[5][1]=y;
        x=r1*c; y=r1*s; p[1][0]=x; p[4][0]=x;
                        p[1][1]=y; p[4][1]=y;
        x=r2*c; y=r2*s; p[2][0]=x; p[3][0]=x;
                        p[2][1]=y; p[3][1]=y;
        // render tooth
        c=cos(a); s=sin(a); a+=da;
        glNormal3f(0.0,0.0,-1.0);   // -Z base
        glVertex3fv(p[0]);
        glVertex3fv(p[2]);
        glVertex3fv(q[2]);
        glVertex3fv(q[0]);
        glNormal3f(0.0,0.0,+1.0);   // +Z base
        glVertex3fv(p[3]);
        glVertex3fv(p[5]);
        glVertex3fv(q[5]);
        glVertex3fv(q[3]);
        glNormal3f(-c,-s,0.0);      // shaft circumference side
        glVertex3fv(p[5]);
        glVertex3fv(p[0]);
        glVertex3fv(q[0]);
        glVertex3fv(q[5]);
        glNormal3f(c,s,0.0);        // outter circumference side
        glVertex3fv(p[2]);
        glVertex3fv(p[3]);
        glVertex3fv(q[3]);
        glVertex3fv(q[2]);
        glNormal3f(-s,c,0.0);
        glVertex3fv(p[4]);
        glVertex3fv(p[3]);
        glVertex3fv(p[2]);
        glVertex3fv(p[1]);
        glNormal3f(s,-c,0.0);
        glVertex3fv(q[1]);
        glVertex3fv(q[2]);
        glVertex3fv(q[3]);
        glVertex3fv(q[4]);

        // points on circles at angle a
        c=cos(a); s=sin(a); a+=da;
        x=r0*c; y=r0*s; q[0][0]=x; q[5][0]=x;
                        q[0][1]=y; q[5][1]=y;
        x=r1*c; y=r1*s; q[1][0]=x; q[4][0]=x;
                        q[1][1]=y; q[4][1]=y;
        x=r2*c; y=r2*s; q[2][0]=x; q[3][0]=x;
                        q[2][1]=y; q[3][1]=y;
        // render gap
        c=cos(a); s=sin(a); a+=da;
        glNormal3f(0.0,0.0,-1.0);   // -Z base
        glVertex3fv(q[0]);
        glVertex3fv(q[1]);
        glVertex3fv(p[1]);
        glVertex3fv(p[0]);
        glNormal3f(0.0,0.0,+1.0);   // +Z base
        glVertex3fv(q[4]);
        glVertex3fv(q[5]);
        glVertex3fv(p[5]);
        glVertex3fv(p[4]);
        glNormal3f(-c,-s,0.0);      // shaft circumference side
        glVertex3fv(q[5]);
        glVertex3fv(q[0]);
        glVertex3fv(p[0]);
        glVertex3fv(p[5]);
        glNormal3f(c,s,0.0);        // outter circumference side
        glVertex3fv(q[1]);
        glVertex3fv(q[4]);
        glVertex3fv(p[4]);
        glVertex3fv(p[1]);
        
    glEnd();
    

这里是glCog(0.1,0.5,0.6,0.1,10);的预览:

这里是glCog(0.2,0.5,0.52,0.2,50);的预览:

但请注意,牙齿并非完全是矩形的。齿数越少,误差越大。如果您想获得完全矩形的牙齿形状,则需要平移最外面的点而不是旋转(或校正它们的计算角度)

使用翻译来解决这个问题:

void glCog(float r0,float r1,float r2,float w,int n)    // shaft/inner/outer radiuses, width, tooths
    
    int i,j;
    float a,da,dr,x,y,c,s;
    float p[6][3],q[6][3];  // slice points
    // divide circle to 2*n slices
    da=2.0*M_PI/float(4*n);
    dr=r2-r1;
    // set z for slice points
    a=-0.5*w; for (i=0;i<3;i++) p[i][2]=a; q[i][2]=a; 
    a=+0.5*w; for (i=3;i<6;i++) p[i][2]=a; q[i][2]=a; 
    // init first slice
    q[0][0]= r0; q[5][0]= r0;
    q[0][1]=0.0; q[5][1]=0.0;
    q[1][0]= r1; q[4][0]= r1;
    q[1][1]=0.0; q[4][1]=0.0;
    x=r1+dr*cos(-da); y=dr*sin(-da);
    q[2][0]=  x; q[3][0]=  x;
    q[2][1]=  y; q[3][1]=  y;
    glBegin(GL_QUADS);
    for (a=0.0,i=0;i<=n;i++)
        
        // points on circles at angle a
        c=cos(a); s=sin(a);
        x=r0*c; y=r0*s; p[0][0]=x; p[5][0]=x;
                        p[0][1]=y; p[5][1]=y;
        x=r1*c; y=r1*s; p[1][0]=x; p[4][0]=x;
                        p[1][1]=y; p[4][1]=y;
        c=cos(a-da); s=sin(a-da); a+=da;
        x+=dr*c;y+=dr*s;p[2][0]=x; p[3][0]=x;
                        p[2][1]=y; p[3][1]=y;
        c=cos(a); s=sin(a); a+=da;
        // render tooth
        glNormal3f(0.0,0.0,-1.0);   // -Z base
        glVertex3fv(p[0]);
        glVertex3fv(p[1]);
        glVertex3fv(q[1]);
        glVertex3fv(q[0]);
        glVertex3fv(p[1]);
        glVertex3fv(p[2]);
        glVertex3fv(q[2]);
        glVertex3fv(q[1]);
        glNormal3f(0.0,0.0,+1.0);   // +Z base
        glVertex3fv(p[3]);
        glVertex3fv(p[4]);
        glVertex3fv(q[4]);
        glVertex3fv(q[3]);
        glVertex3fv(p[4]);
        glVertex3fv(p[5]);
        glVertex3fv(q[5]);
        glVertex3fv(q[4]);
        glNormal3f(-c,-s,0.0);      // shaft circumference side
        glVertex3fv(p[5]);
        glVertex3fv(p[0]);
        glVertex3fv(q[0]);
        glVertex3fv(q[5]);
        glNormal3f(c,s,0.0);        // outter circumference side
        glVertex3fv(p[2]);
        glVertex3fv(p[3]);
        glVertex3fv(q[3]);
        glVertex3fv(q[2]);
        glNormal3f(-s,c,0.0);
        glVertex3fv(p[4]);
        glVertex3fv(p[3]);
        glVertex3fv(p[2]);
        glVertex3fv(p[1]);
        glNormal3f(s,-c,0.0);
        glVertex3fv(q[1]);
        glVertex3fv(q[2]);
        glVertex3fv(q[3]);
        glVertex3fv(q[4]);

        // points on circles at angle a
        c=cos(a); s=sin(a);;
        x=r0*c; y=r0*s; q[0][0]=x; q[5][0]=x;
                        q[0][1]=y; q[5][1]=y;
        x=r1*c; y=r1*s; q[1][0]=x; q[4][0]=x;
                        q[1][1]=y; q[4][1]=y;
        c=cos(a+da); s=sin(a+da); a+=da;
        x+=dr*c;y+=dr*s;q[2][0]=x; q[3][0]=x;
                        q[2][1]=y; q[3][1]=y;
        c=cos(a); s=sin(a); a+=da;
        // render gap
        glNormal3f(0.0,0.0,-1.0);   // -Z base
        glVertex3fv(q[0]);
        glVertex3fv(q[1]);
        glVertex3fv(p[1]);
        glVertex3fv(p[0]);
        glNormal3f(0.0,0.0,+1.0);   // +Z base
        glVertex3fv(q[4]);
        glVertex3fv(q[5]);
        glVertex3fv(p[5]);
        glVertex3fv(p[4]);
        glNormal3f(-c,-s,0.0);      // shaft circumference side
        glVertex3fv(q[5]);
        glVertex3fv(q[0]);
        glVertex3fv(p[0]);
        glVertex3fv(p[5]);
        glNormal3f(c,s,0.0);        // outter circumference side
        glVertex3fv(q[1]);
        glVertex3fv(q[4]);
        glVertex3fv(p[4]);
        glVertex3fv(p[1]);
        
    glEnd();
    

所以只是点 p[2],q[2],p[3],q[3] 略有变化,基面必须用更多的 QUADS 来补偿...

这里是glCog(0.2,0.5,0.6,0.2,10);的预览:

【讨论】:

很好的插图(+1) @Rabbid76 thx ...它是由我自己的 GIF 捕获和编码器捕获的...但是有时在捕获 GL 渲染白框时最后会出现不知道为什么(我开发时没有这样做这可能是多年来 gfx 驱动程序的一些变化) 如果可以的话,我会给你+2这个答案。谢谢先生。

以上是关于使用OpenGL如何实现物体绕体外任意轴旋转?的主要内容,如果未能解决你的问题,请参考以下文章

unity 如何让物体绕自己的中心轴转

将围绕任意轴的旋转解析为围绕 X、Y 和 Z 轴的旋转

Unity3D 控制物体旋转详解 —— 自身绕轴旋转缓慢旋转鼠标控制旋转欧拉数和四元数的关系

绕任意轴旋转的矩阵推导总结

在 OpenGL 场景中绕物体旋转的困难

OpenGL学习——变换——绕x轴来回旋转的木板