因此,我正在OpenGL和C++中进行分配。我正在编写代码以绘制mandelbrot集。但是问题是代码没有编译器错误,但是一旦我运行该程序,它就会崩溃。这是代码:

#define GLEW_STATIC
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <iostream>
#include <cmath>
#include "Shader.h"
using namespace std;

// Tamanho da Janela
GLuint screenWidth = 800, screenHeight = 600;

void mandelbrotSet(GLfloat vertices[]);

int main() {
    const GLint numOfPositions = 5 * 800 * 600;
    GLint numOfPixels = screenWidth * screenHeight;

    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_SAMPLES, 4); // 4 Samples de anti-aliasing
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "Mandelbrot - Pratica 1", nullptr, nullptr);
    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;
    glewInit();
    glViewport(0, 0, screenWidth, screenHeight);

    Shader myShaders("vShader.vs", "fShader.fs");

    GLfloat vertices[numOfPositions];
    //mandelbrotSet(vertices);

    myShaders.Use();

    GLuint VAO;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);
    GLuint VBO;
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(0));
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(1);

    glBindVertexArray(0);
    // Main Loop
    cout << "AUSDHIAUSDH";
    while (!glfwWindowShouldClose(window)) {
        glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glfwPollEvents();

        myShaders.Use();

        glBindVertexArray(VAO);
        glDrawArrays(GL_POINTS, 0, numOfPixels);
        glBindVertexArray(0);

        glfwSwapBuffers(window);
    }

    return 0;
}

void mandelbrotSet(GLfloat vertices[]) {
    double MinRe = -2.0;
    double MaxRe = 1.0;
    double MinIm = -1.2;
    double MaxIm = MinIm + (MaxRe - MinRe) * screenHeight / screenWidth;
    double Re_factor = (MaxRe - MinRe) / (screenWidth - 1);
    double Im_factor = (MaxIm - MinIm) / (screenHeight - 1);
    int MaxIterations = 30;
    int posCount = 0;
    for ( int y = 0; y < screenHeight; ++y )
    {
        double c_im = MaxIm - y * Im_factor;
        for ( int x = 0; x < screenWidth; ++x )
        {
            double c_re = MinRe + x * Re_factor;
            // Calcula se o pixel se o numero complexo c pertence ao set
            double Z_re = c_re, Z_im = c_im; // Set Z = c
            bool isInside = true;
            for ( int n = 0; n<MaxIterations; ++n )
            {
                double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
                if ( Z_re2 + Z_im2 > 4 )
                {
                    isInside = false;
                    break;
                }
                Z_im = 2 * Z_re*Z_im + c_im;
                Z_re = Z_re2 - Z_im2 + c_re;
            }
            if ( isInside )
            {
                vertices[posCount]     = float(x);
                vertices[posCount + 1] = float(y);
                vertices[posCount + 2] = 0.0f;
                vertices[posCount + 3] = 0.0f;
                vertices[posCount + 4] = 0.0f;
                posCount += 5;
            }
            else
            {
                vertices[posCount]     = float(x);
                vertices[posCount + 1] = float(y);
                vertices[posCount + 2] = 1.0f;
                vertices[posCount + 3] = 1.0f;
                vertices[posCount + 4] = 1.0f;
                posCount += 5;
            }
       }
    }
}

由于我不擅长调试,因此我尝试在代码中添加一些提示,但未显示任何内容。同样在 Debug模式下,程序崩溃并显示以下消息:
"Unhandled exception at 0x00C21A47 in Mandelbrot - Pratica 1.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00DA2000)."
注意,我注释了函数调用(mandelbrotSet(vertices)),所以我认为问题不出在函数中。
编辑

另外,当我评论Shader myShaders(“vShader.vs”,“fShader.fs”);时,问题仍然存在。

最佳答案

您正在经历典型的堆栈溢出,因为您尝试在堆栈上分配的数组的大小太大:

const GLint numOfPositions = 5 * 800 * 600;
GLfloat vertices[numOfPositions];

堆栈的大小是有限的(也有堆,但通常更大)。

使用调试器会有所帮助...



在堆上使用动态分配(使用newmalloc)或截断数组的大小。

10-08 00:05