Creating new polygon's at runtime

Hi !

I try to find a way to create polygon’s at runtime in bge. I find this methods in a old thread of the forum:


import bgl
import bge


sce       = bge.logic.getCurrentScene()


def TheList():
    bgl.glBegin(bgl.GL_TRIANGLES)
    bgl.glVertex2f(-1,-1)
    bgl.glVertex2f(1,-1)
    bgl.glVertex2f(-1,1) 
    bgl.glEnd()      


def Draw():
        sce.post_draw = [TheList]
  
      
Draw()


And I find the way how to add normal to it with bgl.glNormal3f() function and add color with bgl.glColor3f(). But the problem of this methods is :

  • “TheList()” function is call at every frame, so over 5000 polygon’s fps drop below 30fps.
  • I don’t find how to add a Blender material to it.
  • It’s don’t receive any shadows from other object
  • I don’t know how to add bullet collider to it

There another way to dynamicaly create polygon’s in bge ? I currently test my CubeSurfer mesher addon’s in BGE
and it’s work really great , addons is really fast but not the way I display the created polydons :frowning:

I talk a bit of this with my brother , a experienced dev , and he think it’s because I redo all triangle at each frame and resend it to the videocard. He not really code a lot of OpenGL in C++ but he know is possible to create triangles or polygons and embedded in the a OpenGL Buffer objects to recall it faster if you need it later ( instead of recreate all at each frame ). Somebody know how ?

Your code is literally drawing the polygons directly through OpenGL, which means the BGE doesn’t know about those vertices, so you can’t do anything with it relative to the BGE. Materials, shadows, shaders, etc. you’d need to program yourself in OpenGL. In that case, that defeats the whole purpose of a game engine :p…or is that what you had in mind?

BGE can’t do VBOs (they are disabled for various reasons). If you need new polygons, the most feasible method is to LibLoad it in.

Also note, the bpy API is separate from the bge API. The bpy module only applies to Blender the editor and the bge module only applies to the BGE during runtime.

Mahlin, can you make a fork, that is a version of blender with VBO on?

they are the key to instancing right?

If I use the exact same mesh in 5 objects right now in blender,
is that any faster then 5 different meshes?

Mahalin: Thanks ! It’s confirm what I finally found. Btw if VBO will be added , if I can’t give them shadows and meshes colliders , it’s not really usable anyway. It’s not possible to create BGE object with all this attribute at runtime? By calling some C++ Function with the Ctypes available function module in python? I will post a video of what I testing right now with my addon (without any change in the code). It’s funny but finally not really usefull if I can’t do anything except drawing it to the screen.

You can enable VBOs in user preferences, at least there is checkbox for it, but I don’t know what are VBOs used for exactly…

Do you need to create polygons (small amount) or a whole object?
Is the mesh changed once or more?

You could just use a donor mesh and move its polygons to match your taste.
Or, if you need to do it once to make a big mesh then you might want to create it outside blender (while in game) in bpy with some socket scripts.

TBH, I’m really not interested in maintaining a fork of the BGE. I enabled it once before as the change is relatively simple. I’m sure one of the new devs could do it…

Adriansnetlis: Thanks but I think is for Blender opengl it’s self , not for the BGL python module. If someone can confirm but I see no difference and don’t know how to call it if it supposed to have difference.

VegetableJuiceF : Nice Idea about the donor mesh ! I can make a huge mesh with many loose triangle and pick triangle I need to build my mesh. It’s not the best but it’s a workaround that can work better than what I currently have with BGL

I will post a video soon of what I currently playing with :slight_smile:

Little video with bgl :

Better result with the “Donor mesh” method from VegetableJuiceF instead trying to work with bgl module.

Nice!

I imagine you could populate meshes in real time with this?

(no matter what only X polygons in the scene?)

and it’s 1 draw call right?

now combine that with a atlas…

Instancing!

Sounds like it might be intense on the CPU though,
I wonder if there is a way to apply the transforms with a thread or on the GPU?

Have chunks, for example a space of 16x16x16 that will have 1 donor mesh for it own.
Now that you know the size of the space you need them to be in, you can create a cube with the size, and replace its mesh with the donor mesh.

Now the fun part, the object which mesh you replaced keeps the display bounds of the initial object and the mesh can be full of verts that are out of view (x,y,-9999).
What is more, the game can actually now lod (fustrum cull?) the view now.
(not seeing the chunk -> not seeing the verts that belong to that chunk -> profit )

Very interesting I like it.When you press escape.Why can’t the blender game engine be made to talk to blender itself to make polygons?