理解这段opengl代码
我是一名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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
void renderScene (void)
是 C 语言的说法“将renderScene
声明为不带参数且不返回值的函数”。 void renderScene() 会有所不同 - 它是 C 中过时的函数声明样式,声明 renderScene 采用固定但未指定数量的参数。另一部分有点奇怪,并且格式具有误导性。如果格式正确,它将显示为:
{ }
内的块与void init();
无关。void init();
声明了一个名为init
的函数(这是上面提到的过时的函数声明样式)。函数声明只是向编译器说“有一个函数叫做 this,这就是它的参数和返回值”的一种方式。由于init
函数从未被调用,因此可以省略此行。{ }
内的块只是一个复合语句。它所做的只是打开一个新的作用域 - 变量submenu
的作用域仅限于该块。还值得指出的是,声明
void init();
本身就是一个时代错误 - 它是出现在同一块中的代码之后的声明,这是 C 标准 1999 年修订版中添加的功能,但这是一个没有原型的声明,正如前面提到的,它早于 1989 年最初的 C 标准。另请注意,不在文件范围内的函数声明本身有些不寻常,尽管完全合法。
void renderScene (void)
is C's way of saying "declarerenderScene
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 thatrenderScene
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:
The block inside the
{ }
is unrelated to thevoid init();
. Thevoid init();
declares a function calledinit
(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 theinit
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 variablesubmenu
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.
具有参数
void
的函数renderScene
意味着它不带任何参数。init
函数初始化程序。换句话说,它启动并准备做某事。应该定义 init 函数
,而不是
它不应该位于
renderScene
内。The function
renderScene
having the parametervoid
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
instead of
and it SHOULD NOT be within
renderScene
.void renderScene (void)
表示该函数不带参数。类似于Java中的void renderScene()。
可能是一个错字, ;不应该在那里。
可能是另一个拼写错误,renderScene 应该有另一个}
void renderScene (void)
means the function takes no arguments.It's similar to void renderScene () in Java.
Probably a typo, The ; shouldn't be there.
Probably another typo, the renderScene should have another }
如果您发布的代码片段正确,则它是 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 namedinit
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 thesubmenu
variable, which only lives inside this new block.