MCPcopy Create free account
hub / github.com/antmachineintelligence/mtgbmcode / tesselate_sphere

Function tesselate_sphere

compute/example/opengl_sphere.cpp:41–112  ·  view source on GitHub ↗

tesselates a sphere with radius, phi_slices, and theta_slices. returns a shared opencl/opengl buffer containing the vertex data.

Source from the content-addressed store, hash-verified

39// tesselates a sphere with radius, phi_slices, and theta_slices. returns
40// a shared opencl/opengl buffer containing the vertex data.
41compute::opengl_buffer tesselate_sphere(float radius,
42 size_t phi_slices,
43 size_t theta_slices,
44 compute::command_queue &queue)
45{
46 using compute::dim;
47
48 const compute::context &context = queue.get_context();
49
50 const size_t vertex_count = phi_slices * theta_slices;
51
52 // create opengl buffer
53 GLuint vbo;
54 vtkgl::GenBuffersARB(1, &vbo);
55 vtkgl::BindBufferARB(vtkgl::ARRAY_BUFFER, vbo);
56 vtkgl::BufferDataARB(vtkgl::ARRAY_BUFFER,
57 sizeof(float) * 4 * vertex_count,
58 NULL,
59 vtkgl::STREAM_DRAW);
60 vtkgl::BindBufferARB(vtkgl::ARRAY_BUFFER, 0);
61
62 // create shared opengl/opencl buffer
63 compute::opengl_buffer vertex_buffer(context, vbo);
64
65 // tesselate_sphere kernel source
66 const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
67 __kernel void tesselate_sphere(float radius,
68 uint phi_slices,
69 uint theta_slices,
70 __global float4 *vertex_buffer)
71 {
72 const uint phi_i = get_global_id(0);
73 const uint theta_i = get_global_id(1);
74
75 const float phi = phi_i * 2.f * M_PI_F / phi_slices;
76 const float theta = theta_i * 2.f * M_PI_F / theta_slices;
77
78 float4 v;
79 v.x = radius * cos(theta) * cos(phi);
80 v.y = radius * cos(theta) * sin(phi);
81 v.z = radius * sin(theta);
82 v.w = 1.f;
83
84 vertex_buffer[phi_i*phi_slices+theta_i] = v;
85 }
86 );
87
88 // build tesselate_sphere program
89 compute::program program =
90 compute::program::create_with_source(source, context);
91 program.build();
92
93 // setup tesselate_sphere kernel
94 compute::kernel kernel(program, "tesselate_sphere");
95 kernel.set_arg<compute::float_>(0, radius);
96 kernel.set_arg<compute::uint_>(1, phi_slices);
97 kernel.set_arg<compute::uint_>(2, theta_slices);
98 kernel.set_arg(3, vertex_buffer);

Callers 1

RenderMethod · 0.85

Calls 7

dimFunction · 0.85
buildMethod · 0.80
get_contextMethod · 0.45
set_argMethod · 0.45

Tested by

no test coverage detected