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