OpenGL - 围绕 Y 轴旋转“曲线”

Posted

技术标签:

【中文标题】OpenGL - 围绕 Y 轴旋转“曲线”【英文标题】:OpenGL - Rotate a 'Curve' About the Y-Axis 【发布时间】:2011-10-26 14:38:36 【问题描述】:

根据我在 Math Stackexchange 上的 question:

我正在为我的 3D 图形课程做一个项目。该项目是用 C++ 和 OpenGL / Glut 构建的。基本上,我创建了一个水平矩形窗口,细分为两个正方形。在左侧,我有一个二维坐标平面,它允许用户指向并单击并定义轮廓“曲线”。然后我需要将这条曲线绕 Y 轴环绕 n 次。

那么,谁能指导我如何使用三角函数来计算连续点的 X 和 Z 值?例如,如果用户单击并创建点:

(1, 1, 0)

并且它们的扫描分辨率 (n) 设置为 10,然后我需要围绕 Y 轴每 36 (360/10) 度重绘该点。

我认为三角函数会在这里帮助我是否正确?如果是这样,有人可以告诉我如何计算翻译点在 3D 空间中的位置吗?自从我使用 Trig 已经有一段时间了,我不相信我们曾经离开过 2D 空间。

编辑:我尝试使用:

x'=xcos(theta)-zsin(theta)

y'=y

z'=xsin(theta)+zcos(theta)

,根据我对 AMPerrine 的 answer 的理解,我认为它没有像我希望的那样工作:

// this is in a loop

// setup the new angle
double angle = i>0 ? (360/sweepResolutionMod)*i : 0;

angle = angle * (M_PI/180);

// for each point...
for( int i=0; i<clickedPoints.size(); i++ )

    // initial point, normalized
    GLfloat tempX = (clickedPoints[i].x-250)/250;
    GLfloat tempY = (clickedPoints[i].y-250)/250;
    GLfloat tempZ = 0.0;

    // log the initial point
    cout << "(" << tempX << ", " << tempY << ", 0.0) by " << angle << " radians = ";

    // generate the new point
    GLfloat newX = (tempX * cos(angle)) - (tempZ * sin(angle));
    GLfloat newY = tempY;
    GLfloat newZ = (tempX * sin(angle)) - (tempZ * cos(angle));

    // log the new point
    cout << "(" << newX << ", " << newY << ", " << newZ << ")\n";

    // render the new point
    glVertex3d(newX, newY, newZ);

这不会产生屏幕输出,而是控制台输出:

(0.048, -0.296, 0.0) by 0 radians = (0.048, -0.296, 0)
(0.376, -0.508, 0.0) by 0 radians = (0.376, -0.508, 0)
(0.72, -0.204, 0.0) by 0 radians = (0.72, -0.204, 0)
(0.652, 0.176, 0.0) by 0 radians = (0.652, 0.176, 0)
(0.368, 0.504, 0.0) by 0 radians = (0.368, 0.504, 0)

(0.048, -0.296, 0.0) by 0.628319 radians = (0.0388328, -0.296, 0.0282137)
(0.376, -0.508, 0.0) by 0.628319 radians = (0.30419, -0.508, 0.221007)
(0.72, -0.204, 0.0) by 0.628319 radians = (0.582492, -0.204, 0.423205)
(0.652, 0.176, 0.0) by 0.628319 radians = (0.527479, 0.176, 0.383236)
(0.368, 0.504, 0.0) by 0.628319 radians = (0.297718, 0.504, 0.216305)

(0.048, -0.296, 0.0) by 1.25664 radians = (0.0148328, -0.296, 0.0456507)
(0.376, -0.508, 0.0) by 1.25664 radians = (0.11619, -0.508, 0.357597)
(0.72, -0.204, 0.0) by 1.25664 radians = (0.222492, -0.204, 0.684761)
(0.652, 0.176, 0.0) by 1.25664 radians = (0.201479, 0.176, 0.620089)
(0.368, 0.504, 0.0) by 1.25664 radians = (0.113718, 0.504, 0.349989)

...

