14 #if defined(__APPLE_CC__)
16 #define GL_SILENCE_DEPRECATION 1
19 #include <Eigen/OpenGLSupport>
20 #if defined(__APPLE_CC__)
21 #include <GLUT/glut.h>
23 #include <GL/freeglut.h>
26 using namespace Eigen;
28 #define VERIFY_MATRIX(CODE,REF) { \
29 glMatrixMode(GL_MODELVIEW); \
32 Matrix<float,4,4,ColMajor> m; m.setZero(); \
33 glGet(GL_MODELVIEW_MATRIX, m); \
34 if(!(REF).cast<float>().isApprox(m)) { \
35 std::cerr << "Expected:\n" << ((REF).cast<float>()) << "\n" << "got\n" << m << "\n\n"; \
37 VERIFY_IS_APPROX((REF).cast<float>(), m); \
40 #define VERIFY_UNIFORM(SUFFIX,NAME,TYPE) { \
41 TYPE value; value.setRandom(); \
43 int loc = glGetUniformLocation(prg_id, #NAME); \
44 VERIFY((loc!=-1) && "uniform not found"); \
45 glUniform(loc,value); \
46 EIGEN_CAT(glGetUniform,SUFFIX)(prg_id,loc,data.data()); \
47 if(!value.isApprox(data)) { \
48 std::cerr << "Expected:\n" << value << "\n" << "got\n" << data << "\n\n"; \
50 VERIFY_IS_APPROX(value, data); \
53 #define VERIFY_UNIFORMi(NAME,TYPE) { \
54 TYPE value = TYPE::Random().eval().cast<float>().cast<TYPE::Scalar>(); \
56 int loc = glGetUniformLocation(prg_id, #NAME); \
57 VERIFY((loc!=-1) && "uniform not found"); \
58 glUniform(loc,value); \
59 glGetUniformiv(prg_id,loc,(GLint*)data.data()); \
60 if(!value.isApprox(data)) { \
61 std::cerr << "Expected:\n" << value << "\n" << "got\n" << data << "\n\n"; \
63 VERIFY_IS_APPROX(value, data); \
68 int infologLength, charsWritten;
70 glGetProgramiv(objectID, GL_INFO_LOG_LENGTH, &infologLength);
73 infoLog =
new GLchar[infologLength];
74 glGetProgramInfoLog(objectID, infologLength, &charsWritten, infoLog);
76 std::cerr <<
"Program info : \n" << infoLog << std::endl;
83 int infologLength, charsWritten;
85 glGetShaderiv(objectID, GL_INFO_LOG_LENGTH, &infologLength);
88 infoLog =
new GLchar[infologLength];
89 glGetShaderInfoLog(objectID, infologLength, &charsWritten, infoLog);
91 std::cerr <<
"Shader info : \n" << infoLog << std::endl;
96 GLint
createProgram(
const char* vtx,
const char* frg,
bool print_errors =
true)
98 GLint prg_id = glCreateProgram();
99 GLint vtx_id = glCreateShader(GL_VERTEX_SHADER);
100 GLint frg_id = glCreateShader(GL_FRAGMENT_SHADER);
103 glShaderSource(vtx_id, 1, &vtx, 0);
104 glCompileShader(vtx_id);
105 glGetShaderiv(vtx_id, GL_COMPILE_STATUS, &ok);
110 std::cerr <<
"vtx compilation failed\n";
111 std::cerr <<
"Source:\n" << vtx <<
"\n";
114 glDeleteShader(vtx_id);
118 glShaderSource(frg_id, 1, &frg, 0);
119 glCompileShader(frg_id);
120 glGetShaderiv(frg_id, GL_COMPILE_STATUS, &ok);
125 std::cerr <<
"frg compilation failed.\n";
126 std::cerr <<
"Source:\n" << frg <<
"\n";
129 glDeleteShader(vtx_id);
130 glDeleteShader(frg_id);
134 glAttachShader(prg_id, vtx_id);
135 glAttachShader(prg_id, frg_id);
136 glLinkProgram(prg_id);
139 glDeleteShader(vtx_id);
140 glDeleteShader(frg_id);
141 glGetProgramiv(prg_id, GL_LINK_STATUS, &ok);
146 std::cerr <<
"linking failed.\n";
149 glDeleteProgram(prg_id);
153 glUseProgram(prg_id);
157 GLint
createProgram(
const std::string& vtx,
const std::string& frg,
bool print_errors =
true)
159 return createProgram(vtx.c_str(), frg.c_str(), print_errors);
164 switch (gl_major_version)
167 switch (gl_minor_version)
170 return "#version 110";
172 return "#version 120";
176 switch (gl_minor_version)
179 return "#version 130";
181 return "#version 140";
183 return "#version 150";
185 return "#version 330";
189 switch (gl_minor_version)
192 return "#version 400";
194 return "#version 410";
196 return "#version 420";
198 return "#version 430";
200 return "#version 440";
202 return "#version 450";
204 return "#version 460";
213 const std::string& find,
214 const std::string& replace)
217 size_t flen = find.length();
218 size_t rlen = replace.length();
219 while ( (
loc =
str.find(find,
loc)) != std::string::npos) {
220 str.replace(
loc, flen, replace);
227 const std::string&
str,
228 const std::vector<std::string>& find,
229 const std::vector<std::string>& replace)
243 const GLubyte* gl_version_string = glGetString(GL_VERSION);
244 std::cerr <<
"GL version: " << gl_version_string << std::endl;
245 std::cerr <<
"GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
248 GLint gl_major_version = gl_version_string[0] -
'0';
249 GLint gl_minor_version = gl_version_string[2] -
'0';
250 bool legacy_gl = gl_major_version < 3 || (gl_major_version == 3 && gl_minor_version < 2);
267 Matrix4f mf44; mf44.setRandom();
270 Matrix4d md44; md44.setRandom();
308 Vector2f vf2; vf2.setRandom(); Vector3f vf23; vf23 << vf2, 0;
310 Vector2d vd2; vd2.setRandom(); Vector3d vd23; vd23 << vd2, 0;
313 Vector3f vf3; vf3.setRandom();
315 Vector3d vd3; vd3.setRandom();
327 Vector2f vf2; vf2.setRandom(); Vector3f vf23; vf23 << vf2, 1;
329 Vector2d vd2; vd2.setRandom(); Vector3d vd23; vd23 << vd2, 1;
332 Vector3f vf3; vf3.setRandom();
334 Vector3d vd3; vd3.setRandom();
344 std::cerr <<
"Warning: fixed-function pipeline was not tested.\n";
353 std::vector<std::string> glsl_vars;
354 glsl_vars.push_back(
"${GLSL_VERSION}");
355 glsl_vars.push_back(
"${FRAG_OUTPUT_DECLARATION}");
356 glsl_vars.push_back(
"${FRAG_OUTPUT_VARIABLE}");
358 std::vector<std::string> glsl_vals;
360 if (gl_major_version >= 3) {
361 glsl_vals.push_back(
"out vec4 fragColor;");
362 glsl_vals.push_back(
"fragColor");
364 glsl_vals.push_back(
"");
365 glsl_vals.push_back(
"gl_FragColor");
373 "void main(void) {\n"
374 " gl_Position = vec4(0,0,0,1);\n"
376 glsl_vars, glsl_vals);
378 #ifdef GL_VERSION_2_0
379 if(GLEW_VERSION_2_0 && GL_VERSION_2_0)
383 "uniform vec2 v2f;\n"
384 "uniform vec3 v3f;\n"
385 "uniform vec4 v4f;\n"
386 "uniform ivec2 v2i;\n"
387 "uniform ivec3 v3i;\n"
388 "uniform ivec4 v4i;\n"
389 "uniform mat2 m2f;\n"
390 "uniform mat3 m3f;\n"
391 "uniform mat4 m4f;\n"
392 "${FRAG_OUTPUT_DECLARATION}\n"
393 "void main(void) { \n"
394 " ${FRAG_OUTPUT_VARIABLE} = 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"
396 glsl_vars, glsl_vals);
399 VERIFY(prg_id > 0 &&
"Failed to create program.");
409 glDeleteProgram(prg_id);
413 std::cerr <<
"Warning: opengl 2.0 was not tested.\n";
415 #ifdef GL_VERSION_2_1
416 if(GLEW_VERSION_2_1 && GL_VERSION_2_1 &&
417 (gl_major_version > 2 || (gl_major_version == 2 && gl_minor_version >= 1)))
421 "uniform mat2x3 m23f;\n"
422 "uniform mat3x2 m32f;\n"
423 "uniform mat2x4 m24f;\n"
424 "uniform mat4x2 m42f;\n"
425 "uniform mat3x4 m34f;\n"
426 "uniform mat4x3 m43f;\n"
427 "${FRAG_OUTPUT_DECLARATION}\n"
428 "void main(void) {\n"
429 " ${FRAG_OUTPUT_VARIABLE} = vec4(m23f[0][0]+m32f[0][0]+m24f[0][0]+m42f[0][0]+m34f[0][0]+m43f[0][0]);\n"
431 glsl_vars, glsl_vals);
434 VERIFY(prg_id > 0 &&
"Failed to create program.");
448 glDeleteProgram(prg_id);
452 std::cerr <<
"Warning: opengl 2.1 was not tested.\n";
454 #ifdef GL_VERSION_3_0
455 if(GLEW_VERSION_3_0 && GL_VERSION_3_0 && gl_major_version >= 3)
459 "uniform uvec2 v2ui;\n"
460 "uniform uvec3 v3ui;\n"
461 "uniform uvec4 v4ui;\n"
462 "${FRAG_OUTPUT_DECLARATION}\n"
463 "void main(void) {\n"
464 " ${FRAG_OUTPUT_VARIABLE} = vec4(v2ui[0]+v3ui[0]+v4ui[0]);\n"
466 glsl_vars, glsl_vals);
469 VERIFY(prg_id > 0 &&
"Failed to create program.");
477 glDeleteProgram(prg_id);
481 std::cerr <<
"Warning: opengl 3.0 was not tested.\n";
484 bool has_fp64_native = (gl_major_version == 4 && gl_minor_version >= 1);
485 bool has_fp64_extension =
false;
486 #ifdef GLEW_ARB_gpu_shader_fp64
487 if(GLEW_ARB_gpu_shader_fp64)
490 if (has_fp64_extension)
494 "#extension GL_ARB_gpu_shader_fp64 : enable\n"
495 "uniform dvec2 dv2;\n"
496 "${FRAG_OUTPUT_DECLARATION}\n"
497 "void main(void) {\n"
498 " ${FRAG_OUTPUT_VARIABLE} = vec4(dv2.x, dv2.y, dv2.x, dv2.y);\n"
500 glsl_vars, glsl_vals);
504 has_fp64_extension =
true;
505 glDeleteProgram(prg_id);
511 if( has_fp64_native || has_fp64_extension )
513 std::vector<std::string> glsl_vars_with_extension = glsl_vars;
514 glsl_vars_with_extension.push_back(
"${GLSL_EXTENSIONS}");
515 std::vector<std::string> glsl_vals_with_extension = glsl_vals;
516 if (has_fp64_extension)
518 glsl_vals_with_extension.push_back(
"#extension GL_ARB_gpu_shader_fp64 : enable");
522 glsl_vals_with_extension.push_back(
"");
527 "${GLSL_EXTENSIONS}\n"
528 "uniform dvec2 v2d;\n"
529 "uniform dvec3 v3d;\n"
530 "uniform dvec4 v4d;\n"
531 "${FRAG_OUTPUT_DECLARATION}\n"
532 "void main(void) {\n"
533 " ${FRAG_OUTPUT_VARIABLE} = vec4(v2d[0]+v3d[0]+v4d[0]);\n"
535 glsl_vars_with_extension, glsl_vals_with_extension);
538 VERIFY(prg_id > 0 &&
"Failed to create program.");
542 glDeleteProgram(prg_id);
545 std::cerr <<
"Warning: dvec (fp64) was not tested.\n";
564 GLint glut_display_mode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
566 #ifndef EIGEN_LEGACY_OPENGL
568 #if defined(__APPLE_CC__)
569 glut_display_mode |= GLUT_3_2_CORE_PROFILE;
570 #elif defined(FREEGLUT)
571 glutInitContextVersion(3, 2);
572 glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
573 glutInitContextProfile(GLUT_CORE_PROFILE);
577 glutInitDisplayMode(glut_display_mode);
578 glutInitWindowPosition(0, 0);
579 glutInitWindowSize(10, 10);
581 int window = glutCreateWindow(
"Eigen");
584 std::cerr <<
"Error: Unable to create GLUT Window.\n";
588 glewExperimental = GL_TRUE;
589 if(glewInit() != GLEW_OK)
591 std::cerr <<
"Warning: Failed to initialize GLEW.\n";
599 glutDestroyWindow(window);