Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/gl_renderers.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
{
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
#ifndef __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
typedef struct Type1 {
90
Vertex location;
91
Color color;
92
} Type1;
93
94
typedef struct Type2 {
95
GLuint unused1;
96
Vertex location;
97
GLfloat unused2;
98
Color color;
99
} Type2;
100
101
Type1 first[3] = {
102
{{-1.0, 0.0}, {0xFF, 0x00, 0x00, 0xFF}},
103
{{ 0.0, 1.0}, {0x00, 0xFF, 0x00, 0xFF}},
104
{{ 1.0, 0.0}, {0x00, 0x00, 0xFF, 0xFF}}
105
};
106
107
Type2 second[3] = {
108
{0.0, {-1.0, 0.0}, 0.0, {0xFF, 0x00, 0x00, 0xFF}},
109
{0.0, { 1.0, 0.0}, 0.0, {0x00, 0x00, 0xFF, 0xFF}},
110
{0.0, { 0.0, -1.0}, 0.0, {0x00, 0xFF, 0x00, 0xFF}}};
111
112
// make two vbo objects
113
GLuint vbo[2];
114
glGenBuffers(2, &vbo[0]);
115
116
// load the first into the context
117
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
118
119
// allocate enough space for 100 vertices
120
glBufferData(GL_ARRAY_BUFFER, sizeof(Type1)*100, NULL, GL_DYNAMIC_DRAW);
121
122
// load the second into the context
123
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
124
125
// allocate enough space for 100 vertices
126
glBufferData(GL_ARRAY_BUFFER, sizeof(Type2)*100, NULL, GL_DYNAMIC_DRAW);
127
128
// DRAW
129
130
GLbyte * pointer;
131
132
// Clear the screen before drawing
133
glClear( GL_COLOR_BUFFER_BIT );
134
135
glEnableClientState(GL_VERTEX_ARRAY);
136
glEnableClientState(GL_COLOR_ARRAY);
137
138
// FIRST
139
// load the first into the context
140
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
141
142
// Load actual data in
143
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Type1)*3, &first[0]);
144
145
// point to the buffer's location data
146
glVertexPointer(2, GL_FLOAT, sizeof(Type1), NULL);
147
148
pointer = (GLbyte*)(((GLbyte*)&first[0].color) - ((GLbyte*)&first[0].location));
149
150
printf("location = %p\n", pointer);
151
// point to the buffer's color data
152
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Type1), pointer);
153
154
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
155
156
// SECOND
157
158
// load the first into the context
159
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
160
161
// Load actual data in
162
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Type2)*3, &second[0]);
163
164
pointer = (GLbyte*)((GLbyte*)&second[0].location - (GLbyte*)&second[0].unused1);
165
166
// point to the buffer's location data
167
printf("location = %p\n", pointer);
168
glVertexPointer(2, GL_FLOAT, sizeof(Type2), pointer);
169
170
pointer = (GLbyte*)((GLbyte*)&second[0].color - (GLbyte*)&second[0].unused1);
171
172
// point to the buffer's location data
173
printf("location = %p\n", pointer);
174
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Type2), pointer);
175
176
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
177
178
glDisableClientState(GL_COLOR_ARRAY);
179
glDisableClientState(GL_VERTEX_ARRAY);
180
181
for(int i = 0; i <= 2; ++ i)
182
{
183
SDL_GL_SetSwapInterval(i);
184
assert(SDL_GL_GetSwapInterval() == i);
185
}
186
187
SDL_GL_SwapBuffers();
188
189
#ifndef __EMSCRIPTEN__
190
// Wait for 3 seconds to give us a chance to see the image
191
SDL_Delay(3000);
192
#endif
193
194
SDL_Quit();
195
196
return 0;
197
}
198
199