-1

这是绘制谢尔宾斯基三角形的分形几何的代码,没有错误,但在调试时出现未处理的异常:

Unhandled exception at 0x77e115de in LastFractLast.exe: 0xC0000005: Access violation writing location 0x000000a8.

它会打开一个名为 glu.h 的文件并指向这一行:

static void APIENTRY glutInit_ATEXIT_HACK(int *argcp, char **argv) { __glutInitWithExit(argcp, argv, exit); } 

这是我的代码:

 #include <glut.h>
GLfloat v[3][2]={{-1.0, -0.58},
{1.0, -0.58}, {0.0, 1.15}};
int n;

void triangle(GLfloat *a, GLfloat *b,
GLfloat *c)
/* display one triangle */
 {
 glVertex2fv(a);
 glVertex2fv(b);
 glVertex2fv(c);
 }
 void divide_triangle(GLfloat *a, GLfloat *b, GLfloat *c,
 int m)
 {
 /* triangle subdivision using vertex numbers */
typedef GLfloat point2[2];
  point2 v0, v1, v2;
 int j;
  if(m>0)
   {
   for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2;
   for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2;
   for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;
   divide_triangle(a, v0, v1, m-1); 
   divide_triangle(c, v1, v2, m-1);
   divide_triangle(b, v2, v0, m-1);
    }
   else(triangle(a,b,c));
   /* draw triangle at end of recursion */
    }



     void display()
          {
       glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
divide_triangle(v[0], v[1], v[2], n);
glEnd();
glFlush();
}

void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW);
glClearColor (1.0, 1.0, 1.0,1.0);
glColor3f(0.0,0.0,0.0);
}
int main(int argc, char **argv)
{
n=4;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
4

1 回答 1

2

在执行 GL 命令之前,您需要一个当前的 GL 上下文。

使用 GLUT,您需要通过创建一个窗口glutCreateWindow()来获取上下文:

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow( "Window" );
glutDisplayFunc(display);
glutMainLoop();

完全的:

#include <GL/glut.h>

/* display one triangle */
void triangle(GLfloat *a, GLfloat *b, GLfloat *c)
{
    glVertex2fv(a);
    glVertex2fv(b);
    glVertex2fv(c);
}

void divide_triangle(GLfloat *a, GLfloat *b, GLfloat *c, int m)
{
    /* triangle subdivision using vertex numbers */
    typedef GLfloat point2[2];
    point2 v0, v1, v2;
    int j;
    if(m>0)
    {
        for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2;
        for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2;
        for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;
        divide_triangle(a, v0, v1, m-1); 
        divide_triangle(c, v1, v2, m-1);
        divide_triangle(b, v2, v0, m-1);
    }
    else
    {
        /* draw triangle at end of recursion */
        triangle(a,b,c);
    }
}

void display()
{
    glClearColor (1.0, 1.0, 1.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-2.0, 2.0, -2.0, 2.0, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3f(0.0,0.0,0.0);
    glBegin(GL_TRIANGLES);
    GLfloat v[3][2]=
        {
        {-1.0, -0.58},
        {1.0, -0.58}, 
        {0.0, 1.15}
        };
    divide_triangle(v[0], v[1], v[2], 4);
    glEnd();

    glFlush();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow( "Window" );
    glutDisplayFunc(display);
    glutMainLoop();
}
于 2012-11-18T07:05:14.423 回答