OpenGL:问题使图标旋转

OpenGL: Issues Making Icons Rotate

本文关键字:图标 旋转 问题 OpenGL      更新时间:2023-10-16

我已经成功地画出了一个有一条线穿过它的正方形,一个有一条线穿过它的梯形,以及一个在梯形上面有一条线穿过它的正方形。在这一点上,我只是试图让所有的图标在屏幕上慢慢旋转,但不知道我错在哪里。

我在之前编写的程序之后建模了我的程序,该程序使多边形在屏幕上行走并跳起来并进行翻转,但由于某种原因,我无法从任何图标中获得任何移动。我的TimerFunction是否不像我期望的那样?

我知道这是乏味的阅读,但我包括我的整个代码,因为我不确定问题可能在哪里…

void init(void);  //function that initializes the window clear color
void DrawsAllIcons(float x[], float y[], int ndraws, int pointsperdraw [],   int drawtype[], float colorr[], float colorg[], float colorb[], float rotate,   float scalex, float scaley, float transx, float transy); //function to draw the functions in the opened window
void SetupRC(void);
void RenderScene(void);
void settrans2 (float rotate, float scalex, float scaley, float transx, float transy);//function that sets the clear color used to set the state of the OpenGL system
void TimerFunction(int );
//square
float rotate = 00.0;
float xCoords [7] = {1.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0};
float yCoords [7] = {1.0,-1.0,-1.0,1.0,1.0,2.0,-2.0};
int numberofDraws = 2;
int pointsPerDraw[2] = {5, 2};
int typeOfDraw[2] = {2,1};
float colorR[3] = {1.0,0.0,0.0};
float colorg[3] = {0.0,1.0,0.0};
float colorb[3] = {0.0,0.0,1.0};
float transx = 5.0;
float transy = 5.0;
float scalex = 2.0;
float scaley = 2.0;

//trapezoid
float rotate2 = 00.0;
float xCoords2 [6] = {1.0, 1.5, -1.5, -1.0, 0.0, 0.0};
float yCoords2 [6] = {1.0,-1.0,-1.0,1.0,2.0,-2.0};
int numberofDraws2 = 2;
int pointsPerDraw2[2] = {4, 2};
int typeOfDraw2[2] = {3,1};
float colorR2[3] = {0.0,0.0,0.0};
float colorg2[3] = {1.0,1.0,0.0};
float colorb2[3] = {0.0,0.0,1.0};
float transx2 = -5.0;
float transy2 = -5.0;
float scalex2 = 2.0;
float scaley2 = 2.0;
//Square on trapezoid
float rotate3 = 00.0;
float xCoords3 [6] = {1.0, 1.0, -1.0, -1.0, 0.0,0.0};
float yCoords3 [6] = {1.0,0.0,0.0,1.0,3.0,-3.0};
int numberofDraws3 = 2;
int pointsPerDraw3[2] = {4, 2};
int typeOfDraw3[2] = {3,1};
float colorR3[3] = {1.0,0.0,0.0};
float colorg3[3] = {0.0,1.0,0.0}; //go down dont overthink
float colorb3[3] = {0.0,0.0,0.0};
float transx3 = 0.0;
float transy3 = 0.0;
float scalex3 = 1.0;
float scaley3 = 1.0;
//trapezoid under square
float rotate4 = 00.0;
float xCoords4 [6] = {1.5, 2.0, -2.0, -1.5, 0.0, 0.0};
float yCoords4 [6] = {0.0,-1.0,-1.0,0.0,3.0,-3.0};
int numberofDraws4 = 2;
int pointsPerDraw4[2] = {4, 2};
int typeOfDraw4[2] = {3,1};
float colorR4[3] = {0.0,0.0,0.0};
float colorg4[3] = {1.0,1.0,0.0};
float colorb4[3] = {0.0,0.0,1.0};
float transx4 = 0.0;
float transy4 = 0.0;
float scalex4 = 1.0;
float scaley4 = 1.0;

