cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

ankurdh
Journeyman III

Need help with anti aliasing this program

Hello, i've rendered a mandelbrot fractal. I'm not understanding how to bring about anti aliasing in it. I've used openGL. Can anyone please help me do it.

0 Likes
4 Replies
ankurdh
Journeyman III

Here is the code.I tried to use glHint(), and i tried to enable GL_POINT_SMOOTH and line smooth. But nothing worked.

 

//code

#include<GL/glut.h>
#include<stdlib.h>
#include<iostream>

#define CENTER_X -0.5
#define CENTER_Y 0.0
#define HEIGHT 3.0
#define WIDTH  3.0

#define ITERATIONS 100

#define N 700
#define M 700

float height = HEIGHT;
float width = WIDTH;
float cx = CENTER_X;
float cy = CENTER_Y;
int max = ITERATIONS;

int n = N;
int m = M;

GLubyte image;

typedef float complex[2];

void add(complex a, complex b, complex p){
    p[0] = a[0] + b[0];
    p[1] = a[1] + b[1];
}

void mult(complex a, complex b, complex p){
    p[0] = a[0] * b[0] - a[1]*b[1];
    p[1] = a[0] * b[1] + a[1]*b[0];
}

float mag2(complex a){
    return (a[0] * a[0] + a[1] * a[1]);   
}

void form (float a, float b, complex p){
    p[0] = a;
    p[1] = b;
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    int i , j , k ;
    float x, y ,v;
    complex c0,c,d;
   
    std::cout<<"Image calculation process is going on. . \n";
    std::cout<<"New image (height,width): ("<<height<<","<<width<<")\n";
    std::cout<<"About point (x,y): ("<<cx<<","<<cy<<")\n";
    for(i = 0 ; i < n ; i ++){
        for(j = 0 ; j < m ; j ++){
            x = i * (width/(n-1)) + cx - (width/2);
            y = j * (height/(m-1)) + cy - (height/2);
           
            form(0,0,c);
            form(x,y,c0);

            for(k = 0 ; k < max ; k ++){
                mult(c,c,d);
                add(d,c0,c);
                v = mag2(c);
                if(v > 4.0)
                    break;
            }

            if(v < 1.0)
                v = 0.0;
            else
                v = 1/(v*k);

            image = v*255;
        }   
    }   

    glDrawPixels(n,m,GL_COLOR_INDEX,GL_UNSIGNED_BYTE,image);
    std::cout<<"Image Rendered.\n";
    glutSwapBuffers();
    glFlush();
}

void reshape(int w, int h){
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0.0,0.0,(GLfloat)n,(GLfloat)m);
    glMatrixMode(GL_MODELVIEW);
        display();
}

void myInit(){
    float redMap[256],greenMap[256],blueMap[256];
    int i;
    glClearColor(1.0,1.0,1.0,1.0);

    for(i = 0 ; i < 256 ; i ++){
        redMap
= (rand()%256)/255.0;
        greenMap = (rand()%256)/255.0;//1.0-i/255.0;
        blueMap
= (rand()%256)/255.0;//(i)/255.0;
    }
    //srand(rand());
    glPixelMapfv(GL_PIXEL_MAP_I_TO_R, 256, redMap);
    glPixelMapfv(GL_PIXEL_MAP_I_TO_G, 256, greenMap);
    glPixelMapfv(GL_PIXEL_MAP_I_TO_B, 256, blueMap);
}

void idle(){
    height -= 0.1;
    width -= 0.1;
    glutPostRedisplay();
}

void mouseHandler(int button, int state, int x , int y){
    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
        float complexX = x * (width/(n-1)) + cx - (width/2);
        float complexY = y * (height/(m-1)) + cy - (height/2);
        //std::cout<<"Mouse Click position in complex plane: (x,y) = ("<<complexX<<","<<complexY<<")\n";
        cx = complexX;
        cy = complexY;
        std::cout<<"Zooming Initiated at point: (x,y) = ("<<cx<<","<<cy<<");\n";
        glutIdleFunc(idle);
    }
}

void printMandelbrotDetails(){
    std::cout<<"Complex Plane details:\nwidth: "<<WIDTH<<"\nheight: "<<HEIGHT<<std::endl;
    std::cout<<"Maximum iterations: "<<max<<"\n";
}

int main(int argc, char ** argv){
   
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(N,M);
    glutCreateWindow("Mandelbrot");

    printMandelbrotDetails();

    myInit();

    glEnable(GL_POINT_SMOOTH);
    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_POLYGON_SMOOTH);
    glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
    glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);

    glEnable(GL_BLEND);
    //glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMouseFunc(mouseHandler);
   
    glutMainLoop();
}

0 Likes

Hello.

From what I see, you are drawing the set into a pixel buffer in main memory and then blitting that buffer using OpenGL's 2D functionality.

There is no way that I know of, that enables you to anti-alias this. The way you're doing it, the pixels get drawn 1:1 the way you computed them.

The only solution in 2D I can think of, is to compute them anti-aliased: either by supersampling or filtering. Enabling line and point smoothing has no effect here - I guess that even fullscreen anti-aliasing wouldn't have any effect as well, though I have never tried that.

Please check the corresponding Wikipedia article, which should give you an idea of where to start.

Have a nice day.

Regards,

Rob

0 Likes

one way to antialias would be to jitter your drawpixels:

- use a additive blend function 

- modulate your drawpixel with a grey color (1/N)

- call drawpixels N times, with different start position

 

Pierre B.

0 Likes

Hi,

 

I am in the same problem.Me too need sone guide lines.Everyone is most welcomed.

 

Regards

0 Likes