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

« back to all changes in this revision

Viewing changes to tests/gl_vertex_buffer.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

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
 
 
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 Color {
 
78
        GLubyte r;
 
79
        GLubyte g;
 
80
        GLubyte b;
 
81
        GLubyte a;
 
82
    } Color;
 
83
    
 
84
    typedef struct Vertex {
 
85
        GLfloat x;
 
86
        GLfloat y;
 
87
        Color color;
 
88
    } Vertex;
 
89
    
 
90
    Vertex vertices[18] = {
 
91
        {-1.00,  0.0, {0xFF, 0x00, 0xFF, 0xFF}},
 
92
        {-1.00,  1.0, {0xFF, 0xFF, 0x00, 0xFF}},
 
93
        {-0.75,  0.0, {0xFF, 0x00, 0x00, 0xFF}},
 
94
        {-0.75,  1.0, {0xFF, 0xFF, 0xFF, 0xFF}},
 
95
        {-0.50,  0.0, {0xFF, 0x00, 0x00, 0xFF}},
 
96
        {-0.50,  1.0, {0xFF, 0xFF, 0x00, 0xFF}},
 
97
        {-0.25,  0.0, {0xFF, 0x00, 0xFF, 0xFF}},
 
98
        {-0.25,  1.0, {0xFF, 0xFF, 0x00, 0xFF}},
 
99
        {-0.00,  0.0, {0xFF, 0x00, 0x00, 0xFF}},
 
100
        {-0.00,  1.0, {0xFF, 0xFF, 0xFF, 0xFF}},
 
101
        { 0.25,  0.0, {0xFF, 0x00, 0x00, 0xFF}},
 
102
        { 0.25,  1.0, {0xFF, 0xFF, 0x00, 0xFF}},
 
103
        { 0.50,  0.0, {0xFF, 0x00, 0xFF, 0xFF}},
 
104
        { 0.50,  1.0, {0xFF, 0xFF, 0x00, 0xFF}},
 
105
        { 0.75,  0.0, {0xFF, 0x00, 0x00, 0xFF}},
 
106
        { 0.75,  1.0, {0xFF, 0xFF, 0xFF, 0xFF}},
 
107
        { 1.00,  0.0, {0xFF, 0x00, 0x00, 0xFF}},
 
108
        { 1.00,  1.0, {0xFF, 0xFF, 0x00, 0xFF}}
 
109
    };
 
110
    
 
111
    Vertex vertices2[18] = {
 
112
        {-1.00,  -1.0, {0xFF, 0x00, 0xFF, 0xFF}},
 
113
        {-1.00,   0.0, {0xFF, 0xFF, 0x00, 0xFF}},
 
114
        {-0.75,  -1.0, {0xFF, 0x00, 0x00, 0xFF}},
 
115
        {-0.75,   0.0, {0xFF, 0xFF, 0xFF, 0xFF}},
 
116
        {-0.50,  -1.0, {0xFF, 0x00, 0x00, 0xFF}},
 
117
        {-0.50,   0.0, {0xFF, 0xFF, 0x00, 0xFF}},
 
118
        {-0.25,  -1.0, {0xFF, 0x00, 0xFF, 0xFF}},
 
119
        {-0.25,   0.0, {0xFF, 0xFF, 0x00, 0xFF}},
 
120
        {-0.00,  -1.0, {0xFF, 0x00, 0x00, 0xFF}},
 
121
        {-0.00,   0.0, {0xFF, 0xFF, 0xFF, 0xFF}},
 
122
        { 0.25,  -1.0, {0xFF, 0x00, 0x00, 0xFF}},
 
123
        { 0.25,   0.0, {0xFF, 0xFF, 0x00, 0xFF}},
 
124
        { 0.50,  -1.0, {0xFF, 0x00, 0xFF, 0xFF}},
 
125
        { 0.50,   0.0, {0xFF, 0xFF, 0x00, 0xFF}},
 
126
        { 0.75,  -1.0, {0xFF, 0x00, 0x00, 0xFF}},
 
127
        { 0.75,   0.0, {0xFF, 0xFF, 0xFF, 0xFF}},
 
128
        { 1.00,  -1.0, {0xFF, 0x00, 0x00, 0xFF}},
 
129
        { 1.00,   0.0, {0xFF, 0xFF, 0x00, 0xFF}}
 
130
    };
 
131
    
 
132
    //    make a vertex buffer for the second set of vertices
 
133
    GLuint vbo = 0;
 
134
    glGenBuffers(1, &vbo);
 
135
    
 
136
    
 
137
    //    bind to it
 
138
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
 
139
    //    send it to gl
 
140
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_READ); // GL_STATIC_READ is not in WebGL!
 
141
    
 
142
    //    unbind from it
 
143
    glBindBuffer(GL_ARRAY_BUFFER, 0);
 
144
    
 
145
 
 
146
    // DRAW
 
147
    
 
148
    // Clear the screen before drawing
 
149
    glClear( GL_COLOR_BUFFER_BIT );
 
150
    
 
151
    // This test ensures that we can use two separate arrays in memory for different
 
152
    // attributes, and that they each can have different stride.
 
153
    // The first test shows implicit striding (the zero indicates tightly packed)
 
154
    // The second test shows explicit striding where the stride is passed in
 
155
    //   even though it also is tightly packed
 
156
    
 
157
    glEnableClientState(GL_VERTEX_ARRAY);
 
158
    glEnableClientState(GL_COLOR_ARRAY);
 
159
    
 
160
    // TEST 1 - clientside data
 
161
    
 
162
    glVertexPointer(2, GL_FLOAT, sizeof(Vertex), vertices);
 
163
    glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].color);
 
164
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
 
165
    glDrawArrays(GL_TRIANGLE_STRIP, 10, 3);
 
166
    
 
167
 
 
168
    
 
169
    // TEST 2 - bind to array buffer, gl*Pointer calls are offsets into the buffer, which was previously uploaded to
 
170
    
 
171
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
 
172
 
 
173
    glVertexPointer(2, GL_FLOAT, sizeof(Vertex), 0);
 
174
    glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), (GLvoid*)((GLvoid*)&vertices2[0].color - (GLvoid*)&vertices2[0]));
 
175
    
 
176
//    gldrawarrays first with a low number of vertices, then with a high number
 
177
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
 
178
    glDrawArrays(GL_TRIANGLE_STRIP, 10, 3);
 
179
 
 
180
    glBindBuffer(GL_ARRAY_BUFFER, 0);
 
181
 
 
182
    glDisableClientState(GL_COLOR_ARRAY);
 
183
    glDisableClientState(GL_VERTEX_ARRAY);
 
184
 
 
185
    SDL_GL_SwapBuffers();
 
186
    
 
187
#if !EMSCRIPTEN
 
188
    // Wait for 3 seconds to give us a chance to see the image
 
189
    SDL_Delay(3000);
 
190
#endif
 
191
    
 
192
    SDL_Quit();
 
193
    
 
194
    return 0;
 
195
}