OpenGL:案例陈述和订单中的错误

时间:2015-10-15 01:00:58

标签: c++ opengl if-statement for-loop case

关于我的代码我有两个问题; 1.为什么我在glutDisplayFunc,2上收到此错误'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。为什么我在整个案例陈述中的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;
}

1 个答案:

答案 0 :(得分:1)

这与基本的C ++有很大关系,而不是OpenGL。

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

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

因此,参数是一个指向函数的指针,该函数具有返回类型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都有一个范围,它允许定义局部变量,并且很清楚它们的范围开始的位置和结束位置。