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

« back to all changes in this revision

Viewing changes to tests/gl_stride.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
#if !EMSCRIPTEN
 
26
#define USE_GLEW 0
 
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
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
    // Set the OpenGL state after creating the context with SDL_SetVideoMode
 
62
 
 
63
    glClearColor( 0, 0, 0, 0 );
 
64
    
 
65
#if !EMSCRIPTEN
 
66
    glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
 
67
#endif
 
68
 
 
69
    glViewport( 0, 0, 640, 480 );
 
70
    
 
71
    glMatrixMode( GL_MODELVIEW );
 
72
    glLoadIdentity();
 
73
    
 
74
    // Clear the screen before drawing
 
75
    glClear( GL_COLOR_BUFFER_BIT );
 
76
    
 
77
    typedef struct Vertex {
 
78
        GLfloat x;
 
79
        GLfloat y;
 
80
    } Vertex;
 
81
    
 
82
    typedef struct Color {
 
83
        GLubyte r;
 
84
        GLubyte g;
 
85
        GLubyte b;
 
86
        GLubyte a;
 
87
    } Color;
 
88
    
 
89
    Vertex vertices[3] = {
 
90
        {-1.0, 0.0},
 
91
        { 0.0, 1.0},
 
92
        { 1.0, 0.0}
 
93
    };
 
94
    
 
95
    Color colors[3] = {
 
96
        {0xFF, 0x00, 0x00, 0xFF},
 
97
        {0x00, 0xFF, 0x00, 0xFF},
 
98
        {0x00, 0x00, 0xFF, 0xFF}
 
99
    };
 
100
    
 
101
    Vertex vertices2[3] = {
 
102
        {-1.0,  0.0},
 
103
        { 1.0,  0.0},
 
104
        { 0.0, -1.0}
 
105
    };
 
106
    
 
107
    Color colors2[3] = {
 
108
        {0xFF, 0x00, 0x00, 0xFF},
 
109
        {0x00, 0x00, 0xFF, 0xFF},
 
110
        {0x00, 0xFF, 0x00, 0xFF}
 
111
    };
 
112
    
 
113
    // DRAW
 
114
    
 
115
    // Clear the screen before drawing
 
116
    glClear( GL_COLOR_BUFFER_BIT );
 
117
    
 
118
    // This test ensures that we can use two separate arrays in memory for different
 
119
    // attributes, and that they each can have different stride.
 
120
    // The first test shows implicit striding (the zero indicates tightly packed)
 
121
    // The second test shows explicit striding where the stride is passed in
 
122
    //   even though it also is tightly packed
 
123
    
 
124
    glEnableClientState(GL_VERTEX_ARRAY);
 
125
    glEnableClientState(GL_COLOR_ARRAY);
 
126
 
 
127
    // TEST 1
 
128
    
 
129
    glVertexPointer(2, GL_FLOAT, 0, vertices);
 
130
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
 
131
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
 
132
    
 
133
    // TEST 2
 
134
    
 
135
    glVertexPointer(2, GL_FLOAT, 8, vertices2);
 
136
    glColorPointer(4, GL_UNSIGNED_BYTE, 4, colors2);
 
137
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
 
138
 
 
139
    glDisableClientState(GL_COLOR_ARRAY);
 
140
    glDisableClientState(GL_VERTEX_ARRAY);
 
141
 
 
142
    SDL_GL_SwapBuffers();
 
143
    
 
144
#if !EMSCRIPTEN
 
145
    // Wait for 3 seconds to give us a chance to see the image
 
146
    SDL_Delay(3000);
 
147
#endif
 
148
 
 
149
    SDL_Quit();
 
150
    
 
151
    return 0;
 
152
}