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.
26
#include "SDL/SDL_image.h"
27
#include "SDL/SDL_opengl.h"
33
int hasext(const char *exts, const char *ext) // from cube2, zlib licensed
35
int len = strlen(ext);
36
if(len) for(const char *cur = exts; (cur = strstr(cur, ext)); cur += len)
38
if((cur == exts || cur[-1] == ' ') && (cur[len] == ' ' || !cur[len])) return 1;
43
int main(int argc, char *argv[])
47
// Slightly different SDL initialization
48
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
49
printf("Unable to initialize SDL: %s\n", SDL_GetError());
53
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
55
screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed*
57
printf("Unable to set video mode: %s\n", SDL_GetError());
63
const char *exts = (const char *)glGetString(GL_EXTENSIONS);
64
assert(hasext(exts, "GL_ARB_texture_compression"));
65
assert(hasext(exts, "GL_EXT_texture_compression_s3tc"));
67
// Set the OpenGL state after creating the context with SDL_SetVideoMode
69
glClearColor( 0, 0, 0, 0 );
72
glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
75
glViewport( 0, 0, 640, 480 );
77
glMatrixMode( GL_PROJECTION );
78
GLfloat matrixData[] = { 2.0/640, 0, 0, 0,
82
glLoadMatrixf(matrixData); // test loadmatrix
84
glMatrixMode( GL_MODELVIEW );
88
// Load the OpenGL textures
93
const int DDS_SIZE = 65664;
94
FILE *dds = fopen("ship.dds", "rb");
96
char *ddsdata = (char*)malloc(DDS_SIZE);
97
assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
100
glGenTextures( 1, &texture );
101
glBindTexture( GL_TEXTURE_2D, texture );
103
assert(!glGetError());
104
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 256, 256, 0, DDS_SIZE-128, ddsdata+128);
105
assert(!glGetError());
107
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
108
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
116
const int DDS_SIZE = 32896;
117
FILE *dds = fopen("bloom.dds", "rb");
119
char *ddsdata = (char*)malloc(DDS_SIZE);
120
assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
123
glGenTextures( 1, &texture2 );
124
glBindTexture( GL_TEXTURE_2D, texture2 );
126
assert(!glGetError());
127
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 256, 256, 0, DDS_SIZE-128, ddsdata+128);
128
assert(!glGetError());
130
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
131
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
134
// third, a non-square texture with mipmaps
139
const int DDS_SIZE = 43920;
140
FILE *dds = fopen("water.dds", "rb");
142
char *ddsdata = (char*)malloc(DDS_SIZE);
143
assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
146
glGenTextures( 1, &texture3 );
147
glBindTexture( GL_TEXTURE_2D, texture3 );
149
assert(!glGetError());
150
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 512, 64, 0, 512*64, ddsdata+128);
151
assert(!glGetError());
153
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
154
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
157
// Prepare and Render
159
// Clear the screen before drawing
160
glClear( GL_COLOR_BUFFER_BIT );
162
// Bind the texture to which subsequent calls refer to
163
glBindTexture( GL_TEXTURE_2D, texture );
165
// Use clientside vertex pointers to render two items
166
GLfloat vertexData[] = { 0, 0, 10, 10, // texture2, position2
175
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
176
glTexCoordPointer(2, GL_FLOAT, 4*4, &vertexData[0]);
177
glEnableClientState(GL_VERTEX_ARRAY);
178
glVertexPointer(2, GL_FLOAT, 4*4, &vertexData[2]);
180
glDrawArrays(GL_QUADS, 0, 4);
182
glBindTexture( GL_TEXTURE_2D, texture3 );
183
glDrawArrays(GL_QUADS, 4, 4);
185
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
186
glDisableClientState(GL_VERTEX_ARRAY);
188
// Render the last item using oldschool glBegin etc
189
glBindTexture( GL_TEXTURE_2D, texture2 );
190
glBegin( GL_TRIANGLE_STRIP );
191
glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 0 );
192
glTexCoord2i( 1, 0 ); glVertex3f( 300, 300, 0 );
193
glTexCoord2i( 1, 1 ); glVertex3f( 300, 400, 0 );
194
glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, 0 );
197
SDL_GL_SwapBuffers();
200
// Wait for 3 seconds to give us a chance to see the image
204
// Now we can delete the OpenGL texture and close down SDL
205
glDeleteTextures( 1, &texture );