push_back dynamically

I've made a simple raytracer, which works fine, and I have a piece of simplified code shown, that creates 4 spheres, each of which is an "Object" ... now all I want to do is wrap this code in a loop that will make a grid of spheres ... for instance, how do I dynamically create 1000 spheres in a 10x10x10 configuration? ... what I'm struggling with conceptually is how to make the instance name dynamically. Any help would be appreciated. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
double radius = 0.5;

Sphere sphere1 (Vect (0, 0, 0), radius);
Sphere sphere2 (Vect (1, 0, 0), radius);
Sphere sphere3 (Vect (0, 1, 0), radius);
Sphere sphere4 (Vect (0, 0, 1), radius);
	
vector<Object*> scene_objects;
scene_objects.push_back(dynamic_cast<Object*>(&sphere1));
scene_objects.push_back(dynamic_cast<Object*>(&sphere2));
scene_objects.push_back(dynamic_cast<Object*>(&sphere3));
scene_objects.push_back(dynamic_cast<Object*>(&sphere4));
First, you don't need to use dynamic_cast there. If Sphere is a child of Object then a conversion to a Object* is supported without a cast.

As for your problem, I think you want something like dynamic allocation (new/delete). Go look it up and see if you can get it working.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
struct object
{
    virtual ~object() {} // a virtual destructor is required
    // ...
};

struct vect { vect( double x, double y, double z ) { /* ... */ } /* ... */ };

struct sphere : object
{
     sphere( vect v, double r ) : location(v), radius(r) { /* ... */ }
     sphere( double x, double y, double z, double r ) : location(x,y,z), radius(r) { /* ... */ }
     // ...

     private:
        vect location ;
        double radius ;
};

int main()
{
    {
        // C++11

        std::vector< std::unique_ptr<object> > scene_objects ; // #include <memory>
        // http://en.cppreference.com/w/cpp/memory/unique_ptr
        
        scene_objects.emplace_back( new sphere( 0, 0, 0, 0.5 ) ) ;
        scene_objects.emplace_back( new sphere( 0, 1, 0, 0.5 ) ) ;
        // etc...

        // use scene_objects ...
    }

    {
        // C++98

        std::vector< object* > scene_objects ;
        
        scene_objects.push_back( new sphere( 0, 0, 0, 0.5 ) ) ;
        scene_objects.push_back( new sphere( 0, 1, 0, 0.5 ) ) ;
        // etc...

        // use scene_objects ...

        // clean up
        for( std::size_t i = 0 ; i < scene_objects.size() ; ++i )
            delete scene_objects[i] ;
    }
}
Well since I don't need the instance name per say, the code below does what I was looking for ... now I know how to delete them at the end though too. Thanks.

1
2
3
4
5
6
7
8
vector<Object*> scene_objects;
for (int grid_x = 0; grid_x < 10; grid_x++) {
	for (int grid_y = 0; grid_y < 10; grid_y++) {
		for (int grid_z = 0; grid_z < 10; grid_z++) {
			scene_objects.push_back(new Sphere (Vect (grid_x, grid_y, grid_z), 0.1));
		}
	}
}


http://img341.imageshack.us/img341/3034/scene0.png
Last edited on
Topic archived. No new replies allowed.