Sign in to follow this  
EonStrife

OpenGL Mandelbrot Set, help !

Recommended Posts

EonStrife    122
Hi, I tried to make a simple application generating Mandelbrot set. I used OpenGL (as graphic API), glee (for the extension), and glfw (for the framework), VC++ 2005 (compiler). When I ran the program, I only saw blank screen with a faint large black box hanging in the upper left. Can anyone tell me what mistake I made and give correction ? Here's the source code :
[source="C"]
#include "stdafx.h"
#include "4.h"
#include <GL/glee.h>
#include <GL/glfw.h>
#include <cmath>
using namespace std;

//Define
#define MAX_ITERATION 1000
#define RES_X	1024
#define RES_Y	768
//Global Variables
bool running=true;
bool keyboardKeys[255]={false};
int	g_width;
int g_height;
GLfloat onePix[3];
GLfloat screen[sizeof(GLfloat)*3*RES_X*RES_Y];

struct coordinate
{
	GLdouble x;
	GLdouble y;
}upperLeft, lowerRight;


//Callbacks
void keyCallBack(int key, int state)
{
	if(state==GLFW_PRESS)
		keyboardKeys[key]=true;
	else if(state==GLFW_RELEASE)
		keyboardKeys[key]=false;
}

void calcHSV(int iteration, GLdouble x, GLdouble y)
{
/*
	GLint Hi;
	GLdouble H, f, p, q, t, h, s, v;
	GLdouble xn;
	h=((GLdouble)iteration/(GLdouble)MAX_ITERATION)*360.0f;
	
	xn=x/(double)pow((double)(x*x+y*y),(double)0.5f);
	s=acos(xn)/360.0f;

	v=pow((double)x*x+y*y,(double)0.5f)/1.41421356238f;
	Hi=((int)floor(h/60.0f))%6;
	f=h/360.0f-Hi;
	p=v*(1.0f-s);
	q=v*(1.0f-f*s);
	t=v*(1.0f-(1.0f-f)*s);
	switch(Hi)
	{
	case 0 : onePix[0]=v; onePix[1]=t; onePix[2]=p;break;
	case 1 : onePix[0]=q; onePix[1]=v; onePix[2]=p;break;
	case 2 : onePix[0]=p; onePix[1]=v; onePix[2]=t;break;
	case 3 : onePix[0]=p; onePix[1]=q; onePix[2]=v;break;
	case 4 : onePix[0]=t; onePix[1]=p; onePix[2]=v;break;
	case 5 : onePix[0]=v; onePix[1]=p; onePix[2]=q;break;
	}
	H=0;
*/
	onePix[0]=(GLdouble)iteration/(GLdouble)MAX_ITERATION;
	onePix[1]=(GLdouble)iteration/(GLdouble)MAX_ITERATION;
	onePix[2]=(GLdouble)iteration/(GLdouble)MAX_ITERATION;
}
void gambarUlang()
{
	int i, j, iteration;
	GLdouble x0, y0, x2, y2, x_t, y_t;
	GLdouble step_x, step_y;
	step_x=(lowerRight.x-upperLeft.x)/((GLdouble)g_width);
	step_y=(upperLeft.y-lowerRight.y)/((GLdouble)g_height);
	for(i=0; i<g_width; i++)
	{
		for(j=0; j<g_height; j++)
		{
			x0=((GLdouble)i)*step_x+upperLeft.x;
			y0=upperLeft.y-((GLdouble)j)*step_y;
			x2 = x0 * x0;
			y2 = y0 * y0;
			iteration=0;
			while((x2 + y2 < 2.0f)&& (iteration<MAX_ITERATION))
			{
				x_t=x2+x0;
				y_t=y2+y0;
				x2=x_t*x_t;
				y2=y_t*y_t;
				iteration++;
			}

			if(iteration==MAX_ITERATION)
			{
				onePix[0]=0.0f;onePix[1]=0.0f;onePix[2]=0.0f;
			}
			else
				calcHSV(iteration, x0, y0);

			glWindowPos2i(i, j);
			glDrawPixels(1, 1, GL_RGB, GL_FLOAT, &onePix);

		}
	}
	
	glReadPixels(0, 0, g_width, g_height, GL_RGB, GL_FLOAT, &screen);
}

void WindowResizeCallback(int width, int height)
{
	if (height==0)									
	{
		height=1;								
	}

	glViewport(0,0,width,height);						

	glMatrixMode(GL_PROJECTION);						
	glLoadIdentity();								


	gluOrtho2D(0.0f, (GLdouble) width, 0.0f, (GLdouble) height);
	glMatrixMode(GL_MODELVIEW);							
	glLoadIdentity();								
	g_width=width;
	g_height=height;
	gambarUlang();
}