(0.048, -0.296, 0.0) by 6.28319 radians = (0.048, -0.296, -1.17566e-17)
(0.376, -0.508, 0.0) by 6.28319 radians = (0.376, -0.508, -9.20934e-17)
(0.72, -0.204, 0.0) by 6.28319 radians = (0.72, -0.204, -1.76349e-16)
(0.652, 0.176, 0.0) by 6.28319 radians = (0.652, 0.176, -1.59694e-16)
(0.368, 0.504, 0.0) by 6.28319 radians = (0.368, 0.504, -9.0134e-17)

我不确定这里到底发生了什么,但我很难弄清楚,所以请不要以为我想获得双重声誉或任何东西,我只是真的卡住了。

编辑 2:这是我的透视子视图的整个显示例程:

void displayPersp(void)

    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();  

    gluLookAt (-2.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0);

    // draw the axis
    glBegin(GL_LINES);
    // x
    glVertex3f(500.0, 0.0, 0.0);
    glVertex3f(-500.0, 0.0, 0.0);
    // y
    glVertex3f(0.0, -500.0, 0.0);
    glVertex3f(0.0, 500.0, 0.0);
    // z
    glVertex3f(0.0, 0.0, -500.0);
    glVertex3f(0.0, 0.0, 500.0);

    glEnd(); 

    cout << endl;

    // loop as many number of times as we are going to draw the points around the Y-Axis
    for( int i=0; i<=sweepResolutionMod; i++ )
    
        cout << endl;

        // setup the new angle
        double angle = i>0 ? (360/sweepResolutionMod)*i : 0;

        angle = angle * (M_PI/180);

        // for each point...
        for( int i=0; i<clickedPoints.size(); i++ )
        
            GLfloat tempX = (clickedPoints[i].x-250)/250;
            GLfloat tempY = (clickedPoints[i].y-250)/250;
            GLfloat tempZ = 0.0;

            cout << "(" << tempX << ", " << tempY << ", 0.0) by " << angle << " degrees = ";

            GLfloat newX = (tempX * cos(angle)) - (tempZ * sin(angle));
            GLfloat newY = tempY;
            GLfloat newZ = (tempX * sin(angle)) - (tempZ * cos(angle));

            cout << "(" << newX << ", " << newY << ", " << newZ << ")\n";

            glVertex3d(newX, newY, newZ);
        

        // the following was my old solution, using OpenGL's rotate(), but that
        // didn't allow me to get back the new point's coordinates.

        /*
        glRotatef(angle, 0.0, 1.0, 0.0);

        // draw a line?
        if( clickedPoints.size() > 1 )
        
            glBegin(GL_LINE_STRIP);

            for(int i=0; i<clickedPoints.size(); i++ )
                 
                glVertex3f((clickedPoints[i].x-250)/250, (clickedPoints[i].y-250)/250, 0.0);     
            

            glEnd();
        

        // everyone gets points
        glBegin(GL_POINTS);

        for(int i=0; i<clickedPoints.size(); i++ )
         
            glVertex3f((clickedPoints[i].x-250)/250, (clickedPoints[i].y-250)/250, 0.0);     
        

        glEnd();
        */
    


    glutSwapBuffers();

编辑 3:这是一个糟糕的插图,说明了我需要做什么。我知道观点似乎不对,但我试图获得的是右侧子视图中的绿色“水平线”(这是使用上面注释掉的 glRotatef() 代码):

最终编辑(供后代使用!):

在与大学老师讨论了一些线性代数之后,这是我最终得到的工作:

