btSoftBodySolverVertexBuffer_OpenGL.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_SOFT_BODY_SOLVER_VERTEX_BUFFER_OPENGL_H
  14. #define BT_SOFT_BODY_SOLVER_VERTEX_BUFFER_OPENGL_H
  15. #include "BulletSoftBody/btSoftBodySolverVertexBuffer.h"
  16. #ifdef USE_MINICL
  17. #include "MiniCL/cl.h"
  18. #else //USE_MINICL
  19. #ifdef __APPLE__
  20. #include <OpenCL/OpenCL.h>
  21. #else
  22. #include <CL/cl.h>
  23. #include <CL/cl_gl.h>
  24. #endif //__APPLE__
  25. #endif//USE_MINICL
  26. #ifdef _WIN32//for glut.h
  27. #include <windows.h>
  28. #endif
  29. //think different
  30. #if defined(__APPLE__) && !defined (VMDMESA)
  31. #include <OpenGL/OpenGL.h>
  32. #include <OpenGL/gl.h>
  33. #include <OpenGL/glu.h>
  34. #include <GLUT/glut.h>
  35. #else
  36. #ifdef _WINDOWS
  37. #include <windows.h>
  38. #include <GL/gl.h>
  39. #include <GL/glu.h>
  40. #else
  41. #include <GL/glut.h>
  42. #endif //_WINDOWS
  43. #endif //APPLE
  44. class btOpenGLInteropVertexBufferDescriptor : public btVertexBufferDescriptor
  45. {
  46. protected:
  47. /** OpenCL context */
  48. cl_context m_context;
  49. /** OpenCL command queue */
  50. cl_command_queue m_commandQueue;
  51. /** OpenCL interop buffer */
  52. cl_mem m_buffer;
  53. /** VBO in GL that is the basis of the interop buffer */
  54. GLuint m_openGLVBO;
  55. public:
  56. /**
  57. * context is the OpenCL context this interop buffer will work in.
  58. * queue is the command queue that kernels and data movement will be enqueued into.
  59. * openGLVBO is the OpenGL vertex buffer data will be copied into.
  60. * vertexOffset is the offset in floats to the first vertex.
  61. * vertexStride is the stride in floats between vertices.
  62. */
  63. btOpenGLInteropVertexBufferDescriptor( cl_command_queue cqCommandQue, cl_context context, GLuint openGLVBO, int vertexOffset, int vertexStride )
  64. {
  65. #ifndef USE_MINICL
  66. cl_int ciErrNum = CL_SUCCESS;
  67. m_context = context;
  68. m_commandQueue = cqCommandQue;
  69. m_vertexOffset = vertexOffset;
  70. m_vertexStride = vertexStride;
  71. m_openGLVBO = openGLVBO;
  72. m_buffer = clCreateFromGLBuffer(m_context, CL_MEM_WRITE_ONLY, openGLVBO, &ciErrNum);
  73. if( ciErrNum != CL_SUCCESS )
  74. {
  75. btAssert( 0 && "clEnqueueAcquireGLObjects(copySoftBodyToVertexBuffer)");
  76. }
  77. m_hasVertexPositions = true;
  78. #else
  79. btAssert(0);//MiniCL shouldn't get here
  80. #endif
  81. }
  82. /**
  83. * context is the OpenCL context this interop buffer will work in.
  84. * queue is the command queue that kernels and data movement will be enqueued into.
  85. * openGLVBO is the OpenGL vertex buffer data will be copied into.
  86. * vertexOffset is the offset in floats to the first vertex.
  87. * vertexStride is the stride in floats between vertices.
  88. * normalOffset is the offset in floats to the first normal.
  89. * normalStride is the stride in floats between normals.
  90. */
  91. btOpenGLInteropVertexBufferDescriptor( cl_command_queue cqCommandQue, cl_context context, GLuint openGLVBO, int vertexOffset, int vertexStride, int normalOffset, int normalStride )
  92. {
  93. #ifndef USE_MINICL
  94. cl_int ciErrNum = CL_SUCCESS;
  95. m_context = context;
  96. m_commandQueue = cqCommandQue;
  97. m_openGLVBO = openGLVBO;
  98. m_buffer = clCreateFromGLBuffer(m_context, CL_MEM_WRITE_ONLY, openGLVBO, &ciErrNum);
  99. if( ciErrNum != CL_SUCCESS )
  100. {
  101. btAssert( 0 && "clEnqueueAcquireGLObjects(copySoftBodyToVertexBuffer)");
  102. }
  103. m_vertexOffset = vertexOffset;
  104. m_vertexStride = vertexStride;
  105. m_hasVertexPositions = true;
  106. m_normalOffset = normalOffset;
  107. m_normalStride = normalStride;
  108. m_hasNormals = true;
  109. #else
  110. btAssert(0);
  111. #endif //USE_MINICL
  112. }
  113. virtual ~btOpenGLInteropVertexBufferDescriptor()
  114. {
  115. clReleaseMemObject( m_buffer );
  116. }
  117. /**
  118. * Return the type of the vertex buffer descriptor.
  119. */
  120. virtual BufferTypes getBufferType() const
  121. {
  122. return OPENGL_BUFFER;
  123. }
  124. virtual cl_context getContext() const
  125. {
  126. return m_context;
  127. }
  128. virtual cl_mem getBuffer() const
  129. {
  130. return m_buffer;
  131. }
  132. };
  133. #endif // #ifndef BT_SOFT_BODY_SOLVER_VERTEX_BUFFER_OPENGL_H