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

« back to all changes in this revision

Viewing changes to tests/sdl_fog_linear.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 main(int argc, char *argv[])
 
34
{
 
35
    SDL_Surface *screen;
 
36
 
 
37
    // Slightly different SDL initialization
 
38
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
 
39
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
 
40
        return 1;
 
41
    }
 
42
 
 
43
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
 
44
 
 
45
    screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
 
46
    if ( !screen ) {
 
47
        printf("Unable to set video mode: %s\n", SDL_GetError());
 
48
        return 1;
 
49
    }
 
50
    
 
51
    // Set the OpenGL state after creating the context with SDL_SetVideoMode
 
52
 
 
53
    glClearColor( 0, 0, 0, 0 );
 
54
    
 
55
#if !EMSCRIPTEN
 
56
    glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
 
57
#endif
 
58
 
 
59
    glViewport( 0, 0, 640, 480 );
 
60
 
 
61
    glMatrixMode( GL_PROJECTION );
 
62
    glPushMatrix(); // just for testing
 
63
    glLoadIdentity();
 
64
 
 
65
    glOrtho( 0, 640, 480, 0, -1000, 1000 );
 
66
    
 
67
    glMatrixMode( GL_MODELVIEW );
 
68
    glLoadIdentity();
 
69
    
 
70
    // Load the OpenGL texture
 
71
 
 
72
    GLuint texture; // Texture object handle
 
73
    SDL_Surface *surface; // Gives us the information to make the texture
 
74
    
 
75
    if ( (surface = IMG_Load("screenshot.png")) ) { 
 
76
    
 
77
        // Check that the image's width is a power of 2
 
78
        if ( (surface->w & (surface->w - 1)) != 0 ) {
 
79
            printf("warning: image.bmp's width is not a power of 2\n");
 
80
        }
 
81
    
 
82
        // Also check if the height is a power of 2
 
83
        if ( (surface->h & (surface->h - 1)) != 0 ) {
 
84
            printf("warning: image.bmp's height is not a power of 2\n");
 
85
        }
 
86
    
 
87
        // Have OpenGL generate a texture object handle for us
 
88
        glGenTextures( 1, &texture );
 
89
    
 
90
        // Bind the texture object
 
91
        glBindTexture( GL_TEXTURE_2D, texture );
 
92
        
 
93
        // Set the texture's stretching properties
 
94
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 
95
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
96
 
 
97
        //SDL_LockSurface(surface);
 
98
 
 
99
        // Add some greyness
 
100
        memset(surface->pixels, 0x66, surface->w*surface->h);
 
101
 
 
102
        // Edit the texture object's image data using the information SDL_Surface gives us
 
103
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, 
 
104
                      GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
 
105
 
 
106
        //SDL_UnlockSurface(surface);
 
107
    } 
 
108
    else {
 
109
        printf("SDL could not load image.bmp: %s\n", SDL_GetError());
 
110
        SDL_Quit();
 
111
        return 1;
 
112
    }    
 
113
    
 
114
    // Free the SDL_Surface only if it was successfully created
 
115
    if ( surface ) { 
 
116
        SDL_FreeSurface( surface );
 
117
    }
 
118
    
 
119
    // Clear the screen before drawing
 
120
    glClear( GL_COLOR_BUFFER_BIT );
 
121
    
 
122
    // Bind the texture to which subsequent calls refer to
 
123
    glBindTexture( GL_TEXTURE_2D, texture );
 
124
 
 
125
    glEnable(GL_FOG);
 
126
    GLfloat fogColor[] = { 1.0, 0.5, 0.5, 0.05 };
 
127
    glFogfv(GL_FOG_COLOR, fogColor);
 
128
    glFogi(GL_FOG_START, 8);
 
129
    glFogi(GL_FOG_END, 13);
 
130
    glFogi(GL_FOG_MODE, GL_LINEAR);
 
131
 
 
132
    assert(glIsEnabled(GL_FOG));
 
133
 
 
134
    glBegin( GL_QUADS );
 
135
        glTexCoord2i( 0, 0 ); glVertex3f( 10, 10, 10 );
 
136
        glTexCoord2i( 1, 0 ); glVertex3f( 300, 10, 10 );
 
137
        glTexCoord2i( 1, 1 ); glVertex3f( 300, 128, 10 );
 
138
        glTexCoord2i( 0, 1 ); glVertex3f( 10, 128, 10 );
 
139
 
 
140
        glTexCoord2f( 0, 0.5 ); glVertex3f( 410, 10, 5 );
 
141
        glTexCoord2f( 1, 0.5 ); glVertex3f( 600, 10, 6 );
 
142
        glTexCoord2f( 1, 1   ); glVertex3f( 630, 200, 7 );
 
143
        glTexCoord2f( 0.5, 1 ); glVertex3f( 310, 250, 8 );
 
144
    glEnd();
 
145
 
 
146
    glBegin( GL_TRIANGLE_STRIP );
 
147
        glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 1 );
 
148
        glTexCoord2i( 1, 0 ); glVertex3f( 300, 300, 1 );
 
149
        glTexCoord2i( 1, 1 ); glVertex3f( 300, 400, 1 );
 
150
        glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, 1 );
 
151
    glEnd();
 
152
 
 
153
#if !EMSCRIPTEN
 
154
    glDisable(GL_TEXTURE_2D);
 
155
#endif
 
156
 
 
157
    glColor3ub(90, 255, 255);
 
158
    glBegin( GL_QUADS );
 
159
        glVertex3f( 10, 410, 5 );
 
160
        glVertex3f( 300, 410, 50 );
 
161
        glVertex3f( 300, 480, 100 );
 
162
        glVertex3f( 10, 470, 5 );
 
163
    glEnd();
 
164
 
 
165
    glBegin( GL_QUADS );
 
166
        glColor3f(1.0, 0, 1.0);   glVertex3f( 410, 410, 10 );
 
167
        glColor3f(0, 1.0, 0);     glVertex3f( 600, 410, 10 );
 
168
        glColor3f(0, 0, 1.0);     glVertex3f( 600, 480, 10 );
 
169
        glColor3f(1.0, 1.0, 1.0); glVertex3f( 410, 470, 10 );
 
170
    glEnd();
 
171
 
 
172
    SDL_GL_SwapBuffers();
 
173
    
 
174
#if !EMSCRIPTEN
 
175
    // Wait for 3 seconds to give us a chance to see the image
 
176
    SDL_Delay(30000);
 
177
#endif
 
178
 
 
179
    // Now we can delete the OpenGL texture and close down SDL
 
180
    glDeleteTextures( 1, &texture );
 
181
    
 
182
    SDL_Quit();
 
183
    
 
184
    return 0;
 
185
}