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

« back to all changes in this revision

Viewing changes to tests/s3tc.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 texture
 
89
 
 
90
    GLuint texture;
 
91
 
 
92
    #define DDS_SIZE 262272
 
93
    FILE *dds = fopen("screenshot.dds", "rb");
 
94
    char *ddsdata = (char*)malloc(512*512*4);//DDS_SIZE);
 
95
    assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
 
96
    fclose(dds);
 
97
 
 
98
    glGenTextures( 1, &texture );
 
99
    glBindTexture( GL_TEXTURE_2D, texture );
 
100
 
 
101
    assert(!glGetError());
 
102
    glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 512, 512, 0, DDS_SIZE-128, ddsdata+128);
 
103
    assert(!glGetError());
 
104
 
 
105
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 
106
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
107
 
 
108
 
 
109
    // Prepare and Render
 
110
 
 
111
    // Clear the screen before drawing
 
112
    glClear( GL_COLOR_BUFFER_BIT );
 
113
    
 
114
    // Bind the texture to which subsequent calls refer to
 
115
    glBindTexture( GL_TEXTURE_2D, texture );
 
116
 
 
117
    // Use clientside vertex pointers to render two items
 
118
    GLfloat vertexData[] = { 0, 0, 10, 10, // texture2, position2
 
119
                             1, 0, 300, 10,
 
120
                             1, 1, 300, 128,
 
121
                             0, 1, 10, 128,
 
122
                             0, 0.5, 410, 10,
 
123
                             1, 0.5, 600, 10,
 
124
                             1, 1, 630, 200,
 
125
                             0.5, 1, 310, 250 };
 
126
 
 
127
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
128
    glTexCoordPointer(2, GL_FLOAT, 4*4, &vertexData[0]);
 
129
    glEnableClientState(GL_VERTEX_ARRAY);
 
130
    glVertexPointer(2, GL_FLOAT, 4*4, &vertexData[2]);
 
131
 
 
132
    glDrawArrays(GL_QUADS, 0, 8);
 
133
 
 
134
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
135
    glDisableClientState(GL_VERTEX_ARRAY);
 
136
 
 
137
    // Render the last item using oldschool glBegin etc
 
138
    glBegin( GL_TRIANGLE_STRIP );
 
139
        glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 0 );
 
140
        glTexCoord2i( 1, 0 ); glVertex3f( 300, 300, 0 );
 
141
        glTexCoord2i( 1, 1 ); glVertex3f( 300, 400, 0 );
 
142
        glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, 0 );
 
143
    glEnd();
 
144
 
 
145
    SDL_GL_SwapBuffers();
 
146
    
 
147
#if !EMSCRIPTEN
 
148
    // Wait for 3 seconds to give us a chance to see the image
 
149
    SDL_Delay(1500);
 
150
#endif
 
151
 
 
152
    // Now we can delete the OpenGL texture and close down SDL
 
153
    glDeleteTextures( 1, &texture );
 
154
    
 
155
    SDL_Quit();
 
156
    
 
157
    return 0;
 
158
}