OpenGL:案例陈述和顺序错误

OpenGL: Errors in Case Statements and Order

我有两个关于我的代码的问题; 1. 为什么我的 glutDisplayFunc 会出现此错误 'glutDisplayFunc' : cannot convert parameter 1 from 'void (__cdecl *)(float [],float [],int,int [],int [],float [],float [],float [],float,float,float,float,float)' to 'void (__cdecl *)(void)' 1> None of the functions with this name in scope match the target type,2。为什么我的 i 在整个案例陈述中都出现初始化和声明错误?我相信我的代码顺序正确,但老实说,这是我第一次将所有这些方面都放在一个函数中。

我不确定您需要哪部分代码来指导我正确的方向,所以我将其全部发布...

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


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_SINGLE|GLUT_RGBA); // Set up the display mode with a buffer and colors
glutInitWindowSize(900,650); //Initialize window size and position
glutInitWindowPosition(0,0); 
glutCreateWindow(header); //  Open and label the window
glutDisplayFunc(RenderScene); //points to the fucntion that will be drawing the item 
SetupRC(); // Set the state of the rendering machine
glutMainLoop(); // Call and activate the main
return 0;
}

void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);//note clear color was set in SetupRC
glLoadIdentity(); 
glViewport(25,25,900,500); //set the viewport to the window dimensions   
glOrtho(-7.0,7.0,-30.0,50.0,1.0,-1.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 rotate = 30.0;
float transx = 3.0;
float transy = 3.0;
float scalex = 1.0;
float scaley = 1.0;
DrawsAllIcons(xCoords, yCoords, numberofDraws, pointsPerDraw,typeOfDraw,colorR,colorg,colorb, rotate, transx,transy,scalex,scaley);
}




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)
{ 
int k=0; //index for arrays
int drawTooIndex = 0;

    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]);
                    settrans2(rotate,scalex,scaley,transx,transy); // is this where it needs to be?
                    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++) //pointsperdraw[k] needs messed with maybe?
                    {
                        glVertex2f(x[i], y[i]);
                        k++;
                    }
                    glEnd();
                    glFlush();
                }
                    break;

            case 2:
                {
                    glColor3f(colorr[j], colorg[j], colorb[j]);
                    settrans2(rotate,scalex,scaley,transx,transy);
                    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]);
                    settrans2(rotate,scalex,scaley,transx,transy);
                    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.560784f, 0.737255f, 0.560784f, 1.0f); // Set clear color to pale green
    return;
}//end of SetupRC

void settrans2(float rotate, float scalex, float scaley, float transx, float transy)
{
glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity();
    glTranslatef(transx,transy,0.0);
    glRotatef(rotate, 0.0, 0.0, 1.0); // where to put this in the program?
    glScalef(scalex, scaley, 1.0);

return;
}

与 OpenGL 相比,这与基本 C++ 的关系更大。

关于第一个问题:glutDisplayFunc()的参数是一个函数指针。您传递的值的类型需要与函数参数的类型相匹配。查看documentation,声明为:

void glutDisplayFunc(void (*func)(void));

所以参数是一个指向函数的指针,该函数具有 return 类型 void,并且没有参数。您正在尝试传递一个具有参数的函数,该参数与声明不匹配。这几乎就是编译器告诉你的内容。

您尝试做的事情不可能奏效还有另一个合理的原因。 GLUT 会在需要重绘时调用您传递给 glutDisplayFunc() 的函数。如果它必须调用您的 DrawAllIcons() 函数,它如何知道要为其参数传递什么值?真的不行。

关于 case 语句中定义的变量: switch 语句定义的方式,每个 case 启动一个新范围。哪一种有意义,因为每个 case 中的代码没有被通常指定新范围的花括号包围。这意味着整个 switch 语句中的所有内容都形成一个范围。

由于控制逻辑可以跳过此作用域内的部分代码,因此您可能会遇到一些奇怪的情况,即跳过变量的定义(可以包括初始化),但稍后会在没有变量的情况下使用它执行的代码中的定义。示例:

switch (...)
{
    case 1:
        ...
        int foo = 7;
        ...
        break;
    case 2:
        ...
        int bar = foo;
        ...
        break;
}

如果选择情况2,foo的值是多少?我们跳过了初始化。它甚至存在吗,因为我们跳过了定义?嗯,确实如此,因为它是在同一范围内较早声明的。另一方面,我们从未执行过包含定义的代码。

为了避免这些没有好的答案的问题,C++ 不允许直接在 case 语句中定义变量。

如果你想在 case 语句中使用局部变量,你总是可以通过启动一个新块来创建一个新的作用域。在上面:

switch (...)
{
    case 1:
        {
            ...
            int foo = 7;
            ...
        }
        break;
    case 2:
        {
            ...
            int bar = foo;
            ...
        }
        break;
}

现在每个 case 中都有一个范围,它允许定义局部变量,并且很清楚它们的范围从哪里开始和结束。