Reputation: 1241
I am trying to to make a Mandelbrot set on the fragment shader. I initially build a rectangle, and use the gl_FragCoords to calculate the algorithm. Here is the code:
#version 330 core
uniform float MaxIterations;
out vec4 pixelColor;
void main()
{
float real = gl_FragCoord.x;
float imag = gl_FragCoord.y;
float Creal = real;
float Cimag = imag;
float r2 = 0.0;
for (float iter = 0.0; iter < MaxIterations && r2 < 4.0; ++iter)
{
float tempreal = real;
real = (tempreal * tempreal) - (imag * imag) + Creal;
imag = 2.0 * tempreal * imag + Cimag;
r2 = (real * real) + (imag * imag);
}
vec3 color;
if (r2 < 4.0)
color = vec3(0.0f, 0.0f, 0.0f);
else
color = vec3(1.0f, 1.0f, 1.0f);
pixelColor = vec4(color, 1.0);
}
This code is actually kind of simplified and adapted version of what I found on the internet. I just assume the y axis as the imaginary and the x axis as the real part. Then I calculate the iterations until a max value passed by an uniform. It should show a black and white rectangle (white outside of the set, and black inside), but I just get a white rectangle. Does anybody knows what is wrong?
EDIT: to add a little bit more detail. Here is the vertices I used for the rectangle
GLfloat vertices[] = {
0.9f, 0.9f,
0.9f, -0.9f,
-0.9f, -0.9f,
-0.9f, 0.9f
};
and here is the vertex shader:
#version 330 core
layout (location = 0) in vec2 position;
void main() {
gl_Position = vec4(position, 0.0f, 1.0f);
}
Upvotes: 2
Views: 2605
Reputation: 1206
Isn't the pixel coordinates somewhat large input values for a mandlebrot set? Try creating a new output from the vertex shader representing the vertex coordinates and use that as a input in the fragment shader:
layout (location = 0) in vec2 position;
out vec2 coord;
void main() {
gl_Position = vec4(position, 0.0f, 1.0f);
coord = position.xy
}
And in the fragment shader:
#version 330 core
uniform float MaxIterations;
out vec4 pixelColor;
in vec2 coord;
void main()
{
float real = coord.x;
float imag = coord.y;
(...)
Upvotes: 2