1
/*******************************************************************
3
* Using SDL With OpenGL *
5
* Tutorial by Kyle Foley (sdw) *
7
* http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL *
9
*******************************************************************/
12
THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION
13
AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.
15
THE ORIGINAL AUTHOR IS KYLE FOLEY.
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.
34
#include "SDL/SDL_image.h"
36
#include "SDL/SDL_opengl.h"
50
const char *vertexShader = "void main(void) \n"
52
" gl_Position = ftransform(); \n"
53
" gl_TexCoord[0] = gl_MultiTexCoord0; \n"
54
" gl_FrontColor = gl_Color; \n"
56
const char *fragmentShader = "uniform sampler2D tex0; \n"
59
" gl_FragColor = gl_Color * texture2D(tex0, gl_TexCoord[0].xy); \n"
62
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
63
glShaderSource(vs, 1, &vertexShader, NULL);
65
glGetShaderiv(vs, GL_COMPILE_STATUS, &ok);
68
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
69
glShaderSource(fs, 1, &fragmentShader, NULL);
71
glGetShaderiv(fs, GL_COMPILE_STATUS, &ok);
74
GLuint program = glCreateProgram();
76
glAttachShader(program, vs);
77
glAttachShader(program, fs);
78
glLinkProgram(program);
79
glGetProgramiv(program, GL_LINK_STATUS, &ok);
81
assert(glIsProgram(program));
82
assert(!glIsProgram(0));
83
assert(!glIsProgram(program+1)); // a number that can't be a real shader
85
glUseProgram(program);
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);
93
glGetShaderiv(vs, GL_COMPILE_STATUS, &ok);
96
glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &infoLen);
101
int main(int argc, char *argv[])
105
// Slightly different SDL initialization
106
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
107
printf("Unable to initialize SDL: %s\n", SDL_GetError());
111
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
113
screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
115
printf("Unable to set video mode: %s\n", SDL_GetError());
119
// Set the OpenGL state after creating the context with SDL_SetVideoMode
121
glClearColor( 0, 0, 0, 0 );
124
glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
127
glViewport( 0, 0, 640, 480 );
129
glMatrixMode( GL_PROJECTION );
130
GLfloat matrixData[] = { 2.0/640, 0, 0, 0,
134
glLoadMatrixf(matrixData); // test loadmatrix
136
glMatrixMode( GL_MODELVIEW );
139
// Load the OpenGL texture
141
GLuint texture; // Texture object handle
142
SDL_Surface *surface; // Gives us the information to make the texture
144
if ( (surface = IMG_Load("screenshot.png")) ) {
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");
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");
156
// Have OpenGL generate a texture object handle for us
157
glGenTextures( 1, &texture );
159
// Bind the texture object
160
glBindTexture( GL_TEXTURE_2D, texture );
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 );
166
//SDL_LockSurface(surface);
169
memset(surface->pixels, 0x66, surface->w*surface->h);
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 );
175
//SDL_UnlockSurface(surface);
178
printf("SDL could not load image.bmp: %s\n", SDL_GetError());
183
// Free the SDL_Surface only if it was successfully created
185
SDL_FreeSurface( surface );
188
// Clear the screen before drawing
189
glClear( GL_COLOR_BUFFER_BIT );
193
// Bind the texture to which subsequent calls refer to
194
glBindTexture( GL_TEXTURE_2D, texture );
196
// Use clientside vertex pointers to render two items
197
GLfloat vertexData[] = { 0, 0, 10, 10, // texture2, position2
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]);
215
glDrawArrays(GL_QUADS, 0, 4);
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);
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);
225
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
226
glDisableClientState(GL_VERTEX_ARRAY);
228
SDL_GL_SwapBuffers();
231
// Wait for 3 seconds to give us a chance to see the image
235
// Now we can delete the OpenGL texture and close down SDL
236
glDeleteTextures( 1, &texture );