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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include <embree3/rtcore.h>
#include <stdio.h>
#include <math.h>
#include <limits>
#include <stdio.h>
#if defined(_WIN32)
# include <conio.h>
# include <windows.h>
#endif
/*
* A minimal tutorial.
*
* It demonstrates how to intersect a ray with a single triangle. It is
* meant to get you started as quickly as possible, and does not output
* an image.
*
* For more complex examples, see the other tutorials.
*
* Compile this file using
*
* gcc -std=c99 \
* -I<PATH>/<TO>/<EMBREE>/include \
* -o minimal \
* minimal.c \
* -L<PATH>/<TO>/<EMBREE>/lib \
* -lembree3
*
* You should be able to compile this using a C or C++ compiler.
*/
/*
* This is only required to make the tutorial compile even when
* a custom namespace is set.
*/
#if defined(RTC_NAMESPACE_USE)
RTC_NAMESPACE_USE
#endif
/*
* We will register this error handler with the device in initializeDevice(),
* so that we are automatically informed on errors.
* This is extremely helpful for finding bugs in your code, prevents you
* from having to add explicit error checking to each Embree API call.
*/
void errorFunction(void* userPtr, enum RTCError error, const char* str)
{
printf("error %d: %s\n", error, str);
}
/*
* Embree has a notion of devices, which are entities that can run
* raytracing kernels.
* We initialize our device here, and then register the error handler so that
* we don't miss any errors.
*
* rtcNewDevice() takes a configuration string as an argument. See the API docs
* for more information.
*
* Note that RTCDevice is reference-counted.
*/
RTCDevice initializeDevice()
{
RTCDevice device = rtcNewDevice(NULL);
if (!device)
printf("error %d: cannot create device\n", rtcGetDeviceError(NULL));
rtcSetDeviceErrorFunction(device, errorFunction, NULL);
return device;
}
/*
* Create a scene, which is a collection of geometry objects. Scenes are
* what the intersect / occluded functions work on. You can think of a
* scene as an acceleration structure, e.g. a bounding-volume hierarchy.
*
* Scenes, like devices, are reference-counted.
*/
RTCScene initializeScene(RTCDevice device)
{
RTCScene scene = rtcNewScene(device);
/*
* Create a triangle mesh geometry, and initialize a single triangle.
* You can look up geometry types in the API documentation to
* find out which type expects which buffers.
*
* We create buffers directly on the device, but you can also use
* shared buffers. For shared buffers, special care must be taken
* to ensure proper alignment and padding. This is described in
* more detail in the API documentation.
*/
RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE);
float* vertices = (float*) rtcSetNewGeometryBuffer(geom,
RTC_BUFFER_TYPE_VERTEX,
0,
RTC_FORMAT_FLOAT3,
3*sizeof(float),
3);
unsigned* indices = (unsigned*) rtcSetNewGeometryBuffer(geom,
RTC_BUFFER_TYPE_INDEX,
0,
RTC_FORMAT_UINT3,
3*sizeof(unsigned),
1);
if (vertices && indices)
{
vertices[0] = 0.f; vertices[1] = 0.f; vertices[2] = 0.f;
vertices[3] = 1.f; vertices[4] = 0.f; vertices[5] = 0.f;
vertices[6] = 0.f; vertices[7] = 1.f; vertices[8] = 0.f;
indices[0] = 0; indices[1] = 1; indices[2] = 2;
}
/*
* You must commit geometry objects when you are done setting them up,
* or you will not get any intersections.
*/
rtcCommitGeometry(geom);
/*
* In rtcAttachGeometry(...), the scene takes ownership of the geom
* by increasing its reference count. This means that we don't have
* to hold on to the geom handle, and may release it. The geom object
* will be released automatically when the scene is destroyed.
*
* rtcAttachGeometry() returns a geometry ID. We could use this to
* identify intersected objects later on.
*/
rtcAttachGeometry(scene, geom);
rtcReleaseGeometry(geom);
/*
* Like geometry objects, scenes must be committed. This lets
* Embree know that it may start building an acceleration structure.
*/
rtcCommitScene(scene);
return scene;
}
/*
* Cast a single ray with origin (ox, oy, oz) and direction
* (dx, dy, dz).
*/
void castRay(RTCScene scene,
float ox, float oy, float oz,
float dx, float dy, float dz)
{
/*
* The intersect context can be used to set intersection
* filters or flags, and it also contains the instance ID stack
* used in multi-level instancing.
*/
struct RTCIntersectContext context;
rtcInitIntersectContext(&context);
/*
* The ray hit structure holds both the ray and the hit.
* The user must initialize it properly -- see API documentation
* for rtcIntersect1() for details.
*/
struct RTCRayHit rayhit;
rayhit.ray.org_x = ox;
rayhit.ray.org_y = oy;
rayhit.ray.org_z = oz;
rayhit.ray.dir_x = dx;
rayhit.ray.dir_y = dy;
rayhit.ray.dir_z = dz;
rayhit.ray.tnear = 0;
rayhit.ray.tfar = std::numeric_limits<float>::infinity();
rayhit.ray.mask = -1;
rayhit.ray.flags = 0;
rayhit.hit.geomID = RTC_INVALID_GEOMETRY_ID;
rayhit.hit.instID[0] = RTC_INVALID_GEOMETRY_ID;
/*
* There are multiple variants of rtcIntersect. This one
* intersects a single ray with the scene.
*/
rtcIntersect1(scene, &context, &rayhit);
printf("%f, %f, %f: ", ox, oy, oz);
if (rayhit.hit.geomID != RTC_INVALID_GEOMETRY_ID)
{
/* Note how geomID and primID identify the geometry we just hit.
* We could use them here to interpolate geometry information,
* compute shading, etc.
* Since there is only a single triangle in this scene, we will
* get geomID=0 / primID=0 for all hits.
* There is also instID, used for instancing. See
* the instancing tutorials for more information */
printf("Found intersection on geometry %d, primitive %d at tfar=%f\n",
rayhit.hit.geomID,
rayhit.hit.primID,
rayhit.ray.tfar);
}
else
printf("Did not find any intersection.\n");
}
void waitForKeyPressedUnderWindows()
{
#if defined(_WIN32)
HANDLE hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(hStdOutput, &csbi)) {
printf("GetConsoleScreenBufferInfo failed: %d\n", GetLastError());
return;
}
/* do not pause when running on a shell */
if (csbi.dwCursorPosition.X != 0 || csbi.dwCursorPosition.Y != 0)
return;
/* only pause if running in separate console window. */
printf("\n\tPress any key to exit...\n");
int ch = getch();
#endif
}
/* -------------------------------------------------------------------------- */
int main()
{
/* Initialization. All of this may fail, but we will be notified by
* our errorFunction. */
RTCDevice device = initializeDevice();
RTCScene scene = initializeScene(device);
/* This will hit the triangle at t=1. */
castRay(scene, 0, 0, -1, 0, 0, 1);
/* This will not hit anything. */
castRay(scene, 1, 1, -1, 0, 0, 1);
/* Though not strictly necessary in this example, you should
* always make sure to release resources allocated through Embree. */
rtcReleaseScene(scene);
rtcReleaseDevice(device);
/* wait for user input under Windows when opened in separate window */
waitForKeyPressedUnderWindows();
return 0;
}
|