~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/s3tc_crunch.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
int hasext(const char *exts, const char *ext) // from cube2, zlib licensed
 
34
{
 
35
    int len = strlen(ext);
 
36
    if(len) for(const char *cur = exts; (cur = strstr(cur, ext)); cur += len)
 
37
    {
 
38
        if((cur == exts || cur[-1] == ' ') && (cur[len] == ' ' || !cur[len])) return 1;
 
39
    }
 
40
    return 0;
 
41
}
 
42
 
 
43
int main(int argc, char *argv[])
 
44
{
 
45
    SDL_Surface *screen;
 
46
 
 
47
    // Slightly different SDL initialization
 
48
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
 
49
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
 
50
        return 1;
 
51
    }
 
52
 
 
53
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
 
54
 
 
55
    screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
 
56
    if ( !screen ) {
 
57
        printf("Unable to set video mode: %s\n", SDL_GetError());
 
58
        return 1;
 
59
    }
 
60
 
 
61
    // Check extensions
 
62
 
 
63
    const char *exts = (const char *)glGetString(GL_EXTENSIONS);
 
64
    assert(hasext(exts, "GL_ARB_texture_compression"));
 
65
    assert(hasext(exts, "GL_EXT_texture_compression_s3tc"));
 
66
    
 
67
    // Set the OpenGL state after creating the context with SDL_SetVideoMode
 
68
 
 
69
    glClearColor( 0, 0, 0, 0 );
 
70
    
 
71
#if !EMSCRIPTEN
 
72
    glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
 
73
#endif
 
74
 
 
75
    glViewport( 0, 0, 640, 480 );
 
76
 
 
77
    glMatrixMode( GL_PROJECTION );
 
78
    GLfloat matrixData[] = { 2.0/640,        0,  0,  0,
 
79
                                   0, -2.0/480,  0,  0,
 
80
                                   0,        0, -1,  0,
 
81
                                  -1,        1,  0,  1 };
 
82
    glLoadMatrixf(matrixData); // test loadmatrix
 
83
 
 
84
    glMatrixMode( GL_MODELVIEW );
 
85
    glLoadIdentity();
 
86
 
 
87
 
 
88
    // Load the OpenGL textures
 
89
 
 
90
    GLuint texture;
 
91
 
 
92
    {
 
93
      const int DDS_SIZE = 65664;
 
94
      FILE *dds = fopen("ship.dds", "rb");
 
95
      assert(dds);
 
96
      char *ddsdata = (char*)malloc(DDS_SIZE);
 
97
      assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
 
98
      fclose(dds);
 
99
 
 
100
      glGenTextures( 1, &texture );
 
101
      glBindTexture( GL_TEXTURE_2D, texture );
 
102
 
 
103
      assert(!glGetError());
 
104
      glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 256, 256, 0, DDS_SIZE-128, ddsdata+128);
 
105
      assert(!glGetError());
 
106
 
 
107
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 
108
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
109
    }
 
110
 
 
111
    // second texture
 
112
 
 
113
    GLuint texture2;
 
114
 
 
115
    {
 
116
      const int DDS_SIZE = 32896;
 
117
      FILE *dds = fopen("bloom.dds", "rb");
 
118
      assert(dds);
 
119
      char *ddsdata = (char*)malloc(DDS_SIZE);
 
120
      assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
 
121
      fclose(dds);
 
122
 
 
123
      glGenTextures( 1, &texture2 );
 
124
      glBindTexture( GL_TEXTURE_2D, texture2 );
 
125
 
 
126
      assert(!glGetError());
 
127
      glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 256, 256, 0, DDS_SIZE-128, ddsdata+128);
 
128
      assert(!glGetError());
 
129
 
 
130
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 
131
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
132
    }
 
133
 
 
134
    // third, a non-square texture with mipmaps
 
135
 
 
136
    GLuint texture3;
 
137
 
 
138
    {
 
139
      const int DDS_SIZE = 43920;
 
140
      FILE *dds = fopen("water.dds", "rb");
 
141
      assert(dds);
 
142
      char *ddsdata = (char*)malloc(DDS_SIZE);
 
143
      assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
 
144
      fclose(dds);
 
145
 
 
146
      glGenTextures( 1, &texture3 );
 
147
      glBindTexture( GL_TEXTURE_2D, texture3 );
 
148
 
 
149
      assert(!glGetError());
 
150
      glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 512, 64, 0, 512*64, ddsdata+128);
 
151
      assert(!glGetError());
 
152
 
 
153
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 
154
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
155
    }
 
156
 
 
157
    // Prepare and Render
 
158
 
 
159
    // Clear the screen before drawing
 
160
    glClear( GL_COLOR_BUFFER_BIT );
 
161
    
 
162
    // Bind the texture to which subsequent calls refer to
 
163
    glBindTexture( GL_TEXTURE_2D, texture );
 
164
 
 
165
    // Use clientside vertex pointers to render two items
 
166
    GLfloat vertexData[] = { 0, 0, 10, 10, // texture2, position2
 
167
                             1, 0, 300, 10,
 
168
                             1, 1, 300, 128,
 
169
                             0, 1, 10, 128,
 
170
                             0, 0.5, 410, 10,
 
171
                             1, 0.5, 600, 10,
 
172
                             1, 1, 630, 200,
 
173
                             0.5, 1, 310, 250 };
 
174
 
 
175
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
176
    glTexCoordPointer(2, GL_FLOAT, 4*4, &vertexData[0]);
 
177
    glEnableClientState(GL_VERTEX_ARRAY);
 
178
    glVertexPointer(2, GL_FLOAT, 4*4, &vertexData[2]);
 
179
 
 
180
    glDrawArrays(GL_QUADS, 0, 4);
 
181
 
 
182
    glBindTexture( GL_TEXTURE_2D, texture3 );
 
183
    glDrawArrays(GL_QUADS, 4, 4);
 
184
 
 
185
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
186
    glDisableClientState(GL_VERTEX_ARRAY);
 
187
 
 
188
    // Render the last item using oldschool glBegin etc
 
189
    glBindTexture( GL_TEXTURE_2D, texture2 );
 
190
    glBegin( GL_TRIANGLE_STRIP );
 
191
        glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 0 );
 
192
        glTexCoord2i( 1, 0 ); glVertex3f( 300, 300, 0 );
 
193
        glTexCoord2i( 1, 1 ); glVertex3f( 300, 400, 0 );
 
194
        glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, 0 );
 
195
    glEnd();
 
196
 
 
197
    SDL_GL_SwapBuffers();
 
198
    
 
199
#if !EMSCRIPTEN
 
200
    // Wait for 3 seconds to give us a chance to see the image
 
201
    SDL_Delay(1500);
 
202
#endif
 
203
 
 
204
    // Now we can delete the OpenGL texture and close down SDL
 
205
    glDeleteTextures( 1, &texture );
 
206
    
 
207
    SDL_Quit();
 
208
    
 
209
    return 0;
 
210
}