理解这段opengl代码

发布于 2024-09-25 12:46:24 字数 859 浏览 4 评论 0原文

我是一名Java程序员,第一次用C语言学习opengl。我想剖析我的导师给我的这个简单代码,没有太多解释:

void renderScene (void) {
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
        glVertex3f(-0.5,-0.5,0.0);
        glVertex3f(0.5,0.0,0.0);
        glVertex3f(0.0,0.5,0.0);
    glEnd();
    glFlush();


   void init(); {
       int submenu;
       submenu = glutCreateMenu(menuApp);
       glutAddMenuEntry("Option A",1);
       glutAddMenuEntry("Option B",2);
       glutAddMenuEntry("Option C",3);

       glutCreateMenu(menuApp);
       glutAddSubMenu("SubMenu",submenu);
       glutAddMenuEntry("Salir",4);

       glutAttachMenu(GLUT_RIGHT_BUTTON);


   }

}

问题:

a) void renderScene (void) 是什么意思?为什么这个函数应该采用 void 参数?

b) void init();到底是什么? {}?为什么两者都有;和 {}?为什么它在 renderScene 函数内部

I'm a Java programmer, learning opengl in C for the first time. I wanna dissect this simple code that my instructor gave me without much explanation:

void renderScene (void) {
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
        glVertex3f(-0.5,-0.5,0.0);
        glVertex3f(0.5,0.0,0.0);
        glVertex3f(0.0,0.5,0.0);
    glEnd();
    glFlush();


   void init(); {
       int submenu;
       submenu = glutCreateMenu(menuApp);
       glutAddMenuEntry("Option A",1);
       glutAddMenuEntry("Option B",2);
       glutAddMenuEntry("Option C",3);

       glutCreateMenu(menuApp);
       glutAddSubMenu("SubMenu",submenu);
       glutAddMenuEntry("Salir",4);

       glutAttachMenu(GLUT_RIGHT_BUTTON);


   }

}

Questions:

a) What does void renderScene (void) means? Why should this function take a void paramether?

b) What the hell is void init(); {}? Why both ; and {}? Why is it inside the renderScene function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终止放荡 2024-10-02 12:46:24

void renderScene (void) 是 C 语言的说法“将 renderScene 声明为不带参数且不返回值的函数”。 void renderScene() 会有所不同 - 它是 C 中过时的函数声明样式,声明 renderScene 采用固定但未指定数量的参数。

另一部分有点奇怪,并且格式具有误导性。如果格式正确,它将显示为:

void init();

{
     int submenu;
     submenu = glutCreateMenu(menuApp);
     glutAddMenuEntry("Option A",1);
     /* ... */
}

{ } 内的块与 void init(); 无关。 void init(); 声明了一个名为 init 的函数(这是上面提到的过时的函数声明样式)。函数声明只是向编译器说“有一个函数叫做 this,这就是它的参数和返回值”的一种方式。由于 init 函数从未被调用,因此可以省略此行。

{ } 内的块只是一个复合语句。它所做的只是打开一个新的作用域 - 变量 submenu 的作用域仅限于该块。


还值得指出的是,声明 void init(); 本身就是一个时代错误 - 它是出现在同一块中的代码之后的声明,这是 C 标准 1999 年修订版中添加的功能,但这是一个没有原型的声明,正如前面提到的,它早于 1989 年最初的 C 标准。

另请注意,不在文件范围内的函数声明本身有些不寻常,尽管完全合法。

void renderScene (void) is C's way of saying "declare renderScene as a function taking no arguments and returning no value". void renderScene() would be something different - it is an obsolete function declaration style in C, declaring that renderScene takes a fixed-but-unspecified number of parameters.

The other part is a little odd, and formatted in a misleading way. Formatted correctly, it would read:

void init();

{
     int submenu;
     submenu = glutCreateMenu(menuApp);
     glutAddMenuEntry("Option A",1);
     /* ... */
}

The block inside the { } is unrelated to the void init();. The void init(); declares a function called init (it's the obsolete function declaration style mentioned above). A function declaration is just a way of saying to the compiler "there's a function somewhere called this, and this is what its arguments and return value are". Since the init function is never called, this line could be omitted.

The block inside the { } is just a compound statement. All it does is open a new scope - the variable submenu has a scope restricted to that block.


It's also worth pointing out that the declaration void init(); is itself an anachronism - it's a declaration that appears after code in the same block, which is a feature added in the 1999 revision of the C standard, but it's a declaration without a prototype, which as mentioned predates the original 1989 C standard.

Note also that function declarations that aren't at file scope are themselves somewhat unusual, although completely legal.

淡淡離愁欲言轉身 2024-10-02 12:46:24

具有参数 void 的函数 renderScene 意味着它不带任何参数。

init 函数初始化程序。换句话说,它启动并准备做某事。

应该定义 init 函数

void init(){
...

}

,而不是

void init();{
...
}

不应该位于 renderScene 内。

The function renderScene having the parameter void means it does not take any parameters.

The init function initalizes the program. In other words it starts up and gets ready to do something.

The init function should be defined

void init(){
...

}

instead of

void init();{
...
}

and it SHOULD NOT be within renderScene.

倒带 2024-10-02 12:46:24

void renderScene (void) 表示该函数不带参数。

类似于Java中的void renderScene()。

void init();到底是什么? {}?

可能是一个错字, ;不应该在那里。

为什么它在renderScene函数内部?

可能是另一个拼写错误,renderScene 应该有另一个}

void renderScene (void) means the function takes no arguments.

It's similar to void renderScene () in Java.

What the hell is void init(); {}?

Probably a typo, The ; shouldn't be there.

Why is it inside the renderScene function?

Probably another typo, the renderScene should have another }

叶落知秋 2024-10-02 12:46:24

如果您发布的代码片段正确,则它是 renderScene 函数的定义。该函数不返回值,也不接受任何参数。

void init(); 是一个声明。它只是让编译器知道名为 init 的函数的详细信息,以便在我们调用它时它可以生成正确的代码——但它是一个不完整的声明(对编译器没有太大帮助),而且它不是在声明范围内的任何地方调用。所以,这基本上只是噪音。

如果我们忽略这一点并继续使用{,则接下来的行是一组语句,可能是为了引入submenu变量而启动的,该变量仅存在于这个新块内。

If the code snippet you posted is correct, it is the definition of the renderScene function. The function does not return a value and does not accept any parameters.

void init(); is a declaration. It just lets the compiler know the details of the function named init so that it can generate the correct code when we call it -- but it's an incomplete declaration (not much help to the compiler) and it's not called anywhere the declaration is in scope. So, it's basically just noise.

If we ignore that and continue with the {, the next lines are a group of statements initiated probably to introduce the submenu variable, which only lives inside this new block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文