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