Blender and OpenCL: The Journey Begins

Latest Update: Improved Code, Collisions and Hose

And so I embark upon an epic journey, just like that I leave my normal life behind and dive into the depths of Blender’s source code armed only with courage inspired by the powers and promises of OpenCL. The treasure I seek is an interactive particle system in the Blender Game Engine, accelerated by OpenCL and capable of visualizing millions of particles. I am not so greedy to desire collisions and sophisticated particle physics on my first quest, interactive visualization of scientific simulations will be a worthy reward for a summers work.

One does not embark on such a voyage without any direction, so I have set forth a preliminary map to guide my efforts as I learn more and triumph over unforeseen perils. The Blender Game Engine (BGE) is made up of several models that interact to provide an interactive 3D experience. The 3D objects are defined in the Blender Kernel, typically they are mesh based and efficiently store the coordinates of their vertices in arrays. The 3D objects are rendered to the screen by the OpenGL Rasterizer which act upon these vertex arrays. The objects are interacted with and manipulated by the Game Logic Module, the meat of which is found in different Actuators provided by the BGE. The particle system I envision is essentially a bunch of simple points that move around the screen based on some defined behavior patterns. This means we want a custom Blender object where the mesh vertices serve as the location of each particle. We want to do custom rendering where we draw a little ball (or use a trick called billboarding for more speed) at each vertex to represent the particle. We can then create a custom actuator which will define the behavior of the particles by changing their location, color and size based on user defined functions (or later, physics).

Each journey has a first step, and mine starts with the rendering aspect. I was able to find the OpenGL code where (VertexArray) meshes are drawn to the screen, and inserted my own code to draw simple gluSpheres at each vertex.

The next step in the rendering aspect will be to figure out where each object in the scene is added to the list of items to be drawn and call a custom rendering routine for only the cube. Once I have a better understanding of this process I can make the particle rendering more general so that any object of a certain type (my future particle type) or that have my future particle actuator will be rendered in this way.

I’ll be setting up a git repo for what I’m doing but for now one could add the following snippet around line 170 of

source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_VAOpenGLRasterizer.cpp

//try to draw at vertices
glColor4f(0.5f, 0.0f, 0.0f, 1.0f);
//iterate over vertex arrays
int iti = 0;
for(ms.begin(it); !ms.end(it); ms.next(it)) {
  //normal meshes have one vertex array so this loop only happens once
  printf("iti: %d\n", iti);
  iti++;
  if(it.totindex == 0)
    continue;

  RAS_TexVert *vertex;
  size_t i;

  //iterate over the vertices in the vertex arrays
  for(i=0; i<it.totindex; i++)
  {
     vertex = &it.vertex[it.index[i]];
     const float* v = vertex->getXYZ();
     //move to the vertex (starts at center of object
     glTranslatef(v[0], v[1], v[2]);
     glBegin(GL_QUADS);
     //draw a sphere
     gluSphere(gluNewQuadric(), .2, 10, 10);
     glEnd();
     //move back to center for the next vertex
     glTranslatef(-v[0], -v[1], -v[2]);
   }
}

I admit that I am no expert in OpenGL (or OpenCL) and all my Blender experience comes from using the Python API, so I am learning at least 2 new technologies and one complex software package hence my description of this as a journey. I look forward to getting involved in the Blender development community and learning a lot along the way!

15 thoughts on “Blender and OpenCL: The Journey Begins

  1. enj Post author

    thanks for the support! definitely interesting stuff matt, thanks for the links!

  2. Pingback: Blender: Creating a Custom Modifier | enj

  3. Pingback: Particles on my Android | enj

  4. Pingback: Simple Particles with OpenCL and OpenGL | enj

  5. Pingback: Blender Game Engine: Particles in the Mix | enj

  6. Pingback: Adventures in OpenCL: Part 1, Getting Startedenj | enj

  7. Pingback: Adventures in OpenCL: Part 1.5, C++ Bindingsenj | enj

  8. Kevin Shenk

    Guys like you give me the confidence that open-source truly can’t be stopped! The benefits of OpenCL are staggering, and to think that this technology could be harnessed with such freedom is invigorating!

    Thank-you for your work!! Thank-you, Thank-you, Thank-you!

  9. Mattias

    This is incredibly important work! So cool. Blender working on the GPU… You’d open some really interesting doors for open source communities. Good luck, looking forward to see how it goes!

Comments are closed.