openglsupport.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #include <main.h>
00011 #include <iostream>
00012 #include <GL/glew.h>
00013 #include <Eigen/OpenGLSupport>
00014 #include <GL/glut.h>
00015 using namespace Eigen;
00016 
00017 
00018 
00019 
00020 #define VERIFY_MATRIX(CODE,REF) { \
00021     glLoadIdentity(); \
00022     CODE; \
00023     Matrix<float,4,4,ColMajor> m; m.setZero(); \
00024     glGet(GL_MODELVIEW_MATRIX, m); \
00025     if(!(REF).cast<float>().isApprox(m)) { \
00026       std::cerr << "Expected:\n" << ((REF).cast<float>()) << "\n" << "got\n" << m << "\n\n"; \
00027     } \
00028     VERIFY_IS_APPROX((REF).cast<float>(), m); \
00029   }
00030 
00031 #define VERIFY_UNIFORM(SUFFIX,NAME,TYPE) { \
00032     TYPE value; value.setRandom(); \
00033     TYPE data; \
00034     int loc = glGetUniformLocation(prg_id, #NAME); \
00035     VERIFY((loc!=-1) && "uniform not found"); \
00036     glUniform(loc,value); \
00037     EIGEN_CAT(glGetUniform,SUFFIX)(prg_id,loc,data.data()); \
00038     if(!value.isApprox(data)) { \
00039       std::cerr << "Expected:\n" << value << "\n" << "got\n" << data << "\n\n"; \
00040     } \
00041     VERIFY_IS_APPROX(value, data); \
00042   }
00043   
00044 #define VERIFY_UNIFORMi(NAME,TYPE) { \
00045     TYPE value = TYPE::Random().eval().cast<float>().cast<TYPE::Scalar>(); \
00046     TYPE data; \
00047     int loc = glGetUniformLocation(prg_id, #NAME); \
00048     VERIFY((loc!=-1) && "uniform not found"); \
00049     glUniform(loc,value); \
00050     glGetUniformiv(prg_id,loc,(GLint*)data.data()); \
00051     if(!value.isApprox(data)) { \
00052       std::cerr << "Expected:\n" << value << "\n" << "got\n" << data << "\n\n"; \
00053     } \
00054     VERIFY_IS_APPROX(value, data); \
00055   }
00056   
00057 void printInfoLog(GLuint objectID)
00058 {
00059     int infologLength, charsWritten;
00060     GLchar *infoLog;
00061     glGetProgramiv(objectID,GL_INFO_LOG_LENGTH, &infologLength);
00062     if(infologLength > 0)
00063     {
00064         infoLog = new GLchar[infologLength];
00065         glGetProgramInfoLog(objectID, infologLength, &charsWritten, infoLog);
00066         if (charsWritten>0)
00067           std::cerr << "Shader info : \n" << infoLog << std::endl;
00068         delete[] infoLog;
00069     }
00070 }
00071 
00072 GLint createShader(const char* vtx, const char* frg)
00073 {
00074   GLint prg_id = glCreateProgram();
00075   GLint vtx_id = glCreateShader(GL_VERTEX_SHADER);
00076   GLint frg_id = glCreateShader(GL_FRAGMENT_SHADER);
00077   GLint ok;
00078   
00079   glShaderSource(vtx_id, 1, &vtx, 0);
00080   glCompileShader(vtx_id);
00081   glGetShaderiv(vtx_id,GL_COMPILE_STATUS,&ok);
00082   if(!ok)
00083   {
00084     std::cerr << "vtx compilation failed\n";
00085   }
00086   
00087   glShaderSource(frg_id, 1, &frg, 0);
00088   glCompileShader(frg_id);
00089   glGetShaderiv(frg_id,GL_COMPILE_STATUS,&ok);
00090   if(!ok)
00091   {
00092     std::cerr << "frg compilation failed\n";
00093   }
00094   
00095   glAttachShader(prg_id, vtx_id);
00096   glAttachShader(prg_id, frg_id);
00097   glLinkProgram(prg_id);
00098   glGetProgramiv(prg_id,GL_LINK_STATUS,&ok);
00099   if(!ok)
00100   {
00101     std::cerr << "linking failed\n";
00102   }
00103   printInfoLog(prg_id);
00104   
00105   glUseProgram(prg_id);
00106   return prg_id;
00107 }
00108 
00109 void test_openglsupport()
00110 {
00111   int argc = 0;
00112   glutInit(&argc, 0);
00113   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00114   glutInitWindowPosition (0,0);
00115   glutInitWindowSize(10, 10);
00116 
00117   if(glutCreateWindow("Eigen") <= 0)
00118   {
00119     std::cerr << "Error: Unable to create GLUT Window.\n";
00120     exit(1);
00121   }
00122   
00123   glewExperimental = GL_TRUE;
00124   if(glewInit() != GLEW_OK)
00125   {
00126     std::cerr << "Warning: Failed to initialize GLEW\n";
00127   }
00128 
00129   Vector3f v3f;
00130   Matrix3f rot;
00131   glBegin(GL_POINTS);
00132   
00133   glVertex(v3f);
00134   glVertex(2*v3f+v3f);
00135   glVertex(rot*v3f);
00136   
00137   glEnd();
00138   
00139   // 4x4 matrices
00140   Matrix4f mf44; mf44.setRandom();
00141   VERIFY_MATRIX(glLoadMatrix(mf44), mf44);
00142   VERIFY_MATRIX(glMultMatrix(mf44), mf44);
00143   Matrix4d md44; md44.setRandom();
00144   VERIFY_MATRIX(glLoadMatrix(md44), md44);
00145   VERIFY_MATRIX(glMultMatrix(md44), md44);
00146   
00147   // Quaternion
00148   Quaterniond qd(AngleAxisd(internal::random<double>(), Vector3d::Random()));
00149   VERIFY_MATRIX(glRotate(qd), Projective3d(qd).matrix());
00150   
00151   Quaternionf qf(AngleAxisf(internal::random<double>(), Vector3f::Random()));
00152   VERIFY_MATRIX(glRotate(qf), Projective3f(qf).matrix());
00153   
00154   // 3D Transform
00155   Transform<float,3,AffineCompact> acf3; acf3.matrix().setRandom();
00156   VERIFY_MATRIX(glLoadMatrix(acf3), Projective3f(acf3).matrix());
00157   VERIFY_MATRIX(glMultMatrix(acf3), Projective3f(acf3).matrix());
00158   
00159   Transform<float,3,Affine> af3(acf3);
00160   VERIFY_MATRIX(glLoadMatrix(af3), Projective3f(af3).matrix());
00161   VERIFY_MATRIX(glMultMatrix(af3), Projective3f(af3).matrix());
00162   
00163   Transform<float,3,Projective> pf3; pf3.matrix().setRandom();
00164   VERIFY_MATRIX(glLoadMatrix(pf3), Projective3f(pf3).matrix());
00165   VERIFY_MATRIX(glMultMatrix(pf3), Projective3f(pf3).matrix());
00166   
00167   Transform<double,3,AffineCompact> acd3; acd3.matrix().setRandom();
00168   VERIFY_MATRIX(glLoadMatrix(acd3), Projective3d(acd3).matrix());
00169   VERIFY_MATRIX(glMultMatrix(acd3), Projective3d(acd3).matrix());
00170   
00171   Transform<double,3,Affine> ad3(acd3);
00172   VERIFY_MATRIX(glLoadMatrix(ad3), Projective3d(ad3).matrix());
00173   VERIFY_MATRIX(glMultMatrix(ad3), Projective3d(ad3).matrix());
00174   
00175   Transform<double,3,Projective> pd3; pd3.matrix().setRandom();
00176   VERIFY_MATRIX(glLoadMatrix(pd3), Projective3d(pd3).matrix());
00177   VERIFY_MATRIX(glMultMatrix(pd3), Projective3d(pd3).matrix());
00178   
00179   // translations (2D and 3D)
00180   {
00181     Vector2f vf2; vf2.setRandom(); Vector3f vf23; vf23 << vf2, 0;
00182     VERIFY_MATRIX(glTranslate(vf2), Projective3f(Translation3f(vf23)).matrix());
00183     Vector2d vd2; vd2.setRandom(); Vector3d vd23; vd23 << vd2, 0;
00184     VERIFY_MATRIX(glTranslate(vd2), Projective3d(Translation3d(vd23)).matrix());
00185     
00186     Vector3f vf3; vf3.setRandom();
00187     VERIFY_MATRIX(glTranslate(vf3), Projective3f(Translation3f(vf3)).matrix());
00188     Vector3d vd3; vd3.setRandom();
00189     VERIFY_MATRIX(glTranslate(vd3), Projective3d(Translation3d(vd3)).matrix());
00190     
00191     Translation<float,3> tf3; tf3.vector().setRandom();
00192     VERIFY_MATRIX(glTranslate(tf3), Projective3f(tf3).matrix());
00193     
00194     Translation<double,3> td3;  td3.vector().setRandom();
00195     VERIFY_MATRIX(glTranslate(td3), Projective3d(td3).matrix());
00196   }
00197   
00198   // scaling (2D and 3D)
00199   {
00200     Vector2f vf2; vf2.setRandom(); Vector3f vf23; vf23 << vf2, 1;
00201     VERIFY_MATRIX(glScale(vf2), Projective3f(Scaling(vf23)).matrix());
00202     Vector2d vd2; vd2.setRandom(); Vector3d vd23; vd23 << vd2, 1;
00203     VERIFY_MATRIX(glScale(vd2), Projective3d(Scaling(vd23)).matrix());
00204     
00205     Vector3f vf3; vf3.setRandom();
00206     VERIFY_MATRIX(glScale(vf3), Projective3f(Scaling(vf3)).matrix());
00207     Vector3d vd3; vd3.setRandom();
00208     VERIFY_MATRIX(glScale(vd3), Projective3d(Scaling(vd3)).matrix());
00209     
00210     UniformScaling<float> usf(internal::random<float>());
00211     VERIFY_MATRIX(glScale(usf), Projective3f(usf).matrix());
00212     
00213     UniformScaling<double> usd(internal::random<double>());
00214     VERIFY_MATRIX(glScale(usd), Projective3d(usd).matrix());
00215   }
00216   
00217   // uniform
00218   {
00219     const char* vtx = "void main(void) { gl_Position = gl_Vertex; }\n";
00220     
00221     if(GLEW_VERSION_2_0)
00222     {
00223       #ifdef GL_VERSION_2_0
00224       const char* frg = ""
00225         "uniform vec2 v2f;\n"
00226         "uniform vec3 v3f;\n"
00227         "uniform vec4 v4f;\n"
00228         "uniform ivec2 v2i;\n"
00229         "uniform ivec3 v3i;\n"
00230         "uniform ivec4 v4i;\n"
00231         "uniform mat2 m2f;\n"
00232         "uniform mat3 m3f;\n"
00233         "uniform mat4 m4f;\n"
00234         "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";
00235         
00236       GLint prg_id = createShader(vtx,frg);
00237       
00238       VERIFY_UNIFORM(fv,v2f, Vector2f);
00239       VERIFY_UNIFORM(fv,v3f, Vector3f);
00240       VERIFY_UNIFORM(fv,v4f, Vector4f);
00241       VERIFY_UNIFORMi(v2i, Vector2i);
00242       VERIFY_UNIFORMi(v3i, Vector3i);
00243       VERIFY_UNIFORMi(v4i, Vector4i);
00244       VERIFY_UNIFORM(fv,m2f, Matrix2f);
00245       VERIFY_UNIFORM(fv,m3f, Matrix3f);
00246       VERIFY_UNIFORM(fv,m4f, Matrix4f);
00247       #endif
00248     }
00249     else
00250       std::cerr << "Warning: opengl 2.0 was not tested\n";
00251     
00252     if(GLEW_VERSION_2_1)
00253     {
00254       #ifdef GL_VERSION_2_1
00255       const char* frg = "#version 120\n"
00256         "uniform mat2x3 m23f;\n"
00257         "uniform mat3x2 m32f;\n"
00258         "uniform mat2x4 m24f;\n"
00259         "uniform mat4x2 m42f;\n"
00260         "uniform mat3x4 m34f;\n"
00261         "uniform mat4x3 m43f;\n"
00262         "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";
00263         
00264       GLint prg_id = createShader(vtx,frg);
00265       
00266       typedef Matrix<float,2,3> Matrix23f;
00267       typedef Matrix<float,3,2> Matrix32f;
00268       typedef Matrix<float,2,4> Matrix24f;
00269       typedef Matrix<float,4,2> Matrix42f;
00270       typedef Matrix<float,3,4> Matrix34f;
00271       typedef Matrix<float,4,3> Matrix43f;
00272       
00273       VERIFY_UNIFORM(fv,m23f, Matrix23f);
00274       VERIFY_UNIFORM(fv,m32f, Matrix32f);
00275       VERIFY_UNIFORM(fv,m24f, Matrix24f);
00276       VERIFY_UNIFORM(fv,m42f, Matrix42f);
00277       VERIFY_UNIFORM(fv,m34f, Matrix34f);
00278       VERIFY_UNIFORM(fv,m43f, Matrix43f);
00279       #endif
00280     }
00281     else
00282       std::cerr << "Warning: opengl 2.1 was not tested\n";
00283     
00284     if(GLEW_VERSION_3_0)
00285     {
00286       #ifdef GL_VERSION_3_0
00287       const char* frg = "#version 150\n"
00288         "uniform uvec2 v2ui;\n"
00289         "uniform uvec3 v3ui;\n"
00290         "uniform uvec4 v4ui;\n"
00291         "out vec4 data;\n"
00292         "void main(void) { data = vec4(v2ui[0]+v3ui[0]+v4ui[0]); }\n";
00293         
00294       GLint prg_id = createShader(vtx,frg);
00295       
00296       typedef Matrix<unsigned int,2,1> Vector2ui;
00297       typedef Matrix<unsigned int,3,1> Vector3ui;
00298       typedef Matrix<unsigned int,4,1> Vector4ui;
00299       
00300       VERIFY_UNIFORMi(v2ui, Vector2ui);
00301       VERIFY_UNIFORMi(v3ui, Vector3ui);
00302       VERIFY_UNIFORMi(v4ui, Vector4ui);
00303       #endif
00304     }
00305     else
00306       std::cerr << "Warning: opengl 3.0 was not tested\n";
00307     
00308     #ifdef GLEW_ARB_gpu_shader_fp64
00309     if(GLEW_ARB_gpu_shader_fp64)
00310     {
00311       #ifdef GL_ARB_gpu_shader_fp64
00312       const char* frg = "#version 150\n"
00313         "uniform dvec2 v2d;\n"
00314         "uniform dvec3 v3d;\n"
00315         "uniform dvec4 v4d;\n"
00316         "out vec4 data;\n"
00317         "void main(void) { data = vec4(v2d[0]+v3d[0]+v4d[0]); }\n";
00318         
00319       GLint prg_id = createShader(vtx,frg);
00320       
00321       typedef Vector2d Vector2d;
00322       typedef Vector3d Vector3d;
00323       typedef Vector4d Vector4d;
00324       
00325       VERIFY_UNIFORM(dv,v2d, Vector2d);
00326       VERIFY_UNIFORM(dv,v3d, Vector3d);
00327       VERIFY_UNIFORM(dv,v4d, Vector4d);
00328       #endif
00329     }
00330     else
00331       std::cerr << "Warning: GLEW_ARB_gpu_shader_fp64 was not tested\n";
00332     #else
00333       std::cerr << "Warning: GLEW_ARB_gpu_shader_fp64 was not tested\n";
00334     #endif
00335   }
00336   
00337 }


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:33:33