-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.cc
106 lines (82 loc) · 2.76 KB
/
texture.cc
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
#include "first.h"
#include <stdio.h>
#include <string.h>
//#include <malloc.h>
#include <assert.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "texture.h"
void
Texture::create_from_data(int w, int h, GLint components,
GLenum format, GLenum type,
void *tex_data)
{
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_2D, tex_id);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, components, w, h,
format, type, tex_data);
glTexImage2D(GL_TEXTURE_2D, 0, components, w, h, 0,
format, type, tex_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST_MIPMAP_NEAREST);
valid = true;
width = w;
height = h;
data = (char *)tex_data;
}
bool
Texture::create_from_file(const char *fname)
{
SDL_Surface *surf;
surf = IMG_Load(fname);
if(!surf) {
fprintf(stderr, "texture::create_from_file: %s\n", IMG_GetError());
return false;
}
#if 0
printf("image loaded:\nsize: %d x %d\n", surf->w, surf->h);
printf("bytes_per_pixel: %d\n", surf->format->BytesPerPixel);
printf("bits_per_pixel: %d\n", surf->format->BitsPerPixel);
printf("rmask, bmask, gmask, amask: %x, %x, %x, %x\n",
surf->format->Rmask,
surf->format->Gmask,
surf->format->Bmask,
surf->format->Amask);
#endif
int bytes_per_pixel = surf->format->BytesPerPixel;
width = surf->w;
height = surf->h;
assert(bytes_per_pixel == 3 || bytes_per_pixel == 4);
data = new char[width * height * bytes_per_pixel];
assert(data);
SDL_LockSurface(surf);
memcpy(data, surf->pixels, width * height * bytes_per_pixel);
SDL_UnlockSurface(surf);
#if 1
// create OpenGL texture
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_2D, tex_id);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int gl_format;
if (surf->format->Rmask > 0xFF)
{
gl_format = (bytes_per_pixel == 3 ? GL_BGR : GL_BGRA);
}
else
{
gl_format = (bytes_per_pixel == 3 ? GL_RGB : GL_RGBA);
}
gluBuild2DMipmaps(GL_TEXTURE_2D, bytes_per_pixel, width, height,
gl_format, GL_UNSIGNED_BYTE, data);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
#endif
SDL_FreeSurface(surf);
return true;
}