简体   繁体   English

OpenGL:Glut.h访问冲突

[英]OpenGL: Glut.h Access Violation

Ok, so I'm developing a small program that randomly generates a single sheet of terrain using a recursive function to subdivide between four given points, and the terrain is made of of quads. 好的,所以我正在开发一个小程序,该程序使用递归函数在四个给定点之间进行细分,从而随机生成一张地形图,并且该地形由四边形组成。 The centre point found on each recursion is given a random height value, and then all of the vertices are saved to an array, which is then looped through when drawing. 在每次递归中找到的中心点都被赋予一个随机的高度值,然后所有的顶点被保存到一个数组中,然后在绘制时循环遍历。

Originally, I was defining the 2D array used to store the vertices on the stack, however depending on the number of subdivisions this can become quite large. 最初,我定义的是用于将顶点存储在堆栈上的2D数组,但是根据细分的数量,这可能会变得非常大。 A depth of 3 for example yields 256 vertices. 例如,深度3会产生256个顶点。 So, I decided defining this array on the heap where possible will be much more efficient for larger sheets of terrain. 因此,我决定在堆上定义此数组,如果可能的话,这对于较大的地形会更有效。 In the Terrain class I define the following array of float pointers. 在Terrain类中,我定义以下浮动指针数组。

float* vertices[]; 

Then, in the constructor for this class, I do the following to initialize it's size on the heap. 然后,在此类的构造函数中,我执行以下操作以初始化其在堆上的大小。 4 to the power of the number of given subdivisions plus one yields 256, and number of vertices that are created. 4等于给定细分数加1的乘方的乘积256,以及所创建的顶点数。

int vertexCount = pow(4.0, subdivisions+1);
*vertices = new float[vertexCount];

Then, each of these pointers points to an array of float values in the heap, defined as follows. 然后,这些指针中的每一个都指向堆中的浮点值数组,定义如下。

for(int i = 0; i<vertexCount + 1; i++)
{
    float* temp = new float[3];
    vertices[i] = temp;
    delete temp;
    temp = 0;
}

My recursive function used to create the terrain looks as follows: 我用于创建地形的递归函数如下所示:

void Terrain::Subdivide(float* a, float* b, float* c, float* d, int subdivisions, float      maxHeight)
{
if(subdivisions>0)
{
    float e[3];
    float f[3];
    float g[3];
    float h[3];
    float centre[3];
    int i = 0;

    srand(time(NULL));

    for(i = 0; i<3; i++) if(i!= 1) e[i] = (a[i]+b[i])/2;
    for(i = 0; i<3; i++) if(i!= 1) f[i] = (b[i]+c[i])/2;
    for(i = 0; i<3; i++) if(i!= 1) g[i] = (c[i]+d[i])/2;
    for(i = 0; i<3; i++) if(i!= 1) h[i] = (d[i]+a[i])/2;

    centre[0] = (h[0]+f[0])/2;
    centre[2] = (e[2]+g[2])/2;

    e[1] = 0;
    f[1] = 0;
    g[1] = 0;
    h[1] = 0;
    centre[1] = (float)rand()/((float)RAND_MAX/maxHeight);

    Subdivide(a, e, centre, h, subdivisions-1 , maxHeight);
    Subdivide(e, b, f, centre, subdivisions-1, maxHeight);
    Subdivide(centre, f, c, g, subdivisions-1, maxHeight);
    Subdivide(h, centre, g, d, subdivisions-1, maxHeight);
}
else
{
    int i = 0;
    for(i = 0; i<3; i++) {vertices[vertexCounter][i] = a[i];} vertexCounter++;
    for(i = 0; i<3; i++) {vertices[vertexCounter][i] = b[i];} vertexCounter++;
    for(i = 0; i<3; i++) {vertices[vertexCounter][i] = c[i];} vertexCounter++;
    for(i = 0; i<3; i++) {vertices[vertexCounter][i] = d[i];} vertexCounter++;
}

} }

The code seems to work when I simply define vertices as a 2D array on the stack (with some changes elsewhere), however this code produces an unhandled exception in Glut.h on the following line, when calling glutInit in my main function: (Line 486) 当我仅将顶点定义为堆栈上的2D数组(其他地方有一些更改)时,该代码似乎可以工作,但是当在主函数中调用glutInit时,此代码在下一行的Glut.h中产生未处理的异常: 486)

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

I've looked through the code so many times now and I can't see where the problem lies, it's even stranger that the exception should be thrown from Glut.h and not the Terrain class. 我已经看过代码很多次了,但我看不出问题出在哪里,更奇怪的是应该从Glut.h而不是Terrain类抛出异常。 Can anyone see anything wrong here? 有人可以在这里看到任何错误吗?

Here's an error 这是一个错误

for(int i = 0; i<vertexCount + 1; i++)

should be 应该

for(int i = 0; i<vertexCount; i++)

The reason we count arrays from zero is so you don't have to add one. 我们将数组从零开始计数的原因是,您不必添加一个。

Here's another mistake 这是另一个错误

for(int i = 0; i<vertexCount; i++)
{
    float* temp = new float[3];
    vertices[i] = temp;
    delete temp;
    temp = 0;
}

should be 应该

for(int i = 0; i<vertexCount; i++)
{
    vertices[i] = new float[3];
}

For some reason you allocate the memory, free it again immediately but continue to use the freed memory in your vertex array. 由于某种原因,您分配了内存,请立即再次释放它,但继续使用顶点数组中释放的内存。 That's going to crash very quickly. 那将很快崩溃。

Third (hopefully final) mistake is 第三个(希望是最后一个)错误是

*vertices = new float[vertexCount];

should be 应该

vertices = new float*[vertexCount];

To simulate a 2D array, the first dimension is an array of of float pointers. 为了模拟2D数组,第一维是浮点指针的数组。 That's what the fixed code allocates. 这就是固定代码分配的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM