Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/gl_ps_worker.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 1
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
void shaders() {
44
#if USE_GLEW
45
glewInit();
46
#endif
47
48
GLint ok;
49
50
const char *vertexShader = "void main(void) \n"
51
"{ \n"
52
" gl_Position = ftransform(); \n"
53
" gl_TexCoord[0] = gl_MultiTexCoord0; \n"
54
" gl_FrontColor = gl_Color; \n"
55
"} \n";
56
const char *fragmentShader = "uniform sampler2D tex0; \n"
57
"void main(void) \n"
58
"{ \n"
59
" gl_FragColor = gl_Color * texture2D(tex0, gl_TexCoord[0].xy); \n"
60
"} \n";
61
62
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
63
glShaderSource(vs, 1, &vertexShader, NULL);
64
glCompileShader(vs);
65
glGetShaderiv(vs, GL_COMPILE_STATUS, &ok);
66
assert(ok);
67
68
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
69
glShaderSource(fs, 1, &fragmentShader, NULL);
70
glCompileShader(fs);
71
glGetShaderiv(fs, GL_COMPILE_STATUS, &ok);
72
assert(ok);
73
74
GLuint program = glCreateProgram();
75
76
glAttachShader(program, vs);
77
glAttachShader(program, fs);
78
glLinkProgram(program);
79
glGetProgramiv(program, GL_LINK_STATUS, &ok);
80
assert(ok);
81
assert(glIsProgram(program));
82
assert(!glIsProgram(0));
83
assert(!glIsProgram(program+1)); // a number that can't be a real shader
84
85
glUseProgram(program);
86
}
87
88
int main(int argc, char *argv[])
89
{
90
SDL_Surface *screen;
91
92
// Slightly different SDL initialization
93
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
94
printf("Unable to initialize SDL: %s\n", SDL_GetError());
95
return 1;
96
}
97
98
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
99
100
screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
101
if ( !screen ) {
102
printf("Unable to set video mode: %s\n", SDL_GetError());
103
return 1;
104
}
105
106
// Set the OpenGL state after creating the context with SDL_SetVideoMode
107
108
glClearColor( 0, 0, 0, 0 );
109
110
#ifndef __EMSCRIPTEN__
111
glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
112
#endif
113
114
glViewport( 0, 0, 640, 480 );
115
116
glMatrixMode( GL_PROJECTION );
117
GLfloat matrixData[] = { 2.0/640, 0, 0, 0,
118
0, -2.0/480, 0, 0,
119
0, 0, -1, 0,
120
-1, 1, 0, 1 };
121
glLoadMatrixf(matrixData); // test loadmatrix
122
123
glMatrixMode( GL_MODELVIEW );
124
glLoadIdentity();
125
126
// Load the OpenGL texture
127
128
GLuint texture; // Texture object handle
129
SDL_Surface *surface; // Gives us the information to make the texture
130
131
if ( (surface = IMG_Load("screenshot.png")) ) {
132
133
// Check that the image's width is a power of 2
134
if ( (surface->w & (surface->w - 1)) != 0 ) {
135
printf("warning: image.bmp's width is not a power of 2\n");
136
}
137
138
// Also check if the height is a power of 2
139
if ( (surface->h & (surface->h - 1)) != 0 ) {
140
printf("warning: image.bmp's height is not a power of 2\n");
141
}
142
143
// Have OpenGL generate a texture object handle for us
144
glGenTextures( 1, &texture );
145
146
// Test binding null
147
glBindTexture( GL_TEXTURE_2D, 0 );
148
149
// Bind the texture object
150
glBindTexture( GL_TEXTURE_2D, texture );
151
152
// Set the texture's stretching properties
153
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
154
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
155
156
//SDL_LockSurface(surface);
157
158
// Add some greyness
159
memset(surface->pixels, 0x66, surface->w*surface->h);
160
161
// Edit the texture object's image data using the information SDL_Surface gives us
162
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,
163
GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
164
165
//SDL_UnlockSurface(surface);
166
}
167
else {
168
printf("SDL could not load image.bmp: %s\n", SDL_GetError());
169
SDL_Quit();
170
return 1;
171
}
172
173
// Free the SDL_Surface only if it was successfully created
174
if ( surface ) {
175
SDL_FreeSurface( surface );
176
}
177
178
// Clear the screen before drawing
179
glClear( GL_COLOR_BUFFER_BIT );
180
181
shaders();
182
183
// Bind the texture to which subsequent calls refer to
184
glBindTexture( GL_TEXTURE_2D, texture );
185
186
// Use clientside vertex pointers to render two items
187
GLfloat vertexData[] = { 0, 0, 10, 10, // texture2, position2
188
1, 0, 300, 10,
189
1, 1, 300, 128,
190
0, 1, 10, 128,
191
0, 0.5, 410, 10,
192
1, 0.5, 600, 10,
193
1, 1, 630, 200,
194
0.5, 1, 310, 250,
195
0, 0, 100, 300,
196
1, 0, 300, 300,
197
1, 1, 300, 400,
198
0, 1, 100, 400 };
199
200
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
201
glTexCoordPointer(2, GL_FLOAT, 4*4, &vertexData[0]);
202
glEnableClientState(GL_VERTEX_ARRAY);
203
glVertexPointer(2, GL_FLOAT, 4*4, &vertexData[2]);
204
205
glDrawArrays(GL_QUADS, 0, 12);
206
207
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
208
glDisableClientState(GL_VERTEX_ARRAY);
209
210
SDL_GL_SwapBuffers();
211
212
#ifndef __EMSCRIPTEN__
213
// Wait for 3 seconds to give us a chance to see the image
214
SDL_Delay(3000);
215
#endif
216
217
// Now we can delete the OpenGL texture and close down SDL
218
glDeleteTextures( 1, &texture );
219
220
SDL_Quit();
221
222
return 0;
223
}
224
225