You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
10 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include <main.h>
  10. #include <iostream>
  11. #include <GL/glew.h>
  12. #include <Eigen/OpenGLSupport>
  13. #include <GL/glut.h>
  14. using namespace Eigen;
  15. #define VERIFY_MATRIX(CODE,REF) { \
  16. glLoadIdentity(); \
  17. CODE; \
  18. Matrix<float,4,4,ColMajor> m; m.setZero(); \
  19. glGet(GL_MODELVIEW_MATRIX, m); \
  20. if(!(REF).cast<float>().isApprox(m)) { \
  21. std::cerr << "Expected:\n" << ((REF).cast<float>()) << "\n" << "got\n" << m << "\n\n"; \
  22. } \
  23. VERIFY_IS_APPROX((REF).cast<float>(), m); \
  24. }
  25. #define VERIFY_UNIFORM(SUFFIX,NAME,TYPE) { \
  26. TYPE value; value.setRandom(); \
  27. TYPE data; \
  28. int loc = glGetUniformLocation(prg_id, #NAME); \
  29. VERIFY((loc!=-1) && "uniform not found"); \
  30. glUniform(loc,value); \
  31. EIGEN_CAT(glGetUniform,SUFFIX)(prg_id,loc,data.data()); \
  32. if(!value.isApprox(data)) { \
  33. std::cerr << "Expected:\n" << value << "\n" << "got\n" << data << "\n\n"; \
  34. } \
  35. VERIFY_IS_APPROX(value, data); \
  36. }
  37. #define VERIFY_UNIFORMi(NAME,TYPE) { \
  38. TYPE value = TYPE::Random().eval().cast<float>().cast<TYPE::Scalar>(); \
  39. TYPE data; \
  40. int loc = glGetUniformLocation(prg_id, #NAME); \
  41. VERIFY((loc!=-1) && "uniform not found"); \
  42. glUniform(loc,value); \
  43. glGetUniformiv(prg_id,loc,(GLint*)data.data()); \
  44. if(!value.isApprox(data)) { \
  45. std::cerr << "Expected:\n" << value << "\n" << "got\n" << data << "\n\n"; \
  46. } \
  47. VERIFY_IS_APPROX(value, data); \
  48. }
  49. void printInfoLog(GLuint objectID)
  50. {
  51. int infologLength, charsWritten;
  52. GLchar *infoLog;
  53. glGetProgramiv(objectID,GL_INFO_LOG_LENGTH, &infologLength);
  54. if(infologLength > 0)
  55. {
  56. infoLog = new GLchar[infologLength];
  57. glGetProgramInfoLog(objectID, infologLength, &charsWritten, infoLog);
  58. if (charsWritten>0)
  59. std::cerr << "Shader info : \n" << infoLog << std::endl;
  60. delete[] infoLog;
  61. }
  62. }
  63. GLint createShader(const char* vtx, const char* frg)
  64. {
  65. GLint prg_id = glCreateProgram();
  66. GLint vtx_id = glCreateShader(GL_VERTEX_SHADER);
  67. GLint frg_id = glCreateShader(GL_FRAGMENT_SHADER);
  68. GLint ok;
  69. glShaderSource(vtx_id, 1, &vtx, 0);
  70. glCompileShader(vtx_id);
  71. glGetShaderiv(vtx_id,GL_COMPILE_STATUS,&ok);
  72. if(!ok)
  73. {
  74. std::cerr << "vtx compilation failed\n";
  75. }
  76. glShaderSource(frg_id, 1, &frg, 0);
  77. glCompileShader(frg_id);
  78. glGetShaderiv(frg_id,GL_COMPILE_STATUS,&ok);
  79. if(!ok)
  80. {
  81. std::cerr << "frg compilation failed\n";
  82. }
  83. glAttachShader(prg_id, vtx_id);
  84. glAttachShader(prg_id, frg_id);
  85. glLinkProgram(prg_id);
  86. glGetProgramiv(prg_id,GL_LINK_STATUS,&ok);
  87. if(!ok)
  88. {
  89. std::cerr << "linking failed\n";
  90. }
  91. printInfoLog(prg_id);
  92. glUseProgram(prg_id);
  93. return prg_id;
  94. }
  95. void test_openglsupport()
  96. {
  97. int argc = 0;
  98. glutInit(&argc, 0);
  99. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  100. glutInitWindowPosition (0,0);
  101. glutInitWindowSize(10, 10);
  102. if(glutCreateWindow("Eigen") <= 0)
  103. {
  104. std::cerr << "Error: Unable to create GLUT Window.\n";
  105. exit(1);
  106. }
  107. glewExperimental = GL_TRUE;
  108. if(glewInit() != GLEW_OK)
  109. {
  110. std::cerr << "Warning: Failed to initialize GLEW\n";
  111. }
  112. Vector3f v3f;
  113. Matrix3f rot;
  114. glBegin(GL_POINTS);
  115. glVertex(v3f);
  116. glVertex(2*v3f+v3f);
  117. glVertex(rot*v3f);
  118. glEnd();
  119. // 4x4 matrices
  120. Matrix4f mf44; mf44.setRandom();
  121. VERIFY_MATRIX(glLoadMatrix(mf44), mf44);
  122. VERIFY_MATRIX(glMultMatrix(mf44), mf44);
  123. Matrix4d md44; md44.setRandom();
  124. VERIFY_MATRIX(glLoadMatrix(md44), md44);
  125. VERIFY_MATRIX(glMultMatrix(md44), md44);
  126. // Quaternion
  127. Quaterniond qd(AngleAxisd(internal::random<double>(), Vector3d::Random()));
  128. VERIFY_MATRIX(glRotate(qd), Projective3d(qd).matrix());
  129. Quaternionf qf(AngleAxisf(internal::random<double>(), Vector3f::Random()));
  130. VERIFY_MATRIX(glRotate(qf), Projective3f(qf).matrix());
  131. // 3D Transform
  132. Transform<float,3,AffineCompact> acf3; acf3.matrix().setRandom();
  133. VERIFY_MATRIX(glLoadMatrix(acf3), Projective3f(acf3).matrix());
  134. VERIFY_MATRIX(glMultMatrix(acf3), Projective3f(acf3).matrix());
  135. Transform<float,3,Affine> af3(acf3);
  136. VERIFY_MATRIX(glLoadMatrix(af3), Projective3f(af3).matrix());
  137. VERIFY_MATRIX(glMultMatrix(af3), Projective3f(af3).matrix());
  138. Transform<float,3,Projective> pf3; pf3.matrix().setRandom();
  139. VERIFY_MATRIX(glLoadMatrix(pf3), Projective3f(pf3).matrix());
  140. VERIFY_MATRIX(glMultMatrix(pf3), Projective3f(pf3).matrix());
  141. Transform<double,3,AffineCompact> acd3; acd3.matrix().setRandom();
  142. VERIFY_MATRIX(glLoadMatrix(acd3), Projective3d(acd3).matrix());
  143. VERIFY_MATRIX(glMultMatrix(acd3), Projective3d(acd3).matrix());
  144. Transform<double,3,Affine> ad3(acd3);
  145. VERIFY_MATRIX(glLoadMatrix(ad3), Projective3d(ad3).matrix());
  146. VERIFY_MATRIX(glMultMatrix(ad3), Projective3d(ad3).matrix());
  147. Transform<double,3,Projective> pd3; pd3.matrix().setRandom();
  148. VERIFY_MATRIX(glLoadMatrix(pd3), Projective3d(pd3).matrix());
  149. VERIFY_MATRIX(glMultMatrix(pd3), Projective3d(pd3).matrix());
  150. // translations (2D and 3D)
  151. {
  152. Vector2f vf2; vf2.setRandom(); Vector3f vf23; vf23 << vf2, 0;
  153. VERIFY_MATRIX(glTranslate(vf2), Projective3f(Translation3f(vf23)).matrix());
  154. Vector2d vd2; vd2.setRandom(); Vector3d vd23; vd23 << vd2, 0;
  155. VERIFY_MATRIX(glTranslate(vd2), Projective3d(Translation3d(vd23)).matrix());
  156. Vector3f vf3; vf3.setRandom();
  157. VERIFY_MATRIX(glTranslate(vf3), Projective3f(Translation3f(vf3)).matrix());
  158. Vector3d vd3; vd3.setRandom();
  159. VERIFY_MATRIX(glTranslate(vd3), Projective3d(Translation3d(vd3)).matrix());
  160. Translation<float,3> tf3; tf3.vector().setRandom();
  161. VERIFY_MATRIX(glTranslate(tf3), Projective3f(tf3).matrix());
  162. Translation<double,3> td3; td3.vector().setRandom();
  163. VERIFY_MATRIX(glTranslate(td3), Projective3d(td3).matrix());
  164. }
  165. // scaling (2D and 3D)
  166. {
  167. Vector2f vf2; vf2.setRandom(); Vector3f vf23; vf23 << vf2, 1;
  168. VERIFY_MATRIX(glScale(vf2), Projective3f(Scaling(vf23)).matrix());
  169. Vector2d vd2; vd2.setRandom(); Vector3d vd23; vd23 << vd2, 1;
  170. VERIFY_MATRIX(glScale(vd2), Projective3d(Scaling(vd23)).matrix());
  171. Vector3f vf3; vf3.setRandom();
  172. VERIFY_MATRIX(glScale(vf3), Projective3f(Scaling(vf3)).matrix());
  173. Vector3d vd3; vd3.setRandom();
  174. VERIFY_MATRIX(glScale(vd3), Projective3d(Scaling(vd3)).matrix());
  175. UniformScaling<float> usf(internal::random<float>());
  176. VERIFY_MATRIX(glScale(usf), Projective3f(usf).matrix());
  177. UniformScaling<double> usd(internal::random<double>());
  178. VERIFY_MATRIX(glScale(usd), Projective3d(usd).matrix());
  179. }
  180. // uniform
  181. {
  182. const char* vtx = "void main(void) { gl_Position = gl_Vertex; }\n";
  183. if(GLEW_VERSION_2_0)
  184. {
  185. #ifdef GL_VERSION_2_0
  186. const char* frg = ""
  187. "uniform vec2 v2f;\n"
  188. "uniform vec3 v3f;\n"
  189. "uniform vec4 v4f;\n"
  190. "uniform ivec2 v2i;\n"
  191. "uniform ivec3 v3i;\n"
  192. "uniform ivec4 v4i;\n"
  193. "uniform mat2 m2f;\n"
  194. "uniform mat3 m3f;\n"
  195. "uniform mat4 m4f;\n"
  196. "void main(void) { gl_FragColor = vec4(v2f[0]+v3f[0]+v4f[0])+vec4(v2i[0]+v3i[0]+v4i[0])+vec4(m2f[0][0]+m3f[0][0]+m4f[0][0]); }\n";
  197. GLint prg_id = createShader(vtx,frg);
  198. VERIFY_UNIFORM(fv,v2f, Vector2f);
  199. VERIFY_UNIFORM(fv,v3f, Vector3f);
  200. VERIFY_UNIFORM(fv,v4f, Vector4f);
  201. VERIFY_UNIFORMi(v2i, Vector2i);
  202. VERIFY_UNIFORMi(v3i, Vector3i);
  203. VERIFY_UNIFORMi(v4i, Vector4i);
  204. VERIFY_UNIFORM(fv,m2f, Matrix2f);
  205. VERIFY_UNIFORM(fv,m3f, Matrix3f);
  206. VERIFY_UNIFORM(fv,m4f, Matrix4f);
  207. #endif
  208. }
  209. else
  210. std::cerr << "Warning: opengl 2.0 was not tested\n";
  211. if(GLEW_VERSION_2_1)
  212. {
  213. #ifdef GL_VERSION_2_1
  214. const char* frg = "#version 120\n"
  215. "uniform mat2x3 m23f;\n"
  216. "uniform mat3x2 m32f;\n"
  217. "uniform mat2x4 m24f;\n"
  218. "uniform mat4x2 m42f;\n"
  219. "uniform mat3x4 m34f;\n"
  220. "uniform mat4x3 m43f;\n"
  221. "void main(void) { gl_FragColor = vec4(m23f[0][0]+m32f[0][0]+m24f[0][0]+m42f[0][0]+m34f[0][0]+m43f[0][0]); }\n";
  222. GLint prg_id = createShader(vtx,frg);
  223. typedef Matrix<float,2,3> Matrix23f;
  224. typedef Matrix<float,3,2> Matrix32f;
  225. typedef Matrix<float,2,4> Matrix24f;
  226. typedef Matrix<float,4,2> Matrix42f;
  227. typedef Matrix<float,3,4> Matrix34f;
  228. typedef Matrix<float,4,3> Matrix43f;
  229. VERIFY_UNIFORM(fv,m23f, Matrix23f);
  230. VERIFY_UNIFORM(fv,m32f, Matrix32f);
  231. VERIFY_UNIFORM(fv,m24f, Matrix24f);
  232. VERIFY_UNIFORM(fv,m42f, Matrix42f);
  233. VERIFY_UNIFORM(fv,m34f, Matrix34f);
  234. VERIFY_UNIFORM(fv,m43f, Matrix43f);
  235. #endif
  236. }
  237. else
  238. std::cerr << "Warning: opengl 2.1 was not tested\n";
  239. if(GLEW_VERSION_3_0)
  240. {
  241. #ifdef GL_VERSION_3_0
  242. const char* frg = "#version 150\n"
  243. "uniform uvec2 v2ui;\n"
  244. "uniform uvec3 v3ui;\n"
  245. "uniform uvec4 v4ui;\n"
  246. "out vec4 data;\n"
  247. "void main(void) { data = vec4(v2ui[0]+v3ui[0]+v4ui[0]); }\n";
  248. GLint prg_id = createShader(vtx,frg);
  249. typedef Matrix<unsigned int,2,1> Vector2ui;
  250. typedef Matrix<unsigned int,3,1> Vector3ui;
  251. typedef Matrix<unsigned int,4,1> Vector4ui;
  252. VERIFY_UNIFORMi(v2ui, Vector2ui);
  253. VERIFY_UNIFORMi(v3ui, Vector3ui);
  254. VERIFY_UNIFORMi(v4ui, Vector4ui);
  255. #endif
  256. }
  257. else
  258. std::cerr << "Warning: opengl 3.0 was not tested\n";
  259. #ifdef GLEW_ARB_gpu_shader_fp64
  260. if(GLEW_ARB_gpu_shader_fp64)
  261. {
  262. #ifdef GL_ARB_gpu_shader_fp64
  263. const char* frg = "#version 150\n"
  264. "uniform dvec2 v2d;\n"
  265. "uniform dvec3 v3d;\n"
  266. "uniform dvec4 v4d;\n"
  267. "out vec4 data;\n"
  268. "void main(void) { data = vec4(v2d[0]+v3d[0]+v4d[0]); }\n";
  269. GLint prg_id = createShader(vtx,frg);
  270. typedef Vector2d Vector2d;
  271. typedef Vector3d Vector3d;
  272. typedef Vector4d Vector4d;
  273. VERIFY_UNIFORM(dv,v2d, Vector2d);
  274. VERIFY_UNIFORM(dv,v3d, Vector3d);
  275. VERIFY_UNIFORM(dv,v4d, Vector4d);
  276. #endif
  277. }
  278. else
  279. std::cerr << "Warning: GLEW_ARB_gpu_shader_fp64 was not tested\n";
  280. #else
  281. std::cerr << "Warning: GLEW_ARB_gpu_shader_fp64 was not tested\n";
  282. #endif
  283. }
  284. }