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@0
|
30 bool Texture::load(const char *fname)
|
nuclear@0
|
31 {
|
nuclear@0
|
32 img_pixmap img;
|
nuclear@0
|
33 img_init(&img);
|
nuclear@0
|
34 if(img_load(&img, fname) == -1) {
|
nuclear@0
|
35 fprintf(stderr, "failed to load texture: %s\n", fname);
|
nuclear@0
|
36 img_destroy(&img);
|
nuclear@0
|
37 return false;
|
nuclear@0
|
38 }
|
nuclear@0
|
39
|
nuclear@0
|
40 unsigned int intfmt = img_glintfmt(&img);
|
nuclear@0
|
41 unsigned int pixfmt = img_glfmt(&img);
|
nuclear@0
|
42 unsigned int pixtype = img_gltype(&img);
|
nuclear@0
|
43
|
nuclear@0
|
44 // if we have the sRGB extension, change the internal formats to sRGB
|
nuclear@0
|
45 if(GLEW_EXT_texture_sRGB) {
|
nuclear@0
|
46 switch(intfmt) {
|
nuclear@0
|
47 case 3:
|
nuclear@0
|
48 case GL_RGB:
|
nuclear@0
|
49 intfmt = GL_SRGB_EXT;
|
nuclear@0
|
50 break;
|
nuclear@0
|
51 case 4:
|
nuclear@0
|
52 case GL_RGBA:
|
nuclear@0
|
53 intfmt = GL_SRGB_ALPHA;
|
nuclear@0
|
54 break;
|
nuclear@0
|
55 case 1:
|
nuclear@0
|
56 case GL_LUMINANCE:
|
nuclear@0
|
57 intfmt = GL_SLUMINANCE;
|
nuclear@0
|
58 break;
|
nuclear@0
|
59 default:
|
nuclear@0
|
60 break;
|
nuclear@0
|
61 }
|
nuclear@0
|
62 }
|
nuclear@0
|
63
|
nuclear@0
|
64 width = img.width;
|
nuclear@0
|
65 height = img.height;
|
nuclear@0
|
66 tex_width = next_pow2(width);
|
nuclear@0
|
67 tex_height = next_pow2(height);
|
nuclear@0
|
68
|
nuclear@0
|
69 if(!tex) {
|
nuclear@0
|
70 glGenTextures(1, &tex);
|
nuclear@0
|
71 }
|
nuclear@0
|
72 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@0
|
73
|
nuclear@0
|
74 if(GLEW_SGIS_generate_mipmap) {
|
nuclear@0
|
75 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
|
nuclear@0
|
76 } else {
|
nuclear@0
|
77 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@0
|
78 }
|
nuclear@0
|
79
|
nuclear@0
|
80 glTexImage2D(GL_TEXTURE_2D, 0, intfmt, tex_width, tex_height, 0, pixfmt, pixtype, 0);
|
nuclear@0
|
81 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, pixfmt, pixtype, img.pixels);
|
nuclear@0
|
82
|
nuclear@0
|
83 tmat.scaling((float)width / (float)tex_width, (float)height / (float)tex_height, 1);
|
nuclear@0
|
84 return true;
|
nuclear@0
|
85 }
|
nuclear@0
|
86
|
nuclear@0
|
87 const Mat4 &Texture::texture_matrix() const
|
nuclear@0
|
88 {
|
nuclear@0
|
89 return tmat;
|
nuclear@0
|
90 }
|
nuclear@0
|
91
|
nuclear@0
|
92 void Texture::bind(bool loadmat) const
|
nuclear@0
|
93 {
|
nuclear@0
|
94 if(!tex) return;
|
nuclear@0
|
95
|
nuclear@0
|
96 if(loadmat) {
|
nuclear@0
|
97 glMatrixMode(GL_TEXTURE);
|
nuclear@0
|
98 glLoadMatrixf(tmat[0]);
|
nuclear@0
|
99 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
100 }
|
nuclear@0
|
101
|
nuclear@0
|
102 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@0
|
103 }
|