Jan-21-2017, 05:59 AM
after generating the 3d perlin noise value(x,y and z respectively(the value is between -1 and 1)),how do i use the generated value to create texture or terrain?
note that i am using opengl.
note that i am using opengl.
(Jan-21-2017, 06:01 AM)Mekire Wrote: [ -> ]Looks like a good place to start:the website that you gave is not python,but it is ok.
http://www.redblobgames.com/maps/terrain-from-noise/
(Jan-21-2017, 06:20 AM)Mekire Wrote: [ -> ]No one is going to provide you with rendering code based on your first post.
import sys import random import pygame as pg import opensimplex as simp SCREEN_SIZE = 500, 500 TILE_SIZE = 1, 1 BACKGROUND = pg.Color("darkslategray") FPS = 60 WIDTH = SCREEN_SIZE[0]//TILE_SIZE[0] HEIGHT = SCREEN_SIZE[1]//TILE_SIZE[1] TERRAIN = [("water", 0.2), ("beach", 0.3), ("forest", 0.4), ("jungle", 0.5), ("savannah", 0.7), ("desert", 0.9), ("snow", 1)] TERRAIN_COLORS = {"water" : pg.Color("lightblue"), "beach" : pg.Color("tan"), "forest" : pg.Color("forestgreen"), "jungle" : pg.Color("darkgreen"), "savannah" : pg.Color("sienna"), "desert" : pg.Color("lightyellow"), "snow" : pg.Color("snow")} def noise(gen, nx, ny, freq=10): # Rescale from -1.0:+1.0 to 0.0:1.0 return gen.noise2d(freq*nx, freq*ny) / 2.0 + 0.5 def gen_noise(gen, freq=10): vals = {} for y in range(WIDTH): for x in range(HEIGHT): nx = float(x)/WIDTH - 0.5 ny = float(y)/HEIGHT - 0.5 vals[x,y] = noise(gen, nx, ny, freq) return vals class App(object): def __init__(self): self.screen = pg.display.get_surface() self.screen_rect = self.screen.get_rect() self.clock = pg.time.Clock() self.done = False self.seed = random.randrange(2**32) freq = random.randrange(5, 10) noise = gen_noise(simp.OpenSimplex(self.seed), freq) self.terrain = self.gen_map(noise) def update(self): """ All updates to all actors occur here. Exceptions include things that are direct results of events which may occasionally occur in the event loop. For example, updates based on held keys should be found here, but updates to single KEYDOWN events would be found in the event loop. """ pass def gen_map(self, noise): mapping = pg.Surface(SCREEN_SIZE).convert() for x,y in noise: pos = TILE_SIZE[0]*x, TILE_SIZE[1]*y for biome, tolerance in TERRAIN: if noise[x,y] < tolerance: mapping.fill(TERRAIN_COLORS[biome], (pos, TILE_SIZE)) break return mapping def render(self): """ All calls to drawing functions here. No game logic. """ self.screen.blit(self.terrain, (0,0)) pg.display.update() def event_loop(self): """ Event handling here. Only things that are explicit results of the given events should be found here. Do not confuse the event and update phases. """ for event in pg.event.get(): if event.type == pg.QUIT: self.done = True def main_loop(self): """ Main loop for your whole app. This doesn't need to be touched until you start writing framerate independant games. """ while not self.done: self.event_loop() self.update() self.render() self.clock.tick(FPS) def main(): """ Prepare pygame and the display and create an App instance. Call the app instance's main_loop function to begin the App. """ pg.init() pg.display.set_mode(SCREEN_SIZE) App().main_loop() pg.quit() sys.exit() if __name__ == "__main__": main()