Rendering a random star field [solved]

Hi there, Im trying to make a star field, like this:

Currently Im just loading a vector with ~6000 spherePrimitives, then drawing them in a loop, and it is very slow (12fps).

Apparently I should use instancing for this. I’ve looked at the OF example, but it’s relatively advanced considering I just want to render a bunch of static spheres. Does anyone have a simple explanation for how I’d do this?

Thanks!

If they are not moving then it’ll be fast if you make a mesh containing vertex data for all the spheres you want to draw.

You’d grab a unit sphere mesh, offset it’s vertices then use ofMesh.append to add it to a final starfield mesh that would end up containing the vertex data to draw all the spheres in the right position with one draw call.

It may even be faster to update the mesh each frame with moving spheres than drawing the spheres in a loop.

Perfect this is exactly what I need, thank you.

So, after I append the sphere vertices to the mesh, I’ve got a bunch of vertices in the mesh that draw will interpret as triangles. How do I then make the spheres look like spheres, instead of like a collection of triangles like this:

Thanks!

Try something like this:

mesh.setMode(OF_PRIMITIVE_LINE_LOOP);
mesh.draw(OF_MESH_FILL);

If that doesn’t work, try setting the mesh to different modes as well. Here are the different of primitive types:
http://openframeworks.cc/documentation/3d/ofMesh.html#show_setMode

And what they look like:
http://openframeworks.cc/documentation/3d/ofMesh.html

hmm I tried them all, but there is definitely something weird going on. eg OF_PRIMITIVE_LINE_LOOP looks like this:

which looks pretty cool but not what Im going for :wink:

When Im adding vertices to my mesh, do I also need to do something with indices? Im pretty new at this…

1 Like

Ok I haven’t tried doing a lot of them so I don’t know if this hits the mark for efficiency, but it looks right.

void ofApp::setup(){
    ofSpherePrimitive sphere;
    ofMesh spheremesh = sphere.getMesh();
        for (int i = 0; i < spheremesh.getNumVertices(); i++){
            spheremesh.setVertex(i, spheremesh.getVertex(i) + ofPoint(ofGetWidth()/2, ofGetHeight()/2, 0));
        }
    mesh.append(spheremesh);

    for (int i = 0; i < spheremesh.getNumVertices(); i++){
    	spheremesh.setVertex(i, spheremesh.getVertex(i) + ofPoint(100, 0, 0));
    }
    mesh.append(spheremesh);
}

void ofApp::draw(){
    cam.begin();
    //If you don't want to use an easy cam, just translate to ofPoint(0,0,500) instead.
    ofTranslate(ofPoint(-ofGetWidth()/2, -ofGetHeight()/2, 500));
    mesh.setMode(OF_PRIMITIVE_TRIANGLES);
    mesh.draw(OF_MESH_FILL);
    cam.end();
}

Edit: it’s pretty fast even with a lot of them.

oh wow, yeah that’s almost exactly what Im doing, except I was using sphere.set before adding to the mesh. Do you know why sphere.set makes the spheres look like the image in my second post? And, how do I set the size of the shape before adding to the mesh otherwise?

Thanks!

Hmm, I’m not sure but it might have to do with the fact that the third argument of set is ofPrimitiveMode, and if you don’t specify it, it defaults to a triangle strip. I’m not sure why that would effect the way the mesh draws though. Try calling sphere.set with OF_PRIMITIVE_TRIANGLES as the third argument and see if that does it.

1 Like

you should un-withdraw your comment, because it is correct!

I set my spheres with:

sphere.set(10, 16, OF_PRIMITIVE_TRIANGLES);

and now they are drawing beautifully in the vbo. Thanks!!

Oh good, I’m glad that works! That was a bit of a shot in the dark honestly. I’d still like to know why that even matters since you’re telling the mesh to draw using triangles anyways. I would think that would override anything the sphere’s mesh was set to. Maybe @hahakid can shed some light on this?