Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/gl_ps.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
// Also, check getting the error log
89
const char *fakeVertexShader = "atbute ve4 blarg; ### AAA\n";
90
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
91
glShaderSource(vs, 1, &fakeVertexShader, NULL);
92
glCompileShader(vs);
93
glGetShaderiv(vs, GL_COMPILE_STATUS, &ok);
94
assert(!ok);
95
GLint infoLen = 0;
96
glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &infoLen);
97
assert(infoLen > 1);
98
}
99
}
100
101
int main(int argc, char *argv[]) {
102
SDL_Surface *screen;
103
104
// Slightly different SDL initialization
105
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
106
printf("Unable to initialize SDL: %s\n", SDL_GetError());
107
return 1;
108
}
109
110
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
111
112
screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
113
if ( !screen ) {
114
printf("Unable to set video mode: %s\n", SDL_GetError());
115
return 1;
116
}
117
118
// Set the OpenGL state after creating the context with SDL_SetVideoMode
119
120
glClearColor( 0, 0, 0, 0 );
121
122
#ifndef __EMSCRIPTEN__
123
glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
124
#endif
125
126
glViewport( 0, 0, 640, 480 );
127
128
glMatrixMode( GL_PROJECTION );
129
GLfloat matrixData[] = { 2.0/640, 0, 0, 0,
130
0, -2.0/480, 0, 0,
131
0, 0, -1, 0,
132
-1, 1, 0, 1 };
133
glLoadMatrixf(matrixData); // test loadmatrix
134
135
glMatrixMode( GL_MODELVIEW );
136
glLoadIdentity();
137
138
// Load the OpenGL texture
139
140
GLuint texture; // Texture object handle
141
SDL_Surface *surface; // Gives us the information to make the texture
142
143
if ( (surface = IMG_Load("screenshot.png")) ) {
144
145
// Check that the image's width is a power of 2
146
if ( (surface->w & (surface->w - 1)) != 0 ) {
147
printf("warning: image.bmp's width is not a power of 2\n");
148
}
149
150
// Also check if the height is a power of 2
151
if ( (surface->h & (surface->h - 1)) != 0 ) {
152
printf("warning: image.bmp's height is not a power of 2\n");
153
}
154
155
// Have OpenGL generate a texture object handle for us
156
glGenTextures( 1, &texture );
157
158
// Bind the texture object
159
glBindTexture( GL_TEXTURE_2D, texture );
160
161
// Set the texture's stretching properties
162
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
163
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
164
165
//SDL_LockSurface(surface);
166
167
// Add some greyness
168
memset(surface->pixels, 0x66, surface->w*surface->h);
169
170
// Edit the texture object's image data using the information SDL_Surface gives us
171
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,
172
GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
173
174
//SDL_UnlockSurface(surface);
175
}
176
else {
177
printf("SDL could not load image.bmp: %s\n", SDL_GetError());
178
SDL_Quit();
179
return 1;
180
}
181
182
// Free the SDL_Surface only if it was successfully created
183
if ( surface ) {
184
SDL_FreeSurface( surface );
185
}
186
187
// Clear the screen before drawing
188
glClear( GL_COLOR_BUFFER_BIT );
189
190
shaders();
191
192
// Bind the texture to which subsequent calls refer to
193
glBindTexture( GL_TEXTURE_2D, texture );
194
195
// Use clientside vertex pointers to render two items
196
GLfloat vertexData[] = { 0, 0, 10, 10, // texture2, position2
197
1, 0, 300, 10,
198
1, 1, 300, 128,
199
0, 1, 10, 128,
200
0, 0.5, 410, 10,
201
1, 0.5, 600, 10,
202
1, 1, 630, 200,
203
0.5, 1, 310, 250,
204
0, 0, 100, 300,
205
1, 0, 300, 300,
206
1, 1, 300, 400,
207
0, 1, 100, 400 };
208
209
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
210
glTexCoordPointer(2, GL_FLOAT, 4*4, &vertexData[0]);
211
glEnableClientState(GL_VERTEX_ARRAY);
212
glVertexPointer(2, GL_FLOAT, 4*4, &vertexData[2]);
213
214
glDrawArrays(GL_QUADS, 0, 12);
215
216
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
217
glDisableClientState(GL_VERTEX_ARRAY);
218
219
SDL_GL_SwapBuffers();
220
221
#ifndef __EMSCRIPTEN__
222
// Wait for 3 seconds to give us a chance to see the image
223
SDL_Delay(3000);
224
#endif
225
226
// Now we can delete the OpenGL texture and close down SDL
227
glDeleteTextures( 1, &texture );
228
229
SDL_Quit();
230
231
return 0;
232
}
233
234