c++ - OpenGL/GLUT 试图使水平杆以不同方式波动

标签 c++ c opengl glut

我被要求做一个非常简单的 Carousel .我做了一个中心杆,我有 4 个水平的摇杆(-45 到 +45 度)。当所有 4 个水平杆同时以相同角度波动时,一切都很好,但我想让这些水平杆在给定时间处于不同角度,一切都表现得很奇怪,那些水平棍子上下跳跃而不是平滑波动

我想知道解决这个问题的诀窍是什么?

这是我尝试做的(在 DrawHorizo​​ntalStick() 函数中),但效果不佳:

if (id % 4 == 0)
    glVertex3f(3.0 * cos(radian_angle_in_range), 7.0 + 3.0 *
        sin(radian_angle_in_range), 0.0);

else if (id % 4 == 1)
    glVertex3f(3.0 * cos(radian_angle_in_range + (PI / 12)), 7.0 + 3.0 * 
        sin(radian_angle_in_range + (PI / 12)), 0.0);

else if (id % 4 == 2)
    glVertex3f(3.0 * cos(radian_angle_in_range + (2 * PI / 12)), 7.0 + 3.0 * 
        sin(radian_angle_in_range + (2 * PI / 12)), 0.0);

else if (id % 4 == 3)
    glVertex3f(3.0 * cos(radian_angle_in_range + (3 * PI / 12)), 7.0 + 3.0 * 
         sin(radian_angle_in_range + (3 * PI / 12)), 0.0);

本质上增加了

  • 第一根棍子的角度0度
  • 与第二根棍子的角度成 15 度
  • 与第三根棍子的角度成 30 度
  • 与第四根棍子的角度成 45 度

完整的源代码:

#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define PI 3.14159265

static GLfloat lpos[] = { 0.0, 5.0, 4.0, 1.0 };
static GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
static GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
static GLfloat gray[] = { 0.5, 0.5, 0.5, 1.0 };
static GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
static GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
static GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
static GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
static GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
static GLfloat darkcyan[] = { 0.0, 0.4, 0.4, 1.0 };
static GLfloat lightgreen[] = { 0.5, 1.0, 0.5, 1.0 };
static float alpha = 0.0;
static float beta = PI / 6.0;
static float zoom = 25.0;
static bool lightSource = true;

float numberOfTriangles = 1;
static GLdouble cpos[3];
bool showNormalVectors = false;


static double xPointer, yPointer, zPointer;
static double fenceHeight = -0.5;
static int angle = 0;
static double radian_angle = 0.0;
static int inRangeAngle = 0;
static double radian_angle_in_range = 0.0;
static int id = 0;

void writemessage()
{
}

void degreeToRadian(){
    radian_angle = ((float)angle / 180) * PI;
}


void pre_cursor(){
    if ((angle % 360 >= 0 && angle % 360 <= 45) || (angle % 360 >= 135 && angle % 360 <= 225) || (angle % 360 >= 315 && angle % 360 <= 360))
        inRangeAngle = 1.0*angle;
}

void degreeToRadianInRange(){
    radian_angle_in_range = ((float)inRangeAngle / 180) * PI;
}

void doGlTranslatef(){
    glTranslatef(xPointer, yPointer, zPointer);
}
void releaseGlTranslatef(){
    glTranslatef(-xPointer, -yPointer, -zPointer);
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 0.01, 50.0);
    glMatrixMode(GL_MODELVIEW);
}

void DrawSticksArroundYard(){
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
    glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, black);
    GLUquadricObj *quadObj;
    // Right-Line
    xPointer = 4.8;
    yPointer = 1.0 + fenceHeight;
    zPointer = -5.0;
    doGlTranslatef();
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.1, 0.1, 10, 10, 10);
    releaseGlTranslatef();
    // Left-Line
    xPointer = -4.8;
    yPointer = 1.0 + fenceHeight;
    zPointer = -5.0;
    doGlTranslatef();
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.1, 0.1, 10, 10, 10);
    releaseGlTranslatef();
    // Back-Line
    xPointer = -4.8;
    yPointer = 1.0 + fenceHeight;
    zPointer = -5.0;
    doGlTranslatef();
    glRotatef(90, 0, 1, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.1, 0.1, 9.6, 10, 10);
    glRotatef(-90, 0, 1, 0);
    releaseGlTranslatef();
    // Front-Line
    xPointer = 4.8;
    yPointer = 1.0 + fenceHeight;
    zPointer = 5.0;
    doGlTranslatef();
    glRotatef(-90, 0, 1, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.1, 0.1, 9.6, 10, 10);
    glRotatef(90, 0, 1, 0);
    releaseGlTranslatef();
    // Pin-Front-Right
    xPointer = 4.8;
    yPointer = 0.0;
    zPointer = 5.0;
    doGlTranslatef();
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    releaseGlTranslatef();
    // Pin-Front-Left
    xPointer = -4.8;
    yPointer = 0.0;
    zPointer = 5.0;
    doGlTranslatef();
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    releaseGlTranslatef();
    // Pin-Back-Left
    xPointer = -4.8;
    yPointer = 0.0;
    zPointer = -5.0;
    doGlTranslatef();
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    releaseGlTranslatef();
    // Pin-Back-Right
    xPointer = 4.8;
    yPointer = 0.0;
    zPointer = -5.0;
    doGlTranslatef();
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    releaseGlTranslatef();
    // Pin-Back-Center
    xPointer = 0.0;
    yPointer = 0.0;
    zPointer = -5.0;
    doGlTranslatef();
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    releaseGlTranslatef();
    // Pin-Front-Center
    xPointer = 0.0;
    yPointer = 0.0;
    zPointer = 5.0;
    doGlTranslatef();
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    releaseGlTranslatef();
    // Pin-Right-Center
    xPointer = 4.8;
    yPointer = 0.0;
    zPointer = 0.0;
    doGlTranslatef();
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    releaseGlTranslatef();
    // Pin-Left-Center
    xPointer = -4.8;
    yPointer = 0.0;
    zPointer = 0.0;
    doGlTranslatef();
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    releaseGlTranslatef();
}

