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

« back to all changes in this revision

Viewing changes to tests/gl_ps_workaround2.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-06-11 15:45:24 UTC
  • mfrom: (1.2.1) (2.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130611154524-rppb3w6tixlegv4n
Tags: 1.4.7~20130611~a1eb425-1
* New snapshot release
* Upload to unstable

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
 
#if !EMSCRIPTEN
26
 
#define USE_GLEW 1
27
 
#endif
28
 
 
29
 
#if USE_GLEW
30
 
#include "GL/glew.h"
31
 
#endif
32
 
 
33
 
#include "SDL/SDL.h"
34
 
#include "SDL/SDL_image.h"
35
 
#if !USE_GLEW
36
 
#include "SDL/SDL_opengl.h"
37
 
#endif
38
 
 
39
 
#include <stdio.h>
40
 
#include <string.h>
41
 
#include <assert.h>
42
 
 
43
 
void shaders() {
44
 
#if USE_GLEW
45
 
  glewInit();
46
 
#endif
47
 
 
48
 
  GLint ok;
49
 
 
50
 
  const char *vertexShader = "void main(void) \n"
51
 
                             "{ \n"
52
 
                             "    gl_Position = ftransform(); \n"
53
 
                             "    gl_TexCoord[0] = gl_MultiTexCoord0; \n"
54
 
                             "    gl_FrontColor = gl_Color; \n"
55
 
                             "} \n";
56
 
  const char *fragmentShader = "uniform sampler2D tex0; \n"
57
 
                               "void main(void) \n"
58
 
                               "{ \n"
59
 
                               "    gl_FragColor = gl_Color * texture2D(tex0, gl_TexCoord[0].xy); \n"
60
 
                               "} \n";
61
 
 
62
 
  GLuint vs = glCreateShader(GL_VERTEX_SHADER);
63
 
  glShaderSource(vs, 1, &vertexShader, NULL);
64
 
  glCompileShader(vs);
65
 
  glGetShaderiv(vs, GL_COMPILE_STATUS, &ok);
66
 
  assert(ok);
67
 
 
68
 
  GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
69
 
  glShaderSource(fs, 1, &fragmentShader, NULL);
70
 
  glCompileShader(fs);
71
 
  glGetShaderiv(fs, GL_COMPILE_STATUS, &ok);
72
 
  assert(ok);
73
 
 
74
 
  GLuint program = glCreateProgram();
75
 
 
76
 
  glAttachShader(program, vs);
77
 
  glAttachShader(program, fs);
78
 
  glLinkProgram(program);
79
 
  glGetProgramiv(program, GL_LINK_STATUS, &ok);
80
 
  assert(ok);
81
 
 
82
 
  glUseProgram(program);
83
 
}
84
 
 
85
 
int main(int argc, char *argv[])
86
 
{
87
 
    SDL_Surface *screen;
88
 
 
89
 
    // Slightly different SDL initialization
90
 
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
91
 
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
92
 
        return 1;
93
 
    }
94
 
 
95
 
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
96
 
 
97
 
    screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
98
 
    if ( !screen ) {
99
 
        printf("Unable to set video mode: %s\n", SDL_GetError());
100
 
        return 1;
101
 
    }
102
 
    
103
 
    // Set the OpenGL state after creating the context with SDL_SetVideoMode
104
 
 
105
 
    glClearColor( 0, 0, 0, 0 );
106
 
    
107
 
#if !EMSCRIPTEN
108
 
    glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
109
 
#endif
110
 
 
111
 
    glViewport( 0, 0, 640, 480 );
112
 
 
113
 
    glMatrixMode( GL_PROJECTION );
114
 
    GLfloat matrixData[] = { 2.0/640,        0,  0,  0,
115
 
                                   0, -2.0/480,  0,  0,
116
 
                                   0,        0, -1,  0,
117
 
                                  -1,        1,  0,  1 };
118
 
    glLoadMatrixf(matrixData); // test loadmatrix
119
 
 
120
 
    glMatrixMode( GL_MODELVIEW );
121
 
    glLoadIdentity();
122
 
    
123
 
    // Load the OpenGL texture
124
 
 
125
 
    GLuint texture; // Texture object handle
126
 
    SDL_Surface *surface; // Gives us the information to make the texture
127
 
    
128
 
    if ( (surface = IMG_Load("screenshot.png")) ) { 
129
 
    
130
 
        // Check that the image's width is a power of 2
131
 
        if ( (surface->w & (surface->w - 1)) != 0 ) {
132
 
            printf("warning: image.bmp's width is not a power of 2\n");
133
 
        }
134
 
    
135
 
        // Also check if the height is a power of 2
136
 
        if ( (surface->h & (surface->h - 1)) != 0 ) {
137
 
            printf("warning: image.bmp's height is not a power of 2\n");
138
 
        }
139
 
    
140
 
        // Have OpenGL generate a texture object handle for us
141
 
        glGenTextures( 1, &texture );
142
 
    
143
 
        // Bind the texture object
144
 
        glBindTexture( GL_TEXTURE_2D, texture );
145
 
        
146
 
        // Set the texture's stretching properties
147
 
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
148
 
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
149
 
 
150
 
        //SDL_LockSurface(surface);
151
 
 
152
 
        // Add some greyness
153
 
        memset(surface->pixels, 0x66, surface->w*surface->h);
154
 
 
155
 
        // Edit the texture object's image data using the information SDL_Surface gives us
156
 
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, 
157
 
                      GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
158
 
 
159
 
        //SDL_UnlockSurface(surface);
160
 
    } 
161
 
    else {
162
 
        printf("SDL could not load image.bmp: %s\n", SDL_GetError());
163
 
        SDL_Quit();
164
 
        return 1;
165
 
    }    
166
 
    
167
 
    // Free the SDL_Surface only if it was successfully created
168
 
    if ( surface ) { 
169
 
        SDL_FreeSurface( surface );
170
 
    }
171
 
    
172
 
    // Clear the screen before drawing
173
 
    glClear( GL_COLOR_BUFFER_BIT );
174
 
    
175
 
    shaders();
176
 
 
177
 
    // Bind the texture to which subsequent calls refer to
178
 
    glBindTexture( GL_TEXTURE_2D, texture );
179
 
 
180
 
    // Use clientside vertex pointers to render two items. In this test we have each
181
 
    // attribute in a separate buffer, packed (i.e. stride == 0)
182
 
    GLfloat vertexData[] = {  10,  10,
183
 
                             300,  10,
184
 
                             300, 128,
185
 
                              10, 128,
186
 
                             410,  10,
187
 
                             600,  10,
188
 
                             630, 200,
189
 
                             310, 250,
190
 
                             100, 300,
191
 
                             300, 300,
192
 
                             300, 400,
193
 
                             100, 400 };
194
 
    GLfloat textureData[] = { 0, 0,
195
 
                              1, 0,
196
 
                              1, 1,
197
 
                              0, 1,
198
 
                              0, 0.5,
199
 
                              1, 0.5,
200
 
                              1,   1,
201
 
                              0.5, 1,
202
 
                              0,   0,
203
 
                              1,   0,
204
 
                              1,   1,
205
 
                              0,   1, };
206
 
 
207
 
    glEnable(GL_TEXTURE_2D); // XXX should be GL_TEXTURE_COORD_ARRAY); and also glEnableClientState! XXX two workarounds here
208
 
    glTexCoordPointer(2, GL_FLOAT, 0, textureData);
209
 
    glEnableClientState(GL_VERTEX_ARRAY);
210
 
    glVertexPointer(2, GL_FLOAT, 0, vertexData);
211
 
 
212
 
    glDrawArrays(GL_QUADS, 0, 12);
213
 
 
214
 
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
215
 
    glDisableClientState(GL_VERTEX_ARRAY);
216
 
 
217
 
    SDL_GL_SwapBuffers();
218
 
    
219
 
#if !EMSCRIPTEN
220
 
    // Wait for 3 seconds to give us a chance to see the image
221
 
    SDL_Delay(3000);
222
 
#endif
223
 
 
224
 
    // Now we can delete the OpenGL texture and close down SDL
225
 
    glDeleteTextures( 1, &texture );
226
 
    
227
 
    SDL_Quit();
228
 
    
229
 
    return 0;
230
 
}