在 Christer Ericson 关于碰撞检测的巨著中,他给出了一种加速 GPU 方法,用于使用以下算法检测凸多边形之间的碰撞。
我对 OpenGL 完全陌生,我的问题是,考虑到以下两点std::vector
:
struct Point{
double x,
double y,
}
如何将其传递std::vector<Point>
给以下函数并在 C++ 中返回结果?我的向量是一个凸多边形顺序逆时针。
// Initialize depth buffer to far Z (1.0)
glClearDepth(1.0f);
glClear(GL_DEPTH_BUFFER_BIT);
// Disable color buffer writes
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
// Enable depth testing
glEnable(GL_DEPTH_TEST);
// Initialize occlusion queries
Gluint query[1], numSamplesRendered;
glGenQueries(1, query);
// Set pixels to always write depth
glDepthFunc(GL_ALWAYS);
glDepthMask(GL_TRUE);
// Draw front faces of object A
glCullFace(GL_BACK);
RenderObject(A);
// Pass pixels if depth is greater than current depth value
glDepthFunc(GL_GREATER);
// Disable depth buffer updates
glDepthMask(GL_FALSE);
// Render back faces of B with occlusion testing enabled
glBeginQuery(GL_SAMPLES_PASSED, query[0]);
glCullFace(GL_FRONT);
RenderObject(B);
glEndQuery(GL_SAMPLES_PASSED);
// If occlusion test indicates no samples rendered, exit with no collision
glGetQueryObjectuiv(query[0], GL_QUERY_RESULT, &numSamplesRendered);
if (numSamplesRendered == 0) return NO_COLLISION;
// Set pixels to always write depth
glDepthFunc(GL_ALWAYS);
glDepthMask(GL_TRUE);
// Draw front faces of object B
glCullFace(GL_BACK);
RenderObject(B);
// Pass pixels if depth is greater than current depth value
glDepthFunc(GL_GREATER);
// Disable depth buffer updates
glDepthMask(GL_FALSE);
// Render back faces of A with occlusion testing enabled
glBeginQuery(GL_SAMPLES_PASSED, query[0]);
glCullFace(GL_FRONT);
RenderObject(A);
glEndQuery(GL_SAMPLES_PASSED);
// If occlusion test indicates no pixels rendered, exit with no collision
glGetQueryObjectuiv(query[0], GL_QUERY_RESULT, &numSamplesRendered);
if (numSamplesRendered == 0) return NO_COLLISION;
// Objects A and B must be intersecting
return COLLISION;