• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

kloffy

Members
  • Content count

    375
  • Joined

  • Last visited

Community Reputation

1318 Excellent

About kloffy

  • Rank
    Member
  1. Suppose I have a general n-polytope, whose faces are a list of (n-1)-polytopes. For example:   A 1-dimensional 1-simplex (line segment) would be represented as two 1-dimensional 0-simplexes (points):   [(0,),(1,)] A 2-dimensional 2-simplex (triangle) would be represented as three 2-dimensional 1-simplexes (line segments): [   [(0,0),(1,0)],   [(1,0),(0,1)], [(0,1),(0,0)] ] And so on... I would like to compute the Cartesian product of two arbitrary polytopes. I came up with the following algorithm, and I am wondering whether it is correct:   def product(xs, ys): if isinstance(xs, list) and isinstance(ys, list): return [product(xs, y) for y in ys] + [product(x, ys) for x in xs] elif isinstance(ys, list): return [product(xs, y) for y in ys] elif isinstance(xs, list): return [product(x, ys) for x in xs] else: return xs + ys You can make things a bit nicer by wrapping things in a class:   class Polytope(object): def __init__(self, data): self.data = data def __mul__(self, other): return Polytope(product(self.data, other.data)) def __str__(self): import pprint return pprint.pformat(self.data) With this, you can do things like:   cube = Polytope([(0,),(1,)]) print("1-cube:", cube, sep="\n") print("2-cube:", cube*cube, sep="\n") print("3-cube:", cube*cube*cube, sep="\n") The results seem correct, but I still need to convince myself that it works in all cases. Since I couldn't find much material online, I was curious if anybody has done this kind of thing or knows any further resources.
  2. Since the proper term has not been mentioned yet: It's a relation. A function is just a relation with the additional requirement that each input is related to exactly one output
  3. Could be hitting the implementation defined maximums for uniforms, maybe check GL_MAX_FRAGMENT_UNIFORM_COMPONENTS. If that is the problem, the modern solution to it would be SSBOs. Alternatively, texture buffers...
  4. Have you looked into this? (Not sure how difficult it would be to translate from GLSL to HLSL.) http://www.icare3d.org/codes-and-projects/codes/opengl_geometry_shader_marching_cubes.html On initial reading, it looks nice, simple, and self-contained. Unfortunately, I never got around to implement my own version of it, maybe one day...
  5. One way to speed these things up in Python is to use numpy:   import numpy as np vectors = np.random.rand(1500, 3) lengths = np.linalg.norm(vectors, axis=1) Voila, no (Python) loops.
  6. You can use gl_FragCoord to get the window coordinates in the fragment shader.
  7. If you want code, look at gluUnProject. Whenever I needed it, I used to look at the Mesa3D sources, but it seems the most recent ones don't have it anymore. Anyway, older version clones can easily be found: https://github.com/krh/mesa/blob/master/src/glu/sgi/libutil/project.c
  8. Turns out I was already 90% there towards using multi draw indirect. Just had to stick the parameters into a draw indirect buffer. def commands(mesh): mesh_count = len(mesh.meshes) for i in range(mesh_count): count = mesh.index_count(i) first_index = mesh.index_offset(i) base_vertex = mesh.vertex_offset(i) instance_count = self.instance_count//mesh_count base_instance = i*instance_count yield count, instance_count, first_index, base_vertex, base_instance self.command_data = np.array(list(commands(self.mesh)), dtype=[ ("count", np.uint32, 1), ("instanceCount", np.uint32, 1), ("firstIndex", np.uint32, 1), ("baseVertex", np.uint32, 1), ("baseInstance", np.uint32, 1), ]) It does wonders for performance, especially with the Python bindings, since it eliminates almost all of the interpreter/wrapper overhead. Nice.
  9. Just a quick update on the reducing draw calls issue: I still don't see a way of doing it without one of the glDraw*Indirect variants. However, that would require pretty recent extensions, some of which are not in core GL yet: https://www.opengl.org/registry/specs/ARB/multi_draw_indirect.txt https://www.opengl.org/registry/specs/ARB/shader_draw_parameters.txt I think for now I might just have to live with the extra draw calls.
  10. I have implemented the glDrawElementsInstancedBaseVertexBaseInstance version:   #!/usr/bin/python3 # -*- coding: utf-8 -*- import os import sys import ctypes import numpy as np from PyQt5 import QtCore, QtGui, QtOpenGL, QtWidgets import OpenGL from OpenGL import GL from OpenGL.GL import shaders # Helper Functions (Omitted unused types for brevity...) INPUT_FUNCTIONS = { (GL.GL_FLOAT, (1,)): GL.glVertexAttribPointer, (GL.GL_FLOAT, (2,)): GL.glVertexAttribPointer, (GL.GL_FLOAT, (3,)): GL.glVertexAttribPointer, (GL.GL_FLOAT, (4,)): GL.glVertexAttribPointer, } UNIFORM_FUNCTIONS = { (GL.GL_FLOAT, ()): GL.glUniform1f, (GL.GL_FLOAT, (1,)): GL.glUniform1fv, (GL.GL_FLOAT, (2,)): GL.glUniform2fv, (GL.GL_FLOAT, (3,)): GL.glUniform3fv, (GL.GL_FLOAT, (4,)): GL.glUniform4fv, (GL.GL_FLOAT, (2, 2)): GL.glUniformMatrix2fv, (GL.GL_FLOAT, (3, 3)): GL.glUniformMatrix3fv, (GL.GL_FLOAT, (4, 4)): GL.glUniformMatrix4fv, } def input_setter(program, key, type): location = GL.glGetAttribLocation(program, key) function = INPUT_FUNCTIONS[type] gltype, shape = type def _input_setter(type, value, stride, offset=0, divisor=0): GL.glEnableVertexAttribArray(location) GL.glBindBuffer(type, value) function(location, shape[0], gltype, GL.GL_FALSE, stride, ctypes.c_void_p(int(offset))) GL.glVertexAttribDivisor(location, divisor) GL.glBindBuffer(type, 0) return _input_setter def uniform_setter(program, key, type): location = GL.glGetUniformLocation(program, key) function = UNIFORM_FUNCTIONS[type] gltype, shape = type return { 0: lambda value, count=1, transpose=False: function(location, value), 1: lambda value, count=1, transpose=False: function(location, count, value), 2: lambda value, count=1, transpose=False: function(location, count, transpose, value) }[len(shape)] VS_SOURCE = """ #version 420 layout(location=0) in vec4 position; layout(location=1) in vec4 color; layout(location=2) in vec4 instance_position; layout(location=3) in vec4 instance_color; out VertexData { vec4 position; vec4 color; } vs; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { vs.position = model * (position * vec4(vec3(0.1), 1.0) + instance_position); vs.color = color * instance_color; gl_Position = projection * view * vs.position; } """ FS_SOURCE = """ #version 420 in VertexData { vec4 position; vec4 color; } vs; layout(location=0) out vec4 frag_color; void main() { frag_color = vs.color; //vec4(1.0f, 1.0f, 1.0f, 1.0f); } """ def vec(v, n=4, dtype=np.float32): result = np.zeros(n, dtype=dtype) result[:min(n,len(v))] = v return result def dir(v, n=4, dtype=np.float32): result = vec(v, n=n, dtype=dtype) result[-1] = 0.0 return result def pos(v, n=4, dtype=np.float32): result = vec(v, n=n, dtype=dtype) result[-1] = 1.0 return result def color_from_ordinal(i): return np.array([(i >> 0) & 1, (i >> 1) & 1, (i >> 2) & 1, 1], dtype=np.float32) class Mesh(object): def __init__(self, vertices, indices): self.vertices = vertices self.indices = indices class MultiMesh(object): @property def vertices(self): return np.concatenate([mesh.vertices for mesh in self.meshes]) @property def indices(self): return np.concatenate([mesh.indices for mesh in self.meshes]) def __init__(self, meshes): self.meshes = meshes def vertex_count(self, i): return len(self.meshes[i].vertices) def vertex_offset(self, i): return sum(self.vertex_count(j) for j in range(i)) def index_count(self, i): return len(self.meshes[i].indices) def index_offset(self, i): return sum(self.index_count(j) for j in range(i)) def make_triangle(): vertex_count = 3 vertices = np.zeros(vertex_count, dtype=[("position", np.float32, 4),("color", np.float32, 4)]) vertices["position"] = [pos(v) for v in [[0.0, +1.0, 0.0], [-1.0, -1.0, 0.0], [+1.0, -1.0, 0.0]]] vertices["color"] = [[1.0, 1.0, 1.0, 1.0]] * vertex_count indices = np.arange(vertex_count, dtype=np.uint8) return Mesh(vertices, indices) def make_rectangle(): vertex_count = 4 vertices = np.zeros(vertex_count, dtype=[("position", np.float32, 4),("color", np.float32, 4)]) vertices["position"] = [pos(v) for v in [[-1.0, -1.0, 0.0], [-1.0, +1.0, 0.0], [+1.0, -1.0, 0.0], [+1.0, +1.0, 0.0]]] vertices["color"] = [[1.0, 1.0, 1.0, 1.0]] * vertex_count indices = np.arange(vertex_count, dtype=np.uint8) return Mesh(vertices, indices) class MainWindow(QtGui.QOpenGLWindow): def initializeGL(self): self.createProgram() self.instance_count = 100 instance_data = np.zeros(self.instance_count, dtype=[("instance_position", np.float32, 4), ("instance_color", np.float32, 4),]) instance_data["instance_position"] = [dir(v) for v in 2.0 * np.random.rand(self.instance_count, 4) - 1.0] instance_data["instance_color"] = [color_from_ordinal(i) for i in np.random.randint(1, 8, size=self.instance_count)] self.mesh = MultiMesh([make_triangle(), make_rectangle()]) self.createVAO(instance_data, self.mesh.vertices, self.mesh.indices) def createProgram(self): self.program = shaders.compileProgram( shaders.compileShader(VS_SOURCE, GL.GL_VERTEX_SHADER), shaders.compileShader(FS_SOURCE, GL.GL_FRAGMENT_SHADER), ) self.inputs = {key: input_setter(self.program, key, type) for key, type in { "instance_position": (GL.GL_FLOAT, (4,)), "instance_color": (GL.GL_FLOAT, (4,)), "position": (GL.GL_FLOAT, (4,)), "color": (GL.GL_FLOAT, (4,)), }.items()} self.uniforms = {key: uniform_setter(self.program, key, type) for key, type in { "model": (GL.GL_FLOAT, (4, 4)), "view": (GL.GL_FLOAT, (4, 4)), "projection": (GL.GL_FLOAT, (4, 4)), }.items()} def createVAO(self, instance_data, vertex_data, index_data): self.vao = GL.glGenVertexArrays(1) GL.glBindVertexArray(self.vao) # Create Buffers self.instance_vbo = GL.glGenBuffers(1) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.instance_vbo) GL.glBufferData(GL.GL_ARRAY_BUFFER, instance_data.nbytes, instance_data, GL.GL_STATIC_DRAW) self.vbo = GL.glGenBuffers(1) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vbo) GL.glBufferData(GL.GL_ARRAY_BUFFER, vertex_data.nbytes, vertex_data, GL.GL_STATIC_DRAW) self.ibo = GL.glGenBuffers(1) GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, self.ibo) GL.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, index_data.nbytes, index_data, GL.GL_STATIC_DRAW) self.inputs["instance_position"](GL.GL_ARRAY_BUFFER, self.instance_vbo, instance_data.itemsize, 0 * np.dtype(np.float32).itemsize, divisor=1) self.inputs["instance_color"](GL.GL_ARRAY_BUFFER, self.instance_vbo, instance_data.itemsize, 4 * np.dtype(np.float32).itemsize, divisor=1) self.inputs["position"](GL.GL_ARRAY_BUFFER, self.vbo, vertex_data.itemsize, 0 * np.dtype(np.float32).itemsize) self.inputs["color"](GL.GL_ARRAY_BUFFER, self.vbo, vertex_data.itemsize, 4 * np.dtype(np.float32).itemsize) GL.glBindVertexArray(0) def resizeGL(self, width, height): self.size = np.array([width, height], dtype=np.float32) self.aspect = self.size/np.min(self.size) def paintGL(self): GL.glClearColor(0.5, 0.5, 0.5, 1.0) GL.glClear(GL.GL_COLOR_BUFFER_BIT) try: GL.glUseProgram(self.program) self.uniforms["model"](np.identity(4, dtype=np.float32)) self.uniforms["view"](np.identity(4, dtype=np.float32)) self.uniforms["projection"](np.diag(np.r_[1.0/self.aspect,1,1])) GL.glBindVertexArray(self.vao) mesh_count = len(self.mesh.meshes) for i in range(mesh_count): index_count = self.mesh.index_count(i) index_offset = ctypes.c_void_p(self.mesh.index_offset(i) * np.dtype(np.uint8).itemsize) base_vertex = self.mesh.vertex_offset(i) instance_count = self.instance_count//mesh_count base_instance = i*instance_count GL.glDrawElementsInstancedBaseVertexBaseInstance(GL.GL_TRIANGLE_STRIP, index_count, GL.GL_UNSIGNED_BYTE, index_offset, instance_count, base_vertex, base_instance) finally: GL.glBindVertexArray(0) GL.glUseProgram(0) def keyReleaseEvent(self, event): if event.key() == QtCore.Qt.Key_Escape: QtWidgets.QApplication.instance().quit() if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) format = QtGui.QSurfaceFormat() format.setDepthBufferSize(24) format.setStencilBufferSize(8) format.setProfile(QtGui.QSurfaceFormat.CoreProfile) format.setVersion(4, 2) window = MainWindow() window.setFormat(format) window.resize(640, 480) window.show() sys.exit(app.exec_()) Apologies that the code is a bit verbose (OpenGL... :-/), but it is completely self-contained. Just needs PyQT5, PyOpenGL and numpy. The key section is the paint method:   def paintGL(self): GL.glClearColor(0.5, 0.5, 0.5, 1.0) GL.glClear(GL.GL_COLOR_BUFFER_BIT) try: GL.glUseProgram(self.program) self.uniforms["model"](np.identity(4, dtype=np.float32)) self.uniforms["view"](np.identity(4, dtype=np.float32)) self.uniforms["projection"](np.diag(np.r_[1.0/self.aspect,1,1])) GL.glBindVertexArray(self.vao) mesh_count = len(self.mesh.meshes) for i in range(mesh_count): index_count = self.mesh.index_count(i) index_offset = ctypes.c_void_p(self.mesh.index_offset(i) * np.dtype(np.uint8).itemsize) base_vertex = self.mesh.vertex_offset(i) instance_count = self.instance_count//mesh_count base_instance = i*instance_count GL.glDrawElementsInstancedBaseVertexBaseInstance(GL.GL_TRIANGLE_STRIP, index_count, GL.GL_UNSIGNED_BYTE, index_offset, instance_count, base_vertex, base_instance) finally: GL.glBindVertexArray(0) GL.glUseProgram(0) Consequently, this version has one draw call per mesh. Still scratching my head over how to do it with a single draw call.
  11. Yes, having a single draw call was my initial goal, but I haven't quite figured out an elegant way to achieve it. Could you elaborate more or perhaps point me to some material on the topic? I can kind of see it with shader storage buffer objects and somehow indexing into them using instance attributes, but I am not sure if that is what you mean.
  12. Awesome, thanks! In my case, all per object state is in the instance attributes, so using combined buffers seems like the way to go. I just saw there is a glDrawElementsInstancedBaseVertexBaseInstance (what a name, just rolls off the tongue), which I may be able to use, and then all of the binding would be moved out of the loop. Still trying to get my head around how exactly it would fit into the overall framework.
  13. I have multiple meshes combined into one VBO and I need to render multiple instances of each mesh. As far as I can tell, there are quite a few ways to go about it. First, there is the question whether to have multiple index buffers or just one. Since we are already combining the vertices into one buffer, my initial thought was to do the same for indices. The option with multiple index buffers would be:   Bind Vertex Buffer Foreach Mesh: Bind Instance Attributes Bind Index Buffer glDrawElementsInstanced(GL_TRIANGLES, mesh.num_instances, GL_UNSIGNED_INT, 0, mesh.num_indices) And the combined index buffer would be:   Bind Vertex Buffer Bind Index Buffer for mesh in meshes: Bind Instance Attributes glDrawElementsInstanced(GL_TRIANGLES, mesh.num_instances, GL_UNSIGNED_INT, mesh.base_index, mesh.num_indices) Furthermore, there is glDrawElementsInstancedBaseVertex, which as far as I can tell just saves me from offsetting the indices a priori. Finally, there is also glDrawElementsIndirect, which seems the most flexible, but also requires rather recent OpenGL. Anyway, just wondering if my understanding is correct and if anyone has some experience on the topic they would like to share.
  14. In case anyone is interested, here is a recursive version for arbitrary numbers of arguments.   def outer(func): def _outer(*args, **kwargs): def __outer(args, items): if items: xs, *tail = items if isinstance(xs, list): return [__outer(args, [x] + tail) for x in xs] else: return __outer(args + [xs], tail) else: return func(*args, **kwargs) return __outer([], args) return _outer Seems fairly straightforward now, not sure why I was scratching my head over it at the time... :)
  15. Yes, that is probably not a bad way to do it if you really want to make the function work on any iterable. Somehow, the user would have to disambiguate (is x an iterable that should be iterated or not). However, for my purposes it is perfectly fine to assume that only certain types will be iterated over (I chose list in my example to keep it simple).