void displayPersp(void)

   glClear(GL_COLOR_BUFFER_BIT);

   gluLookAt (-2.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0);

   glMatrixMode (GL_MODELVIEW);
   glLoadIdentity ();  

   // draw the axis
   glBegin(GL_LINES);
     // x
     glVertex3f(500.0, 0.0, 0.0);
     glVertex3f(-500.0, 0.0, 0.0);
     // y
     glVertex3f(0.0, -500.0, 0.0);
     glVertex3f(0.0, 500.0, 0.0);
     // z
     glVertex3f(0.0, 0.0, -500.0);
     glVertex3f(0.0, 0.0, 500.0);

   glEnd(); 

   cout << endl;

   double previousTheta = 0.0;

   for( int i=0; i<=sweepResolutionMod; i++ )
   
     double theta = i>0 ? (360/sweepResolutionMod)*i : 0;

     theta = theta * (M_PI/180);

     if( clickedPoints.size() > 1 )
     
       // the 'vertical' piece
       glBegin(GL_LINE_STRIP);

       for(int i=0; i<clickedPoints.size(); i++ )
            
         // normalize
         GLfloat tempX = (clickedPoints[i].x-250)/250;
         GLfloat tempY = (clickedPoints[i].y-250)/250;
         GLfloat tempZ = 0.0;

         // new points
         GLfloat newX = ( tempX * cos(theta) ) + ( tempZ * sin(theta) );
         GLfloat newY = tempY;
         GLfloat newZ = ( tempZ * cos(theta) ) - ( tempX * sin(theta) );

         glVertex3f(newX, newY, newZ);     
       

       glEnd();

       // the 'horizontal' piece
       if( previousTheta != theta )
       
         glBegin(GL_LINES);

         for(int i=0; i<clickedPoints.size(); i++ )
              
           // normalize
           GLfloat tempX = (clickedPoints[i].x-250)/250;
           GLfloat tempY = (clickedPoints[i].y-250)/250;
           GLfloat tempZ = 0.0;

           // new points
           GLfloat newX = ( tempX * cos(theta) ) + ( tempZ * sin(theta) );
           GLfloat newY = tempY;
           GLfloat newZ = ( tempZ * cos(theta) ) - ( tempX * sin(theta) );

           // previous points
           GLfloat previousX = ( tempX * cos(previousTheta) ) + ( tempZ * sin(previousTheta) );
           GLfloat previousY = tempY;
           GLfloat previousZ = ( tempZ * cos(previousTheta) ) - ( tempX * sin(previousTheta) );

           // horizontal component           
           glVertex3f(newX, newY, newZ);     
           glVertex3f(previousX, previousY, previousZ);     
         

         glEnd();
       
     

     previousTheta = theta;
   

   glutSwapBuffers();

【问题讨论】:

那么surface of revolution? 【参考方案1】:

编辑 2:好的,我看到了您遇到的问题 - 这是我忘记的限制(所以我之前发布的代码完全错误,根本不起作用)。问题是你不能在glBegin/glEnd 对之间调用glRotate——如果你这样做了,它会设置一个错误标志,并且不会再进行绘图了。

这确实意味着您几乎必须自己处理轮换。幸运的是,这比您尝试的要简单一些:

static const double pi = 3.1416;

for (int point=0; point<NUM_POINTS; point++) 
    glBegin(GL_LINE_STRIP);
    for (double theta = 0.0; theta < 2.0 * pi; theta += pi/6.0) 
        double x = cos(theta);
        double z = sin(theta);
        glVertex3d(points[point][0]*x, points[point][1], -1.0-points[point][0]*z);
    
    glEnd();
   

按原样,此代码沿 Z 轴使用 -1.0 作为旋转中心。您显然可以将其移动到您希望的位置,但剪裁截头体之外的任何内容显然都不会显示。

还要注意,要获得线框,您必须分别绘制“垂直”线和“水平”线,因此代码如下所示:

for (int point=0; point<NUM_POINTS; point++) 
    glBegin(GL_LINE_STRIP);
    for (double theta = 0.0; theta < 2.0 * pi; theta += pi/6.0) 
        double x = cos(theta);
        double z = sin(theta);
        glVertex3d(points[point][0]*x, points[point][1], -1.0 - points[point][0]*z);
    
    glEnd();


for (double theta = 0.0; theta < 2.0 * pi; theta += pi/6.0) 
    glBegin(GL_LINE_STRIP);
    for (int point=0; point<NUM_POINTS; point++) 
        double x = cos(theta);
        double z = sin(theta);
        glVertex3d(points[point][0]*x, points[point][1], -1.0 - points[point][0]*z);
    
    glEnd();

【讨论】:

