简体   繁体   中英

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. For example when I rotate around x axis it looks like it was turned around y axis.

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? Be careful how you use 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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