rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <imago2.h>
|
nuclear@0
|
3 #include "texture.h"
|
nuclear@0
|
4 #include "opengl.h"
|
nuclear@0
|
5
|
nuclear@0
|
6 Texture::Texture()
|
nuclear@0
|
7 {
|
nuclear@0
|
8 width = height = tex_width = tex_height = 0;
|
nuclear@0
|
9 tex = 0;
|
nuclear@0
|
10 }
|
nuclear@0
|
11
|
nuclear@0
|
12 Texture::~Texture()
|
nuclear@0
|
13 {
|
nuclear@0
|
14 if(tex) {
|
nuclear@0
|
15 glDeleteTextures(1, &tex);
|
nuclear@0
|
16 }
|
nuclear@0
|
17 }
|
nuclear@0
|
18
|
nuclear@0
|
19 static unsigned int next_pow2(unsigned int x)
|
nuclear@0
|
20 {
|
nuclear@0
|
21 --x;
|
nuclear@0
|
22 x |= x >> 1;
|
nuclear@0
|
23 x |= x >> 2;
|
nuclear@0
|
24 x |= x >> 4;
|
nuclear@0
|
25 x |= x >> 8;
|
nuclear@0
|
26 x |= x >> 16;
|
nuclear@0
|
27 return x + 1;
|
nuclear@0
|
28 }
|
nuclear@0
|
29
|
nuclear@1
|
30 int Texture::get_width() const
|
nuclear@1
|
31 {
|
nuclear@1
|
32 return width;
|
nuclear@1
|
33 }
|
nuclear@1
|
34
|
nuclear@1
|
35 int Texture::get_height() const
|
nuclear@1
|
36 {
|
nuclear@1
|
37 return height;
|
nuclear@1
|
38 }
|
nuclear@1
|
39
|
nuclear@0
|
40 bool Texture::load(const char *fname)
|
nuclear@0
|
41 {
|
nuclear@0
|
42 img_pixmap img;
|
nuclear@0
|
43 img_init(&img);
|
nuclear@0
|
44 if(img_load(&img, fname) == -1) {
|
nuclear@0
|
45 fprintf(stderr, "failed to load texture: %s\n", fname);
|
nuclear@0
|
46 img_destroy(&img);
|
nuclear@0
|
47 return false;
|
nuclear@0
|
48 }
|
nuclear@0
|
49
|
nuclear@0
|
50 unsigned int intfmt = img_glintfmt(&img);
|
nuclear@0
|
51 unsigned int pixfmt = img_glfmt(&img);
|
nuclear@0
|
52 unsigned int pixtype = img_gltype(&img);
|
nuclear@0
|
53
|
nuclear@0
|
54 // if we have the sRGB extension, change the internal formats to sRGB
|
nuclear@0
|
55 if(GLEW_EXT_texture_sRGB) {
|
nuclear@0
|
56 switch(intfmt) {
|
nuclear@0
|
57 case 3:
|
nuclear@0
|
58 case GL_RGB:
|
nuclear@0
|
59 intfmt = GL_SRGB_EXT;
|
nuclear@0
|
60 break;
|
nuclear@0
|
61 case 4:
|
nuclear@0
|
62 case GL_RGBA:
|
nuclear@0
|
63 intfmt = GL_SRGB_ALPHA;
|
nuclear@0
|
64 break;
|
nuclear@0
|
65 case 1:
|
nuclear@0
|
66 case GL_LUMINANCE:
|
nuclear@0
|
67 intfmt = GL_SLUMINANCE;
|
nuclear@0
|
68 break;
|
nuclear@0
|
69 default:
|
nuclear@0
|
70 break;
|
nuclear@0
|
71 }
|
nuclear@0
|
72 }
|
nuclear@0
|
73
|
nuclear@0
|
74 width = img.width;
|
nuclear@0
|
75 height = img.height;
|
nuclear@0
|
76 tex_width = next_pow2(width);
|
nuclear@0
|
77 tex_height = next_pow2(height);
|
nuclear@0
|
78
|
nuclear@0
|
79 if(!tex) {
|
nuclear@0
|
80 glGenTextures(1, &tex);
|
nuclear@0
|
81 }
|
nuclear@0
|
82 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@0
|
83
|
nuclear@0
|
84 if(GLEW_SGIS_generate_mipmap) {
|
nuclear@0
|
85 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
|
nuclear@0
|
86 } else {
|
nuclear@0
|
87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@0
|
88 }
|
nuclear@0
|
89
|
nuclear@0
|
90 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, tex_width, tex_height, 0, pixfmt, pixtype, 0);
|
nuclear@0
|
91 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, pixfmt, pixtype, img.pixels);
|
nuclear@0
|
92
|
nuclear@0
|
93 tmat.scaling((float)width / (float)tex_width, (float)height / (float)tex_height, 1);
|
nuclear@0
|
94 return true;
|
nuclear@0
|
95 }
|
nuclear@0
|
96
|
nuclear@0
|
97 const Mat4 &Texture::texture_matrix() const
|
nuclear@0
|
98 {
|
nuclear@0
|
99 return tmat;
|
nuclear@0
|
100 }
|
nuclear@0
|
101
|
nuclear@0
|
102 void Texture::bind(bool loadmat) const
|
nuclear@0
|
103 {
|
nuclear@0
|
104 if(!tex) return;
|
nuclear@0
|
105
|
nuclear@0
|
106 if(loadmat) {
|
nuclear@0
|
107 glMatrixMode(GL_TEXTURE);
|
nuclear@0
|
108 glLoadMatrixf(tmat[0]);
|
nuclear@0
|
109 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
110 }
|
nuclear@0
|
111
|
nuclear@0
|
112 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@0
|
113 }
|