diff options
author | Gael Guennebaud <g.gael@free.fr> | 2010-10-12 10:39:28 +0200 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2010-10-12 10:39:28 +0200 |
commit | 20be8ad91e7a97c5564281312409d5532c9f64b0 (patch) | |
tree | 50ca1342ea6387048c8a54876df4d4b7e89444c3 /unsupported/test | |
parent | b8bb80400751b97a9ee8872efa7def6ebbb4b697 (diff) |
add support for uniforms
Diffstat (limited to 'unsupported/test')
-rw-r--r-- | unsupported/test/CMakeLists.txt | 6 | ||||
-rw-r--r-- | unsupported/test/openglsupport.cpp | 175 |
2 files changed, 177 insertions, 4 deletions
diff --git a/unsupported/test/CMakeLists.txt b/unsupported/test/CMakeLists.txt index f6e561914..9204657e5 100644 --- a/unsupported/test/CMakeLists.txt +++ b/unsupported/test/CMakeLists.txt @@ -87,9 +87,11 @@ endif() find_package(OpenGL) find_package(GLUT) -if(OPENGL_FOUND AND GLUT_FOUND AND GLUT_LIBRARIES) +find_package(GLEW) +if(OPENGL_FOUND AND GLUT_FOUND AND GLUT_LIBRARIES AND GLEW_FOUND) ei_add_property(EIGEN_TESTED_BACKENDS "opengl, ") - ei_add_test(openglsupport "" "${GLUT_LIBRARIES}" ) + set(EIGEN_GL_LIB ${GLUT_LIBRARIES} ${GLEW_LIBRARIES}) + ei_add_test(openglsupport "" "${EIGEN_GL_LIB}" ) else() ei_add_property(EIGEN_MISSING_BACKENDS "opengl, ") endif() diff --git a/unsupported/test/openglsupport.cpp b/unsupported/test/openglsupport.cpp index 065679a40..3f67eed52 100644 --- a/unsupported/test/openglsupport.cpp +++ b/unsupported/test/openglsupport.cpp @@ -24,8 +24,9 @@ #include <main.h> #include <iostream> -#include <GL/glut.h> +#include <GL/glew.h> #include <Eigen/OpenGLSupport> +#include <GL/glut.h> using namespace Eigen; @@ -42,6 +43,84 @@ using namespace Eigen; VERIFY_IS_APPROX((REF).cast<float>(), m); \ } +#define VERIFY_UNIFORM(NAME,TYPE) { \ + TYPE value; value.setRandom(); \ + TYPE data; \ + int loc = glGetUniformLocation(prg_id, #NAME); \ + VERIFY((loc!=-1) && "uniform not found"); \ + glUniform(loc,value); \ + glGetUniformfv(prg_id,loc,data.data()); \ + if(!value.isApprox(data)) { \ + std::cerr << "Expected:\n" << value << "\n" << "got\n" << data << "\n\n"; \ + } \ + VERIFY_IS_APPROX(value, data); \ + } + +#define VERIFY_UNIFORMi(NAME,TYPE) { \ + TYPE value; value.setRandom(); \ + TYPE data; \ + int loc = glGetUniformLocation(prg_id, #NAME); \ + VERIFY((loc!=-1) && "uniform not found"); \ + glUniform(loc,value); \ + glGetUniformiv(prg_id,loc,(GLint*)data.data()); \ + if(!value.isApprox(data)) { \ + std::cerr << "Expected:\n" << value << "\n" << "got\n" << data << "\n\n"; \ + } \ + VERIFY_IS_APPROX(value, data); \ + } + +void printInfoLog(GLuint objectID) +{ + int infologLength, charsWritten; + GLchar *infoLog; + glGetProgramiv(objectID,GL_INFO_LOG_LENGTH, &infologLength); + if(infologLength > 0) + { + infoLog = new GLchar[infologLength]; + glGetProgramInfoLog(objectID, infologLength, &charsWritten, infoLog); + if (charsWritten>0) + std::cerr << "Shader info : \n" << infoLog << std::endl; + delete[] infoLog; + } +} + +GLint createShader(const char* vtx, const char* frg) +{ + GLint prg_id = glCreateProgram(); + GLint vtx_id = glCreateShader(GL_VERTEX_SHADER); + GLint frg_id = glCreateShader(GL_FRAGMENT_SHADER); + GLint ok; + + glShaderSource(vtx_id, 1, &vtx, 0); + glCompileShader(vtx_id); + glGetShaderiv(vtx_id,GL_COMPILE_STATUS,&ok); + if(!ok) + { + std::cerr << "vtx compilation failed\n"; + } + + glShaderSource(frg_id, 1, &frg, 0); + glCompileShader(frg_id); + glGetShaderiv(frg_id,GL_COMPILE_STATUS,&ok); + if(!ok) + { + std::cerr << "frg compilation failed\n"; + } + + glAttachShader(prg_id, vtx_id); + glAttachShader(prg_id, frg_id); + glLinkProgram(prg_id); + glGetProgramiv(prg_id,GL_LINK_STATUS,&ok); + if(!ok) + { + std::cerr << "linking failed\n"; + } + printInfoLog(prg_id); + + glUseProgram(prg_id); + return prg_id; +} + void test_openglsupport() { int argc = 0; @@ -52,9 +131,15 @@ void test_openglsupport() if(glutCreateWindow("Eigen") <= 0) { - std::cerr << "Unable to create GLUT Window.\n"; + std::cerr << "Error: Unable to create GLUT Window.\n"; exit(1); } + + glewExperimental = GL_TRUE; + if(glewInit() != GLEW_OK) + { + std::cerr << "Warning: Failed to initialize GLEW\n"; + } Vector3f v3f; Matrix3f rot; @@ -144,4 +229,90 @@ void test_openglsupport() VERIFY_MATRIX(glScale(usd), Projective3d(usd).matrix()); } + // uniform + { + const char* vtx = "void main(void) { gl_Position = gl_Vertex; }\n"; + + if(GLEW_VERSION_2_0) + { + const char* frg = "" + "uniform vec2 v2f;\n" + "uniform vec3 v3f;\n" + "uniform vec4 v4f;\n" + "uniform ivec2 v2i;\n" + "uniform ivec3 v3i;\n" + "uniform ivec4 v4i;\n" + "uniform mat2 m2f;\n" + "uniform mat3 m3f;\n" + "uniform mat4 m4f;\n" + "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"; + + GLint prg_id = createShader(vtx,frg); + + VERIFY_UNIFORM(v2f, Vector2f); + VERIFY_UNIFORM(v3f, Vector3f); + VERIFY_UNIFORM(v4f, Vector4f); + VERIFY_UNIFORMi(v2i, Vector2i); + VERIFY_UNIFORMi(v3i, Vector3i); + VERIFY_UNIFORMi(v4i, Vector4i); + VERIFY_UNIFORM(m2f, Matrix2f); + VERIFY_UNIFORM(m3f, Matrix3f); + VERIFY_UNIFORM(m4f, Matrix4f); + } + else + std::cerr << "Warning: opengl 2.0 was not tested\n"; + + if(GLEW_VERSION_2_1) + { + const char* frg = "#version 120\n" + "uniform mat2x3 m23f;\n" + "uniform mat3x2 m32f;\n" + "uniform mat2x4 m24f;\n" + "uniform mat4x2 m42f;\n" + "uniform mat3x4 m34f;\n" + "uniform mat4x3 m43f;\n" + "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"; + + GLint prg_id = createShader(vtx,frg); + + typedef Matrix<float,2,3> Matrix23f; + typedef Matrix<float,3,2> Matrix32f; + typedef Matrix<float,2,4> Matrix24f; + typedef Matrix<float,4,2> Matrix42f; + typedef Matrix<float,3,4> Matrix34f; + typedef Matrix<float,4,3> Matrix43f; + + VERIFY_UNIFORM(m23f, Matrix23f); + VERIFY_UNIFORM(m32f, Matrix32f); + VERIFY_UNIFORM(m24f, Matrix24f); + VERIFY_UNIFORM(m42f, Matrix42f); + VERIFY_UNIFORM(m34f, Matrix34f); + VERIFY_UNIFORM(m43f, Matrix43f); + } + else + std::cerr << "Warning: opengl 2.1 was not tested\n"; + + if(GLEW_VERSION_3_0) + { + const char* frg = "#version 150\n" + "uniform uvec2 v2ui;\n" + "uniform uvec3 v3ui;\n" + "uniform uvec4 v4ui;\n" + "out vec4 data;\n" + "void main(void) { data = vec4(v2ui[0]+v3ui[0]+v4ui[0]); }\n"; + + GLint prg_id = createShader(vtx,frg); + + typedef Matrix<unsigned int,2,1> Vector2ui; + typedef Matrix<unsigned int,3,1> Vector3ui; + typedef Matrix<unsigned int,4,1> Vector4ui; + + VERIFY_UNIFORMi(v2ui, Vector2ui); +// VERIFY_UNIFORMi(v3ui, Vector3ui); +// VERIFY_UNIFORMi(v4ui, Vector4ui); + } + else + std::cerr << "Warning: opengl 3.0 was not tested\n"; + } + } |