Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenGL:Case语句和顺序中的错误_C++_Opengl_If Statement_For Loop_Case - Fatal编程技术网

C++ OpenGL:Case语句和顺序中的错误

C++ OpenGL:Case语句和顺序中的错误,c++,opengl,if-statement,for-loop,case,C++,Opengl,If Statement,For Loop,Case,关于我的代码,我有两个问题;1.为什么我会收到此错误“glutDisplayFunc”:无法将参数1从“void(u cdecl*)(float[],float[],int,int[],int[],int[],float[],float[],float[],float[],float,float,float,float)”转换为“void(u cdecl*)(void)” 1> 作用域中具有此名称的函数都与我的glutDisplayFunc 2上的目标类型不匹配。为什么在我的案例陈述中,I上出现

关于我的代码,我有两个问题;1.为什么我会收到此错误
“glutDisplayFunc”:无法将参数1从“void(u cdecl*)(float[],float[],int,int[],int[],int[],float[],float[],float[],float[],float,float,float,float)”转换为“void(u cdecl*)(void)”
1> 作用域中具有此名称的函数都与我的glutDisplayFunc 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;
}
void init(void)//初始化窗口清除颜色的函数
无效绘图图标(浮动x[],浮动y[],整数ndraws,整数点绘制[],整数绘图类型[],浮动颜色R[],浮动颜色G[],浮动颜色B[],浮动旋转,浮动缩放x,浮动缩放y,浮动平移x,浮动平移y)//函数在打开的窗口中绘制函数
无效(无效);
void RenderScene(void);
void settrans2(浮动旋转、浮动缩放、浮动缩放、浮动平移、浮动平移)//函数,用于设置用于设置OpenGL系统状态的清晰颜色
int main(int argc,字符**argv)
{
char header[]=“这个坏男孩会画出你能想到的任何图标”;//设置窗口标题
glutInit(&argc,argv);//初始化glopen实用工具工具包
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);//使用缓冲区和颜色设置显示模式
GLUTINITWindowsSize(900650);//初始化窗口大小和位置
位置(0,0);
glutCreateWindow(标题);//打开并标记窗口
glutDisplayFunc(RenderScene);//指向将绘制项的函数
SetupRC();//设置渲染计算机的状态
glutMainLoop();//调用并激活主
返回0;
}
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);//注意,清除颜色是在SetupRC中设置的
glLoadIdentity();
glViewport(25,25900500);//将视口设置为窗口尺寸
格洛托(-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};
浮动旋转=30.0;
浮动transx=3.0;
浮动横档=3.0;
float-scalex=1.0;
浮动比例=1.0;
DrawsAllIcons(xCoords、yCoords、numberofDraws、pointsPerDraw、typeOfDraw、colorR、colorg、colorb、rotate、transx、transy、scalex、scaley);
}
无效DrawsAllIcons(浮点x[],浮点y[],整数ndraws,整数pointsperdraw[],整数drawtype[],浮点颜色R[],浮点颜色G[],浮点颜色B[],浮点旋转,浮点缩放x,浮点缩放y,浮点平移x,浮点平移y)
{ 
int k=0;//数组的索引
int-drawTooIndex=0;

对于(int j=0;j),这与基本C++相比OpenGL有更大的作用。

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

void glutDisplayFunc(void (*func)(void));
因此,参数是指向返回类型为
void
且没有参数的函数的指针。您试图传递的函数有参数,但与声明不匹配。这正是编译器告诉您的

还有另一个合理的原因,为什么您尝试的操作不可能工作。GLUT将在需要重画时调用传递给
glutDisplayFunc()
的函数。如果它必须调用您的
DrawAllIcons()
函数,它如何知道为其参数传递什么值?它确实不能

case
语句中定义的变量上:按照
switch
语句的定义方式,每个
case
都不会启动一个新的作用域。这是有意义的,因为每个
case
中的代码没有被通常指定一个新作用域的大括号包围。这意味着nside整个
switch
语句形成一个范围

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

switch (...)
{
    case 1:
        ...
        int foo = 7;
        ...
        break;
    case 2:
        ...
        int bar = foo;
        ...
        break;
}
如果选择了案例2,那么
foo
的值是多少?我们跳过了初始化。它是否存在,因为我们跳过了定义?它确实存在,因为它之前在同一范围内声明过。另一方面,我们从未执行过包含定义的代码

为了避免这些问题没有真正的答案,C++在“代码> CASE < /COD>语句中直接不允许变量定义。 如果希望在case语句中包含局部变量,则始终可以通过启动新块来创建新范围。在上面的示例中:

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

现在,在每个
案例中都有一个作用域,它允许定义局部变量,并且很清楚它们的作用域从何处开始,从何处结束。

Reto,我发现了代码中的错误,但出于某种原因,没有任何东西像我想要的那样打印出来?你能快速浏览一下,看看我哪里出错了吗?我发现了我的错误
settrans
把事情搞砸了,它们是不是定位错了?