int main(int argc, char* *argv)
{
char header[]="This Bad Boy'll Draw any Icon you can think of";  //set up window title
glutInit(&argc, argv); // initialize glopen utility toolkit
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); // Set up the display mode with a buffer and colors **
glutInitWindowSize(560,440); //window size and position
glutInitWindowPosition(140,20);
SetupRC();
glutCreateWindow(header); //  Open and label the window
glutDisplayFunc(RenderScene); //points to the function that will be drawing the item  // Set the state of the rendering machine
glutTimerFunc(30, TimerFunction, 1);
glutMainLoop(); // Call and activate the main
return 0;
}
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);//note clear color was set in SetupRC
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); 
glViewport(0,0,540,440); //set viewpoint to dimensions
glOrtho(-20.0,20.0,-20.0,20.0,1.0,-1.0);
glClear(GL_COLOR_BUFFER_BIT);
DrawsAllIcons(xCoords, yCoords, numberofDraws, pointsPerDraw,typeOfDraw,colorR,colorg,colorb, rotate, transx,transy,scalex,scaley); //used GL_LINE_STRIP for the square to show that that case worked
DrawsAllIcons(xCoords2, yCoords2, numberofDraws2, pointsPerDraw2,typeOfDraw2,colorR2,colorg2,colorb2, rotate2, transx2,transy2,scalex2,scaley2); //used GL_POLYGON and GL_LINE for rest
DrawsAllIcons(xCoords3, yCoords3, numberofDraws3, pointsPerDraw3,typeOfDraw3,colorR3,colorg3,colorb3, rotate3, transx3,transy3,scalex3,scaley3);
DrawsAllIcons(xCoords4, yCoords4, numberofDraws4, pointsPerDraw4,typeOfDraw4,colorR4,colorg4,colorb4, rotate4, transx4,transy4,scalex4,scaley4);
glEnd();
glutSwapBuffers(); //
}


void DrawsAllIcons (float x[], float y[], int ndraws, int pointsperdraw [], int drawtype[], float colorr[], float colorg[], float colorb[], float rotateD, float transxD, float transyD, float scalexD, float scaleyD)
{ 
settrans2(rotateD,scalexD,scaleyD,transxD, transyD);
int k=0; //index for arrays
int drawTooIndex = 0;
ndraws=ndraws-1;
    for (int j=0; j<=ndraws; j++) //runs through
    {
        int whatCase = drawtype[j]; //sees what type of draw
        drawTooIndex +=pointsperdraw[j];
        switch (whatCase)
        {
            case 1:
                {
                    glColor3f(colorr[j],colorg[j],colorb[j]);
                    glBegin(GL_LINES);
                    glVertex2f(x[k], y[k]); //sets vertex at the first point at k in the point arrays
                    int i = k+1;
                    k++;
                    for (i; i <drawTooIndex; i++) 
                    {
                        glVertex2f(x[i], y[i]);
                        k++;
                    }
                    glEnd();
                    glFlush();
                }
                    break;
            case 2:
                {
                    glColor3f(colorr[j], colorg[j], colorb[j]);
                    glBegin(GL_LINE_STRIP);
                    glVertex2f(x[k], y[k]);
                    int m = k+1;
                    k++;
                    for (m; m <drawTooIndex; m++)
                    {
                        glVertex2f(x[m], y[m]);
                        k++;
                    }
                    glEnd();
                    glFlush();
                }
                    break;
            case 3:
                {
                    glColor3f(colorr[j], colorg[j], colorb[j]);
                    glShadeModel(GL_FLAT);
                    glBegin(GL_POLYGON);
                    glVertex2f(x[k], y[k]);
                    int n = k+1; //gets index of where to start drawing in the x and y arrays
                    k++;
                    for (n; n <drawTooIndex; n++)
                    {
                        glVertex2f(x[n], y[n]);
                        k++;
                    }
                    glEnd();
                    glFlush();
                }
                    break;
    }
}
}
void SetupRC(void) 
{ // function sets the clear color of an open window, and then clears the open window
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set clear color to pale green
    return;
}//end of SetupRC
void settrans2(float rotateDD, float scalexDD, float scaleyDD, float transxDD, float transyDD)
{
glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity();
    glTranslatef(transxDD,transyDD,0.0);
    glRotatef(rotateDD, 0.0, 0.0, 1.0); // where to put this in the program?
    glScalef(scalexDD, scaleyDD, 1.0);
return;
}
void TimerFunction(int value)
{
    rotate+=5.0;
    rotate2+=5.0;
    rotate3+=5.0;
    rotate4+=5.0;
} 

