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);
82
glUseProgram(program);
85
int main(int argc, char *argv[])
89
// Slightly different SDL initialization
90
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
91
printf("Unable to initialize SDL: %s\n", SDL_GetError());
95
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
97
screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
99
printf("Unable to set video mode: %s\n", SDL_GetError());
103
// Set the OpenGL state after creating the context with SDL_SetVideoMode
105
glClearColor( 0, 0, 0, 0 );
108
glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
111
glViewport( 0, 0, 640, 480 );
113
glMatrixMode( GL_PROJECTION );
114
GLfloat matrixData[] = { 2.0/640, 0, 0, 0,
118
glLoadMatrixf(matrixData); // test loadmatrix
120
glMatrixMode( GL_MODELVIEW );
123
// Load the OpenGL texture
125
GLuint texture; // Texture object handle
126
SDL_Surface *surface; // Gives us the information to make the texture
128
if ( (surface = IMG_Load("screenshot.png")) ) {
130
// Check that the image's width is a power of 2
131
if ( (surface->w & (surface->w - 1)) != 0 ) {
132
printf("warning: image.bmp's width is not a power of 2\n");
135
// Also check if the height is a power of 2
136
if ( (surface->h & (surface->h - 1)) != 0 ) {
137
printf("warning: image.bmp's height is not a power of 2\n");
140
// Have OpenGL generate a texture object handle for us
141
glGenTextures( 1, &texture );
143
// Bind the texture object
144
glBindTexture( GL_TEXTURE_2D, texture );
146
// Set the texture's stretching properties
147
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
148
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
150
//SDL_LockSurface(surface);
153
memset(surface->pixels, 0x66, surface->w*surface->h);
155
// Edit the texture object's image data using the information SDL_Surface gives us
156
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,
157
GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels );
159
//SDL_UnlockSurface(surface);
162
printf("SDL could not load image.bmp: %s\n", SDL_GetError());
167
// Free the SDL_Surface only if it was successfully created
169
SDL_FreeSurface( surface );
172
// Clear the screen before drawing
173
glClear( GL_COLOR_BUFFER_BIT );
177
// Bind the texture to which subsequent calls refer to
178
glBindTexture( GL_TEXTURE_2D, texture );
180
// Use clientside vertex pointers to render two items. In this test we have each
181
// attribute in a separate buffer, packed (i.e. stride == 0)
182
GLfloat vertexData[] = { 10, 10,
194
GLfloat textureData[] = { 0, 0,
207
glEnable(GL_TEXTURE_2D); // XXX should be GL_TEXTURE_COORD_ARRAY); and also glEnableClientState! XXX two workarounds here
208
glTexCoordPointer(2, GL_FLOAT, 0, textureData);
209
glEnableClientState(GL_VERTEX_ARRAY);
210
glVertexPointer(2, GL_FLOAT, 0, vertexData);
212
glDrawArrays(GL_QUADS, 0, 12);
214
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
215
glDisableClientState(GL_VERTEX_ARRAY);
217
SDL_GL_SwapBuffers();
220
// Wait for 3 seconds to give us a chance to see the image
224
// Now we can delete the OpenGL texture and close down SDL
225
glDeleteTextures( 1, &texture );