std::vector<Point> poisson_sample(double height, double width, int new_points_count, double octaves, double persistence, double scale, double low, double high, double seed) {
  double cellsize = high / sqrt(2.0);
  Grid<Point> grid(ceil(height / cellsize), ceil(width / cellsize));
  RandomQueue<Point> processing_list;
  std::vector<Point> output_list;
  Point p, q;
  double min_dist;

  Point first(random_real(height), random_real(width));
  processing_list.push(first);
  output_list.push_back(first);

  grid.add(image_to_grid(first, cellsize), first);

  while (!processing_list.empty()) {
    p = processing_list.pop();
    for (int i = 0; i < new_points_count; ++i) {
      q = generateRandomPointAround(p, min_dist);
      min_dist = scaled_octave_noise(octaves, persistence, scale, low, high, q.x, q.y, seed);
      if (in_rectangle(height, width, q) && !in_neighborhood(grid, q, min_dist, cellsize, height, width)) {
        processing_list.push(q);
        output_list.push_back(q);
        grid.add(image_to_grid(q, cellsize), q);
      }
    }
  }

  return output_list;
}
void calculateNoise(float *normalWithNoisePointer, float *normalPointer) {	
	//noise values calculated
	float noise = scaled_octave_noise(8, 0.75, 2, -1, 1, normalPointer[0], normalPointer[1], normalPointer[2]);
	normalWithNoisePointer[3] = 1;

	/*//normals are bumped according to the sin value of the noise
	normalWithNoisePointer[0] = normalPointer[0] + sin(noise);
	normalWithNoisePointer[1] = normalPointer[1] + sin(noise);
	normalWithNoisePointer[2] = normalPointer[2] + sin(noise);*/
	
	
	//normals are bumped according to the value of the noise
	/*normalWithNoisePointer[0] = normalPointer[0] + noise;
	normalWithNoisePointer[1] = normalPointer[1] + noise;
	normalWithNoisePointer[2] = normalPointer[2] + noise;*/
	

	//normals without being bumped
	normalWithNoisePointer[0] = normalPointer[0];
	normalWithNoisePointer[1] = normalPointer[1];
	normalWithNoisePointer[2] = normalPointer[2];
}
Esempio n. 3
0
void GzRasterize(GzRender *render,GzCoord *imageV, GzCoord *imageN,GzCoord *screenV,GzTextureIndex *textureC)
{
	
	float xMin,xMax,yMin,yMax;
	float a1,b1,c1,a2,b2,c2,a3,b3,c3;
	float aX,bY,cZ,d;
	GzCoord v0,v1,v2;
	GzEdge edge[3];
	
	GzCoord tempCoord1, tempCoord2;

	// gourad shading values
	GzColor cpA,cpB,cpC,cpD;
	
	// phong shading values
	GzCoord imageVa, imageVb, imageVc, imageVd; //image vertices
	GzCoord imageNa, imageNb, imageNc, imageNd; //image normals

	GzTextureIndex texCPa,texCPb,texCPc,texCPd;

	//Find xmin,xmax,ymin and ymax to obtain bounding rectangle for the triangle
	xMin = min(screenV[0][X],min(screenV[1][X],screenV[2][X]));
	yMin = min(screenV[0][Y],min(screenV[1][Y],screenV[2][Y]));

	xMax = max(screenV[0][X],max(screenV[1][X],screenV[2][X]));
	yMax = max(screenV[0][Y],max(screenV[1][Y],screenV[2][Y]));

	xMin = max((int)(ceil(xMin)),0);
	yMin = max((int)(ceil(yMin)),0);

	xMax = min((int)(floor(xMax)),render->display->xres);
	yMax = min((int)(floor(yMax)),render->display->yres);

	
	// Setting vertices of the triangle and the edges of the triangle in anti-clockwise direction for processing

	v0[X] = screenV[0][X];
	v0[Y] = screenV[0][Y];
	v0[Z] = screenV[0][Z];

	v1[X] = screenV[1][X];
	v1[Y] = screenV[1][Y];
	v1[Z] = screenV[1][Z];

	v2[X] = screenV[2][X];
	v2[Y] = screenV[2][Y];
	v2[Z] = screenV[2][Z];

	//edge 0 v0 -> v2
	edge[0].start[X] = v0[X];
	edge[0].start[Y] = v0[Y];
	edge[0].start[Z] = v0[Z];
		
	edge[0].end[X] = v2[X];
	edge[0].end[Y] = v2[Y];
	edge[0].end[Z] = v2[Z];
	
	//edge 1 v2 -> v1
	edge[1].start[X] = v2[X];
	edge[1].start[Y] = v2[Y];
	edge[1].start[Z] = v2[Z];
	
	edge[1].end[X] = v1[X];
	edge[1].end[Y] = v1[Y];
	edge[1].end[Z] = v1[Z];
	
	//edge 2 v1 -> v0
	edge[2].start[X] = v1[X];
	edge[2].start[Y] = v1[Y];
	edge[2].start[Z] = v1[Z];
	
	edge[2].end[X] = v0[X];
	edge[2].end[Y] = v0[Y];
	edge[2].end[Z] = v0[Z];
	
	/* Computing Line Equation coefficients */

	a1 = edge[0].end[Y] - edge[0].start[Y];
	b1 = edge[0].start[X] - edge[0].end[X];
	c1 = (((edge[0].end[X] - edge[0].start[X]) * edge[0].start[Y]) - ((edge[0].end[Y] - edge[0].start[Y]) * edge[0].start[X]));

	a2 = edge[1].end[Y] - edge[1].start[Y];
	b2 = edge[1].start[X] - edge[1].end[X];
	c2 = (((edge[1].end[X] - edge[1].start[X]) * edge[1].start[Y]) - ((edge[1].end[Y] - edge[1].start[Y]) * edge[1].start[X]));

	a3 = edge[2].end[Y] - edge[2].start[Y];
	b3 = edge[2].start[X] - edge[2].end[X];
	c3 = (((edge[2].end[X] - edge[2].start[X]) * edge[2].start[Y]) - ((edge[2].end[Y] - edge[2].start[Y]) * edge[2].start[X]));
		
	/*	Finding Crossproduct of 2 edges to obtain the plane equation for the triangle	*/
	aX = (((edge[0].end[Y] - edge[0].start[Y]) * (edge[1].end[Z] - edge[1].start[Z])) - ((edge[0].end[Z] - edge[0].start[Z]) * (edge[1].end[Y] - edge[1].start[Y])));
	bY = -(((edge[0].end[X] - edge[0].start[X]) * (edge[1].end[Z] - edge[1].start[Z])) - ((edge[0].end[Z] - edge[0].start[Z]) * (edge[1].end[X] - edge[1].start[X])));
	cZ = (((edge[0].end[X] - edge[0].start[X]) * (edge[1].end[Y] - edge[1].start[Y])) - ((edge[0].end[Y] - edge[0].start[Y]) * (edge[1].end[X] - edge[1].start[X])));
	d = -(aX * screenV[0][X] + bY * screenV[0][Y] + cZ * screenV[0][Z]);
		
	/*	setting values for pixels which will be constant through the interpolation calculation	*/
	tempCoord1[X] = edge[0].end[X] - edge[0].start[X];
	tempCoord2[X] = edge[1].end[X] - edge[1].start[X];
	
	tempCoord1[Y] = edge[0].end[Y] - edge[0].start[Y];
	tempCoord2[Y] = edge[1].end[Y] - edge[1].start[Y];

	//For Gourad Shading
	if(render->interp_mode == GZ_COLOR)
	{	
		//calculate the color for each vertex and set up the interpolation plane coefficients
		GzColor vertexClr[3];
		int dummy1 = 0,dummy2 = 0;	
		GzShade(render,imageV[0],imageN[0],vertexClr[0],textureC[0],dummy1,dummy2);
		GzShade(render,imageV[1],imageN[1],vertexClr[1],textureC[1],dummy1,dummy2);
		GzShade(render,imageV[2],imageN[2],vertexClr[2],textureC[2],dummy1,dummy2);		

		tempCoord1[Z] = vertexClr[2][0] - vertexClr[0][0];
		tempCoord2[Z] = vertexClr[1][0] - vertexClr[2][0];
												
		cpA[0] = tempCoord1[Y] * tempCoord2[Z] - tempCoord1[Z] * tempCoord2[Y];
		cpB[0] = tempCoord1[Z] * tempCoord2[X] - tempCoord1[X] * tempCoord2[Z];
		cpC[0] = tempCoord1[X] * tempCoord2[Y] - tempCoord1[Y] * tempCoord2[X];
		cpD[0] = -(cpA[0] * edge[0].start[X] + cpB[0] * edge[0].start[Y] + cpC[0] * vertexClr[0][0]);

		tempCoord1[Z] = 0;
		tempCoord2[Z] = 0;

		tempCoord1[Z] = vertexClr[2][1] - vertexClr[0][1];
		tempCoord2[Z] = vertexClr[1][1] - vertexClr[2][1];
												
		cpA[1] = tempCoord1[Y] * tempCoord2[Z] - tempCoord1[Z] * tempCoord2[Y];
		cpB[1] = tempCoord1[Z] * tempCoord2[X] - tempCoord1[X] * tempCoord2[Z];
		cpC[1] = tempCoord1[X] * tempCoord2[Y] - tempCoord1[Y] * tempCoord2[X];
		cpD[1] = -(cpA[1] * edge[0].start[X] + cpB[1] * edge[0].start[Y] + cpC[1] * vertexClr[0][1]);

		tempCoord1[Z] = 0;
		tempCoord2[Z] = 0;

		tempCoord1[Z] = vertexClr[2][2] - vertexClr[0][2];
		tempCoord2[Z] = vertexClr[1][2] - vertexClr[2][2];
												
		cpA[2] = tempCoord1[Y] * tempCoord2[Z] - tempCoord1[Z] * tempCoord2[Y];
		cpB[2] = tempCoord1[Z] * tempCoord2[X] - tempCoord1[X] * tempCoord2[Z];
		cpC[2] = tempCoord1[X] * tempCoord2[Y] - tempCoord1[Y] * tempCoord2[X];
		cpD[2] = -(cpA[2] * edge[0].start[X] + cpB[2] * edge[0].start[Y] + cpC[2] * vertexClr[0][2]);

		tempCoord1[Z] = 0;
		tempCoord2[Z] = 0;
			
	}
	
	/*	For phong shading  */
	else if(render->interp_mode == GZ_NORMALS)
	{	
		/* calculate interpolation plane coefficient for planes and normals	*/
		for(int i = 0; i < 3; i++)
		{
			/* For vertices	*/
			tempCoord1[Z] = imageV[2][i] - imageV[0][i];
			tempCoord2[Z] = imageV[1][i] - imageV[2][i];						
					
			imageVa[i] = tempCoord1[Y] * tempCoord2[Z] - tempCoord1[Z] * tempCoord2[Y];
			imageVb[i] = tempCoord1[Z] * tempCoord2[X] - tempCoord1[X] * tempCoord2[Z];
			imageVc[i] = tempCoord1[X] * tempCoord2[Y] - tempCoord1[Y] * tempCoord2[X];
			imageVd[i] = -(imageVa[i] * edge[0].start[X] + imageVb[i] * edge[0].start[Y] + imageVc[i] * imageV[0][i]);

			/* For normals	*/
			tempCoord1[Z] = imageN[2][i] - imageN[0][i];
			tempCoord2[Z] = imageN[1][i] - imageN[2][i];
		
			imageNa[i] = tempCoord1[Y] * tempCoord2[Z] - tempCoord1[Z] * tempCoord2[Y];
			imageNb[i] = tempCoord1[Z] * tempCoord2[X] - tempCoord1[X] * tempCoord2[Z];
			imageNc[i] = tempCoord1[X] * tempCoord2[Y] - tempCoord1[Y] * tempCoord2[X];
			imageNd[i] = -(imageNa[i] * edge[0].start[X] + imageNb[i] * edge[0].start[Y] + imageNc[i] * imageN[0][i]);
		}

		GzTextureIndex perspTextC[3];
		float screenSpaceZ[3];
			
		for(int i = 0; i < 3; i++)
		{			
			screenSpaceZ[i] = -(aX * screenV[i][X] + bY * screenV[i][Y] + d)/cZ;
			//transform from image space to perspective space by warping 
			perspTextC[i][U] = textureC[i][U] / ((screenSpaceZ[i]/(INT_MAX - screenSpaceZ[i])) + 1);
			perspTextC[i][V] = textureC[i][V] / ((screenSpaceZ[i]/(INT_MAX - screenSpaceZ[i])) + 1);						

		}

		
		for( int i = 0; i < 2; i++ )
		{
			// set up interpolation coefficients for perspective space texture coordinate
			tempCoord1[Z] = perspTextC[2][i] - perspTextC[0][i];
			tempCoord2[Z] = perspTextC[1][i] - perspTextC[2][i];
									
			texCPa[i] = tempCoord1[Y] * tempCoord2[Z] - tempCoord1[Z] * tempCoord2[Y];
			texCPb[i] = tempCoord1[Z] * tempCoord2[X] - tempCoord1[X] * tempCoord2[Z];
			texCPc[i] = tempCoord1[X] * tempCoord2[Y] - tempCoord1[Y] * tempCoord2[X];
			texCPd[i] = -(texCPa[i] * edge[0].start[X] + texCPb[i] * edge[0].start[Y] + texCPc[i] * perspTextC[0][i]); 			

		}

				
	}
	
	int zMax = 0;

	for( int y = yMin; y <= yMax; y++ )
	{
		for( int x = xMin; x <= xMax; x++ )
		{			
			if((a1 * x + b1 * y + c1 > 0 && a2 * x + b2 * y + c2 > 0 && a3 * x + b3 * y + c3 > 0) || ((a1 * x + b1 * y + c1) <= 0 && (a2 * x + b2 * y + c2) <= 0 && (a3 * x + b3 * y + c3) <= 0))
			{
				GzDepth newZ = (GzDepth)(-( aX * x + bY * y  + d ) / cZ);

				// Check if the new value of z is in front of the camera
				if( newZ > 0 )
				{	
					if(newZ > zMax)
					{
						zMax = newZ;
					}
					GzIntensity r, g, b, a;
					GzDepth oldZ;
					
					GzGetDisplay( render->display, x, y, &r, &g, &b, &a, &oldZ);			

					//If the value of new z id greater than old z then it can be skipped because it will be hidden
					if( newZ < oldZ )
					{
												
						GzColor color;						
						

						/* For Phong shading	*/
						switch(render->interp_mode){
						case GZ_NORMALS:
						{
							/* Perform biliniear interpolation, normalize it and calculate the color at the current pixel by calling GzShade*/
							GzCoord imageVi, imageNi;
							GzTextureIndex textC;
							
							for(int i = 0; i < 3; i++)
							{
								imageVi[i] = -(imageVa[i] * x + imageVb[i] * y + imageVd[i]) / imageVc[i];							                               						                                   
								imageNi[i] = -(imageNa[i] * x + imageNb[i] * y  + imageNd[i]) / imageNc[i];							                                 						                                     
							}							

							for( int i = 0; i < 2; i++ )
							{															                                                         																								
								//calculate interpolation plane for texture coords and unwarp it from perspective to image space
								textC[i] = -(texCPa[i] * x + texCPb[i] * y + texCPd[i]) / texCPc[i];
								textC[i] = GzperspToimage( newZ, textC[i] );									
							}													
						
							float normalizer = sqrt(imageNi[X] * imageNi[X] + imageNi[Y] * imageNi[Y] + imageNi[Z] * imageNi[Z]);
							imageNi[X] = imageNi[X]/normalizer;
							imageNi[Y] = imageNi[Y]/normalizer;
							imageNi[Z] = imageNi[Z]/normalizer;														
							GzShade(render,imageVi,imageNi,color,textC,newZ,zMax);								
							break;
						}
						case GZ_COLOR:							
						{
							/*	Interpolate the colors for Gourad Shading at each vertex	*/
							color[RED] = -(cpA[0] * x + cpB[0] * y + cpD[0])/cpC[0];
							color[GREEN] = -(cpA[1] * x + cpB[1] * y  + cpD[1])/cpC[1];
							color[BLUE] = -(cpA[2] * x + cpB[2] * y  + cpD[2])/cpC[2];														

							break;
						}					
						default:
						{
							/*	Default flat shading	*/
							color[RED] = render->flatcolor[RED];
							color[GREEN] = render->flatcolor[GREEN];
							color[BLUE] = render->flatcolor[BLUE];							
							break;
						}		
						}
						/* Put the calculated values of the color onto the display	*/
						
						if(GRAINY_NOISE_EFFECT)
						{
							float noise = scaled_octave_noise(8,0.75,2,0,4,x,y,newZ/(oldZ - newZ));							
							GzPutDisplay(render->display, x, y, int(ctoi(color[RED]) * noise),int(ctoi(color[GREEN]) * noise),int(ctoi(color[BLUE]) * noise),a,newZ);								  								 								  								  											
						}
						else
						{
							GzPutDisplay(render->display, x, y, int(ctoi(color[RED])),int(ctoi(color[GREEN])),int(ctoi(color[BLUE])),a,newZ);								  								 								  								  				
						}
					}
				}
			}						
		}
	} 
}