~glmark2-dev/glmark2/libmatrix-util

« back to all changes in this revision

Viewing changes to texture.cpp

  • Committer: Alexandros Frantzis
  • Date: 2010-07-12 10:06:29 UTC
  • Revision ID: git-v1:32841650dbc96bb732093df311cbc7425515e5ab
Use waf for build system.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "texture.h"
2
 
 
3
 
int load_texture(const char pFilename[], GLuint *pTexture)
4
 
{
5
 
    SDL_Surface *surface;
6
 
    GLenum texture_format = GL_RGB;
7
 
    GLint  nOfColors;
8
 
 
9
 
    if ((surface = SDL_LoadBMP(pFilename))) {
10
 
        if ((surface->w & (surface->w - 1)) != 0)
11
 
            printf("warning: image.bmp's width is not a power of 2\n");
12
 
 
13
 
        if ((surface->h & (surface->h - 1)) != 0)
14
 
            printf("warning: image.bmp's height is not a power of 2\n");
15
 
 
16
 
        nOfColors = surface->format->BytesPerPixel;
17
 
        if (nOfColors == 4) {
18
 
            if (surface->format->Rmask == 0x000000ff)
19
 
                texture_format = GL_RGBA;
20
 
            else {
21
 
                fprintf(stderr, "Error: %s: Unsupported pixel format BGRA\n", pFilename);
22
 
                return 1;
23
 
            }
24
 
        }
25
 
        else {
26
 
            if (nOfColors == 3) {
27
 
                if (surface->format->Rmask == 0x000000ff)
28
 
                    texture_format = GL_RGB;
29
 
                else {
30
 
                    fprintf(stderr, "Error: %s: Unsupported pixel format BGR\n", pFilename);
31
 
                    return 1;
32
 
                }
33
 
            }
34
 
            else {
35
 
                printf("warning: the image is not truecolor..  this will probably break\n");
36
 
            }
37
 
        }
38
 
 
39
 
        glGenTextures(3, pTexture);
40
 
 
41
 
        // Create Nearest Filtered Texture
42
 
        glBindTexture(GL_TEXTURE_2D, pTexture[0]);
43
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
44
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
45
 
        glTexImage2D(GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0,
46
 
                     texture_format, GL_UNSIGNED_BYTE, surface->pixels);
47
 
 
48
 
        // Create Linear Filtered Texture
49
 
        glBindTexture(GL_TEXTURE_2D, pTexture[1]);
50
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
51
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
52
 
        glTexImage2D(GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0,
53
 
                     texture_format, GL_UNSIGNED_BYTE, surface->pixels);
54
 
 
55
 
        // Create trilinear filtered mipmapped texture
56
 
        glBindTexture(GL_TEXTURE_2D, pTexture[2]);
57
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
58
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
59
 
        glTexImage2D(GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0,
60
 
                     texture_format, GL_UNSIGNED_BYTE, surface->pixels);
61
 
        glGenerateMipmap(GL_TEXTURE_2D);
62
 
    }
63
 
    else {
64
 
        fprintf(stderr, "SDL could not load image.bmp: %s\n", SDL_GetError());
65
 
        return 0;
66
 
    }
67
 
 
68
 
    if (surface)
69
 
        SDL_FreeSurface(surface);
70
 
 
71
 
    return 1;
72
 
}