这就是我最初拥有它的方式,它工作得很好。但是,这只会给我绕 Y 轴旋转的线。我需要围绕 Y 轴绘制点,并使用 LINES 或 QUADS 或其他方式绘制连接它们的水平组件。这意味着我需要知道新翻译点的实际坐标。 我已经编辑了我的原始帖子以包含我的原始工作代码。 @Josh:好的,那么使用 GL_LINE_STRIP 或 GL_LINE_LOOP 有什么问题?如果需要,您可以取回您的点的屏幕坐标,但目前尚不清楚 GL_LINE_STRIP(或 GL_LINE_LOOP)不会给您什么。 GL_LINE_STRIP 将给我从“底部”到“顶部”的线绘制,相对于 Y 轴,但它不会连接点“水平”,我需要这样做来创建一个网格。 @Josh:对不起,但我不太清楚你在这里所说的“'底部'到'顶部'”与“水平”是什么意思。【参考方案2】:

三角函数以弧度为单位,而不是度数。

我还怀疑您的视口设置不正确,这解释了为什么您在屏幕上看不到任何内容。通常,当我认为东西没有渲染时,通常是因为我没有正确配置相机、照明和其他东西。

【讨论】:

这应该是评论:) @Gabe:好吧,我将角度声明更改为 angle = angle * (M_PI/180);,但现在我记录的 newZ 值已超出 -0.xeX 范围......而且我相信我的视口设置正确,因为如果我只是在静态 Z = 0.0 中渲染点,并使用 glRotatef() 将它们围绕 Y 轴移动,它会正确渲染。我必须计算点,所以我可以水平连接这些点来创建一个网格。 @Sanjay:我也对此持怀疑态度,但我认为进行这一更改可以解决问题(假设没有其他设置不正确),所以我认为提交更合适它作为一个答案。【参考方案3】:

看起来你正在尝试构造一个surface of revolution/solid of revolution/"lathe object"。

一个工作示例:

#include <GL/glut.h>
#include <glm/glm.hpp>
#include <vector>
#include <cmath>

using namespace std;
using namespace glm;

struct Vertex

    Vertex( const vec3& position, const vec3& normal )
        : position( position )
        , normal( normal ) 
    
    vec3 position;
    vec3 normal;
;

// spin the pts array around the Z axis.
// pts.x will become the radius, and pts.y will become the height
// pts should be sorted by y-coordinate
vector< Vertex > Lathe( const vector< vec2 >& pts, unsigned int segments = 32 )

    // precalculate circle points
    vector< vec2 > circlePts;
    for( unsigned int i = 0; i <= segments; ++i )
    
        float angle = ( i / (float)segments ) * 3.14159f * 2.0f;
        circlePts.push_back( vec2( cos( angle ), sin( angle ) ) );
    

    // fill each layer
    typedef vector< vec3 > Layer;
    typedef vector< Layer > Layers;
    Layers layers( pts.size(), Layer( circlePts.size() ) );
    for( size_t i = 0; i < pts.size(); ++i )
    
        for( unsigned int j = 0; j < circlePts.size(); ++j )
        
            layers[i][j] = vec3( circlePts[j] * pts[i].x, pts[i].y );
        
    

    // move through layers generating triangles
    vector< Vertex > verts;
    for( size_t i = 1; i < layers.size(); ++i )
    
        const Layer& prvLayer = layers[ i-1 ];
        const Layer& curLayer = layers[ i-0 ];
        for( size_t j = 1; j < circlePts.size(); ++j )
        
            //    upper = cur layer
            //        UL -- UR  
            // left   | 0 /  |  right 
            // = j-1  |  / 1 |  = j-0
            //        LL -- LR  
            //    lower = prv layer
            const vec3& LL = prvLayer[ j-1 ]; // lower-left
            const vec3& LR = prvLayer[ j-0 ]; // lower-right
            const vec3& UL = curLayer[ j-1 ]; // upper-left
            const vec3& UR = curLayer[ j-0 ]; // upper-right

            // triangle0: LL -> UR -> UL
            const vec3 normal0 = normalize( cross( UR - LL, UL - LL ) );
            verts.push_back( Vertex( LL, normal0 ) );
            verts.push_back( Vertex( UR, normal0 ) );
            verts.push_back( Vertex( UL, normal0 ) );

            // triangle1: LL -> LR -> UR
            const vec3 normal1 = normalize( cross( LR - LL, UL - LL ) );
            verts.push_back( Vertex( LL, normal1 ) );
            verts.push_back( Vertex( LR, normal1 ) );
            verts.push_back( Vertex( UR, normal1 ) );
        
    

    return verts;


