C++ 在OpenGl中渲染mandelbrot集

C++ 在OpenGl中渲染mandelbrot集,c++,opengl,complex-numbers,freeglut,mandelbrot,C++,Opengl,Complex Numbers,Freeglut,Mandelbrot,我必须渲染mandelbrot集,我想知道是否有人能指出我代码中的一些缺陷-目前,输出窗口只是显示一个黑屏。我认为我的mandelbrot数学是正确的,因为我使用了相同的代码来输出mandelbrot的.tga-这与我用来输出像素的OpenGl方法有关吗 完整代码: #include <Windows.h> #include <GL\glew.h> #include <GL\freeglut.h> #include <iostream> #incl

我必须渲染mandelbrot集,我想知道是否有人能指出我代码中的一些缺陷-目前,输出窗口只是显示一个黑屏。我认为我的mandelbrot数学是正确的,因为我使用了相同的代码来输出mandelbrot的.tga-这与我用来输出像素的OpenGl方法有关吗

完整代码:

#include <Windows.h>
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <complex>
#include <fstream>
#include <thread>
#include <mutex>
#include <vector>
#include <Windows.h>

// Import things we need from the standard library
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::complex;
using std::cout;
using std::endl;
using std::ofstream;
// ...other useful includes
using std::cout;
using std::endl;
using std::thread;
using std::mutex;
using std::lock;
using std::unique_lock;
using std::vector;

const int width = 600, height = 600; // window size
int windowID;

// The number of times to iterate before we assume that a point isn't in the
// Mandelbrot set.
// (You may need to turn this up if you zoom further into the set.)
const int MAX_ITERATIONS = 500;

bool fullScreen = false;
bool need_to_draw = true;

//****************************************

// Render the Mandelbrot set into the image array.
// The parameters specify the region on the complex plane to plot.
void compute_mandelbrot(double left, double right, double top, double bottom)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the screen buffer
    glBegin(GL_POINTS); // start drawing in single pixel mode
    for (int y = 0; y < height; ++y)
    {
        for (int x = 0; x < width; ++x)
        {
            // Work out the point in the complex plane that
            // corresponds to this pixel in the output image.
            complex<double> c(left + (x * (right - left) / width),
                top + (y * (bottom - top) / height));

            // Start off z at (0, 0).
            complex<double> z(0.0, 0.0);

            // Iterate z = z^2 + c until z moves more than 2 units
            // away from (0, 0), or we've iterated too many times.
            int iterations = 0;
            while (abs(z) < 2.0 && iterations < MAX_ITERATIONS)
            {
                z = (z * z) + c;

                ++iterations;
            }

            if (iterations == MAX_ITERATIONS)
            {
                glColor3f(1.0, 0.0, 0.0); // Set color to draw mandelbrot
                // z didn't escape from the circle.
                // This point is in the Mandelbrot set.
                glVertex2i(x, y);
            }
            else
            {
                glColor3f(0.0, 0.0, 0.0); //Set pixel to black
                // z escaped within less than MAX_ITERATIONS
                // iterations. This point isn't in the set.
                glVertex2i(x, y);
            }
        }
    }
    glEnd();
    glutSwapBuffers();
    need_to_draw = false;
}

