aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-06-27 10:53:25 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-06-27 10:53:25 -0700
commite88277692926ab32557f54337e70fdc25cda91fe (patch)
tree5bc9e05d6f9e76c4762796dda441a8501cc6df80
parent1f91ac1e073caaee66eaf7e16f1d6c47de93e385 (diff)
preparatory work for anisotropy
-rw-r--r--src/library_gl.js8
-rw-r--r--tests/aniso.c189
-rwxr-xr-xtests/runner.py4
3 files changed, 200 insertions, 1 deletions
diff --git a/src/library_gl.js b/src/library_gl.js
index 593bc1d8..53e587de 100644
--- a/src/library_gl.js
+++ b/src/library_gl.js
@@ -114,6 +114,10 @@ var LibraryGL = {
GL.compressionExt = Module.ctx.getExtension('WEBGL_compressed_texture_s3tc') ||
Module.ctx.getExtension('MOZ_WEBGL_compressed_texture_s3tc') ||
Module.ctx.getExtension('WEBKIT_WEBGL_compressed_texture_s3tc');
+
+ GL.anisotropicExt = Module.ctx.getExtension('EXT_texture_filter_anisotropic') ||
+ Module.ctx.getExtension('MOZ_EXT_texture_filter_anisotropic') ||
+ Module.ctx.getExtension('WEBKIT_EXT_texture_filter_anisotropic');
}
},
@@ -994,7 +998,9 @@ var LibraryGL = {
case 0x1F03 /* GL_EXTENSIONS */: // Add various extensions that we can support
return allocate(intArrayFromString(Module.ctx.getSupportedExtensions().join(' ') +
' GL_EXT_texture_env_combine GL_ARB_texture_env_crossbar GL_ATI_texture_env_combine3 GL_NV_texture_env_combine4 GL_EXT_texture_env_dot3 GL_ARB_multitexture GL_ARB_vertex_buffer_object GL_EXT_framebuffer_object GL_ARB_vertex_program GL_ARB_fragment_program GL_ARB_shading_language_100 GL_ARB_shader_objects GL_ARB_vertex_shader GL_ARB_fragment_shader GL_ARB_texture_cube_map GL_EXT_draw_range_elements' +
- (GL.compressionExt ? ' GL_ARB_texture_compression GL_EXT_texture_compression_s3tc' : '')), 'i8', ALLOC_NORMAL);
+ (GL.compressionExt ? ' GL_ARB_texture_compression GL_EXT_texture_compression_s3tc' : '') +
+ (GL.anisotropicExt ? ' GL_EXT_texture_filter_anisotropic' : '')
+ ), 'i8', ALLOC_NORMAL);
}
return glGetString(name_);
};
diff --git a/tests/aniso.c b/tests/aniso.c
new file mode 100644
index 00000000..f2735f49
--- /dev/null
+++ b/tests/aniso.c
@@ -0,0 +1,189 @@
+/*******************************************************************
+ * *
+ * 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>
+#include <assert.h>
+
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
+
+int hasext(const char *exts, const char *ext) // from cube2, zlib licensed
+{
+ int len = strlen(ext);
+ if(len) for(const char *cur = exts; (cur = strstr(cur, ext)); cur += len)
+ {
+ if((cur == exts || cur[-1] == ' ') && (cur[len] == ' ' || !cur[len])) return 1;
+ }
+ return 0;
+}
+
+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;
+ }
+
+ // Check extensions
+
+ const char *exts = (const char *)glGetString(GL_EXTENSIONS);
+ assert(hasext(exts, "GL_EXT_texture_filter_anisotropic"));
+
+ GLint aniso;
+ glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso);
+ printf("Max anisotropy: %d\n", aniso);
+ assert(aniso >= 4);
+
+ // Set the OpenGL state after creating the context with SDL_SetVideoMode
+
+ glClearColor( 0, 0, 0, 0 );
+
+#if !EMSCRIPTEN
+ glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
+#endif
+
+ glViewport( 0, 0, 800, 600 );
+
+ glMatrixMode( GL_PROJECTION );
+ GLfloat matrixData[] = { 2.0/640, 0, 0, 0,
+ 0, -2.0/480, 0, 0,
+ 0, 0, -2.0/480, 0,
+ -1, 1, 0, 1 };
+ glLoadMatrixf(matrixData); // test loadmatrix
+
+ glMatrixMode( GL_MODELVIEW );
+ glLoadIdentity();
+
+
+ // Load the OpenGL texture
+
+ GLuint texture, texture2;
+
+ const int DDS_SIZE = 43920;
+ FILE *dds = fopen("water.dds", "rb");
+ assert(dds);
+ char *ddsdata = (char*)malloc(DDS_SIZE);
+ assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
+ fclose(dds);
+
+ {
+ glGenTextures( 1, &texture );
+ glBindTexture( GL_TEXTURE_2D, texture );
+
+ char *curr = ddsdata + 128;
+ int level = 0;
+ int w = 512;
+ int h = 64;
+ while (level < 5) {
+ printf("uploading level %d: %d, %d\n", level, w, h);
+ assert(!glGetError());
+ glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);
+ assert(!glGetError());
+ curr += MAX(w, 4)*MAX(h, 4);
+ w /= 2;
+ h /= 2;
+ level++;
+ }
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+ }
+ {
+ glGenTextures( 1, &texture2 );
+ glBindTexture( GL_TEXTURE_2D, texture2 );
+
+ char *curr = ddsdata + 128;
+ int level = 0;
+ int w = 512;
+ int h = 64;
+ while (level < 5) {
+ printf("uploading level %d: %d, %d\n", level, w, h);
+ assert(!glGetError());
+ glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);
+ assert(!glGetError());
+ curr += MAX(w, 4)*MAX(h, 4);
+ w /= 2;
+ h /= 2;
+ level++;
+ }
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 4);
+ }
+
+ // Prepare and Render
+
+ // Clear the screen before drawing
+ glClear( GL_COLOR_BUFFER_BIT );
+
+ // Bind the texture to which subsequent calls refer to
+ int w = 8;
+ int n = 20;
+ for (int x = 0; x < n; x++) {
+ for (int y = 0; y < n*2; y++) {
+ glBindTexture( GL_TEXTURE_2D, texture );
+ glBegin( GL_TRIANGLE_STRIP );
+ glTexCoord2i( 0, 0 ); glVertex3f( x*w, y*(w), 0 );
+ glTexCoord2i( 1, 0 ); glVertex3f( (x+1)*(w-2*y/n), y*(w), 0 );
+ glTexCoord2i( 1, 1 ); glVertex3f( (x+1)*(w-2*y/n), (y+1)*(w), 0 );
+ glTexCoord2i( 0, 1 ); glVertex3f( x*w, (y+1)*(w), 0 );
+ glEnd();
+ glBindTexture( GL_TEXTURE_2D, texture2 );
+ glBegin( GL_TRIANGLE_STRIP );
+ glTexCoord2i( 0, 0 ); glVertex3f( n*w + x*w, y*(w), 0 );
+ glTexCoord2i( 1, 0 ); glVertex3f( n*w + (x+1)*(w-2*y/n), y*(w), 0 );
+ glTexCoord2i( 1, 1 ); glVertex3f( n*w + (x+1)*(w-2*y/n), (y+1)*(w), 0 );
+ glTexCoord2i( 0, 1 ); glVertex3f( n*w + x*w, (y+1)*(w), 0 );
+ glEnd();
+ }
+ }
+
+ SDL_GL_SwapBuffers();
+
+#if !EMSCRIPTEN
+ // Wait for 3 seconds to give us a chance to see the image
+ SDL_Delay(10000);
+#endif
+
+ // Now we can delete the OpenGL texture and close down SDL
+ glDeleteTextures( 1, &texture );
+
+ SDL_Quit();
+
+ return 0;
+}
diff --git a/tests/runner.py b/tests/runner.py
index 08086e2f..852928ef 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -7906,6 +7906,10 @@ elif 'browser' in str(sys.argv):
shutil.move('water.dds', 'water.donotfindme.dds') # make sure we load from the compressed
self.btest('s3tc_crunch.c', reference='s3tc_crunch.png', args=['--pre-js', 'pre.js'])
+ def zzztest_aniso(self):
+ shutil.copyfile(path_from_root('tests', 'water.dds'), 'water.dds')
+ self.btest('aniso.c', reference='aniso.png', args=['--preload-file', 'water.dds', '-s', 'GL_DEBUG=1'])
+
def test_pre_run_deps(self):
# Adding a dependency in preRun will delay run
open(os.path.join(self.get_dir(), 'pre.js'), 'w').write('''