CodeBase - Noise Library Part 2: NoiseUtils.dba
Return to the CodeBase listing
Category: Libraries
Version: 1.1
Information
Uploaded: 31st Oct 2010 10:11
Modified: 5th Nov 2010 12:08
Author: The Sab
Summary
Noise Utilities to be used with NoiseGen
Full Description
Noise Utilities Instructions<br /> <br /> NoiseUtils_Setup must be called my your main program and after NoiseGen_Setup.<br /> <br /> Here is an example of how all this works:<br /> <br /> `*****************************************<br /> #include "NoiseGen.dba"<br /> #include "NoiseUtils.dba"<br /> <br /> #constant DBIMAGE 1 `A Dark Basic image number<br /> <br /> set display mode 640, 480, 32<br /> set cursor 0, 300<br /> <br /> NoiseGen_Setup()<br /> NoiseUtils_Setup()<br /> <br /> mod1 = Create_Module(MOD_PERLIN)<br /> Set_OctaveCount(mod1, 10)<br /> Set_NoiseQuality(mod1, QUALITY_BEST)<br /> <br /> mod2 = Create_Module(MOD_SCALEBIAS)<br /> Set_Source0(mod2, mod1)<br /> Set_Scale(mod2, 0.9)<br /> <br /> print "Generating Noise Map..."<br /> noise = NoiseMap_Create(SHAPE_PLANE)<br /> NoiseMap_SetSourceModule(noise, mod2)<br /> NoiseMap_SetSize(noise, 256, 256)<br /> NoiseMap_SetBounds(noise, 0.0, 4.0, 1.0, 5.0)<br /> NoiseMap_Build(noise)<br /> <br /> print "Building Gradients..."<br /> grad = Gradient_Create()<br /> Gradient_Clear(grad)<br /> Gradient_AddPoint(grad, -1.0, rgb(0, 0, 0))<br /> Gradient_AddPoint(grad, 1.0, rgb(255, 255, 255))<br /> Gradient_AddPoint(grad, 0.0, rgb(128, 0, 0))<br /> <br /> print "Rendering Image...This takes some time..."<br /> renderer = Renderer_Create()<br /> Renderer_SetSourceNoiseMap(renderer, noise)<br /> Renderer_SetDestImage(renderer, DBIMAGE)<br /> Renderer_SetGradient(renderer, grad)<br /> Renderer_Render(renderer)<br /> <br /> print "Writing Image to file..."<br /> writer = Writer_Create()<br /> Writer_SetDestFilename(writer, "testImage.bmp")<br /> Writer_SetSourceImage(writer, DBIMAGE)<br /> Writer_WriteDestFile(writer)<br /> <br /> print "Process Complete"<br /> <br /> paste image DBIMAGE, 0, 0<br /> <br /> wait key<br /> <br /> end<br /> <br /> `*****************************************<br /> <br /> Now, here are all the Utility functions, and what they do. For the Module functions, <br /> please see the GenReadme.txt.<br /> <br /> NoiseMaps:<br /> <br /> n_index = NoiseMap_Create(shape_constant)<br /> Shapes come in three flavors: SHAPE_PLANE noise is evenly distributed between defined borders <br /> SHAPE_CYLINDER seamless cylinderical map<br /> SHAPE_SPHERE seamless sphere map<br /> <br /> NoiseMap_SetSize(n_index, width, height) Sets the size of the noisemap array, and the resulting image if rendered. Width and height should be integers.<br /> <br /> NoiseMap_SetBounds(index, lowerXBound, upperXBound, lowerYBound, upperYBound) Sets the area of the given shape to be processed. All values are floating point.<br /> SHAPE_PLANE - All bounds mark an area on an arbitrary plane<br /> SHAPE_CYLINDER - XBounds are in degrees from -180.0 to 180.0. YBounds are arbitrary<br /> SHAPE_SPHERE - XBounds are West to East: -180.0 to 180.0. YBounds are North to South: -90.0 to 90.0 (I know it seams backwards, but the image is drawn from top to bottom). Defining borders less than the max will effectively zoom in on the selected area.<br /> <br /> NoiseMap_SetSourceModule(n_index, m_index) Sets which noise module the noise map will draw its values from.<br /> <br /> NoiseMap_SetDestNMap(n_index, nm_index) ***Not implimented due to technical problems*** Currently only one noiseMap array can exist at a time. Since DBPro cannot instance arrays, I am having trouble finding an elegant way of fixing this.<br /> <br /> NoiseMap_EnableSeamless(n_index, boolean) SHAPE_PLANE only. When this is set to true, the noisemap will be generated as a seamless repeating texture.<br /> <br /> NoiseMap_Build(n_index) This function will fill the noiseMap array with values. ***When this function is called, it will overwrite all noiseMaps previously generated. If you have more than one noiseMap defined, process them into images before building the next noiseMap.<br /> <br /> NoiseMap_GetValue(n_index, x, y) Will retrieve the value at the given x, y coordinates. Currently it will only retrieve the value of the last noiseMap built.<br /> <br /> NoiseMap_GetWidth(n_index) Retrieves the width and height of the noiseMap<br /> NoiseMap_GetHeight(n_index) <br /> <br /> NoiseMap_GetLowerXBound(n_index) Retrieves the bounds of the given shape from which the noiseMap will be filled.<br /> NoiseMap_GetUpperXBound(n_index)<br /> NoiseMap_GetLowerYBound(n_index)<br /> NoiseMap_GetUpperYBound(n_index)<br /> <br /> NoiseMap_IsSeamlessEnabled(n_index) SHAPE_PLANE only. Returns whether the noiseMap will be seamless along the borders.<br /> <br /> <br /> Color Gradients:<br /> <br /> g_index = Gradient_Create() Creates a color gradient. By default, this is a grayscale gradient. Due to typical array problems, there is a hard cap of 255 gradients, each with a max of 255 points. So don't go crazy.<br /> <br /> Gradient_Clear(g_index) Empties the given gradient.<br /> <br /> Gradient_AddPoint(g_index, position_value, color_value) Adds a gradient point to the gradient. Position_value should be a floating point, color should be a dword (DBPro's rgb(r, g, b) function is mighty handy here). The position_value is typically in the -1.0..1.0 range to match the noisemaps. All values outside of the supplied range will be clamped to the nearest color value.<br /> <br /> Gradient_GetColor(g_index, position_value) This function is used internally, but you can use it too if you want to figure out what color a given value will produce. Position_value can be any floating point number, not just positions previously assigned. The color returned will be interpolated.<br /> <br /> Gradient_GetPointCount(g_index) Returns the number of gradient points in the current gradient.<br /> <br /> Gradient_BuildGrayscale(g_index) Builds a grayscale gradient for you with -1.0 as black, 1.0 as white. This is the default gradient.<br /> <br /> Gradient_BuildTerrain(g_index) Builds a color gradient to simulate oceans, shallows, beaches, grasslands, hills, mountains, and snowy peaks.<br /> <br /> <br /> Image Renderers:<br /> <br /> r_index = Renderer_Create() Creates an image renderer.<br /> <br /> Renderer_SetSourceNoiseMap(r_index, n_index) Assigns a previously defined noiseMap to the renderer. Technically, this doesn't do much, since it can only draw the last noiseMap built, but it is required to put it in here anyway.<br /> <br /> Renderer_SetDestImage(r_index, image_number) Assigns a DBPro image to the renderer. If an image already exists with that number, it will be erased. You have been warned.<br /> <br /> Renderer_EnableLight(r_index, boolean) If set to true, lighting effects will be applied to the image. This does not change any values in the noiseMap, just how the image is rendered.<br /> <br /> Renderer_EnableWrap(r_index, boolean) This will only matter if light is enabled. If set to true, lighting and shading effects will be wrapped around the borders of the image. Otherwise, they won't. If you are going to tile the image, or are making a cylinder or sphere map, you should turn this on.<br /> <br /> Renderer_SetBackground(r_index, b_index) Not fully implemented yet, as alpha channels work a little different in DBPro then they do in C++. When implemented, you can have a background image assigned and the map will be blended over it.<br /> <br /> Renderer_SetLightAzimuth(r_index, float) Default 45.0. Sets the direction of the light source. 0.0 is directly east, 90.0 is directly south. I know it seams a little backwards, but the original code draws the images from bottom to top, whereas I draw mine from top to bottom. I will correct it later.<br /> <br /> Renderer_SetLightElev(r_index, float) Default 45.0. Sets the elevation of the light source. 90.0 is directly over-head, 0.0 is directly on the azimuth.<br /> <br /> Renderer_SetLightBrightness(r_index, float) Default 1.0. Sets how bright the light source is.<br /> <br /> Renderer_SetLightColor(r_index, dword) Default rgb(255, 255, 255). Sets the color of the light source.<br /> <br /> Renderer_SetLightContrast(r_index, float) Default 1.0. Sets the contrast of the image. Higher darker shadows.<br /> <br /> Renderer_SetLightIntensity(r_index, float) Not implemented... kinda. This variable exists in the original code, but is not used in any calculations. An independant intesity variable is used however, but it is the sum total of all the light effects used for a given pixel, and so is not a constant value.<br /> <br /> Renderer_EnableDisplay(r_index, boolean) Default false. If set to true, the image will be displayed on screen as it is being rendererd.<br /> <br /> Renderer_SetScreenCoords(r_index, x, y) If display is enabled, these are the coordinates that the image will be displayed at on the screen.<br /> <br /> Renderer_IsLightEnabled(r_index) Returns 0 if it is not enabled, 1 if it is.<br /> Renderer_IsWrapEnabled(r_index) <br /> Renderer_IsDisplayEnabled(r_index)<br /> <br /> Renderer_GetLightAzimuth(r_index) Standard batch of Get functions. The return the values assigned to those parameters.<br /> Renderer_GetLightElev(r_index)<br /> Renderer_GetLightBrightness(r_index)<br /> Renderer_GetLightColor(r_index)<br /> Renderer_GetLightContrast(r_index)<br /> Renderer_GetScreenX(r_index)<br /> Renderer_GetScreenY(r_index)<br /> <br /> Renderer_Render(r_index) This renders the noiseMap to the supplied DBPro image using all gradients and lighting options. *Note: DBPro cannot write pixels directly to an image, so I have it search out for an available DBPro bitmap to write to as a buffer. Once it is complete, it captures the image from the bitmap buffer and then frees the buffer. At least one DBPro bitmap must be available for this function to work. DBPro has 32 of them.<br /> <br /> Renderer_RenderNormalMap(r_index) This renders the noiseMap as just a grayscale, ignoring all lighting and gradients. This is useful for making bumpmaps, and is also much faster than using a grayscale gradient with the normal Render function. At least when it is working. Right now it makes a bluish purple image. I am pretty sure it has something to do with how DBPro uses the alpha channel, but I am still working the bugs out of this one.<br /> <br /> <br /> Image Writers:<br /> <br /> w_index = Writer_Create() Creates an image writer.<br /> <br /> Writer_SetDestFilename(w_index, filename string) Sets the name of the image file to create. Must be a .bmp, .dds, .jpg, or .dib. You can also include a directory path in the string.<br /> <br /> Writer_SetSourceImage(w_index, image_number) Tells the writer which image to write. Ideally the same image number you fed to the renderer, but the writer doesn't really care. It will write any image given to it to a file for you.<br /> <br /> Writer_GetDestFilename(w_index) Returns the string you hopefully provided to it earlier.<br /> <br /> Writer_WriteDestFile(w_index) This function actually creates the image file. If the file already exists, it will be overwritten with no warning.
Comments
No comments yet.