简体   繁体   中英

Program not entering the main()

So, I am doing an assignment in OpenGL and C++. I am writing a code to draw the mandelbrot set. But the problem is that the code has no compiler errors, but as soon as I run the program, it crashes. Here is the code:

#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;
            }
       }
    }
}

Since I am not good at debugging, I tried to put some couts in the code, but nothing shows. Also in debugging mode, the program crashes with the message:

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

Notice that I commented the function call (mandelbrotSet(vertices)), so I don't think the problem is in the function. EDIT

Also when I comment Shader myShaders("vShader.vs", "fShader.fs"); the problem still persists.

You are experiencing typical stack overflow because the size of array you are trying to allocate on stack is just too big:

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

The stack is limited in size (heap too, but much larger [usually]).

And use of debugger would help...

...is there a way to work around that?

Use dynamic allocation on heap (with new or malloc ) or truncate size of array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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