2017-03-22 5 views
1

mandelbrot 세트를 렌더링해야하며 누군가 내 코드로 몇 가지 결함을 지적 할 수 있는지 궁금합니다. 출력 창에는 검은 색 화면이 나타납니다. 나는 내 mandelbrot 수학이 정확하다고 생각한다. 왜냐하면 동일한 코드를 사용하여 .manualbrot의 출력을했기 때문이다. 픽셀을 출력하는 데 사용하는 OpenGl 메서드와 관련이 있는가?OpenGl에서 mandelbrot을 렌더링

전체 코드 :

#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 단위로 픽셀 매핑을 제공하지 않습니다 ID는, 그것은 +/- (1,1입니다 , 1) 큐브. 당신은 지금 당장 테스트 할 수있는 위치에, 3 & 4 인수를 바꾸어해야 할 수도 있습니다

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

:

사용 glOrtho() 당신이 원하는 행렬을 얻을 수 있습니다.

+0

3 번째 및 4 번째 인수를 변경하면 아무런 효과가 없습니다. –

+0

이것은 완벽하게 작동했습니다. 아무 것도 조정하거나 조정할 필요조차 없었습니다. 감사! – Borzi

관련 문제