diff options
-rw-r--r-- | src/library.js | 1 | ||||
-rw-r--r-- | src/library_gl.js | 5 | ||||
-rw-r--r-- | src/library_sdl.js | 8 | ||||
-rw-r--r-- | src/postamble.js | 2 | ||||
-rw-r--r-- | tests/gl/LICENSE.TXT | 11 | ||||
-rw-r--r-- | tests/gl/sdl_ogl.c | 190 | ||||
-rw-r--r-- | tests/gl/tutorial2.frag | 11 | ||||
-rw-r--r-- | tests/gl/tutorial2.vert | 19 | ||||
-rw-r--r-- | tests/runner.py | 17 |
9 files changed, 262 insertions, 2 deletions
diff --git a/src/library.js b/src/library.js index 0fb73611..ddb90675 100644 --- a/src/library.js +++ b/src/library.js @@ -736,4 +736,5 @@ var Library = { }; load('library_sdl.js'); +load('library_gl.js'); diff --git a/src/library_gl.js b/src/library_gl.js new file mode 100644 index 00000000..ad0134eb --- /dev/null +++ b/src/library_gl.js @@ -0,0 +1,5 @@ +mergeInto(Library, { + glGetString: function() { + }, +}); + diff --git a/src/library_sdl.js b/src/library_sdl.js index 533e69aa..5f1a415f 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -5,7 +5,7 @@ mergeInto(Library, { height: 240 }; SDL_SURFACES = {}; - return 1; + return 0; // success }, SDL_GetVideoInfo: function() { @@ -30,7 +30,7 @@ mergeInto(Library, { SDL_SetVideoMode: function(width, height, depth, flags, canvas) { // ^^^^^^ a 'canvas' parameter is added here; supply a canvas from JS there // or, define __CANVAS__. - canvas = canvas || __CANVAS__; + canvas = canvas || Module.__CANVAS__; var surf = _malloc(14*QUANTUM_SIZE); // SDL_Surface has 14 fields of quantum size SDL_SURFACES[surf] = { width: width, @@ -96,6 +96,10 @@ mergeInto(Library, { SDL_ShowCursor: function(toggle) { // TODO + }, + + SDL_GetError: function() { + return Pointer_make(intArrayFromString("SDL is cool"), null); } }); diff --git a/src/postamble.js b/src/postamble.js index 8f1f6cb8..ba295ea7 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -32,6 +32,8 @@ function run(args) { } Module['run'] = run; +// {{PRE_RUN_ADDITIONS}} + #if INVOKE_RUN run(args); #endif diff --git a/tests/gl/LICENSE.TXT b/tests/gl/LICENSE.TXT new file mode 100644 index 00000000..45e1d8ad --- /dev/null +++ b/tests/gl/LICENSE.TXT @@ -0,0 +1,11 @@ +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. diff --git a/tests/gl/sdl_ogl.c b/tests/gl/sdl_ogl.c new file mode 100644 index 00000000..829213d8 --- /dev/null +++ b/tests/gl/sdl_ogl.c @@ -0,0 +1,190 @@ +/******************************************************************* + * * + * Using SDL With OpenGL * + * * + * Tutorial by Kyle Foley (sdw) * + * * + * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL * + * * + *******************************************************************/ + +/* + Combined with opengl.org tutorial #2, + http://www.opengl.org/wiki/Tutorial2:_VAOs,_VBOs,_Vertex_and_Fragment_Shaders_%28C_/_SDL%29 +*/ + +#include "SDL/SDL.h" +#include "SDL/SDL_opengl.h" + +#include <stdio.h> + +char* filetobuf(char *file) +{ + FILE *fptr; + long length; + char *buf; + + fptr = fopen(file, "r"); /* Open file for reading */ + if (!fptr) /* Return NULL on failure */ + return NULL; + fseek(fptr, 0, SEEK_END); /* Seek to the end of the file */ + length = ftell(fptr); /* Find out how many bytes into the file we are */ + buf = (char*)malloc(length+1); /* Allocate a buffer for the entire length of the file and a null terminator */ + fseek(fptr, 0, SEEK_SET); /* Go back to the beginning of the file */ + fread(buf, length, 1, fptr); /* Read the contents of the file in to the buffer */ + fclose(fptr); /* Close the file */ + buf[length] = 0; /* Null terminator */ + + return buf; /* Return the buffer */ +} + +void drawscene() +{ + int i; /* Simple iterator */ + GLuint vao, vbo[2]; /* Create handles for our Vertex Array Object and two Vertex Buffer Objects */ + + /* We're going to create a simple diamond made from lines */ + const GLfloat diamond[4][2] = { + { 0.0, 1.0 }, /* Top point */ + { 1.0, 0.0 }, /* Right point */ + { 0.0, -1.0 }, /* Bottom point */ + { -1.0, 0.0 } }; /* Left point */ + + const GLfloat colors[4][3] = { + { 1.0, 0.0, 0.0 }, /* Red */ + { 0.0, 1.0, 0.0 }, /* Green */ + { 0.0, 0.0, 1.0 }, /* Blue */ + { 1.0, 1.0, 1.0 } }; /* White */ + + /* These pointers will receive the contents of our shader source code files */ + GLchar *vertexsource, *fragmentsource; + + /* These are handles used to reference the shaders */ + GLuint vertexshader, fragmentshader; + + /* This is a handle to the shader program */ + GLuint shaderprogram; + + /* Allocate and assign a Vertex Array Object to our handle */ + glGenVertexArrays(1, &vao); + + /* Bind our Vertex Array Object as the current used object */ + glBindVertexArray(vao); + + /* Allocate and assign two Vertex Buffer Objects to our handle */ + glGenBuffers(2, vbo); + + /* Bind our first VBO as being the active buffer and storing vertex attributes (coordinates) */ + glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); + + /* Copy the vertex data from diamond to our buffer */ + /* 8 * sizeof(GLfloat) is the size of the diamond array, since it contains 8 GLfloat values */ + glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), diamond, GL_STATIC_DRAW); + + /* Specify that our coordinate data is going into attribute index 0, and contains two floats per vertex */ + glVertexAttribPointer((GLuint)0, 2, GL_FLOAT, GL_FALSE, 0, 0); + + /* Enable attribute index 0 as being used */ + glEnableVertexAttribArray(0); + + /* Bind our second VBO as being the active buffer and storing vertex attributes (colors) */ + glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); + + /* Copy the color data from colors to our buffer */ + /* 12 * sizeof(GLfloat) is the size of the colors array, since it contains 12 GLfloat values */ + glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), colors, GL_STATIC_DRAW); + + /* Specify that our color data is going into attribute index 1, and contains three floats per vertex */ + glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0); + + /* Enable attribute index 1 as being used */ + glEnableVertexAttribArray(1); + + /* Read our shaders into the appropriate buffers */ + vertexsource = filetobuf("tutorial2.vert"); + fragmentsource = filetobuf("tutorial2.frag"); + + /* Assign our handles a "name" to new shader objects */ + vertexshader = glCreateShader(GL_VERTEX_SHADER); + fragmentshader = glCreateShader(GL_FRAGMENT_SHADER); + + /* Associate the source code buffers with each handle */ + glShaderSource(vertexshader, 1, (const GLchar**)&vertexsource, 0); + glShaderSource(fragmentshader, 1, (const GLchar**)&fragmentsource, 0); + + /* Compile our shader objects */ + glCompileShader(vertexshader); + glCompileShader(fragmentshader); + + /* Assign our program handle a "name" */ + shaderprogram = glCreateProgram(); + + /* Attach our shaders to our program */ + glAttachShader(shaderprogram, vertexshader); + glAttachShader(shaderprogram, fragmentshader); + + /* Bind attribute index 0 (coordinates) to in_Position and attribute index 1 (color) to in_Color */ + glBindAttribLocation(shaderprogram, 0, "in_Position"); + glBindAttribLocation(shaderprogram, 1, "in_Color"); + + /* Link our program, and set it as being actively used */ + glLinkProgram(shaderprogram); + glUseProgram(shaderprogram); + + /* Loop our display increasing the number of shown vertexes each time. + * Start with 2 vertexes (a line) and increase to 3 (a triangle) and 4 (a diamond) */ + for (i=4; i <= 4; i++) + { + /* Make our background black */ + glClearColor(0.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + /* Invoke glDrawArrays telling that our data is a line loop and we want to draw 2-4 vertexes */ + glDrawArrays(GL_LINE_LOOP, 0, i); + + /* Swap our buffers to make our changes visible */ + SDL_GL_SwapBuffers(); + + /* Sleep for 2 seconds */ + SDL_Delay(2000); + } + + /* Cleanup all the things we bound and allocated */ + glUseProgram(0); + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + glDetachShader(shaderprogram, vertexshader); + glDetachShader(shaderprogram, fragmentshader); + glDeleteProgram(shaderprogram); + glDeleteShader(vertexshader); + glDeleteShader(fragmentshader); + glDeleteBuffers(2, vbo); + glDeleteVertexArrays(1, &vao); + free(vertexsource); + free(fragmentsource); +} + +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( 512, 384, 32, SDL_OPENGL); // *changed* + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + drawscene(); + + SDL_Quit(); + + return 0; +} diff --git a/tests/gl/tutorial2.frag b/tests/gl/tutorial2.frag new file mode 100644 index 00000000..faa30eaf --- /dev/null +++ b/tests/gl/tutorial2.frag @@ -0,0 +1,11 @@ +#version 150 +// It was expressed that some drivers required this next line to function properly +precision highp float; + +in vec3 ex_Color; +out vec4 gl_FragColor; + +void main(void) { + // Pass through our original color with full opacity. + gl_FragColor = vec4(ex_Color,1.0); +} diff --git a/tests/gl/tutorial2.vert b/tests/gl/tutorial2.vert new file mode 100644 index 00000000..d9de08e3 --- /dev/null +++ b/tests/gl/tutorial2.vert @@ -0,0 +1,19 @@ +#version 150 +// in_Position was bound to attribute index 0 and in_Color was bound to attribute index 1 +in vec2 in_Position; +in vec3 in_Color; + +// We output the ex_Color variable to the next shader in the chain +out vec3 ex_Color; +void main(void) { + // Since we are using flat lines, our input only had two points: x and y. + // Set the Z coordinate to 0 and W coordinate to 1 + + gl_Position = vec4(in_Position.x, in_Position.y, 0.0, 1.0); + + // GLSL allows shorthand use of vectors too, the following is also valid: + // gl_Position = vec4(in_Position, 0.0, 1.0); + // We're simply passing the color through unmodified + + ex_Color = in_Color; +} diff --git a/tests/runner.py b/tests/runner.py index 4739f29d..f76602c7 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -154,6 +154,7 @@ if 'benchmark' not in sys.argv: ## Does a complete test - builds, runs, checks output, etc. def do_test(self, src, expected_output, args=[], output_nicerizer=None, output_processor=None, no_build=False, main_file=None, js_engines=None, post_build=None, basename='src.cpp'): print 'Running test:', inspect.stack()[1][3].replace('test_', ''), '[%s,%s,%s]' % (COMPILER.split(os.sep)[-1], 'llvm-optimizations' if LLVM_OPTS else '', 'reloop&optimize' if RELOOP else '') + if main_file is not None and main_file[-2:] == '.c': basename = 'src.c' dirname = self.get_dir() filename = os.path.join(dirname, basename) if not no_build: @@ -1274,6 +1275,22 @@ if 'benchmark' not in sys.argv: src = open(path_from_root(['tests', 'fasta.cpp']), 'r').read() self.do_test(src, j, [str(i)], lambda x: x.replace('\n', '*'), no_build=i>1) + def zzztest_gl(self): + # Switch to gcc from g++ - we don't compile properly otherwise (why?) + global COMPILER + if COMPILER != LLVM_GCC: return + COMPILER = LLVM_GCC.replace('g++', 'gcc') + + def post(filename): + src = open(filename, 'r').read().replace( + '// {{PRE_RUN_ADDITIONS}}', + '''Module["__CANVAS__"] = { + getContext: function() {}, + };''' + ) + open(filename, 'w').write(src) + self.do_test(path_from_root(['tests', 'gl']), '*?*', main_file='sdl_ogl.c', post_build=post) + def test_cubescript(self): # XXX Warning: Running this in SpiderMonkey can lead to an extreme amount of memory being # used, see Mozilla bug 593659. |