c++ - 检测到堆栈粉碎,中止,OpenGl freeglut

标签 c++ opengl stack runtime-error stack-smash

所以我有下面的图形透视投影代码:

#include <GL/glut.h>
#include <iostream>
#include <unistd.h>
#include <math.h> 

#define UpperBD 5 
#define PI      3.1415926 
#define Num_pts 10

float Xe = 200.0f;
float Ye = 200.0f;
float Ze = 200.0f;
float Rho = sqrt(pow(Xe, 2) + pow(Ye, 2) + pow(Ze, 2));
float D_focal = 20.0f;

typedef struct {
    float X[UpperBD];
    float Y[UpperBD];
    float Z[UpperBD];
} pworld;

typedef struct {
    float X[UpperBD];
    float Y[UpperBD];
    float Z[UpperBD];
} pviewer;

typedef struct {
    float X[UpperBD];
    float Y[UpperBD];
} pperspective;

void mydisplay()
{
    // define x-y coordinate 
    float p1x = -1.0f, p1y = 0.0f;
    float p2x = 1.0f, p2y = 0.0f;
    float p3x = 0.0f, p3y = 1.0f;
    float p4x = 0.0f, p4y = -1.0f;

    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    pworld  world;
    pviewer viewer;
    pperspective perspective;

    //define the x-y-z world coordinate 
    world.X[0] = 0.0;    world.Y[0] = 0.0;   world.Z[0] = 0.0;    // origin  
    world.X[1] = 50.0;   world.Y[1] = 0.0;   world.Z[1] = 0.0;    // x-axis
    world.X[2] = 0.0;    world.Y[2] = 50.0;  world.Z[2] = 0.0;    // y-axis    
    world.X[3] = 0.0;    world.Y[3] = 0.0;   world.Z[3] = 50.0;   // y-axis 

    float sPheta = Ye / sqrt(pow(Xe, 2) + pow(Ye, 2));
    float cPheta = Xe / sqrt(pow(Xe, 2) + pow(Ye, 2));
    float sPhi = sqrt(pow(Xe, 2) + pow(Ye, 2)) / Rho;
    float cPhi = Ze / Rho;

    float xMin = 1000.0, xMax = -1000.0;
    float yMin = 1000.0, yMax = -1000.0;

    for (int i = 0; i <= UpperBD; i++)
    {
        viewer.X[i] = -sPheta * world.X[i] + cPheta * world.Y[i];
        viewer.Y[i] = -cPheta * cPhi * world.X[i]
            - cPhi * sPheta * world.Y[i]
            + sPhi * world.Z[i];
        viewer.Z[i] = -sPhi * cPheta * world.X[i]
            - sPhi * cPheta * world.Y[i]
            - cPheta * world.Z[i] + Rho;
    }

    for (int i = 0; i <= UpperBD; i++)
    {
        perspective.X[i] = D_focal * viewer.X[i] / viewer.Z[i];
        perspective.Y[i] = D_focal * viewer.Y[i] / viewer.Z[i];
        if (perspective.X[i] > xMax) xMax = perspective.X[i];
        if (perspective.X[i] < xMin) xMin = perspective.X[i];
        if (perspective.Y[i] > yMax) yMax = perspective.Y[i];
        if (perspective.Y[i] < yMin) yMin = perspective.Y[i];
        /*
        std::cout << "xMin " << xMin << std::endl;
        std::cout << "xMax " << xMax << std::endl;
        std::cout << "yMin " << yMin << std::endl;
        std::cout << "yMax " << yMax << std::endl;
        */
    }
    for (int i = 0; i <= UpperBD; i++)
    {
        if ((xMax - xMin) != 0) perspective.X[i] = perspective.X[i] / (xMax - xMin);
        if ((yMax - yMin) != 0) perspective.Y[i] = perspective.Y[i] / (yMax - yMin);
        //std::cout << i << perspective.X[i] << perspective.Y[i] << std::endl;
    }


    glBegin(GL_LINES);
    // cross at the display screen 
    //glVertex2f(p1x,p1y);  
    //glVertex2f(p2x,p2y);
    //glVertex2f(p3x, p3y);
    //glVertex2f(p4x, p4y);


    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);
    glVertex2f(perspective.X[0], perspective.Y[0]);
    glVertex2f(perspective.X[1], perspective.Y[1]);
    glColor3f(0.0, 1.0, 0.0);
    glVertex2f(perspective.X[0], perspective.Y[0]);
    glVertex2f(perspective.X[2], perspective.Y[2]);
    glColor3f(0.0, 0.0, 1.0);
    glVertex2f(perspective.X[0], perspective.Y[0]);
    glVertex2f(perspective.X[3], perspective.Y[3]);

    glEnd();

    glFlush();

}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("132 transformation pipeline");
    glutDisplayFunc(mydisplay);
    glutMainLoop();
}

我能够编译这段代码,但每当我运行它时,我都会收到错误“检测到堆栈粉碎”。我不知道为什么会这样。其他人也有同样的代码可以工作,但它对我不起作用。我为此使用 ubuntu。对此错误的任何帮助将不胜感激。

最佳答案

替换:

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

与:

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

在所有情况下。

关于c++ - 检测到堆栈粉碎,中止,OpenGl freeglut,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52546541/

相关文章:

c++ - 在练习 TDD(单元测试)的同时学习 OpenGL

windows - 具有 OpenGL 和 SDL 预编译 Windows 分发版的 Haskell 或 Ocaml

c++ - 使用 STL 堆栈作为 C++ 类的成员

c++ - Qt串口读取字符串不好

c++ - const char 声明导致堆栈溢出

android - 运行 add_qt_android_apk 时,CMake Android 工具链无法为 arm 找到 strip 命令

c++ - 画出这个形状

c++ - 使用 unique_ptr 制作的堆元素是否移动到堆栈上仍然分配在堆上?

assembly - 在堆栈帧中推送返回值

c++ - 检查 "cin"是否为字符串