int main(int argc, char** argv) 
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    GLsizei windowX = (glutGet(GLUT_SCREEN_WIDTH) - width) / 2;
    GLsizei windowY = (glutGet(GLUT_SCREEN_HEIGHT) - height) / 2;
    glutInitWindowPosition(windowX, windowY);
    glutInitWindowSize(width, height);
    windowID = glutCreateWindow("Mandelbrot");

    if (need_to_draw)
    {
        compute_mandelbrot(-2.0, 1.0, 1.125, -1.125);
    }

    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glViewport(0, 0, (GLsizei)width, (GLsizei)height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glutMainLoop();

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//从标准库导入我们需要的东西
使用std::chrono::duration\u cast;
使用std::chrono::毫秒;
使用std::complex;
使用std::cout;
使用std::endl;
使用std::of流;
//…其他有用的包括
使用std::cout;
使用std::endl;
使用std::线程;
使用std::mutex;
使用std::lock;
使用std::unique_锁;
使用std::vector;
常数int宽度=600,高度=600;//窗口大小
intwindowid;
//在我们假设某个点不在
//曼德布罗特集。
//(如果进一步放大集合,可能需要将其放大。)
const int MAX_迭代次数=500;
bool全屏=假;
布尔需要绘制=真;
//****************************************
//将Mandelbrot集渲染到图像数组中。
//这些参数指定复杂平面上要打印的区域。
void compute_mandelbrot(左双、右双、上双、下双)
{
glClear(GL_颜色_缓冲区_位| GL_深度_缓冲区_位);//清除屏幕缓冲区
glBegin(GL_点);//以单像素模式开始绘图
对于(int y=0;y
标识
GL_投影
矩阵不会像您假设的那样为您提供1:1单位到像素的映射,它是+/-(1,1,1)立方体

使用
glOrtho()
获取所需的矩阵:

glOrtho( 0, width, 0, height, -1, 1 );
总而言之:

#include <GL/glut.h>

#include <complex>
using std::complex;

// Render the Mandelbrot set into the image array.
// The parameters specify the region on the complex plane to plot.
void compute_mandelbrot( double left, double right, double top, double bottom )
{
    // The number of times to iterate before we assume that a point isn't in the
    // Mandelbrot set.
    // (You may need to turn this up if you zoom further into the set.)
    const int MAX_ITERATIONS = 500;

    const int width = glutGet( GLUT_WINDOW_WIDTH );
    const int height = glutGet( GLUT_WINDOW_HEIGHT );

    glBegin( GL_POINTS ); // start drawing in single pixel mode
    for( int y = 0; y < height; ++y )
    {
        for( int x = 0; x < width; ++x )
        {
            // Work out the point in the complex plane that
            // corresponds to this pixel in the output image.
            complex<double> c( left + ( x * ( right - left ) / width ),
                top + ( y * ( bottom - top ) / height ) );

            // Start off z at (0, 0).
            complex<double> z( 0.0, 0.0 );

            // Iterate z = z^2 + c until z moves more than 2 units
            // away from (0, 0), or we've iterated too many times.
            int iterations = 0;
            while( abs( z ) < 2.0 && iterations < MAX_ITERATIONS )
            {
                z = ( z * z ) + c;

                ++iterations;
            }

            if( iterations == MAX_ITERATIONS )
            {
                glColor3f( 1.0, 0.0, 0.0 ); // Set color to draw mandelbrot
                // z didn't escape from the circle.
                // This point is in the Mandelbrot set.
                glVertex2i( x, y );
            }
            else
            {
                glColor3f( 0.0, 0.0, 0.0 ); //Set pixel to black
                // z escaped within less than MAX_ITERATIONS
                // iterations. This point isn't in the set.
                glVertex2i( x, y );
            }
        }
    }
    glEnd();
}

void display()
{
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    glClear( GL_COLOR_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    const int width = glutGet( GLUT_WINDOW_WIDTH );
    const int height = glutGet( GLUT_WINDOW_HEIGHT );
    glOrtho( 0, width, 0, height, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    compute_mandelbrot( -2.0, 1.0, 1.125, -1.125 );
    glutSwapBuffers();
}

int main( int argc, char** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
    glutInitWindowSize( 300, 300 );
    glutCreateWindow( "Mandelbrot" );
    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}
#包括
#包括
使用std::complex;
//将Mandelbrot集渲染到图像数组中。
//这些参数指定复杂平面上要打印的区域。
void compute_mandelbrot(左双、右双、上双、下双)
{
//在我们假设某个点不在
//曼德布罗特集。
//(如果进一步放大集合,可能需要将其放大。)
const int MAX_迭代次数=500;
const int width=glutGet(GLUT\u窗口\u宽度);
const int height=glutGet(GLUT\u窗口\u高度);
glBegin(GL_点);//以单像素模式开始绘图
对于(int y=0;y