glutTimerFunc()只触发一次,您需要在回调中重新武装它们以获得另一个,以及用新值重新绘制GLUT:

void TimerFunction(int value)
{
    rotate+=5.0;
    rotate2+=5.0;
    rotate3+=5.0;
    rotate4+=5.0;
    glutPostRedisplay();
    glutTimerFunc( 30, TimerFunction, 1 );
}

一起:

#include <GL/glut.h>
//square
float rotate = 00.0;
float xCoords [7] = {1.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0};
float yCoords [7] = {1.0,-1.0,-1.0,1.0,1.0,2.0,-2.0};
int numberofDraws = 2;
int pointsPerDraw[2] = {5, 2};
int typeOfDraw[2] = {2,1};
float colorR[3] = {1.0,0.0,0.0};
float colorg[3] = {0.0,1.0,0.0};
float colorb[3] = {0.0,0.0,1.0};
float transx = 5.0;
float transy = 5.0;
float scalex = 2.0;
float scaley = 2.0;
//trapezoid
float rotate2 = 00.0;
float xCoords2 [6] = {1.0, 1.5, -1.5, -1.0, 0.0, 0.0};
float yCoords2 [6] = {1.0,-1.0,-1.0,1.0,2.0,-2.0};
int numberofDraws2 = 2;
int pointsPerDraw2[2] = {4, 2};
int typeOfDraw2[2] = {3,1};
float colorR2[3] = {0.0,0.0,0.0};
float colorg2[3] = {1.0,1.0,0.0};
float colorb2[3] = {0.0,0.0,1.0};
float transx2 = -5.0;
float transy2 = -5.0;
float scalex2 = 2.0;
float scaley2 = 2.0;
//Square on trapezoid
float rotate3 = 00.0;
float xCoords3 [6] = {1.0, 1.0, -1.0, -1.0, 0.0,0.0};
float yCoords3 [6] = {1.0,0.0,0.0,1.0,3.0,-3.0};
int numberofDraws3 = 2;
int pointsPerDraw3[2] = {4, 2};
int typeOfDraw3[2] = {3,1};
float colorR3[3] = {1.0,0.0,0.0};
float colorg3[3] = {0.0,1.0,0.0}; //go down dont overthink
float colorb3[3] = {0.0,0.0,0.0};
float transx3 = 0.0;
float transy3 = 0.0;
float scalex3 = 1.0;
float scaley3 = 1.0;
//trapezoid under square
float rotate4 = 00.0;
float xCoords4 [6] = {1.5, 2.0, -2.0, -1.5, 0.0, 0.0};
float yCoords4 [6] = {0.0,-1.0,-1.0,0.0,3.0,-3.0};
int numberofDraws4 = 2;
int pointsPerDraw4[2] = {4, 2};
int typeOfDraw4[2] = {3,1};
float colorR4[3] = {0.0,0.0,0.0};
float colorg4[3] = {1.0,1.0,0.0};
float colorb4[3] = {0.0,0.0,1.0};
float transx4 = 0.0;
float transy4 = 0.0;
float scalex4 = 1.0;
float scaley4 = 1.0;
void settrans2(float rotateDD, float scalexDD, float scaleyDD, float transxDD, float transyDD)
{
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity();
    glTranslatef(transxDD,transyDD,0.0);
    glRotatef(rotateDD, 0.0, 0.0, 1.0); // where to put this in the program?
    glScalef(scalexDD, scaleyDD, 1.0);
    return;
}
void DrawsAllIcons( float x[], float y[], int ndraws, int pointsperdraw [], int drawtype[], float colorr[], float colorg[], float colorb[], float rotateD, float transxD, float transyD, float scalexD, float scaleyD)
{ 
    settrans2(rotateD,scalexD,scaleyD,transxD, transyD);
    int k=0; //index for arrays
    int drawTooIndex = 0;
    ndraws=ndraws-1;
    for (int j=0; j<=ndraws; j++) //runs through
    {
        int whatCase = drawtype[j]; //sees what type of draw
        drawTooIndex +=pointsperdraw[j];
        switch (whatCase)
        {
        case 1:
            {
                glColor3f(colorr[j],colorg[j],colorb[j]);
                glBegin(GL_LINES);
                glVertex2f(x[k], y[k]); //sets vertex at the first point at k in the point arrays
                int i = k+1;
                k++;
                for (i; i <drawTooIndex; i++) 
                {
                    glVertex2f(x[i], y[i]);
                    k++;
                }
                glEnd();
                glFlush();
            }
            break;
        case 2:
            {
                glColor3f(colorr[j], colorg[j], colorb[j]);
                glBegin(GL_LINE_STRIP);
                glVertex2f(x[k], y[k]);
                int m = k+1;
                k++;
                for (m; m <drawTooIndex; m++)
                {
                    glVertex2f(x[m], y[m]);
                    k++;
                }
                glEnd();
                glFlush();
            }
            break;
        case 3:
            {
                glColor3f(colorr[j], colorg[j], colorb[j]);
                glShadeModel(GL_FLAT);
                glBegin(GL_POLYGON);
                glVertex2f(x[k], y[k]);
                int n = k+1; //gets index of where to start drawing in the x and y arrays
                k++;
                for (n; n <drawTooIndex; n++)
                {
                    glVertex2f(x[n], y[n]);
                    k++;
                }
                glEnd();
                glFlush();
            }
            break;
        }
    }
}
void RenderScene(void)
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set clear color to pale green
    glClear(GL_COLOR_BUFFER_BIT);//note clear color was set in SetupRC
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); 
    glViewport(0,0,540,440); //set viewpoint to demensions
    glOrtho(-20.0,20.0,-20.0,20.0,1.0,-1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    DrawsAllIcons(xCoords, yCoords, numberofDraws, pointsPerDraw,typeOfDraw,colorR,colorg,colorb, rotate, transx,transy,scalex,scaley); //used GL_LINE_STRIP for the square to show that that case worked
    DrawsAllIcons(xCoords2, yCoords2, numberofDraws2, pointsPerDraw2,typeOfDraw2,colorR2,colorg2,colorb2, rotate2, transx2,transy2,scalex2,scaley2); //used GL_POLYGON and GL_LINE for rest
    DrawsAllIcons(xCoords3, yCoords3, numberofDraws3, pointsPerDraw3,typeOfDraw3,colorR3,colorg3,colorb3, rotate3, transx3,transy3,scalex3,scaley3);
    DrawsAllIcons(xCoords4, yCoords4, numberofDraws4, pointsPerDraw4,typeOfDraw4,colorR4,colorg4,colorb4, rotate4, transx4,transy4,scalex4,scaley4);
    glEnd();
    glutSwapBuffers(); //
}
void TimerFunction(int value)
{
    rotate+=5.0;
    rotate2+=5.0;
    rotate3+=5.0;
    rotate4+=5.0;
    glutPostRedisplay();
    glutTimerFunc( 30, TimerFunction, 1 );
}
int main(int argc, char* *argv)
{
    char header[]="This Bad Boy'll Draw any Icon you can think of";  //set up window title
    glutInit(&argc, argv); // initialize glopen utility toolkit
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); // Set up the display mode with a buffer and colors **
    glutInitWindowSize(560,440); //window size and positoin
    glutInitWindowPosition(140,20);
    glutCreateWindow(header); //  Open and label the window
    glutDisplayFunc(RenderScene); //points to the fucntion that will be drawing the item  // Set the state of the rendering machine
    glutTimerFunc(30, TimerFunction, 1);
    glutMainLoop(); // Call and activate the main
    return 0;
}