diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-04-12 20:36:39 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-04-12 20:36:39 -0700 |
commit | 3818506c0f6e29e9ce4f73ae053e3c35d4245adb (patch) | |
tree | 03e3f3bae5bbef105ccc1d858645cfe18346ea16 /tests | |
parent | 30b6096c5b066b8e073b34f4a20337aa534bf8fb (diff) |
add glReadPixels and sdl/gl test for it
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/runner.py | 6 | ||||
-rw-r--r-- | tests/sdl_gl_read.c | 155 |
2 files changed, 161 insertions, 0 deletions
diff --git a/tests/runner.py b/tests/runner.py index 06d143f7..a479cf9f 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -6997,6 +6997,12 @@ elif 'browser' in str(sys.argv): Popen(['python', EMCC, '-O2', '--minify', '0', os.path.join(self.get_dir(), 'sdl_audio.c'), '--preload-file', 'sound.ogg', '--preload-file', 'sound2.wav', '-o', 'page.html', '-s', 'EXPORTED_FUNCTIONS=["_main", "_play", "_play2"]']).communicate() self.run_browser('page.html', '', '/report_result?1') + def test_sdl_gl_read(self): + # SDL, OpenGL, readPixels + open(os.path.join(self.get_dir(), 'sdl_gl_read.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_gl_read.c')).read())) + Popen(['python', EMCC, os.path.join(self.get_dir(), 'sdl_gl_read.c'), '-o', 'something.html']).communicate() + self.run_browser('something.html', '.', '/report_result?1') + def zzztest_sdl_ogl(self): # SDL, OpenGL, textures, immediate mode shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) diff --git a/tests/sdl_gl_read.c b/tests/sdl_gl_read.c new file mode 100644 index 00000000..552eb8c0 --- /dev/null +++ b/tests/sdl_gl_read.c @@ -0,0 +1,155 @@ +// Built from glbook/hello triange and sdl_ogl, see details there + +#include "SDL/SDL.h" +#include "SDL/SDL_image.h" +#include "SDL/SDL_opengl.h" + +#include <stdio.h> +#include <stdlib.h> + +GLuint programObject; +int width = 512; +int height = 256; + +GLuint LoadShader ( GLenum type, const char *shaderSrc ) +{ + GLuint shader; + GLint compiled; + + shader = glCreateShader ( type ); + if ( shader == 0 ) + return 0; + + glShaderSource ( shader, 1, &shaderSrc, NULL ); + glCompileShader ( shader ); + glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled ); + if ( !compiled ) + { + GLint infoLen = 0; + glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &infoLen ); + if ( infoLen > 1 ) + { + char* infoLog = malloc (sizeof(char) * infoLen ); + glGetShaderInfoLog ( shader, infoLen, NULL, infoLog ); + printf ( "Error compiling shader:\n%s\n", infoLog ); + free ( infoLog ); + } + glDeleteShader ( shader ); + return 0; + } + return shader; +} + +int Init () +{ + GLbyte vShaderStr[] = + "attribute vec4 vPosition; \n" + "void main() \n" + "{ \n" + " gl_Position = vPosition; \n" + "} \n"; + + GLbyte fShaderStr[] = + "precision mediump float;\n"\ + "void main() \n" + "{ \n" + " gl_FragColor = vec4 ( 0.0, 0.0, 1.0, 1.0 );\n" + "} \n"; + + GLuint vertexShader; + GLuint fragmentShader; + GLint linked; + + vertexShader = LoadShader ( GL_VERTEX_SHADER, vShaderStr ); + fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fShaderStr ); + + programObject = glCreateProgram ( ); + if ( programObject == 0 ) + return 0; + + glAttachShader ( programObject, vertexShader ); + glAttachShader ( programObject, fragmentShader ); + glBindAttribLocation ( programObject, 0, "vPosition" ); + glLinkProgram ( programObject ); + glGetProgramiv ( programObject, GL_LINK_STATUS, &linked ); + if ( !linked ) + { + GLint infoLen = 0; + glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen ); + if ( infoLen > 1 ) + { + char* infoLog = malloc (sizeof(char) * infoLen ); + glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog ); + printf ( "Error linking program:\n%s\n", infoLog ); + free ( infoLog ); + } + glDeleteProgram ( programObject ); + return GL_FALSE; + } + + glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); + return GL_TRUE; +} + +/// +// Draw a triangle using the shader pair created in Init() +// +void Draw () +{ + GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f, + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f }; + + // No clientside arrays, so do this in a webgl-friendly manner + GLuint vertexPosObject; + glGenBuffers(1, &vertexPosObject); + glBindBuffer(GL_ARRAY_BUFFER, vertexPosObject); + glBufferData(GL_ARRAY_BUFFER, 9*4, vVertices, GL_STATIC_DRAW); + + glViewport ( 0, 0, width, height ); + glClear ( GL_COLOR_BUFFER_BIT ); + glUseProgram ( programObject ); + + glBindBuffer(GL_ARRAY_BUFFER, vertexPosObject); + glVertexAttribPointer(0 /* ? */, 3, GL_FLOAT, 0, 0, 0); + glEnableVertexAttribArray(0); + + glDrawArrays ( GL_TRIANGLES, 0, 3 ); +} + +void Verify() { + unsigned char *data = malloc(width*height*4); + glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); + // Should see some blue, and nothing else + int seen = 0; + int ok = 1; + for (int x = 0; x < width*height; x++) { + seen = seen || data[x*4+2] != 0; + ok = ok && (data[x*4+0] == 0); + ok = ok && (data[x*4+1] == 0); + } + int result = seen && ok; + REPORT_RESULT(); +} + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + screen = SDL_SetVideoMode(width, height, 32, SDL_OPENGL); + if (!screen) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + Init(); + Draw(); + Verify(); + + return 0; +} + |