MCPcopy Create free account
hub / github.com/RenderKit/embree / createSphere

Function createSphere

tutorials/dynamic_scene/dynamic_scene_device.cpp:16–80  ·  view source on GitHub ↗

adds a sphere to the scene */

Source from the content-addressed store, hash-verified

14
15/* adds a sphere to the scene */
16unsigned int createSphere (RTCBuildQuality quality, const Vec3fa& pos, const float r)
17{
18 /* create a triangulated sphere */
19 RTCGeometry geom = rtcNewGeometry (g_device, RTC_GEOMETRY_TYPE_TRIANGLE);
20 rtcSetGeometryBuildQuality(geom, quality);
21
22 /* map triangle and vertex buffer */
23 RTCBuffer vertexBuffer = rtcNewBufferHostDevice(g_device, sizeof(Vertex) * data.numTheta*(data.numPhi+1));
24 Vertex* vertices = (Vertex*)rtcGetBufferData(vertexBuffer);
25 rtcSetGeometryBuffer(geom,RTC_BUFFER_TYPE_VERTEX,0,RTC_FORMAT_FLOAT3,vertexBuffer,0,sizeof(Vertex),data.numTheta*(data.numPhi+1));
26 rtcReleaseBuffer(vertexBuffer);
27
28 RTCBuffer indexBuffer = rtcNewBufferHostDevice(g_device, sizeof(Triangle) * 2*data.numTheta*(data.numPhi-1));
29 Triangle* triangles = (Triangle*)rtcGetBufferData(indexBuffer);
30 rtcSetGeometryBuffer(geom,RTC_BUFFER_TYPE_INDEX,0,RTC_FORMAT_UINT3,indexBuffer,0,sizeof(Triangle),2*data.numTheta*(data.numPhi-1));
31 rtcReleaseBuffer(indexBuffer);
32
33 /* create sphere geometry */
34 int tri = 0;
35 const float rcpNumTheta = rcp((float)data.numTheta);
36 const float rcpNumPhi = rcp((float)data.numPhi);
37 for (int phi=0; phi<=data.numPhi; phi++)
38 {
39 for (int theta=0; theta<data.numTheta; theta++)
40 {
41 const float phif = phi*float(pi)*rcpNumPhi;
42 const float thetaf = theta*2.0f*float(pi)*rcpNumTheta;
43 Vertex& v = vertices[phi*data.numTheta+theta];
44 v.x = pos.x + r*sin(phif)*sin(thetaf);
45 v.y = pos.y + r*cos(phif);
46 v.z = pos.z + r*sin(phif)*cos(thetaf);
47 }
48 if (phi == 0) continue;
49
50 for (int theta=1; theta<=data.numTheta; theta++)
51 {
52 int p00 = (phi-1)*data.numTheta+theta-1;
53 int p01 = (phi-1)*data.numTheta+theta%data.numTheta;
54 int p10 = phi*data.numTheta+theta-1;
55 int p11 = phi*data.numTheta+theta%data.numTheta;
56
57 if (phi > 1) {
58 triangles[tri].v0 = p10;
59 triangles[tri].v1 = p01;
60 triangles[tri].v2 = p00;
61 tri++;
62 }
63
64 if (phi < data.numPhi) {
65 triangles[tri].v0 = p11;
66 triangles[tri].v1 = p01;
67 triangles[tri].v2 = p10;
68 tri++;
69 }
70 }
71 }
72
73 rtcCommitBuffer(vertexBuffer);

Callers 3

device_initFunction · 0.70
runMethod · 0.50

Calls 12

rtcNewBufferHostDeviceFunction · 0.85
rtcGetBufferDataFunction · 0.85
rtcSetGeometryBufferFunction · 0.85
rtcReleaseBufferFunction · 0.85
rtcCommitBufferFunction · 0.85
rtcCommitGeometryFunction · 0.85
rtcAttachGeometryFunction · 0.85
rtcReleaseGeometryFunction · 0.85
rcpFunction · 0.50
sinFunction · 0.50
cosFunction · 0.50

Tested by

no test coverage detected