Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/gl_stride.c
7085 views
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
#ifndef __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
SDL_Surface *screen;
45
46
// Slightly different SDL initialization
47
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
48
printf("Unable to initialize SDL: %s\n", SDL_GetError());
49
return 1;
50
}
51
52
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
53
54
screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
55
if ( !screen ) {
56
printf("Unable to set video mode: %s\n", SDL_GetError());
57
return 1;
58
}
59
60
// Set the OpenGL state after creating the context with SDL_SetVideoMode
61
62
glClearColor( 0, 0, 0, 0 );
63
64
#ifndef __EMSCRIPTEN__
65
glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
66
#endif
67
68
glViewport( 0, 0, 640, 480 );
69
70
glMatrixMode( GL_MODELVIEW );
71
glLoadIdentity();
72
73
// Clear the screen before drawing
74
glClear( GL_COLOR_BUFFER_BIT );
75
76
typedef struct Vertex {
77
GLfloat x;
78
GLfloat y;
79
} Vertex;
80
81
typedef struct Color {
82
GLubyte r;
83
GLubyte g;
84
GLubyte b;
85
GLubyte a;
86
} Color;
87
88
Vertex vertices[3] = {
89
{-1.0, 0.0},
90
{ 0.0, 1.0},
91
{ 1.0, 0.0}
92
};
93
94
Color colors[3] = {
95
{0xFF, 0x00, 0x00, 0xFF},
96
{0x00, 0xFF, 0x00, 0xFF},
97
{0x00, 0x00, 0xFF, 0xFF}
98
};
99
100
Vertex vertices2[3] = {
101
{-1.0, 0.0},
102
{ 1.0, 0.0},
103
{ 0.0, -1.0}
104
};
105
106
Color colors2[3] = {
107
{0xFF, 0x00, 0x00, 0xFF},
108
{0x00, 0x00, 0xFF, 0xFF},
109
{0x00, 0xFF, 0x00, 0xFF}
110
};
111
112
// DRAW
113
114
// Clear the screen before drawing
115
glClear( GL_COLOR_BUFFER_BIT );
116
117
// This test ensures that we can use two separate arrays in memory for different
118
// attributes, and that they each can have different stride.
119
// The first test shows implicit striding (the zero indicates tightly packed)
120
// The second test shows explicit striding where the stride is passed in
121
// even though it also is tightly packed
122
123
glEnableClientState(GL_VERTEX_ARRAY);
124
glEnableClientState(GL_COLOR_ARRAY);
125
126
// TEST 1
127
128
glVertexPointer(2, GL_FLOAT, 0, vertices);
129
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
130
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
131
132
// TEST 2
133
134
glVertexPointer(2, GL_FLOAT, 8, vertices2);
135
glColorPointer(4, GL_UNSIGNED_BYTE, 4, colors2);
136
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
137
138
glDisableClientState(GL_COLOR_ARRAY);
139
glDisableClientState(GL_VERTEX_ARRAY);
140
141
SDL_GL_SwapBuffers();
142
143
#ifndef __EMSCRIPTEN__
144
// Wait for 3 seconds to give us a chance to see the image
145
SDL_Delay(3000);
146
#endif
147
148
SDL_Quit();
149
150
return 0;
151
}
152
153