aboutsummaryrefslogtreecommitdiff
path: root/09-september/tomcat/renderer/texture.c
blob: e653c5a752a1cdd7dc845b66a6f80da1e1c4b6e5 (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
#include "../util/util.h"
#include "renderer.h"
#include "texture.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

#define MAX_HASH_TEXTURES 1024
static Texture *texture_hash_table[MAX_HASH_TEXTURES];

Texture *texture_new(const char *path)
{
    return texture_with_name_new(path, path);
}

Texture *texture_with_name_new(const char *name, const char *path)
{
    if(strlen(name) > MAX_PATH_LENGTH)
        Util_FatalError("File following texture name is too long: %s", name);

    Texture *tex;
    tex = texture_get(name);

    if(tex != NULL)
        return tex;

    if(render.num_textures >= MAX_TEXTURES)
        return NULL;

    /** Alloc the new texture **/
    tex = malloc( sizeof(Texture) );
    memset(tex, 0, sizeof(Texture) );
    tex->number_of_rows = 1;
    tex->type = GL_TEXTURE_2D;

    SDL_Surface *data = IMG_Load(path);

    if(data == NULL)
        Util_FatalError("Texture %s could not be found!\n", path);

    glGenTextures(1, &tex->tex_id);
    glBindTexture(GL_TEXTURE_2D, tex->tex_id);

    SDL_LockSurface(data);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data->w, data->h, GL_FALSE, GL_RGBA, GL_UNSIGNED_BYTE, data->pixels);
    SDL_UnlockSurface(data);
    SDL_FreeSurface(data);

    /** Configure the texture **/
    glGenerateMipmap(GL_TEXTURE_2D);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -2.4);

    glBindTexture(GL_TEXTURE_2D, 0);

    /** Register inside the resource manager **/
    unsigned int hash_ = Util_Hash( name );
    hash_ %= MAX_HASH_TEXTURES;

    render.textures[render.num_textures] = tex;
    render.num_textures += 1;

    strcpy(tex->_name, name);
    tex->_hash_next = texture_hash_table[hash_];
    texture_hash_table[hash_] = tex;

    tex->hash_ = hash_;

    /** Return the final result **/
    return tex;
}

Texture *texture_cubemap_new(const char *paths[6])
{
    return texture_cubemap_with_name_new(paths[0], paths);
}

Texture *texture_cubemap_with_name_new(const char *name, const char *paths[6])
{
    Texture *tex;
    tex = texture_get(name);

    if(tex != NULL)
    {
        puts("s");
        return tex;
    }

    if(render.num_textures >= MAX_TEXTURES)
        return NULL;

    /** Alloc the new texture **/
    tex = malloc( sizeof(Texture) );
    memset(tex, 0, sizeof(Texture) );
    tex->number_of_rows = 1;
    tex->type = GL_TEXTURE_CUBE_MAP;

    glGenTextures(1, &tex->tex_id);
    glBindTexture(GL_TEXTURE_CUBE_MAP, tex->tex_id);

    SDL_Surface *data;

    int i;
    for(i = 0; i < 6; i++)
    {
        data = IMG_Load(paths[i]);

        if(data == NULL)
            Util_FatalError("Texture %s could not be found!\n", paths[i]);

        SDL_LockSurface(data);
        /** All the textures sides are linearly stored so we just add "i" **/
        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA,
                     data->w, data->h, GL_FALSE, GL_RGBA, GL_UNSIGNED_BYTE, data->pixels);

        SDL_UnlockSurface(data);
        SDL_FreeSurface(data);
    }

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

    /** Register inside the resource manager **/
    unsigned int hash_ = Util_Hash( name );
    hash_ %= MAX_HASH_TEXTURES;

    render.textures[render.num_textures] = tex;
    render.num_textures += 1;

    strcpy(tex->_name, name);
    tex->_hash_next = texture_hash_table[hash_];
    texture_hash_table[hash_] = tex;

    tex->hash_ = hash_;

    /** Return the final result **/
    return tex;
}

Texture *texture_get(const char *name)
{
    Texture *tex;

    unsigned int hash_ = Util_Hash( name );
    hash_ %= MAX_HASH_TEXTURES;

    if(texture_hash_table[hash_] != NULL)
    {
        for(tex = texture_hash_table[hash_]; tex; tex = tex->_hash_next)
        {
            if( tex->hash_ == hash_ )
                return tex;
        }
    }
    return NULL;
}

void texture_bind(Texture *tex, int slot)
{
    glActiveTexture(GL_TEXTURE0 + slot);

    if(tex->type != GL_TEXTURE_2D && tex->type != GL_TEXTURE_CUBE_MAP)
        return;


    glBindTexture(tex->type, tex->tex_id);
}

void texture_purge(Texture *tex)
{
    /** Purge the opengl data **/
    if(tex->tex_id != 0)
    {
        glDeleteTextures(1, &tex->tex_id);
        tex->tex_id = 0;
    }
}