~ubuntu-branches/ubuntu/saucy/emscripten/saucy-proposed

« back to all changes in this revision

Viewing changes to tests/aniso.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************
 
2
 *                                                                 *
 
3
 *                        Using SDL With OpenGL                    *
 
4
 *                                                                 *
 
5
 *                    Tutorial by Kyle Foley (sdw)                 *
 
6
 *                                                                 *
 
7
 * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL *
 
8
 *                                                                 *
 
9
 *******************************************************************/
 
10
 
 
11
/*
 
12
THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION
 
13
AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.
 
14
 
 
15
THE ORIGINAL AUTHOR IS KYLE FOLEY.
 
16
 
 
17
THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
 
18
OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
 
19
MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
 
20
ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
 
21
RESULTING FROM THE USE, MODIFICATION, OR
 
22
REDISTRIBUTION OF THIS SOFTWARE.
 
23
*/
 
24
 
 
25
#include "SDL/SDL.h"
 
26
#include "SDL/SDL_image.h"
 
27
#include "SDL/SDL_opengl.h"
 
28
 
 
29
#include <stdio.h>
 
30
#include <string.h>
 
31
#include <assert.h>
 
32
 
 
33
#define MAX(x, y) ((x) > (y) ? (x) : (y))
 
34
 
 
35
int hasext(const char *exts, const char *ext) // from cube2, zlib licensed
 
36
{
 
37
    int len = strlen(ext);
 
38
    if(len) for(const char *cur = exts; (cur = strstr(cur, ext)); cur += len)
 
39
    {
 
40
        if((cur == exts || cur[-1] == ' ') && (cur[len] == ' ' || !cur[len])) return 1;
 
41
    }
 
42
    return 0;
 
43
}
 
44
 
 
45
int main(int argc, char *argv[])
 
46
{
 
47
    SDL_Surface *screen;
 
48
 
 
49
    // Slightly different SDL initialization
 
50
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
 
51
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
 
52
        return 1;
 
53
    }
 
54
 
 
55
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
 
56
 
 
57
    screen = SDL_SetVideoMode( 600, 600, 16, SDL_OPENGL ); // *changed*
 
58
    if ( !screen ) {
 
59
        printf("Unable to set video mode: %s\n", SDL_GetError());
 
60
        return 1;
 
61
    }
 
62
 
 
63
    // Check extensions
 
64
 
 
65
    const char *exts = (const char *)glGetString(GL_EXTENSIONS);
 
66
    assert(hasext(exts, "GL_EXT_texture_filter_anisotropic"));
 
67
    
 
68
    GLint aniso;
 
69
    glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso);
 
70
    printf("Max anisotropy: %d (using that)\n", aniso);
 
71
    assert(aniso >= 4);
 
72
 
 
73
    // Set the OpenGL state after creating the context with SDL_SetVideoMode
 
74
 
 
75
    glClearColor( 0, 0, 0, 0 );
 
76
    
 
77
#if !EMSCRIPTEN
 
78
    glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
 
79
#endif
 
80
 
 
81
    glViewport( 0, 0, 600, 600 );
 
82
 
 
83
    glMatrixMode( GL_PROJECTION );
 
84
    GLfloat matrixData[] = { 2.0/600,        0,        0,  0,
 
85
                                   0, -2.0/600,        0,  0,
 
86
                                   0,        0, -2.0/600,  0,
 
87
                                  -1,        1,        0,  1 };
 
88
    glLoadMatrixf(matrixData); // test loadmatrix
 
89
 
 
90
    glMatrixMode( GL_MODELVIEW );
 
91
    glLoadIdentity();
 
92
 
 
93
 
 
94
    // Load the OpenGL texture
 
95
 
 
96
    GLuint texture, texture2;
 
97
 
 
98
    const int DDS_SIZE = 43920;
 
99
    FILE *dds = fopen("water.dds", "rb");
 
100
    assert(dds);
 
101
    char *ddsdata = (char*)malloc(DDS_SIZE);
 
102
    assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
 
103
    fclose(dds);
 
104
 
 
105
    {
 
106
      glGenTextures( 1, &texture );
 
107
      glBindTexture( GL_TEXTURE_2D, texture );
 
108
 
 
109
      char *curr = ddsdata + 128;
 
110
      int level = 0;
 
111
      int w = 512;
 
112
      int h = 64;
 
113
      while (level < 5) {
 
114
        printf("uploading level %d: %d, %d\n", level, w, h);
 
115
        assert(!glGetError());
 
116
        glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);
 
117
        assert(!glGetError());
 
118
        curr += MAX(w, 4)*MAX(h, 4);
 
119
        w /= 2;
 
120
        h /= 2;
 
121
        level++;
 
122
      }
 
