简体   繁体   English

图元通过滑块在世界空间中绕轴x,y,z旋转

[英]primitives rotation around the axis x, y, z in World Space by slider

Primitives are centred in World Space. 基本体以世界空间为中心。 I want to rotate them by slider as in image below. 我想通过滑块旋转它们,如下图所示。

程序界面

So far I'm using this code: 到目前为止,我正在使用以下代码:

This is rot object definition: 这是腐烂的对象定义:

struct rotation
{
    float x = 180;
    float y = 180;
    float z = 180;
}

This stuff is executed when user changes slider's position 当用户更改滑块的位置时执行此操作

void MyFrame::OnSliderChanged(wxScrollEvent &event)
{
    //cur_sel is currently selected primitive and scene
    switch(event.GetId())
    {
    case ID_SLIDER0: //slider X
        GLCanvas->rot[GLCanvas->cur_sel].x = event.GetPosition();
        break;
    case ID_SLIDER1: //slider Y
        GLCanvas->rot[GLCanvas->cur_sel].y = event.GetPosition();
        break;
    case ID_SLIDER2: //slider Z
        GLCanvas->rot[GLCanvas->cur_sel].z = event.GetPosition();
        break;
    }
}

This stuff is executed all time (on frames change): 这些东西一直执行(随着帧数的变化):

glRotatef(rot[cur_sel].x,rot[cur_sel].x, 0.0f,  0.0f);
glRotatef(rot[cur_sel].y,0.0f,  rot[cur_sel].y, 0.0f);
glRotatef(rot[cur_sel].z,0.0f,  0.0f, rot[cur_sel].z);

I know it isn't good approach because sometimes ( when I mix x,y,z positions) rotations aren't performed properly. 我知道这不是一个好方法,因为有时(当我混合x,y,z位置时)旋转无法正确执行。 For example when I rotate around x axis it looks like it was turned around y axis. 例如,当我绕x轴旋转时,看起来好像是绕y轴旋转。

I'm looking for properly approach. 我正在寻找正确的方法。

Would you like to send your code in a cpp file, and I can have a look for you? 您想将代码发送到cpp文件中吗,我可以帮您找一下吗? Be careful how you use glRotatef. 请注意如何使用glRotatef。 Normally you need to set the angle as a first argument and then decide upon the axis of rotation. 通常,您需要将角度设置为第一个参数,然后确定旋转轴。

So: 所以:

glRotatef(45.f,1,0,0); //x 
glRotatef(20.f,0,1,0); //y 
glRotatef(64.f,0,0,1); //z

You can always change the order of rotation or match it to your own needs. 您始终可以更改轮换顺序或使其符合自己的需求。 So if you want to represent the rotation around the Y-axis as X, then you need: 因此,如果要将围绕Y轴的旋转表示为X,则需要:

glRotatef(m_y,1,0,0); //x but named y
glRotatef(m_x,0,1,0); //y but named x
glRotatef(m_z,0,0,1); //z

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

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