// mouse state
int btn;
ivec2 startMouse;
ivec2 startRot, curRot;

void mouse(int button, int state, int x, int y )

    if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
    
        btn = button;
        startMouse = ivec2( x, glutGet( GLUT_WINDOW_HEIGHT ) - y );
        startRot = curRot;
    


void motion( int x, int y )

    ivec2 curMouse( x, glutGet( GLUT_WINDOW_HEIGHT ) - y );
    if( btn == GLUT_LEFT_BUTTON )
    
        curRot = startRot + ( curMouse - startMouse );
    
    glutPostRedisplay();


vector< Vertex > model;
void display()

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    double ar = w / h;
    gluPerspective( 60, ar, 0.1, 40 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glTranslatef( 0, 0, -10 );

    glPushMatrix();
    glRotatef( curRot.x % 360, 0, 1, 0 );
    glRotatef( -curRot.y % 360, 1, 0, 0 );

    // draw model
    if( !model.empty() )
    
        glColor3ub( 255, 0, 0 );
        glEnableClientState( GL_VERTEX_ARRAY );
        glEnableClientState( GL_NORMAL_ARRAY );
        glVertexPointer( 3, GL_FLOAT, sizeof(Vertex), &model[0].position );
        glNormalPointer( GL_FLOAT, sizeof(Vertex), &model[0].normal );
        glDrawArrays( GL_TRIANGLES, 0, model.size() );
        glDisableClientState( GL_VERTEX_ARRAY );
        glDisableClientState( GL_NORMAL_ARRAY );
    

    // draw bounding cube
    glDisable( GL_LIGHTING );
    glColor3ub( 255, 255, 255 );
    glutWireCube( 7 );
    glEnable( GL_LIGHTING );

    glPopMatrix();

    glutSwapBuffers();


int main( int argc, char **argv )

    vector< vec2 > pts;
    pts.push_back( vec2( 0.1, -3 ) );
    pts.push_back( vec2( 2, -2 ) );
    pts.push_back( vec2( 3, -1 ) );
    pts.push_back( vec2( 1, 0 ) );
    pts.push_back( vec2( 3, 1 ) );
    pts.push_back( vec2( 4, 2 ) );
    pts.push_back( vec2( 4, 3 ) );
    model = Lathe( pts );

    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );
    glutCreateWindow( "GLUT" );
    glutDisplayFunc( display );
    glutMouseFunc( mouse );
    glutMotionFunc( motion );

    glEnable( GL_DEPTH_TEST );

    // set up lighting
    glShadeModel( GL_SMOOTH );
    glEnable( GL_COLOR_MATERIAL );
    glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
    glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );
    glEnable( GL_LIGHTING );

    // set up "headlamp"-like light
    glEnable( GL_LIGHT0 );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    GLfloat position[] =  0, 0, 1, 0 ;
    glLightfv( GL_LIGHT0, GL_POSITION, position );

    glPolygonMode( GL_FRONT, GL_FILL );
    glPolygonMode( GL_BACK, GL_LINE );

    glutMainLoop();
    return 0;

【讨论】:

您确定Layers layers( pts.size(), circlePts.size() ); 是工作代码吗?至少在我的系统上没有接受两个数字的向量构造函数。你的意思是Layers layers( pts.size(), Layer&lt;LaycirclePts.size()&gt;());?它出现了here,所以你可能也想回答这个问题。 @BDL:是的,在 VS2015 中不适合我。虽然在 VS2008 或 VS2010 上可能在 2011 年工作过。我会解决的,谢谢提醒!

以上是关于OpenGL - 围绕 Y 轴旋转“曲线”的主要内容,如果未能解决你的问题,请参考以下文章

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

使用OpenGL在C ++中同时围绕其(x,y&z)轴旋转对象

围绕对象旋转的矩阵乘法opengl

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

如何围绕任意轴旋转一个点?

围绕世界轴OpenGL旋转对象