程序没有进入main()

时间:2014-08-24 23:08:44

标签: c++ opengl stack-overflow

所以,我正在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;
            }
       }
    }
}

由于我不擅长调试,我试图在代码中添加一些couts,但没有显示。同样在调试模式下,程序崩溃并显示以下消息:

"Unhandled exception at 0x00C21A47 in Mandelbrot - Pratica 1.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00DA2000)."

请注意,我评论了函数调用(mandelbrotSet(vertices)),所以我不认为问题出在函数中。 修改

当我评论Shader myShaders时(&#34; vShader.vs&#34;,&#34; fShader.fs&#34;);问题仍然存在。

1 个答案:

答案 0 :(得分:3)

您遇到典型的堆栈溢出,因为您尝试在堆栈上分配的数组的大小太大了:

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

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

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

  

...有办法解决这个问题吗?

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