2
votes

Background

I'm working in python/pyglet to create a procedurally generated terrain. I'm visualizing it with pyglet by running a lambda function for each x and y coordinate on the screen and then painting that pixel in the color I want it.

This is not very optimal.

I have a sneaking suspicion that there is a way to create a rectangular object and tell the GL engine to render a texture upon it, giving the GL engine a lambda function that will return the color in exchange for an x and y coordinate.

Pseudo pyglet example

def get_color(pos):
    x, y = pos
    color = x * y % 255
    return [color, color, color]


width = 1680
height = 1024

win = window.Window(fullscreen=False, vsync=True, width=width, height=height)

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0, win.width, 0, win.height)
glTextureMapper(get_color)
win.clear()

points = [0, 0, width, 0, width, height, 0, height]
pyglet.graphics.draw(int(len(points) / 2), GL_QUADS, ('v2f', points))

TLDR Question

How do I color a polygon using a lambda function? Is this a shader?

Pyglet examples are preferred, but examples in any language are welcome

2
OpenGL does not use polygons, just triangles (and lines and points). The color for a pixel is interpolated between the vertices that define the primitive (triangle, line or point). If you want a function for each pixel, then better use shaders. If you have already computed colors for each pixel, perhaps a texture is useful. My best advice is follow some tutorial or a book about modern OpenGL (ver >= 3.x) - Ripi2

2 Answers

1
votes

This is probably not a complete answer, but might get you a bit further.
Also keep in mind that I've never worked much with textures at all weirdly enough.

What you might be able to do, is create a texture-group for any set of quads, and add them into a batch. Because I'm not entirely sure why you need a lambda function?

class TextureGroup(pyglet.graphics.Group):
    def set_state(self):
        glEnable(texture.target)
        glBindTexture(texture.target, texture.id)

    def unset_state(self):
        glDisable(texture.target)

vertice_mem = {}

batch = pyglet.graphics.Batch()
texture_group = TextureGroup()
vertice_list = batch.add(2, pyglet.gl.GL_QUADS, None, ('v2f', points))
vertice_mem[(x,y)] = vertex_list

This is one way to optimize how textures are added to certain faces and also add those faces into a batch rendering them a lot faster and more dynamic.

If you need to update the vertices, you can always access the vertice_list.vertices and change the x,y pair for that particular face.

You can use the custom group to create certain textures "on the fly", or on this case pre-rendered certain textures which you could stitch together to create "procedural" textures.

What @Ripi2 says is also a good tip, read up on how procedural memory mapping works and what it actually is. Most graphics you see in sand-box games are actual clever re-use of textures to create the illusion of on-demand graphics.

The last and final tip is that you will most likely need to look into shaders, they are by far the fastest way to auto-generate "textures" by manipulating and creating turbulence in textures, else will be heavily taxing on your system because you will need to iterate over the pixel area in some way.

But again, this is not my area of experience - only a fellow travelers bits and pieces of information gathered over the years.

1
votes

As pointed out strictly speaking OpenGL cannot draw rectangles (quads), but you can always use a diagonal between two corners to make two triangles. (There can be some differences in interpolation between corners, but they probably won't affect you.)

And while I'm being pedantic, you're not coloring pixels, you're colouring points on your 3D terrain object surface. (I'm assuming you want the terrain to keep the same colors when viewed from any angle or distance.)

OK, what I hope will be the real answer. Is your procedural terrain generation coloring done once, or does it change dynamically?

If you only create and color the terrain once, use a texture map. You'll need to learn how to assign texture coordinates at the corners of the terrain (TL;DR: they'll be (0.0,0.0), (1.0,0.0), (1.0,1.0), (0.0, 1.0) ) and how to use glTexImage2D to transfer an image to the GPU.

Texture maps are just 2D raster images. Pyglet should be able to handle this for you, if not either Pillow or the Python Imaging Library packages.

If you want the terrain to change colors on the fly, you need a fragment shader, a small function written in OpenGL Shading Language that executes every time the terrain is drawn. And due to the way OpenGL works you'll also have to write a vertex shader that handles the 3D coordinate transformations.

This will mean more thinking and coding that the texture map approach. But everything in OpenGL (or DirectX, or RenderMan, …) is done with shaders these days, so you might as well start learning. And shaders let you do any kind of procedural modelling and animation, not just colors. For example, you could dynamically generate your terrain heights as well as colors.

Hope this helps.