Path: blob/main/test/browser/test_anisotropic.c
4150 views
/*******************************************************************1* *2* Using SDL With OpenGL *3* *4* Tutorial by Kyle Foley (sdw) *5* *6* http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL *7* *8*******************************************************************/910/*11THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION12AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.1314THE ORIGINAL AUTHOR IS KYLE FOLEY.1516THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY17OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF18MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,19ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE20RESULTING FROM THE USE, MODIFICATION, OR21REDISTRIBUTION OF THIS SOFTWARE.22*/2324#include "SDL/SDL.h"25#include "SDL/SDL_image.h"26#include "SDL/SDL_opengl.h"2728#include <stdio.h>29#include <stdlib.h>30#include <string.h>31#include <assert.h>3233#define MAX(x, y) ((x) > (y) ? (x) : (y))3435int hasext(const char *exts, const char *ext) // from cube2, zlib licensed36{37int len = strlen(ext);38if(len) for(const char *cur = exts; (cur = strstr(cur, ext)); cur += len)39{40if((cur == exts || cur[-1] == ' ') && (cur[len] == ' ' || !cur[len])) return 1;41}42return 0;43}4445int main(int argc, char *argv[])46{47SDL_Surface *screen;4849// Slightly different SDL initialization50if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {51printf("Unable to initialize SDL: %s\n", SDL_GetError());52return 1;53}5455SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*5657screen = SDL_SetVideoMode( 600, 600, 16, SDL_OPENGL ); // *changed*58if ( !screen ) {59printf("Unable to set video mode: %s\n", SDL_GetError());60return 1;61}6263// Check extensions6465const char *exts = (const char *)glGetString(GL_EXTENSIONS);66assert(hasext(exts, "GL_EXT_texture_filter_anisotropic"));6768const char *vendor = (const char *)glGetString(GL_VENDOR);69printf("vendor: %s\n", vendor);7071GLint aniso;72glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso);73printf("Max anisotropy: %d (using that)\n", aniso);74assert(aniso >= 4);7576// Set the OpenGL state after creating the context with SDL_SetVideoMode7778glClearColor( 0, 0, 0, 0 );7980glEnable( GL_TEXTURE_2D ); // Needed when we're using the fixed-function pipeline.8182glViewport( 0, 0, 600, 600 );8384glMatrixMode( GL_PROJECTION );85GLfloat matrixData[] = { 2.0/600, 0, 0, 0,860, -2.0/600, 0, 0,870, 0, -2.0/600, 0,88-1, 1, 0, 1 };89glLoadMatrixf(matrixData); // test loadmatrix9091glMatrixMode( GL_MODELVIEW );92glLoadIdentity();939495// Load the OpenGL texture9697GLuint texture, texture2;9899const int DDS_SIZE = 43920;100FILE *dds = fopen("water.dds", "rb");101assert(dds);102char *ddsdata = (char*)malloc(DDS_SIZE);103assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);104fclose(dds);105106{107glGenTextures( 1, &texture );108glBindTexture( GL_TEXTURE_2D, texture );109110char *curr = ddsdata + 128;111int level = 0;112int w = 512;113int h = 64;114while (level < 5) {115printf("uploading level %d: %d, %d\n", level, w, h);116assert(!glGetError());117#if TEST_TEXSUBIMAGE118glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, NULL);119glCompressedTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, w, h, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w*h, curr);120#else121glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);122#endif123assert(!glGetError());124curr += MAX(w, 4)*MAX(h, 4);125w /= 2;126h /= 2;127level++;128}129glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );130glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );131}132{133glGenTextures( 1, &texture2 );134glBindTexture( GL_TEXTURE_2D, texture2 );135136char *curr = ddsdata + 128;137int level = 0;138int w = 512;139int h = 64;140while (level < 5) {141printf("uploading level %d: %d, %d\n", level, w, h);142assert(!glGetError());143#if TEST_TEXSUBIMAGE144glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, NULL);145glCompressedTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, w, h, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w*h, curr);146#else147glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);148#endif149assert(!glGetError());150curr += MAX(w, 4)*MAX(h, 4);151w /= 2;152h /= 2;153level++;154}155glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );156glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );157glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);158}159{160assert(!glGetError());161glBindFramebuffer(GL_RENDERBUFFER, 0);162assert(glGetError());163164GLint out = 321;165assert(!glGetError());166glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &out); // invalid, just test output167assert(out == 0);168}169170// Prepare and Render171172// Clear the screen before drawing173glClear( GL_COLOR_BUFFER_BIT );174175// Bind the texture to which subsequent calls refer to176int w = 10;177int n = 15;178glBindTexture( GL_TEXTURE_2D, texture );179for (int x = 0; x < n; x++) {180int start = x*w*2;181glBegin( GL_TRIANGLES );182glTexCoord2i( 1, 0 ); glVertex2i( start , 0 );183glTexCoord2i( 0, 0 ); glVertex3f( start+w, 300, 0 );184glTexCoord2i( 1, 1 ); glVertex3f( start-w, 300, 0 );185glEnd();186}187glBindTexture( GL_TEXTURE_2D, texture2 );188for (int x = 0; x < n; x++) {189int start = n*w*2 + x*w*2;190glBegin( GL_TRIANGLES );191glTexCoord2i( 1, 0 ); glVertex3f( start , 0, 0 );192glTexCoord2i( 0, 0 ); glVertex3f( start+w, 300, 0 );193glTexCoord2i( 1, 1 ); glVertex3f( start-w, 300, 0 );194glEnd();195}196/*197int w = 8;198int n = 20;199for (int x = 0; x < n; x++) {200for (int y = 0; y < n*2; y++) {201glBindTexture( GL_TEXTURE_2D, texture );202glBegin( GL_TRIANGLE_STRIP );203glTexCoord2i( 0, 0 ); glVertex3f( x*w, y*(w), 0 );204glTexCoord2i( 1, 0 ); glVertex3f( (x+1)*(w-2*y/n), y*(w), 0 );205glTexCoord2i( 1, 1 ); glVertex3f( (x+1)*(w-2*y/n), (y+1)*(w), 0 );206glTexCoord2i( 0, 1 ); glVertex3f( x*w, (y+1)*(w), 0 );207glEnd();208glBindTexture( GL_TEXTURE_2D, texture2 );209glBegin( GL_TRIANGLE_STRIP );210glTexCoord2i( 0, 0 ); glVertex3f( n*w + x*w, y*(w), 0 );211glTexCoord2i( 1, 0 ); glVertex3f( n*w + (x+1)*(w-2*y/n), y*(w), 0 );212glTexCoord2i( 1, 1 ); glVertex3f( n*w + (x+1)*(w-2*y/n), (y+1)*(w), 0 );213glTexCoord2i( 0, 1 ); glVertex3f( n*w + x*w, (y+1)*(w), 0 );214glEnd();215}216}217*/218SDL_GL_SwapBuffers();219220#ifndef __EMSCRIPTEN__221// Wait for 3 seconds to give us a chance to see the image222SDL_Delay(2000);223#endif224225// Now we can delete the OpenGL texture and close down SDL226glDeleteTextures( 1, &texture );227228SDL_Quit();229230// check for asm compilation bug with aliased functions with different sigs231232glBegin( GL_TRIANGLE_STRIP );233void (*f)(int, int) = glVertex2i;234if ((long)f % 16 == 4) f(5, 7);235void (*g)(int, int) = glVertex3f;236if ((long)g % 16 == 4) g(5, 7);237return (int)((long)f + (long)g);238}239240241242