void MousePosCallback(int x, int y)
{
}

void MouseButtonCallback(int button, int action)
{
}

int WindowsCloseCallback()
{	
	running=false;
	return GL_TRUE;
}

void initGL()
{
//	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
//	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
//	glClearDepth(1.0f);									// Depth Buffer Setup
//	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
//	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
														// Initialization Went OK

}
void DrawGLScene()
{
//	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glLoadIdentity();									// Reset The Current Modelview Matrix
														// Keep Going
	glWindowPos2i(0, 0);
	glDrawPixels(g_width, g_height, GL_RGB, GL_FLOAT, &screen);
}

void keyboardProcess()
{
	if(keyboardKeys[GLFW_KEY_ESC]==true) running=false;
	if(keyboardKeys[GLFW_KEY_SPACE]==true) gambarUlang();
}

//Main
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{

	glfwInit();
	
	if(!glfwOpenWindow(800, 600, 8, 8, 8, 0, 0, 0, GLFW_WINDOW))
	{
		glfwTerminate();
		return 0;
	}

	////////////////
	upperLeft.x=-1.0f;
	upperLeft.y=1.0f;
	lowerRight.x=1.0f;
	lowerRight.y=-1.0f;
	////////////////
	glfwSetWindowTitle("Mandelbrot Set");
	glfwSetWindowSizeCallback(&WindowResizeCallback);
	glfwSetKeyCallback(&keyCallBack);
	glfwSetMousePosCallback(&MousePosCallback);
	glfwSetMouseButtonCallback(&MouseButtonCallback);
	glfwSetWindowCloseCallback(&WindowsCloseCallback);

	initGL();
	while(running)
	{
		DrawGLScene();
		glfwSwapBuffers();
		keyboardProcess();
	}

	glfwTerminate();
	return 0;
}



Thanks :)

Share this post


Link to post
Share on other sites
nmi    978
Was WindowResizeCallback() really called ?

When calculating a mandelbrot set, you have to test if the complex number is in a circle of radius 2 in the complex plane (i.e. if 'x^2 + y^2 < 4'), but you check if 'x^2 + y^2 < 2'. Also the initial value of z in the mandelbrot formula 'z = z^2 + c' is zero and c is the position of the pixel you want to calculate the color for.

Calculation with complex numbers is also different, since you would write (to calculate: z = z^2 + c):

t = zr*zr - zi*zi;
zi = 2*zr*zi;
zr = t;
zr += cr;
zi += ci;


[Edited by - nmi on December 14, 2006 8:48:01 AM]

Share this post


Link to post
Share on other sites
EonStrife    122
Thanks nmi,
I have made the corrections. However, the mandelbrot set still doesn't look right :
http://img.photobucket.com/albums/v708/EonStrife/mandelbrot1.jpg

Here is the corrected version of the source:
[source="c"]
// 4.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "4.h"
#include <GL/glee.h>
#include <GL/glfw.h>
#include <cmath>
using namespace std;

//Define
#define MAX_ITERATION 18
#define RES_X 1024
#define RES_Y 768
//Global Variables
bool running=true;
bool keyboardKeys[255]={false};
int g_width;
int g_height;
GLfloat onePix[3];
GLfloat screen[sizeof(GLfloat)*3*RES_X*RES_Y];

struct coordinate
{
GLdouble x;
GLdouble y;
}upperLeft, lowerRight;



