Turo Lamminen
2015-04-29 1525e3962396e5cbf210dc35029a470bf6ab1570
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
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#include <SDL.h>
#include <glew.h>
 
#include "mojodds.h"
 
 
// example how to use mojodds with OpenGL
// this program doesn't actually draw anything
// use apitrace, vogl or similar tool to see what it does
 
 
static void pumpGLErrors() {
    GLenum err;
    while ((err = glGetError()) != GL_NO_ERROR) {
        switch (err) {
        case GL_INVALID_ENUM:
            printf("GL error GL_INVALID_ENUM\n");
            break;
 
        case GL_INVALID_VALUE:
            printf("GL error GL_INVALID_VALUE\n");
            break;
 
        default:
            printf("Unknown GL error %04x\n", err);
            break;
        }
    }
}
 
 
int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Usage: %s DDS-file\n", argv[0]);
        return 0;
    }
 
    FILE *f = fopen(argv[1], "rb");
    if (!f) {
        printf("Error opening %s: %s (%d)\n", argv[1], strerror(errno), errno);
        return 1;
    }
 
    fseek(f, 0, SEEK_END);
    long size = ftell(f);
    fseek(f, 0, SEEK_SET);
 
    char *contents = malloc(size);
    size_t readbytes = fread(contents, 1, size, f);
    if (readbytes != size) {
        printf("Only got %u of %ld bytes: %s\n", (unsigned int) readbytes, size, strerror(errno));
        free(contents);
        fclose(f);
        return 2;
    }
 
    fclose(f);
 
    int isDDS = MOJODDS_isDDS(contents, size);
    printf("isDDS: %d\n", isDDS);
    if (isDDS) {
        SDL_Init(SDL_INIT_VIDEO);
        SDL_Window *window = SDL_CreateWindow("OpenGL DDS test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 0, 0, SDL_WINDOW_OPENGL);
        if (window == NULL) {
            printf("Could not create window: %s\n", SDL_GetError());
            free(contents);
            return 3;
        }
 
        SDL_GLContext context = SDL_GL_CreateContext(window);
        if (context == NULL) {
            printf("Could not create GL context: %s\n", SDL_GetError());
            SDL_DestroyWindow(window);
            free(contents);
            return 4;
        }
 
        // clear and swap to get better traces
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        SDL_GL_SwapWindow(window);
 
        glewInit();
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        SDL_GL_SwapWindow(window);
 
        // glewInit might raise errors if we got an OpenGL 3.0 context
        // ignore them
        // in this case glGetError is simpler than fooling around with callbacks
        GLenum err = GL_NO_ERROR;
        while ((err = glGetError()) != GL_NO_ERROR) { }
 
        const void *tex = NULL;
        unsigned long texlen = 0;
        unsigned int glfmt = 0, w = 0, h = 0, miplevels = 0;
        int retval = MOJODDS_getTexture(contents, size, &tex, &texlen, &glfmt, &w, &h, &miplevels);
 
        bool isCompressed = true;
        GLenum internalFormat = glfmt;
        if (glfmt == GL_BGRA || glfmt == GL_BGR) {
            isCompressed = false;
            if (glfmt == GL_BGR) {
                internalFormat = GL_RGB8;
            } else {
                internalFormat = GL_RGBA8;
            }
        }
 
        GLuint texId = 0;
        // we leak this but don't care
        glGenTextures(1, &texId);
        glBindTexture(GL_TEXTURE_2D, texId);
 
        for (unsigned int miplevel = 0; miplevel < miplevels; miplevel++) {
            const void *miptex = NULL;
            unsigned long miptexlen = 0;
            unsigned int mipW = 0, mipH = 0;
            retval = MOJODDS_getMipMapTexture(miplevel, glfmt, tex, texlen, w, h, &miptex, &miptexlen, &mipW, &mipH);
            if (!retval) {
                printf("MOJODDS_getMipMapTexture(%u) error: %d\n", miplevel, retval);
                continue;
            }
 
            if (isCompressed) {
                glCompressedTexImage2D(GL_TEXTURE_2D, miplevel, glfmt, mipW, mipH, 0, miptexlen, miptex);
            } else {
                glTexImage2D(GL_TEXTURE_2D, miplevel, internalFormat, mipW, mipH, 0, glfmt, GL_UNSIGNED_BYTE, miptex);
            }
            pumpGLErrors();
        }
 
        // and now the same with ARB_texture_storage if it's available
        if (GLEW_ARB_texture_storage) {
            glGenTextures(1, &texId);
            glBindTexture(GL_TEXTURE_2D, texId);
            glTexStorage2D(GL_TEXTURE_2D, miplevels, internalFormat, w, h);
            pumpGLErrors();
 
            for (unsigned int miplevel = 0; miplevel < miplevels; miplevel++) {
                const void *miptex = NULL;
                unsigned long miptexlen = 0;
                unsigned int mipW = 0, mipH = 0;
                retval = MOJODDS_getMipMapTexture(miplevel, glfmt, tex, texlen, w, h, &miptex, &miptexlen, &mipW, &mipH);
                if (!retval) {
                    printf("MOJODDS_getMipMapTexture(%u) error: %d\n", miplevel, retval);
                    continue;
                }
 
                if (isCompressed) {
                    glCompressedTexSubImage2D(GL_TEXTURE_2D, miplevel, 0, 0, mipW, mipH, glfmt, miptexlen, miptex);
                } else {
                    glTexSubImage2D(GL_TEXTURE_2D, miplevel, 0, 0, mipW, mipH, glfmt, GL_UNSIGNED_BYTE, miptex);
                }
                pumpGLErrors();
            }
        }
 
        // one last clear and swap for clean trace end
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        SDL_GL_SwapWindow(window);
 
        SDL_GL_DeleteContext(context);
        SDL_DestroyWindow(window);
    }
 
    free(contents);
 
    return 0;
}