Frames Overlap

Hello guys,
I m newcomer to OpenGL and so far I have learned on how to use GlDrawpixels(…). I succeeded in rendering 1 color (using GL_LUMINANCE). However, when I used GL_RGB for 256color, the animation frames overlap each other. It is weird because it worked fine for 1-color. Can someone give me any hint whats going on? Here is a bit of mycode.

 #include <GL/glut.h>
#include <GL/gl.h>
#include <math.h>		
#include <string.h>		

#define NS_WINSIZE 512

const float PI=3.14159265358979;

float XPoint(float,float, float,float, float,float);
float YPoint(float,float, float,float, float,float);
void fractal(float,float,int,const float);

unsigned char img1[NS_WINSIZE * NS_WINSIZE][3];

void display()
{
  glClear(GL_COLOR_BUFFER_BIT);

	for (unsigned int i = 0; i < 48; i++)
	{
		memset(img1,0, NS_WINSIZE * NS_WINSIZE);
		fractal(0,0,20,PI*i/24);
 		glDrawPixels(NS_WINSIZE, NS_WINSIZE, GL_RGB, GL_UNSIGNED_BYTE, img1);
	
		glFlush();
	}
}

int main(int argc, char** argv){
	glutInit(&argc, argv);
	glutInitWindowSize(NS_WINSIZE, NS_WINSIZE);
	glutCreateWindow("Fractal Viewer");
	glutDisplayFunc(display);
	glutMainLoop();

	return 0;
}

float XPoint(float xc,float yc, float theta,float scale, float x,float y){
  return ((x*cos(theta)-y*sin(theta))*scale+xc);
}

float YPoint(float xc,float yc, float theta,float scale, float x,float y){
  return ((x*sin(theta)+y*cos(theta))*scale+yc);
}

void fractal(float x, float y, int depth, const float ang) {
  if (depth==0) {

	int xc,yc;
	
	xc = int( (x-(-2)) * NS_WINSIZE/4);
	yc = int( (y-(-2)) * NS_WINSIZE/4);

	img1[NS_WINSIZE*yc+xc][0]=204;
	img1[NS_WINSIZE*yc+xc][1]=51;
	img1[NS_WINSIZE*yc+xc][2]=153;
	
  } else {
	
	  	fractal(XPoint(1,0,0+ang,.70710678,x,y),YPoint(1,0,0+ang,.70710678,x,y),depth-1,ang);
    	fractal(XPoint(-1,0,0-ang,.70710678,x,y),YPoint(-1,0,0-ang,.70710678,x,y),depth-1,ang);
  }
}
 

The colors blend works well. The only problem is overlapping frames.

Forgot to mention that the frames overlap while the they going down of Y-axis (which means animation keep going down instead of sitting at a same place until it ends).

I tried to run your code and it froze my system, then I realized it was just taking a really long time to complete :wink:

Interesting… what fractal is that? Anything famous?

When I get into a pickle like this, I usually sprinkle a few break points around in my code and run the debugger, so I can see the flow and what’s happening to my data as she runs.

Your imgl array is not well allocated.
First, it is a bi-dimensionnal array, while glDrawPixels expects a one-dimension pointer.
You should have imgl be allocated this way:

unsigned char img1[NS_WINSIZE * NS_WINSIZE * 3];

The memset looks like a waste of time, because you define all the values of the array anyway, and I don’t see any dependency between pixels in your fractal computation. So it can be called only once outside of the loop, it not at all. But I may miss something.
Anyway, my point is that once you moved to a single dimensioned array, then your memset line should look like this:

memset(img1,0, NS_WINSIZE * NS_WINSIZE * 3);

And finally, in the fractal() function, you should assign values this way:

img1[(NS_WINSIZE*yc+xc)*3]=204;
img1[(NS_WINSIZE*yc+xc)*3 + 1]=51;
img1[(NS_WINSIZE*yc+xc)*3 + 2]=153;

Also, you have to know that glDrawPixels is usually optimized for 32-bit format, rather than 24-bit, so you may consider using GL_RGBA or GL_BGRA instead, replacing 3 by 4 and leaving the alpha value as 255