aboutsummaryrefslogtreecommitdiff
path: root/tests/aniso.c
blob: b925149d3d369c27a8041e425fbb8ea4d986298d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************
 *                                                                 *
 *                        Using SDL With OpenGL                    *
 *                                                                 *
 *                    Tutorial by Kyle Foley (sdw)                 *
 *                                                                 *
 * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL *
 *                                                                 *
 *******************************************************************/

/*
THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION
AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN.

THE ORIGINAL AUTHOR IS KYLE FOLEY.

THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
RESULTING FROM THE USE, MODIFICATION, OR
REDISTRIBUTION OF THIS SOFTWARE.
*/

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_opengl.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAX(x, y) ((x) > (y) ? (x) : (y))

int hasext(const char *exts, const char *ext) // from cube2, zlib licensed
{
    int len = strlen(ext);
    if(len) for(const char *cur = exts; (cur = strstr(cur, ext)); cur += len)
    {
        if((cur == exts || cur[-1] == ' ') && (cur[len] == ' ' || !cur[len])) return 1;
    }
    return 0;
}

int main(int argc, char *argv[])
{
    SDL_Surface *screen;

    // Slightly different SDL initialization
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }

    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new*

    screen = SDL_SetVideoMode( 600, 600, 16, SDL_OPENGL ); // *changed*
    if ( !screen ) {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }

    // Check extensions

    const char *exts = (const char *)glGetString(GL_EXTENSIONS);
    assert(hasext(exts, "GL_EXT_texture_filter_anisotropic"));

    const char *vendor = (const char *)glGetString(GL_VENDOR);
    printf("vendor: %s\n", vendor);

    GLint aniso;
    glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso);
    printf("Max anisotropy: %d (using that)\n", aniso);
    assert(aniso >= 4);

    // Set the OpenGL state after creating the context with SDL_SetVideoMode

    glClearColor( 0, 0, 0, 0 );

    glEnable( GL_TEXTURE_2D ); // Needed when we're using the fixed-function pipeline.

    glViewport( 0, 0, 600, 600 );

    glMatrixMode( GL_PROJECTION );
    GLfloat matrixData[] = { 2.0/600,        0,        0,  0,
                                   0, -2.0/600,        0,  0,
                                   0,        0, -2.0/600,  0,
                                  -1,        1,        0,  1 };
    glLoadMatrixf(matrixData); // test loadmatrix

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();


    // Load the OpenGL texture

    GLuint texture, texture2;

    const int DDS_SIZE = 43920;
    FILE *dds = fopen("water.dds", "rb");
    assert(dds);
    char *ddsdata = (char*)malloc(DDS_SIZE);
    assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE);
    fclose(dds);

    {
      glGenTextures( 1, &texture );
      glBindTexture( GL_TEXTURE_2D, texture );

      char *curr = ddsdata + 128;
      int level = 0;
      int w = 512;
      int h = 64;
      while (level < 5) {
        printf("uploading level %d: %d, %d\n", level, w, h);
        assert(!glGetError());
        glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);
        assert(!glGetError());
        curr += MAX(w, 4)*MAX(h, 4);
        w /= 2;
        h /= 2;
        level++;
      }
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    }
    {
      glGenTextures( 1, &texture2 );
      glBindTexture( GL_TEXTURE_2D, texture2 );

      char *curr = ddsdata + 128;
      int level = 0;
      int w = 512;
      int h = 64;
      while (level < 5) {
        printf("uploading level %d: %d, %d\n", level, w, h);
        assert(!glGetError());
        glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);
        assert(!glGetError());
        curr += MAX(w, 4)*MAX(h, 4);
        w /= 2;
        h /= 2;
        level++;
      }
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
    }
    {
      assert(!glGetError());
      glBindFramebuffer(GL_RENDERBUFFER, 0);
      assert(glGetError());

      GLint out = 321;
      assert(!glGetError());
      glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &out); // invalid, just test output
      assert(out == 0);
    }

    // Prepare and Render

    // Clear the screen before drawing
    glClear( GL_COLOR_BUFFER_BIT );

    // Bind the texture to which subsequent calls refer to
    int w = 10;
    int n = 15;
    glBindTexture( GL_TEXTURE_2D, texture );
    for (int x = 0; x < n; x++) {
      int start = x*w*2;
      glBegin( GL_TRIANGLES );
        glTexCoord2i( 1, 0 ); glVertex2i( start  ,   0 );
        glTexCoord2i( 0, 0 ); glVertex3f( start+w, 300, 0 );
        glTexCoord2i( 1, 1 ); glVertex3f( start-w, 300, 0 );
      glEnd();
    }
    glBindTexture( GL_TEXTURE_2D, texture2 );
    for (int x = 0; x < n; x++) {
      int start = n*w*2 + x*w*2;
      glBegin( GL_TRIANGLES );
        glTexCoord2i( 1, 0 ); glVertex3f( start  ,   0, 0 );
        glTexCoord2i( 0, 0 ); glVertex3f( start+w, 300, 0 );
        glTexCoord2i( 1, 1 ); glVertex3f( start-w, 300, 0 );
      glEnd();
    }
/*
    int w = 8;
    int n = 20;
    for (int x = 0; x < n; x++) {
      for (int y = 0; y < n*2; y++) {
        glBindTexture( GL_TEXTURE_2D, texture );
        glBegin( GL_TRIANGLE_STRIP );
          glTexCoord2i( 0, 0 ); glVertex3f( x*w,           y*(w), 0 );
          glTexCoord2i( 1, 0 ); glVertex3f( (x+1)*(w-2*y/n),     y*(w), 0 );
          glTexCoord2i( 1, 1 ); glVertex3f( (x+1)*(w-2*y/n), (y+1)*(w), 0 );
          glTexCoord2i( 0, 1 ); glVertex3f( x*w,       (y+1)*(w), 0 );
        glEnd();
        glBindTexture( GL_TEXTURE_2D, texture2 );
        glBegin( GL_TRIANGLE_STRIP );
          glTexCoord2i( 0, 0 ); glVertex3f( n*w + x*w,           y*(w), 0 );
          glTexCoord2i( 1, 0 ); glVertex3f( n*w + (x+1)*(w-2*y/n),     y*(w), 0 );
          glTexCoord2i( 1, 1 ); glVertex3f( n*w + (x+1)*(w-2*y/n), (y+1)*(w), 0 );
          glTexCoord2i( 0, 1 ); glVertex3f( n*w + x*w,       (y+1)*(w), 0 );
        glEnd();
      }
    }
*/
    SDL_GL_SwapBuffers();

#ifndef __EMSCRIPTEN__
    // Wait for 3 seconds to give us a chance to see the image
    SDL_Delay(2000);
#endif

    // Now we can delete the OpenGL texture and close down SDL
    glDeleteTextures( 1, &texture );

    SDL_Quit();

    // check for asm compilation bug with aliased functions with different sigs
    void (*f)(int, int) = glVertex2i;
    if ((int)f % 16 == 4) f(5, 7);
    void (*g)(int, int) = glVertex3f;
    if ((int)g % 16 == 4) g(5, 7);
    return (int)f + (int)g;
}