void calcHSV(int iteration, GLdouble x, GLdouble y)
{
/*
GLint Hi;
GLdouble H, f, p, q, t, h, s, v;
GLdouble xn;
GLdouble mu;
mu=(GLdouble)iteration+1.0-log((double)log((double)(x*x+y*y)))/log(2.0);

//h=((GLdouble)iteration/(GLdouble)MAX_ITERATION)*360.0f;
h=mu*360.0;

xn=x/(double)pow((double)(x*x+y*y),(double)0.5);
s=acos(xn)/360.0;

v=pow((double)x*x+y*y,(double)0.5)/1.41421356238;
Hi=((int)floor(h/60.0))%6;
f=h/360.0-Hi;
p=v*(1.0-s);
q=v*(1.0-f*s);
t=v*(1.0-(1.0-f)*s);
switch(Hi)
{
case 0 : onePix[0]=v; onePix[1]=t; onePix[2]=p;break;
case 1 : onePix[0]=q; onePix[1]=v; onePix[2]=p;break;
case 2 : onePix[0]=p; onePix[1]=v; onePix[2]=t;break;
case 3 : onePix[0]=p; onePix[1]=q; onePix[2]=v;break;
case 4 : onePix[0]=t; onePix[1]=p; onePix[2]=v;break;
case 5 : onePix[0]=v; onePix[1]=p; onePix[2]=q;break;
}
H=0;
*/

GLdouble mu;
mu=(GLdouble)iteration+1.0-log((double)log((double)(x*x+y*y)))/log(2.0);
onePix[0]=mu;
onePix[1]=mu;
onePix[2]=mu;
//onePix[0]=1.0f-(GLdouble)iteration/(GLdouble)MAX_ITERATION;
//onePix[1]=1.0f-(GLdouble)iteration/(GLdouble)MAX_ITERATION;
//onePix[2]=1.0f-(GLdouble)iteration/(GLdouble)MAX_ITERATION;
}
void gambarUlang()
{
int i, j, iteration;
GLdouble x0, y0, x2, y2, x, y;
GLdouble step_x, step_y;
step_x=(lowerRight.x-upperLeft.x)/((GLdouble)g_width);
step_y=(upperLeft.y-lowerRight.y)/((GLdouble)g_height);
for(i=0; i<g_width; i++)
{
for(j=0; j<g_height; j++)
{
x0=((GLdouble)i)*step_x+upperLeft.x;
y0=upperLeft.y-((GLdouble)j)*step_y;
x=0;y=0;x2=0;y2=0;
iteration=0;
while((x2 + y2 < 4.0)&& (iteration<MAX_ITERATION))
{
y=2*x*y+y0;
x=x*x-y*y+x0;
x2=x*x;
y2=y*y;
iteration++;
}

if(iteration==MAX_ITERATION)
{
onePix[0]=0.0f;onePix[1]=0.0f;onePix[2]=0.0f;
}
else
calcHSV(iteration, x, y);

glWindowPos2i(i, j);
glDrawPixels(1, 1, GL_RGB, GL_FLOAT, onePix);

}
}

glReadPixels(0, 0, g_width, g_height, GL_RGB, GL_FLOAT, screen);
}

//Callbacks
void keyCallBack(int key, int state)
{
if(state==GLFW_PRESS)
keyboardKeys[key]=true;
else if(state==GLFW_RELEASE)
keyboardKeys[key]=false;
}

void WindowResizeCallback(int width, int height)
{
if (height==0)
height=1;


glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluOrtho2D(0.0f, (GLdouble) width, 0.0f, (GLdouble) height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
g_width=width;
g_height=height;
gambarUlang();
}

void MousePosCallback(int x, int y)
{
}

void MouseButtonCallback(int button, int action)
{
}

int WindowsCloseCallback()
{
running=false;
return GL_TRUE;
}

void initGL()
{

}

void DrawGLScene()
{

glLoadIdentity();
glWindowPos2i(0, 0);
glDrawPixels(g_width, g_height, GL_RGB, GL_FLOAT, screen);
}

void keyboardProcess()
{
if(keyboardKeys[GLFW_KEY_ESC]==true) running=false;
if(keyboardKeys[GLFW_KEY_SPACE]==true) gambarUlang();
}

//Main
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{

glfwInit();

if(!glfwOpenWindow(800, 600, 8, 8, 8, 0, 0, 0, GLFW_WINDOW))
{
glfwTerminate();
return 0;
}

////////////////
upperLeft.x=-1.0;
upperLeft.y=1.0;
lowerRight.x=1.0;
lowerRight.y=-1.0;
////////////////
glfwSetWindowTitle("Mandelbrot Set");
glfwSetWindowSizeCallback(&WindowResizeCallback);
glfwSetKeyCallback(&keyCallBack);
glfwSetMousePosCallback(&MousePosCallback);
glfwSetMouseButtonCallback(&MouseButtonCallback);
glfwSetWindowCloseCallback(&WindowsCloseCallback);

initGL();
while(running)
{
DrawGLScene();
glfwSwapBuffers();
keyboardProcess();
}

glfwTerminate();
return 0;
}


Share this post


Link to post
Share on other sites
nmi    978
This one won't work either:

while((x2 + y2 < 4.0)&& (iteration<MAX_ITERATION))
{
y=2*x*y+y0;
x=x*x-y*y+x0;
x2=x*x;
y2=y*y;
iteration++;
}



Try this instead:

x = y = x2 = y2 = iteration = 0;
while((x2 + y2 < 4.0)&& (iteration++ < MAX_ITERATION))
{
x2 = x*x;
y2 = y*y;
y = 2*x*y + y0;
x = x2 - y2 + x0;
}


This also avoids a temporary by calculating the squares and reusing them for the calculation of the next x.

Share this post


Link to post
Share on other sites
EonStrife    122
BTW, my simple mandelbrot program is available at :
http://www.geocities.com/eon_strife/Mandelbrot.zip

Left-click -> to zoom
Right-click -> to cancel zoom
backspace -> to return to previous zoom state

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

  • Popular Now