Skip to content

Instantly share code, notes, and snippets.

@jcowles
Last active July 6, 2019 02:52
Show Gist options
  • Save jcowles/af59657e884ecd839f37809f7fdf95e4 to your computer and use it in GitHub Desktop.
Save jcowles/af59657e884ecd839f37809f7fdf95e4 to your computer and use it in GitHub Desktop.
#define __forceinline inline
#include <iostream>
#include <embree2/rtcore.h>
#include <embree2/rtcore_ray.h>
// yuck. Required by Embree.
#include <xmmintrin.h>
#include <pmmintrin.h>
int main(int argc, char** argv)
{
// MM_YUCK. Required by Embree.
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
RTCDevice device = rtcNewDevice(NULL);
if (rtcDeviceGetError(device) != RTC_NO_ERROR) {
std::cout << "Failed to create device.\n";
return 1;
}
RTCScene scene = rtcDeviceNewScene(device,
RTC_SCENE_DYNAMIC, /* allowed to update our scene */
RTC_INTERSECT1); /* trace single rays, not packets */
if (rtcDeviceGetError(device) != RTC_NO_ERROR) {
std::cout << "Failed to create scene.\n";
return 1;
}
// static, deformable, or dynamic
RTCGeometryFlags geomFlags = RTC_GEOMETRY_STATIC;
size_t numTriangles = 1;
size_t numVertices = 3;
unsigned geomID = rtcNewTriangleMesh(scene, geomFlags,
numTriangles, numVertices, 1);
if (rtcDeviceGetError(device) != RTC_NO_ERROR) {
std::cout << "Failed to create geom.\n";
return 1;
}
struct Vertex { float x, y, z, a; };
struct Triangle { int v0, v1, v2; };
Vertex* v = (Vertex*) rtcMapBuffer(scene, geomID, RTC_VERTEX_BUFFER);
// fill vertices here
v[0].x = -10; v[0].y = -10; v[0].z = 10;
v[1].x = 20; v[1].y = -10; v[1].z = 10;
v[2].x = 20; v[2].y = 20; v[2].z = 10;
rtcUnmapBuffer(scene, geomID, RTC_VERTEX_BUFFER);
Triangle* t = (Triangle*) rtcMapBuffer(scene, geomID, RTC_INDEX_BUFFER);
// fill triangle indices here
t[0].v0 = 0;
t[0].v1 = 1;
t[0].v2 = 2;
rtcUnmapBuffer(scene, geomID, RTC_INDEX_BUFFER);
rtcCommit(scene);
RTCRay ray;
ray.org[0] = 0.0;
ray.org[1] = 0.0;
ray.org[2] = 0.0;
ray.dir[0] = 0.0;
ray.dir[1] = 0.0;
ray.dir[2] = 20.0;
ray.tnear = 0.0;
ray.tfar = 1.0;
ray.geomID = RTC_INVALID_GEOMETRY_ID;
rtcIntersect(scene, ray);
if (rtcDeviceGetError(device) != RTC_NO_ERROR) {
std::cout << "Query failed.\n";
return 1;
} else {
std::cout << "Hit: "
<< (ray.geomID == RTC_INVALID_GEOMETRY_ID
? -1 : (int)ray.geomID)
<< " known: " << geomID << "\n";
}
rtcDeleteScene(scene);
rtcDeleteDevice(device);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment