Subdivide or subsurf at runtime in BGE

Hi,

I’ve been searching for a way to subdivide a plane at runtime in BGE. I’ve found some posts where it is discussed (even one from 2003(!)) but none offer the solution (except saying they found a working example… without posting the example).

So, can anyone help me with this?

I’m curious as well if you can make this work. The thing is that an object is converted to a compatible mesh for the game engine when you press P. So if the conversion does work with a subdivision modifier it will be applied during conversion. You’ll end up with the same mesh as when you applied the sudivision yourself. But this is just theory guessing and a little knowledge of the blender source.

You could edit the vertices yourself in game. You could let a GLSL shader do some magic (which would be the fastest)

What are you trying to accomplish exactly. If you need a texture on a plane and you want to modify the corners you could just modify the UV coords. As long as there are 4 vertices I guess you will not have the effect you described in your other post.

Hmm I don’t know what I should do with the uv coords to make it work. Do you mean I should match the uv’s with the modified vertex coordinates?

What I’m trying to accomplish: I have 5 planes with render2texture. The planes can be modified so that they fit 5 physical screens. Basically it’s a simple projectionmapping setup. The problem is that if a plane has only 4 vertices and you move them, the texture deforms a bit weird (it distributes the texture over two triangles instead of a quad). So I thought I may be able to callibrate a plane using 4 vertices, then subdivide it so the texture will be distributed more evenly.

But I think this workaround is also a dead end (looks like subdivide doesnt work in runtime)

  1. You cannot create vertex’s in game, but you can edit those that are there
  2. Modifiers are applied before the engine starts.

And the problem with your textures is likely that you have a ‘twist’ in your polygon. Remember that the order of vertices is not the same each time you play the game.

Just tried the UV trick and it doesn’t work either. You get the same effect as when moving the vertices.
You’ll need to move to the GPU for this to work:
http://www.reedbeta.com/blog/2012/05/26/quadrilateral-interpolation-part-1/
Should be doable…

Here’s another example with code http://www.xyzw.us/~cass/qcoord/

here’s the important difference:
if(use_q)
{
float tx = scale_texcoord * top;
glBegin(GL_QUADS);

    glTexCoord2f(-1, -1);
    glVertex2f  (-1, -1);
    
    glTexCoord4f(-tx,  tx, 0, tx);
    glVertex2f  (-top,  1);
    
    glTexCoord4f( tx,  tx, 0, tx);
    glVertex2f  ( top,  1);
    
    glTexCoord2f( 1, -1);
    glVertex2f  ( 1, -1);
    
    glEnd();
}
else
{
    glBegin(GL_QUADS);
    
    glTexCoord2f(-1, -1);
    glVertex2f  (-1, -1);
    
    glTexCoord2f(-1,  1);
    glVertex2f  (-top,  1);
    
    glTexCoord2f( 1,  1);
    glVertex2f  ( top,  1);
    
    glTexCoord2f( 1, -1);
    glVertex2f  ( 1, -1);
    
    glEnd();
} 

The bge has a wrapper around openGL: http://www.blender.org/documentation/blender_python_api_2_69_1/bgl.html

Ol jsut read the articles you mentioned and i think this might do the trick. But how do i implement this? I’m no expert when it comes down to opengl and don,t know how to use it in blender either…

I’d been reading and trying something. I need more time but I’d expected this to work. It doesn’t however:
from bge import logic

vertex_shader = “”"

void main(void)
{
// original vertex position, no changes
gl_Position = ftransform();
// coordinate of the 1st texture channel
gl_TexCoord[0] = gl_MultiTexCoord0;
}
“”"

fragment_shader ="""

uniform sampler2D color_0;

void main(void)
{
gl_FragColor = texture2D(color_0, vec2(gl_TexCoord[0].x / gl_TexCoord[0].w, gl_TexCoord[0].y));
}
“”"

object = logic.getCurrentController().owner
for mesh in object.meshes:
print(mesh.numPolygons)
for material in mesh.materials:
shader = material.getShader()
if shader != None:
if not shader.isValid():
shader.setSource(vertex_shader, fragment_shader, True)

        # get the first texture channel of the material
        shader.setSampler('color_0', 0)

Perhaps someone with more GLSL experience can shred a light?