void DrawYardFloor(){
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, lightgreen);
    glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, lightgreen);
    glBegin(GL_POLYGON);
    glNormal3f(0, 1, 0);
    glVertex3f(-5.3, -0.005, -5.3);
    glVertex3f(-5.3, -0.005, 5.3);
    glVertex3f(5.3, -0.005, 5.3);
    glVertex3f(5.3, -0.005, -5.3);
    glEnd();
}

void DrawCenterPin(){
    xPointer = 0.0;
    yPointer = 0.0;
    zPointer = 0.0;
    doGlTranslatef();
    glRotatef(-90, 1, 0, 0);
    GLUquadricObj *quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.2, 7, 10, 10);
    glRotatef(90, 1, 0, 0);
    releaseGlTranslatef();
}



void DrawHorizontalStick(){
    glLineWidth(10);
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
    glVertex3f(0.0, 7.0, 0.0);
    glVertex3f(3.0 * cos(radian_angle_in_range), 7.0 + 3.0 * sin(radian_angle_in_range), 0.0);

    //if (id % 4 == 0)
    //  glVertex3f(3.0 * cos(radian_angle_in_range), 7.0 + 3.0 * sin(radian_angle_in_range), 0.0);
    //else if (id % 4 == 1)
    //  glVertex3f(3.0 * cos(radian_angle_in_range + (PI / 12)), 7.0 + 3.0 * sin(radian_angle_in_range + (PI / 12)), 0.0);
    //else if (id % 4 == 2)
    //  glVertex3f(3.0 * cos(radian_angle_in_range + (2 * PI / 12)), 7.0 + 3.0 * sin(radian_angle_in_range + (2 * PI / 12)), 0.0);
    //else if (id % 4 == 3)
    //  glVertex3f(3.0 * cos(radian_angle_in_range + (3 * PI / 12)), 7.0 + 3.0 * sin(radian_angle_in_range + (3 * PI / 12)), 0.0);

    glEnd();
}

