打开GL旋转

时间:2015-11-25 21:02:17

标签: c++ opengl

我试图在网格中绘制100个箭头,并尝试根据我读取的输入大小旋转每个箭头。但是当我尝试旋转一个箭头时,所有箭头都会一下子旋转。它出什么问题了 ?

void drawArrow(void) {

    float y1 = 2.0;
    float y2 = 2.15;
    float y3 = 2.4;


    int count2 = 0;
    for (float col = 0; col < 10; col++) {
        y1 -= 0.5;

        y2 -= 0.5;
        y3 -= 0.5;


        float x1 = -2.25;
        float x2 = -2.20;
        float x3 = -2.15;
        float x4 = -2.35;
        float x5 = -2.30;

        for (float rows = 0; rows < 10; rows++) {

            glPushMatrix(); 


            glTranslatef(-0.35 + 0.5 - 0.1, 0.4 - 0.01 + 0.1, 0.0);

            float m = (windx[count2] * windx[count2]) + (windy[count2] * windy[count2]);
            float rotator = sqrt(m);
            glRotatef(rotator-50, 0.0, 0.0, 1.0);

            glBegin(GL_LINE_STRIP);//start drawing a line loop

            glColor3f(1.0, 0.0, 0.0);
            glVertex3f(x1, y1, 0.0f);
            glVertex3f(x2, y2, 0.0f);
            glVertex3f(x3, y2, 0.0f); // drawing a arrow
            glVertex3f(x1, y3, 0.0f);
            glVertex3f(x4, y2, 0.0f);
            glVertex3f(x5, y2, 0.0f);
            glVertex3f(x1, y1, 0.0f);

            x1 += .5;
            x2 += .5;
            x3 += .5;
            x4 += .5;
            x5 += .5;
            count2 ++;
            glEnd();//end drawing of line loop 
            glPopMatrix();
        }
}

int main(int argc, char **argv) {

    glMatrixMode(GL_PROJECTION);
    glutInit(&argc, argv); // Initialize GLUT  
    glutInitDisplayMode(GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)  
    glutInitWindowSize(1000, 800); // Set the width and height of the window  
    glutInitWindowPosition(100, 100); // Set the position of the window  
    glutCreateWindow("Weather Analysis"); // Set the title for the window  
    readDataFile();             //Read Data From File
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering  

    glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for reshaping  



    glutMainLoop(); // Enter GLUT's main loop 
    void reshape(int width, int height) {
    glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window  
    glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed  
    glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)  
    gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes  
    glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly  
}

void display(void) {
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red  
    glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)  
    glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations  

    glTranslatef(0.0f, 0.5f, -6.0f);

    renderPrimitive(); // Render the primitive  
    drawArrow();
    glFlush(); // Flush 
}

并且我也不知道在GLtranslatef方法中输入什么。我真的是个初学者,对不起。enter image description here

1 个答案:

答案 0 :(得分:2)

尝试在一个rutine中绘制箭头,然后调用它DrawArrow我建议它的大小必须在[-1.0f; 1.0f]之间,而不是使用仿射变换将其放置在正确的位置,首先是所有平移旋转。

void DrawArrow(void)
{
    float x1 = 0;
    float x2 = -0.05;
    float x3 = -0.1;
    float x4 = 0.1;
    float x5 = 0.05;

    float y1 = -0.15;
    float y2 = 0;
    float y3 = 0.15;


    glBegin(GL_LINE_STRIP);//start drawing a line loop

        glColor3f(1.0, 0.0, 0.0);
        glVertex3f(x1, y1, 0.0f);
        glVertex3f(x2, y2, 0.0f);
        glVertex3f(x3, y2, 0.0f); // drawing a arrow
        glVertex3f(x1, y3, 0.0f);
        glVertex3f(x4, y2, 0.0f);
        glVertex3f(x5, y2, 0.0f);
        glVertex3f(x1, y1, 0.0f);

    glEnd();//
}

比你的数组绘图功能(你可以纠正不准确):

void drawArrows(void) {
    float y1 = 2.0;


    int count2 = 0;
    for (float col = 0; col < 10; col++) {
        y1 -= 0.5;

        float x1 = -2.25;

        for (float rows = 0; rows < 10; rows++) {

            glPushMatrix(); 

            glTranslatef(x1, y1, 0.0);

            float m = (windx[count2] * windx[count2]) + (windy[count2] * windy[count2]);
            float rotator = sqrt(m);
            glRotatef(rotator-50, 0.0, 0.0, 1.0);

            DrawArrow();

            x1 += .5;

            count2 ++;
            glPopMatrix();
        }
    }
}

我做了一些测试,结果将是这样的(使用恒定旋转器)。

enter image description here