aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEhsan Akhgari <ehsan.akhgari@gmail.com>2012-04-13 10:32:45 -0400
committerEhsan Akhgari <ehsan.akhgari@gmail.com>2012-04-13 10:32:45 -0400
commit74cfb3b59c714d2e0ef91d0ad3fe02bc096faf56 (patch)
tree4e743664d4d139c35b4e656f267cb2a0d4799114 /tests
parent55d9a3bc975cb4e9fefb79aeca6d5b9ef5fcea1f (diff)
parentffae4e81b6eec71922b662eea9a75f5693994cb2 (diff)
Merge remote-tracking branch 'upstream/incoming' into glmatrix
Conflicts: src/library_gl.js
Diffstat (limited to 'tests')
-rwxr-xr-xtests/runner.py13
-rw-r--r--tests/screenshot.pngbin0 -> 329895 bytes
-rw-r--r--tests/sdl_gl_read.c155
-rw-r--r--tests/sdl_ogl.c148
4 files changed, 316 insertions, 0 deletions
diff --git a/tests/runner.py b/tests/runner.py
index 6db096d9..a479cf9f 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -6997,6 +6997,19 @@ 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'))
+ self.reftest(path_from_root('tests', 'gears.png'))
+ Popen(['python', EMCC, path_from_root('tests', 'sdl_ogl.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate()
+ self.run_browser('something.html', 'You should see animating gears.', '/report_result?1779')
+
def test_worker(self):
# Test running in a web worker
output = Popen(['python', EMCC, path_from_root('tests', 'hello_world_worker.cpp'), '-o', 'worker.js'], stdout=PIPE, stderr=PIPE).communicate()
diff --git a/tests/screenshot.png b/tests/screenshot.png
new file mode 100644
index 00000000..44cf844a
--- /dev/null
+++ b/tests/screenshot.png
Binary files differ
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;
+}
+
diff --git a/tests/sdl_ogl.c b/tests/sdl_ogl.c
new file mode 100644
index 00000000..6884f478
--- /dev/null
+++ b/tests/sdl_ogl.c
@@ -0,0 +1,148 @@
+/*******************************************************************
+ * *
+ * Using SDL With OpenGL *
+ * *
+ * Tutorial by Kyle Foley (sdw) *
+ * *
+ * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL *
+ * *
+ *******************************************************************/
+
+/*
+THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION
+AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.
+
+THE ORIGINAL AUTHOR IS KYLE FOLEY.
+
+THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
+OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
+MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
+ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
+RESULTING FROM THE USE, MODIFICATION, OR
+REDISTRIBUTION OF THIS SOFTWARE.
+*/
+
+#include "SDL/SDL.h"
+#include "SDL/SDL_image.h"
+#include "SDL/SDL_opengl.h"
+
+#include <stdio.h>
+#include <string.h>
+
+int main(int argc, char *argv[])
+{
+ SDL_Surface *screen;
+
+ // Slightly different SDL initialization
+ if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
+ printf("Unable to initialize SDL: %s\n", SDL_GetError());
+ return 1;
+ }
+
+ SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
+
+ screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
+ if ( !screen ) {
+ printf("Unable to set video mode: %s\n", SDL_GetError());
+ return 1;
+ }
+
+ // Set the OpenGL state after creating the context with SDL_SetVideoMode
+
+ glClearColor( 0, 0, 0, 0 );
+
+ glEnable( GL_TEXTURE_2D ); // Need this to display a texture
+
+ glViewport( 0, 0, 640, 480 );
+
+ glMatrixMode( GL_PROJECTION );
+ glLoadIdentity();
+
+ glOrtho( 0, 640, 480, 0, -1, 1 );
+
+ glMatrixMode( GL_MODELVIEW );
+ glLoadIdentity();
+
+ // Load the OpenGL texture
+
+ GLuint texture; // Texture object handle
+ SDL_Surface *surface; // Gives us the information to make the texture
+
+ if ( (surface = IMG_Load("screenshot.png")) ) {
+
+ // Check that the image's width is a power of 2
+ if ( (surface->w & (surface->w - 1)) != 0 ) {
+ printf("warning: image.bmp's width is not a power of 2\n");
+ }
+
+ // Also check if the height is a power of 2
+ if ( (surface->h & (surface->h - 1)) != 0 ) {
+ printf("warning: image.bmp's height is not a power of 2\n");
+ }
+
+ // Have OpenGL generate a texture object handle for us
+ glGenTextures( 1, &texture );
+
+ // Bind the texture object
+ glBindTexture( GL_TEXTURE_2D, texture );
+
+ // Set the texture's stretching properties
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+
+ // Add some greyness
+ memset(surface->pixels, 0x66, surface->w*surface->h);
+
+ // Edit the texture object's image data using the information SDL_Surface gives us
+ glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
+ }
+ else {
+ printf("SDL could not load image.bmp: %s\n", SDL_GetError());
+ SDL_Quit();
+ return 1;
+ }
+
+ // Free the SDL_Surface only if it was successfully created
+ if ( surface ) {
+ SDL_FreeSurface( surface );
+ }
+
+ // Clear the screen before drawing
+ glClear( GL_COLOR_BUFFER_BIT );
+
+ // Bind the texture to which subsequent calls refer to
+ glBindTexture( GL_TEXTURE_2D, texture );
+
+ glBegin( GL_QUADS );
+ // Top-left vertex (corner)
+ glTexCoord2i( 0, 0 );
+ glVertex3f( 100, 100, 0 );
+
+ // Bottom-left vertex (corner)
+ glTexCoord2i( 1, 0 );
+ glVertex3f( 600, 100, 0 );
+
+ // Bottom-right vertex (corner)
+ glTexCoord2i( 1, 1 );
+ glVertex3f( 600, 328, 0 );
+
+ // Top-right vertex (corner)
+ glTexCoord2i( 0, 1 );
+ glVertex3f( 100, 328, 0 );
+ glEnd();
+
+ SDL_GL_SwapBuffers();
+
+#if !EMSCRIPTEN
+ // Wait for 3 seconds to give us a chance to see the image
+ SDL_Delay(3000);
+#endif
+
+ // Now we can delete the OpenGL texture and close down SDL
+ glDeleteTextures( 1, &texture );
+
+ SDL_Quit();
+
+ return 0;
+}