当我运行此代码时,控制台将“无效值”显示为错误。我在书中进行了研究,但没有找到解决方法。他们甚至没有提到此值的含义。我想这将是存储在gluNewQuadric
返回的指针中的地址号,该地址号可能超出GLUquadric类型的范围,但无法确定或修复它
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#ifndef CALLBACK
#define CALLBACK
#endif
void CALLBACK erro(GLenum coderro)
{
const GLubyte *estring;
estring=gluErrorString(coderro);
fprintf(stderr, "Erro de quadrica: %s\n", estring);
exit(0);
}
void tela ()
{
GLUquadric *quadrica;
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-30.0, 30.0, -30.0, 30.0, 1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0.0, 0.0, -20.0);
glColor3f(1.0, 0.0, 0.0);
quadrica=gluNewQuadric();
gluQuadricCallback(quadrica, GLU_ERROR, erro);
gluQuadricDrawStyle(quadrica, GLU_FILL);
gluDisk(quadrica, 0.0, 5.0, 1, 1);
glFlush();
}
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(500, 500);
glutCreateWindow("Tesselacao");
glutDisplayFunc(tela);
glutMainLoop();
}
答案 0 :(得分:2)
请参阅gluDisk
的规范:
void gluDisk( GLUquadric* quad, GLdouble inner, GLdouble outer, GLint slices, GLint loops);
参数
[...]
slices
指定围绕z轴的细分数量。
这意味着slices
必须至少为3,因为要生成形状,必须在磁盘周围至少有3个点。
例如
gluDisk(quadrica, 0.0, 5.0, 3, 1);
将生成一个三角形,
gluDisk(quadrica, 0.0, 5.0, 5, 1);
将生成一个五边形。
答案 1 :(得分:0)
如果您在调试器中运行此命令并在erro
处设置断点,则会发现以下内容(来自GDB的输出):
Breakpoint 1, erro (coderro=100901) at test.c:12
12 estring=gluErrorString(coderro);
(gdb) bt
#0 erro (coderro=100901) at test.c:12
#1 0xf7dd7782 in gluQuadricError (qobj=0x81ddb70, which=100901) at src/libutil/quad.c:81
#2 0xf7dd85ad in gluPartialDisk (qobj=0x81ddb70, innerRadius=0, outerRadius=5, slices=1, loops=1, startAngle=0, sweepAngle=360) at src/libutil/quad.c:450
#3 0xf7dd84c8 in gluDisk (qobj=0x81ddb70, innerRadius=0, outerRadius=5, slices=1, loops=1) at src/libutil/quad.c:427
#4 0x08048af2 in tela () at test.c:36
#5 0xf7f72da8 in fghRedrawWindow () from /usr/lib/libglut.so.3
#6 0xf7f72df3 in fghcbDisplayWindow () from /usr/lib/libglut.so.3
#7 0xf7f77a90 in fgEnumWindows () from /usr/lib/libglut.so.3
#8 0xf7f72e47 in fghDisplayAll () from /usr/lib/libglut.so.3
#9 0xf7f73f39 in glutMainLoopEvent () from /usr/lib/libglut.so.3
#10 0xf7f73f97 in glutMainLoop () from /usr/lib/libglut.so.3
#11 0x08048b52 in main (argc=1, argv=0xffffd4e4) at test.c:48
因此,错误来自gluDisk
。现在,简单尝试使用其参数,让我猜测罪魁祸首是slices=1
,这确实没有道理。将其设置为2
避免进入错误回调。