From 20be8ad91e7a97c5564281312409d5532c9f64b0 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Tue, 12 Oct 2010 10:39:28 +0200 Subject: add support for uniforms --- unsupported/test/openglsupport.cpp | 175 ++++++++++++++++++++++++++++++++++++- 1 file changed, 173 insertions(+), 2 deletions(-) (limited to 'unsupported/test/openglsupport.cpp') 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 #include -#include +#include #include +#include using namespace Eigen; @@ -42,6 +43,84 @@ using namespace Eigen; VERIFY_IS_APPROX((REF).cast(), 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 Matrix23f; + typedef Matrix Matrix32f; + typedef Matrix Matrix24f; + typedef Matrix Matrix42f; + typedef Matrix Matrix34f; + typedef Matrix 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 Vector2ui; + typedef Matrix Vector3ui; + typedef Matrix Vector4ui; + + VERIFY_UNIFORMi(v2ui, Vector2ui); +// VERIFY_UNIFORMi(v3ui, Vector3ui); +// VERIFY_UNIFORMi(v4ui, Vector4ui); + } + else + std::cerr << "Warning: opengl 3.0 was not tested\n"; + } + } -- cgit v1.2.3