2015-10-15 62 views
1

我已經成功繪製了一條正方形,其中有一條直線穿過它,一條帶有直線的梯形以及一條直線穿過梯形的正方形。在這一點上,我只是試圖讓所有的圖標在屏幕上緩慢旋轉,但無法弄清楚我出錯的地方。OpenGL:使圖標旋轉的問題

我已經爲我的程序建模了一段我之前寫過的程序,它使得多邊形在屏幕上行走並跳起來做翻轉,但由於某種原因,我無法從任何圖標中獲得任何移動。我的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; 
} 

回答

1

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; 
} 
+0

哇,不敢相信我錯過了那個。謝啦! – Bob