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
#define MAX(x, y) ((x) > (y) ? (x) : (y))
35
int hasext(const char *exts, const char *ext) // from cube2, zlib licensed
37
int len = strlen(ext);
38
if(len) for(const char *cur = exts; (cur = strstr(cur, ext)); cur += len)
40
if((cur == exts || cur[-1] == ' ') && (cur[len] == ' ' || !cur[len])) return 1;
45
int main(int argc, char *argv[])
49
// Slightly different SDL initialization
50
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
51
printf("Unable to initialize SDL: %s\n", SDL_GetError());
55
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
57
screen = SDL_SetVideoMode( 600, 600, 16, SDL_OPENGL ); // *changed*
59
printf("Unable to set video mode: %s\n", SDL_GetError());
65
const char *exts = (const char *)glGetString(GL_EXTENSIONS);
66
assert(hasext(exts, "GL_EXT_texture_filter_anisotropic"));
69
glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso);
70
printf("Max anisotropy: %d (using that)\n", aniso);
73
// Set the OpenGL state after creating the context with SDL_SetVideoMode
75
glClearColor( 0, 0, 0, 0 );
78
glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL
81
glViewport( 0, 0, 600, 600 );
83
glMatrixMode( GL_PROJECTION );
84
GLfloat matrixData[] = { 2.0/600, 0, 0, 0,
88
glLoadMatrixf(matrixData); // test loadmatrix
90
glMatrixMode( GL_MODELVIEW );
94
// Load the OpenGL texture
96
GLuint texture, texture2;
98
const int DDS_SIZE = 43920;
99
FILE *dds = fopen("water.dds", "rb");
101
char *ddsdata = (char*)malloc(DDS_SIZE);
102
assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
106
glGenTextures( 1, &texture );
107
glBindTexture( GL_TEXTURE_2D, texture );
109
char *curr = ddsdata + 128;
114
printf("uploading level %d: %d, %d\n", level, w, h);
115
assert(!glGetError());
116
glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);
117
assert(!glGetError());
118
curr += MAX(w, 4)*MAX(h, 4);
123
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
124
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
127
glGenTextures( 1, &texture2 );
128
glBindTexture( GL_TEXTURE_2D, texture2 );
130
char *curr = ddsdata + 128;
135
printf("uploading level %d: %d, %d\n", level, w, h);
136
assert(!glGetError());
137
glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);
138
assert(!glGetError());
139
curr += MAX(w, 4)*MAX(h, 4);
144
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
145
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
146
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
149
assert(!glGetError());
150
glBindFramebuffer(GL_RENDERBUFFER, 0);
151
assert(glGetError());
154
// Prepare and Render
156
// Clear the screen before drawing
157
glClear( GL_COLOR_BUFFER_BIT );
159
// Bind the texture to which subsequent calls refer to
162
glBindTexture( GL_TEXTURE_2D, texture );
163
for (int x = 0; x < n; x++) {
165
glBegin( GL_TRIANGLES );
166
glTexCoord2i( 1, 0 ); glVertex3f( start , 0, 0 );
167
glTexCoord2i( 0, 0 ); glVertex3f( start+w, 300, 0 );
168
glTexCoord2i( 1, 1 ); glVertex3f( start-w, 300, 0 );
171
glBindTexture( GL_TEXTURE_2D, texture2 );
172
for (int x = 0; x < n; x++) {
173
int start = n*w*2 + x*w*2;
174
glBegin( GL_TRIANGLES );
175
glTexCoord2i( 1, 0 ); glVertex3f( start , 0, 0 );
176
glTexCoord2i( 0, 0 ); glVertex3f( start+w, 300, 0 );
177
glTexCoord2i( 1, 1 ); glVertex3f( start-w, 300, 0 );
183
for (int x = 0; x < n; x++) {
184
for (int y = 0; y < n*2; y++) {
185
glBindTexture( GL_TEXTURE_2D, texture );
186
glBegin( GL_TRIANGLE_STRIP );
187
glTexCoord2i( 0, 0 ); glVertex3f( x*w, y*(w), 0 );
188
glTexCoord2i( 1, 0 ); glVertex3f( (x+1)*(w-2*y/n), y*(w), 0 );
189
glTexCoord2i( 1, 1 ); glVertex3f( (x+1)*(w-2*y/n), (y+1)*(w), 0 );
190
glTexCoord2i( 0, 1 ); glVertex3f( x*w, (y+1)*(w), 0 );
192
glBindTexture( GL_TEXTURE_2D, texture2 );
193
glBegin( GL_TRIANGLE_STRIP );
194
glTexCoord2i( 0, 0 ); glVertex3f( n*w + x*w, y*(w), 0 );
195
glTexCoord2i( 1, 0 ); glVertex3f( n*w + (x+1)*(w-2*y/n), y*(w), 0 );
196
glTexCoord2i( 1, 1 ); glVertex3f( n*w + (x+1)*(w-2*y/n), (y+1)*(w), 0 );
197
glTexCoord2i( 0, 1 ); glVertex3f( n*w + x*w, (y+1)*(w), 0 );
202
SDL_GL_SwapBuffers();
205
// Wait for 3 seconds to give us a chance to see the image
209
// Now we can delete the OpenGL texture and close down SDL
210
glDeleteTextures( 1, &texture );