Question

I've been trying to write a function that will put out values between 0 and 1 which will, in turn, be used to generate a Perlin Noise. Whenever I try to compile the code I get this: error: incompatible types when assigning to type ‘float[(sizetype)(width)][(sizetype)(height)]’ from type ‘float *’

I've been trying to pass the pointer of a multidimensional to the function so that it could be filled with the values. I have read that you have to first allocate the memory of the array before passing it. Also I have read that you can use a double pointer in the function declaration.

I do not know what's wrong, though. Btw, I'm using the command gcc -Wall file.c -o file to compile it.

Edit: Sorry for not being so clear. I'm trying to assign the values from the 2-D array "smoothNoise" to the 3-D array "leNoise". That's where the problem is cropping up.

#include <math.h>
#include <stdio.h> 

/* This is a macro for determining the size of an array */
#define ARRAY_SIZE(x)  (sizeof(x) / sizeof(x[0]))

/* Now for some simple linear interpolation */
float Interpolate(float x0, float x1, float alpha)
{
   return x0 * (1 - alpha) + alpha * x1;
}

/*
 * Functions for generating Perlin Noise. */

void GeneratePerlinNoise(float **perlinNoise, int width, int height, int octave) {

  /* Initialize the array */
    float noise[width][height];

    int i,j;

    for (i = 0; i < width; i++)
   {
    for (j = 0; j < height; j++)
       {
        /* Return a random number between 0 and 1.
     * This code may not work.
         */
    noise[i][j] = random();
    }
 }

 float smoothNoise[width][height];

 int samplePeriod = 1 << octave; /* calculates 2 ^ k */
 float sampleFrequency = 1.0f / samplePeriod;

 for (i = 0; i < width; i++)
{
  /* calculate the horizontal sampling indices */
  int sample_i0 = (i / samplePeriod) * samplePeriod;
  int sample_i1 = (sample_i0 + samplePeriod) % width; /* wrap around */
  float horizontal_blend = (i - sample_i0) * sampleFrequency;

  for (j = 0; j < height; j++)
  {
     /* calculate the vertical sampling indices */
     int sample_j0 = (j / samplePeriod) * samplePeriod;
     int sample_j1 = (sample_j0 + samplePeriod) % height; /* wrap around */
     float vertical_blend = (j - sample_j0) * sampleFrequency;

     /* blend the top two corners */
     float top = Interpolate(noise[sample_i0][sample_j0],
        noise[sample_i1][sample_j0], horizontal_blend);

     /*blend the bottom two corners */
     float bottom = Interpolate(noise[sample_i0][sample_j1],
        noise[sample_i1][sample_j1], horizontal_blend);

     /* final blend */
     smoothNoise[i][j] = Interpolate(top, bottom, vertical_blend);
   }
}

float leNoise[octave][width][height];

float persistance = 0.5f;

for (i = 0; i < octave; i++)
{
   /* Big problem here: moving data from a two-dimensional array
* to a three-dimensional array */
    leNoise[i] = smoothNoise[i];
}

 float amplitude = 1.0f;
 float totalAmplitude = 0.0f;

 /*blend noise together */
 int octaveCount;
 for (octaveCount = octave - 1; octaveCount >= 0; octave--)
 {
    amplitude *= persistance;
    totalAmplitude += amplitude;

    for (i = 0; i < width; i++)
    {
       for (j = 0; j < height; j++)
       {
          perlinNoise[i][j] += leNoise[octave][i][j] * amplitude;
       }
    }
  }

  /*normalisation */
  for (i = 0; i < width; i++)
 {
   for (j = 0; j < height; j++)
    {
      perlinNoise[i][j] /= totalAmplitude;
    }
  }
}

  int main()
{
/* These are some variables which will be used in the 
 * Perlin Noise functions.  These may not work.
 */
   /* Make some space in the RAM for our big array. */
   float **leNoise = malloc( 500 * sizeof( float ) );
   int i;

   for (i=0; i < 500; i++) {
 leNoise[i] = malloc(500 * sizeof(float *) );
   }

   GeneratePerlinNoise(leNoise, 500, 500, 7); 

   return 0;
 }
Était-ce utile?

La solution

This assignment is culprit, as far I know.

for (i = 0; i < octave; i++)
{
   /* Big problem here: moving data from a two-dimensional array to a three-dimensional array */
   leNoise[i] = smoothNoise[i];
}

Because C doesn't have anything called array-to-array copying. You should manually copy individual items from source to destination by carefully looping through arrays.

Ex, leNoise[x][y][z] = smoothNoise[i][j]

Autres conseils

clang's error message is better:

error: array type 'float [width][height]' is not assignable

arrays are not assignable. You have declared

float leNoise[octave][width][height];

and then try to assign the two-dimensional component arrays:

for (i = 0; i < octave; i++)
{
   /* Big problem here: moving data from a two-dimensional array
* to a three-dimensional array */
    leNoise[i] = smoothNoise[i];
}

Since arrays are not assignable, you'd have to copy the values one-by-one [directly or with memcpy] here.

But,

float smoothNoise[width][height];

smoothNoise is a width×height array, and smoothNoise[i] is a float[height] (that is then converted to a pointer to its initial element), so you have a dimension mismatch, I don't know what you intend to do here, so I can't suggest a fix.

#include<stdlib.h>

You have to include it,it will remove the other warning And regarding array i dont know if such copy is possible from 3D to 2D array So cant help.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top