void DrawVerticalStick(){
    glLineWidth(4);
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
    glVertex3f(3.0 * cos(radian_angle_in_range), 7.0 + 3.0 * sin(radian_angle_in_range), 0.0);
    glVertex3f(3.0 * cos(radian_angle_in_range), 7.0 + 3.0 * sin(radian_angle_in_range) - 0.5, 0.0);
    glEnd();
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64);
    cpos[0] = zoom * cos(beta) * sin(alpha);
    cpos[1] = zoom * sin(beta);
    cpos[2] = zoom * cos(beta) * cos(alpha);
    gluLookAt(cpos[0], cpos[1], cpos[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    if (lightSource == true){
        glLightfv(GL_LIGHT0, GL_POSITION, lpos);
        glMaterialfv(GL_FRONT, GL_EMISSION, white);
        glPushMatrix();
        glTranslatef(lpos[0], lpos[1], lpos[2]);
        glutSolidSphere(0.1, 10, 8);
        glPopMatrix();
        glMaterialfv(GL_FRONT, GL_EMISSION, black);
    }

    DrawYardFloor();
    DrawSticksArroundYard();
    DrawCenterPin();

    glRotatef(angle, 0, 1, 0);
    for (int i = 0; i < 4; i++){
        glPushMatrix();
        glRotatef(i * 360 / 4, 0, 1, 0);

        DrawHorizontalStick();
        // DrawVerticalStick();
        // DrawCabin();

        glPopMatrix();
        id++;
    }
    glRotatef(-angle, 0, 1, 0);


    glutSwapBuffers();
    glFlush();
}


void keyboard(unsigned char key, int x, int y)
{
    static int polygonmode[2];

    switch (key) {
    case 27:
        exit(0);
        break;
    case 'x':
        if (lightSource == true)
            lpos[0] = lpos[0] + 0.2;
        glutPostRedisplay();
        break;
    case 'X':
        if (lightSource == true)
            lpos[0] = lpos[0] - 0.2;
        glutPostRedisplay();
        break;
    case 'y':
        if (lightSource == true)
            lpos[1] = lpos[1] + 0.2;
        glutPostRedisplay();
        break;
    case 'Y':
        if (lightSource == true)
            lpos[1] = lpos[1] - 0.2;
        glutPostRedisplay();
        break;
    case 'z':
        if (lightSource == true)
            lpos[2] = lpos[2] + 0.2;
        glutPostRedisplay();
        break;
    case 'Z':
        if (lightSource == true)
            lpos[2] = lpos[2] - 0.2;
        glutPostRedisplay();
        break;

    case '+':
        if (zoom != 1.5)zoom = zoom - 0.5;
        glutPostRedisplay();
        break;
    case '-':
        if (zoom != 30)zoom = zoom + 0.5;
        glutPostRedisplay();
        break;
    case '0':
        if (lightSource == true){
            glDisable(GL_LIGHT0);
            lightSource = false;
        }
        else{
            glEnable(GL_LIGHT0);
            lightSource = true;
        }
        glutPostRedisplay();
        break;

    case 'e':
        if (fenceHeight < 2)
            fenceHeight += 0.5;
        glutPostRedisplay();
        break;
    case 'd':
        if (fenceHeight > -0.5)
            fenceHeight -= 0.5;
        glutPostRedisplay();
        break;

    case 'w':
        glGetIntegerv(GL_POLYGON_MODE, polygonmode);
        if (polygonmode[0] == GL_FILL)
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        glutPostRedisplay();
        break;
    case 'n':
        angle++;
        degreeToRadian();
        pre_cursor();
        degreeToRadianInRange();
        glutPostRedisplay();
        break;
    case 'm':
        angle--;
        degreeToRadian();
        pre_cursor();
        degreeToRadianInRange();
        glutPostRedisplay();
        break;
    default:
        break;
    }
}


void specialkey(GLint key, int x, int y)
{
    switch (key) {
    case GLUT_KEY_RIGHT:
        alpha = alpha + PI / 180;
        if (alpha > 2 * PI) alpha = alpha - 2 * PI;
        glutPostRedisplay();
        break;
    case GLUT_KEY_LEFT:
        alpha = alpha - PI / 180;
        if (alpha < 0) alpha = alpha + 2 * PI;
        glutPostRedisplay();
        break;
    case GLUT_KEY_UP:
        if (beta < 0.45*PI) beta = beta + PI / 180;
        glutPostRedisplay();
        break;
    case GLUT_KEY_DOWN:
        if (beta > -0.05*PI) beta = beta - PI / 180;
        glutPostRedisplay();
        break;
    default:
        break;
    }
}


int main(int argc, char** argv)
{
    writemessage();
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(1200, 800);
    glutInitWindowPosition(0, 0);
    glutCreateWindow(argv[0]);

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);

    /* initially GL_FILL mode (default), later GL_LINE to show wireframe */
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    glEnable(GL_LIGHTING);
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
    glEnable(GL_LIGHT0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0, 5.0, 10.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutSpecialFunc(specialkey);

    glutMainLoop();
    return 0;
}

截图:

enter image description here enter image description here enter image description here

最佳答案

定义一个函数update,它将被定期调用。

void update(int x)
{
    radian_angle_in_range += 1.0; // change this angle according to your need
    glutPostRedisplay();

    // reregister the timer
    glutTimerFunc(TIME_IN_MILLISECONDS, update, 1);
}

glutMainLoop(); 之前的 main 函数中注册第一个回调

glutTimerFunc(TIME_IN_MILLISECONDS, update, 1);

关于 glutTimerFunc:

https://www.opengl.org/resources/libraries/glut/spec3/node64.html

关于c++ - OpenGL/GLUT 试图使水平杆以不同方式波动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26774074/

相关文章:

c++ - 我可以在同一个类中创建对象吗?

c++ - GameBoy 经典模拟器 - 如何初始化 VRAM (0x8000)

c++ - OpenGL 图像加载 64 位深度

c++ - 错误 C2955 : use of class template requires template argument list

C++ 代码在 VS2010 中工作但在 2013 中不工作

c - 受类型转换影响的变量

C 共享内存段故障

c - 在 C 中将字符串转换为 morze 时 Printf() 不起作用

c++ - 在结构 C++ 中存储 RGB 值

c++ - 将纹理 bmp 文件作为纹理平铺到 OpenGL 中的矩形上?