2017-03-22 3 views
1

マンデルブロセットをレンダリングしなければならず、誰かが自分のコードでいくつかの欠陥を指摘できるかどうか疑問に思っていました - 現時点では、出力ウィンドウには黒い画面が表示されます。私はmandelbrotの数学が正しいと思います。なぜなら、同じコードを使ってマンデルブロの.tgaを出力したからです。それは、ピクセルを出力するために使用しているOpenGlメソッドと関係がありますか?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; 
} 
+0

これはおそらくここでOpenGLを使用するのに最も非効率な方法です。 –

+1

@DietrichEpp:ええと、これらのピクセルプロッタは2-3ヶ月間表示されます。 – genpfault

+0

その人のために申し訳ありません^^ ...あなたには別の方法がありますか? – Borzi

答えて

1

GL_PROJECTION行列はあなたが想定しているように1対1ユニット対ピクセルのマッピングを与えないアイデンティティは、それが+/-(1,1です、1)立方体。あなたがいない今テストする位置に、第三&第四引数を転置する必要があるかもしれません

glOrtho(0, width, 0, height, -1, 1); 

使用glOrtho()は、あなたがしたい行列を取得します。

+0

第3引数と第4引数を入れ替えても効果はありません。 –

+0

これは完全に機能しました。何も調整/調整する必要はありませんでした。ありがとう! – Borzi

関連する問題