123
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 
124
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
125
    }
 
126
    {
 
127
      glGenTextures( 1, &texture2 );
 
128
      glBindTexture( GL_TEXTURE_2D, texture2 );
 
129
 
 
130
      char *curr = ddsdata + 128;
 
131
      int level = 0;
 
132
      int w = 512;
 
133
      int h = 64;
 
134
      while (level < 5) {
 
135
        printf("uploading level %d: %d, %d\n", level, w, h);
 
136
        assert(!glGetError());
 
137
        glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);
 
138
        assert(!glGetError());
 
139
        curr += MAX(w, 4)*MAX(h, 4);
 
140
        w /= 2;
 
141
        h /= 2;
 
142
        level++;
 
143
      }
 
144
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 
145
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
146
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
 
147
    }
 
148
    {
 
149
      assert(!glGetError());
 
150
      glBindFramebuffer(GL_RENDERBUFFER, 0);
 
151
      assert(glGetError());
 
152
    }
 
153
 
 
154
    // Prepare and Render
 
155
 
 
156
    // Clear the screen before drawing
 
157
    glClear( GL_COLOR_BUFFER_BIT );
 
158
    
 
159
    // Bind the texture to which subsequent calls refer to
 
160
    int w = 10;
 
161
    int n = 15;
 
162
    glBindTexture( GL_TEXTURE_2D, texture );
 
163
    for (int x = 0; x < n; x++) {
 
164
      int start = x*w*2;
 
165
      glBegin( GL_TRIANGLES );
 
166
        glTexCoord2i( 1, 0 ); glVertex3f( start  ,   0, 0 );
 
167
        glTexCoord2i( 0, 0 ); glVertex3f( start+w, 300, 0 );
 
168
        glTexCoord2i( 1, 1 ); glVertex3f( start-w, 300, 0 );
 
169
      glEnd();
 
170
    }
 
171
    glBindTexture( GL_TEXTURE_2D, texture2 );
 
172
    for (int x = 0; x < n; x++) {
 
173
      int start = n*w*2 + x*w*2;
 
174
      glBegin( GL_TRIANGLES );
 
175
        glTexCoord2i( 1, 0 ); glVertex3f( start  ,   0, 0 );
 
176
        glTexCoord2i( 0, 0 ); glVertex3f( start+w, 300, 0 );
 
177
        glTexCoord2i( 1, 1 ); glVertex3f( start-w, 300, 0 );
 
178
      glEnd();
 
179
    }
 
180
/*
 
181
    int w = 8;
 
182
    int n = 20;
 
183
    for (int x = 0; x < n; x++) {
 
184
      for (int y = 0; y < n*2; y++) {
 
185
        glBindTexture( GL_TEXTURE_2D, texture );
 
186
        glBegin( GL_TRIANGLE_STRIP );
 
187
          glTexCoord2i( 0, 0 ); glVertex3f( x*w,           y*(w), 0 );
 
188
          glTexCoord2i( 1, 0 ); glVertex3f( (x+1)*(w-2*y/n),     y*(w), 0 );
 
189
          glTexCoord2i( 1, 1 ); glVertex3f( (x+1)*(w-2*y/n), (y+1)*(w), 0 );
 
190
          glTexCoord2i( 0, 1 ); glVertex3f( x*w,       (y+1)*(w), 0 );
 
191
        glEnd();
 
192
        glBindTexture( GL_TEXTURE_2D, texture2 );
 
193
        glBegin( GL_TRIANGLE_STRIP );
 
194
          glTexCoord2i( 0, 0 ); glVertex3f( n*w + x*w,           y*(w), 0 );
 
195
          glTexCoord2i( 1, 0 ); glVertex3f( n*w + (x+1)*(w-2*y/n),     y*(w), 0 );
 
196
          glTexCoord2i( 1, 1 ); glVertex3f( n*w + (x+1)*(w-2*y/n), (y+1)*(w), 0 );
 
197
          glTexCoord2i( 0, 1 ); glVertex3f( n*w + x*w,       (y+1)*(w), 0 );
 
198
        glEnd();
 
199
      }
 
200
    }
 
201
*/
 
202
    SDL_GL_SwapBuffers();
 
203
    
 
204
#if !EMSCRIPTEN
 
205
    // Wait for 3 seconds to give us a chance to see the image
 
206
    SDL_Delay(2000);
 
207
#endif
 
208
 
 
209
    // Now we can delete the OpenGL texture and close down SDL
 
210
    glDeleteTextures( 1, &texture );
 
211
    
 
212
    SDL_Quit();
 
213
    
 
214
    return 0;
 
215
}