Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/test_anisotropic.c
4150 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
#include "SDL/SDL.h"
26
#include "SDL/SDL_image.h"
27
#include "SDL/SDL_opengl.h"
28
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include <assert.h>
33
34
#define MAX(x, y) ((x) > (y) ? (x) : (y))
35
36
int hasext(const char *exts, const char *ext) // from cube2, zlib licensed
37
{
38
int len = strlen(ext);
39
if(len) for(const char *cur = exts; (cur = strstr(cur, ext)); cur += len)
40
{
41
if((cur == exts || cur[-1] == ' ') && (cur[len] == ' ' || !cur[len])) return 1;
42
}
43
return 0;
44
}
45
46
int main(int argc, char *argv[])
47
{
48
SDL_Surface *screen;
49
50
// Slightly different SDL initialization
51
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
52
printf("Unable to initialize SDL: %s\n", SDL_GetError());
53
return 1;
54
}
55
56
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*
57
58
screen = SDL_SetVideoMode( 600, 600, 16, SDL_OPENGL ); // *changed*
59
if ( !screen ) {
60
printf("Unable to set video mode: %s\n", SDL_GetError());
61
return 1;
62
}
63
64
// Check extensions
65
66
const char *exts = (const char *)glGetString(GL_EXTENSIONS);
67
assert(hasext(exts, "GL_EXT_texture_filter_anisotropic"));
68
69
const char *vendor = (const char *)glGetString(GL_VENDOR);
70
printf("vendor: %s\n", vendor);
71
72
GLint aniso;
73
glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso);
74
printf("Max anisotropy: %d (using that)\n", aniso);
75
assert(aniso >= 4);
76
77
// Set the OpenGL state after creating the context with SDL_SetVideoMode
78
79
glClearColor( 0, 0, 0, 0 );
80
81
glEnable( GL_TEXTURE_2D ); // Needed when we're using the fixed-function pipeline.
82
83
glViewport( 0, 0, 600, 600 );
84
85
glMatrixMode( GL_PROJECTION );
86
GLfloat matrixData[] = { 2.0/600, 0, 0, 0,
87
0, -2.0/600, 0, 0,
88
0, 0, -2.0/600, 0,
89
-1, 1, 0, 1 };
90
glLoadMatrixf(matrixData); // test loadmatrix
91
92
glMatrixMode( GL_MODELVIEW );
93
glLoadIdentity();
94
95
96
// Load the OpenGL texture
97
98
GLuint texture, texture2;
99
100
const int DDS_SIZE = 43920;
101
FILE *dds = fopen("water.dds", "rb");
102
assert(dds);
103
char *ddsdata = (char*)malloc(DDS_SIZE);
104
assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
105
fclose(dds);
106
107
{
108
glGenTextures( 1, &texture );
109
glBindTexture( GL_TEXTURE_2D, texture );
110
111
char *curr = ddsdata + 128;
112
int level = 0;
113
int w = 512;
114
int h = 64;
115
while (level < 5) {
116
printf("uploading level %d: %d, %d\n", level, w, h);
117
assert(!glGetError());
118
#if TEST_TEXSUBIMAGE
119
glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, NULL);
120
glCompressedTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, w, h, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w*h, curr);
121
#else
122
glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);
123
#endif
124
assert(!glGetError());
125
curr += MAX(w, 4)*MAX(h, 4);
126
w /= 2;
127
h /= 2;
128
level++;
129
}
130
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
131
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
132
}
133
{
134
glGenTextures( 1, &texture2 );
135
glBindTexture( GL_TEXTURE_2D, texture2 );
136
137
char *curr = ddsdata + 128;
138
int level = 0;
139
int w = 512;
140
int h = 64;
141
while (level < 5) {
142
printf("uploading level %d: %d, %d\n", level, w, h);
143
assert(!glGetError());
144
#if TEST_TEXSUBIMAGE
145
glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, NULL);
146
glCompressedTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, w, h, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w*h, curr);
147
#else
148
glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);
149
#endif
150
assert(!glGetError());
151
curr += MAX(w, 4)*MAX(h, 4);
152
w /= 2;
153
h /= 2;
154
level++;
155
}
156
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
157
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
158
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
159
}
160
{
161
assert(!glGetError());
162
glBindFramebuffer(GL_RENDERBUFFER, 0);
163
assert(glGetError());
164
165
GLint out = 321;
166
assert(!glGetError());
167
glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &out); // invalid, just test output
168
assert(out == 0);
169
}
170
171
// Prepare and Render
172
173
// Clear the screen before drawing
174
glClear( GL_COLOR_BUFFER_BIT );
175
176
// Bind the texture to which subsequent calls refer to
177
int w = 10;
178
int n = 15;
179
glBindTexture( GL_TEXTURE_2D, texture );
180
for (int x = 0; x < n; x++) {
181
int start = x*w*2;
182
glBegin( GL_TRIANGLES );
183
glTexCoord2i( 1, 0 ); glVertex2i( start , 0 );
184
glTexCoord2i( 0, 0 ); glVertex3f( start+w, 300, 0 );
185
glTexCoord2i( 1, 1 ); glVertex3f( start-w, 300, 0 );
186
glEnd();
187
}
188
glBindTexture( GL_TEXTURE_2D, texture2 );
189
for (int x = 0; x < n; x++) {
190
int start = n*w*2 + x*w*2;
191
glBegin( GL_TRIANGLES );
192
glTexCoord2i( 1, 0 ); glVertex3f( start , 0, 0 );
193
glTexCoord2i( 0, 0 ); glVertex3f( start+w, 300, 0 );
194
glTexCoord2i( 1, 1 ); glVertex3f( start-w, 300, 0 );
195
glEnd();
196
}
197
/*
198
int w = 8;
199
int n = 20;
200
for (int x = 0; x < n; x++) {
201
for (int y = 0; y < n*2; y++) {
202
glBindTexture( GL_TEXTURE_2D, texture );
203
glBegin( GL_TRIANGLE_STRIP );
204
glTexCoord2i( 0, 0 ); glVertex3f( x*w, y*(w), 0 );
205
glTexCoord2i( 1, 0 ); glVertex3f( (x+1)*(w-2*y/n), y*(w), 0 );
206
glTexCoord2i( 1, 1 ); glVertex3f( (x+1)*(w-2*y/n), (y+1)*(w), 0 );
207
glTexCoord2i( 0, 1 ); glVertex3f( x*w, (y+1)*(w), 0 );
208
glEnd();
209
glBindTexture( GL_TEXTURE_2D, texture2 );
210
glBegin( GL_TRIANGLE_STRIP );
211
glTexCoord2i( 0, 0 ); glVertex3f( n*w + x*w, y*(w), 0 );
212
glTexCoord2i( 1, 0 ); glVertex3f( n*w + (x+1)*(w-2*y/n), y*(w), 0 );
213
glTexCoord2i( 1, 1 ); glVertex3f( n*w + (x+1)*(w-2*y/n), (y+1)*(w), 0 );
214
glTexCoord2i( 0, 1 ); glVertex3f( n*w + x*w, (y+1)*(w), 0 );
215
glEnd();
216
}
217
}
218
*/
219
SDL_GL_SwapBuffers();
220
221
#ifndef __EMSCRIPTEN__
222
// Wait for 3 seconds to give us a chance to see the image
223
SDL_Delay(2000);
224
#endif
225
226
// Now we can delete the OpenGL texture and close down SDL
227
glDeleteTextures( 1, &texture );
228
229
SDL_Quit();
230
231
// check for asm compilation bug with aliased functions with different sigs
232
233
glBegin( GL_TRIANGLE_STRIP );
234
void (*f)(int, int) = glVertex2i;
235
if ((long)f % 16 == 4) f(5, 7);
236
void (*g)(int, int) = glVertex3f;
237
if ((long)g % 16 == 4) g(5, 7);
238
return (int)((long)f + (long)g);
239
}
240
241
242